Simplify code by using ConstantInt::getRawValue instead of checking to see
authorChris Lattner <sabre@nondot.org>
Wed, 23 Jul 2003 15:22:26 +0000 (15:22 +0000)
committerChris Lattner <sabre@nondot.org>
Wed, 23 Jul 2003 15:22:26 +0000 (15:22 +0000)
whether the constant is signed or unsigned, then casting

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

17 files changed:
lib/Analysis/ConstantRange.cpp
lib/Support/ConstantRange.cpp
lib/Target/CBackend/CBackend.cpp
lib/Target/CBackend/Writer.cpp
lib/Target/SparcV9/SparcV9AsmPrinter.cpp
lib/Target/SparcV9/SparcV9InstrInfo.cpp
lib/Target/X86/InstSelectSimple.cpp
lib/Target/X86/Printer.cpp
lib/Target/X86/X86AsmPrinter.cpp
lib/Target/X86/X86ISelSimple.cpp
lib/Transforms/Scalar/InstructionCombining.cpp
lib/Transforms/Scalar/ScalarReplAggregates.cpp
lib/Transforms/TransformInternals.h
lib/VMCore/AsmWriter.cpp
lib/VMCore/ConstantRange.cpp
lib/VMCore/Constants.cpp
support/lib/Support/ConstantRange.cpp

index b7ef5e05110343674f9d3a8a73ee61a9af8000df..c9d8ae6fbbc930f5108c2b77bf17c12ed9eb992e 100644 (file)
@@ -126,10 +126,7 @@ uint64_t ConstantRange::getSetSize() const {
   // Simply subtract the bounds...
   Constant *Result = *(Constant*)Upper - *(Constant*)Lower;
   assert(Result && "Subtraction of constant integers not implemented?");
-  if (getType()->isSigned())
-    return (uint64_t)cast<ConstantSInt>(Result)->getValue();
-  else
-    return cast<ConstantUInt>(Result)->getValue();
+  return cast<ConstantInt>(Result)->getRawValue();
 }
 
 
index b7ef5e05110343674f9d3a8a73ee61a9af8000df..c9d8ae6fbbc930f5108c2b77bf17c12ed9eb992e 100644 (file)
@@ -126,10 +126,7 @@ uint64_t ConstantRange::getSetSize() const {
   // Simply subtract the bounds...
   Constant *Result = *(Constant*)Upper - *(Constant*)Lower;
   assert(Result && "Subtraction of constant integers not implemented?");
-  if (getType()->isSigned())
-    return (uint64_t)cast<ConstantSInt>(Result)->getValue();
-  else
-    return cast<ConstantUInt>(Result)->getValue();
+  return cast<ConstantInt>(Result)->getRawValue();
 }
 
 
index 6d6f145b92aa673c3521b83971c02b755d8cd701..c15c23e3695837bd0895923301726f27fc622024 100644 (file)
@@ -324,9 +324,7 @@ void CWriter::printConstantArray(ConstantArray *CPA) {
 
     // Do not include the last character, which we know is null
     for (unsigned i = 0, e = CPA->getNumOperands()-1; i != e; ++i) {
-      unsigned char C = (ETy == Type::SByteTy) ?
-        (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() :
-        (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue();
+      unsigned char C = cast<ConstantInt>(CPA->getOperand(i))->getRawValue();
       
       // Print it out literally if it is a printable character.  The only thing
       // to be careful about is when the last letter output was a hex escape
index 6d6f145b92aa673c3521b83971c02b755d8cd701..c15c23e3695837bd0895923301726f27fc622024 100644 (file)
@@ -324,9 +324,7 @@ void CWriter::printConstantArray(ConstantArray *CPA) {
 
     // Do not include the last character, which we know is null
     for (unsigned i = 0, e = CPA->getNumOperands()-1; i != e; ++i) {
-      unsigned char C = (ETy == Type::SByteTy) ?
-        (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() :
-        (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue();
+      unsigned char C = cast<ConstantInt>(CPA->getOperand(i))->getRawValue();
       
       // Print it out literally if it is a printable character.  The only thing
       // to be careful about is when the last letter output was a hex escape
index 6080e99e73f5005975d414bc13e36a24cddfa2dd..019d447e502a561782a4187f599f8b48453ba5b0 100644 (file)
@@ -585,9 +585,7 @@ static string getAsCString(const ConstantArray *CVA) {
   const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
   Result = "\"";
   for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
-    unsigned char C = (ETy == Type::SByteTy) ?
-      (unsigned char)cast<ConstantSInt>(CVA->getOperand(i))->getValue() :
-      (unsigned char)cast<ConstantUInt>(CVA->getOperand(i))->getValue();
+    unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
 
     if (C == '"') {
       Result += "\\\"";
index 75bea0d7774d2d717b3d1e6024ac2d66218de977..39d6dcc6a15aac75082aa1725048b49c79c4bc50 100644 (file)
@@ -43,10 +43,8 @@ GetConstantValueAsUnsignedInt(const Value *V,
   if (isa<Constant>(V))
     if (const ConstantBool *CB = dyn_cast<ConstantBool>(V))
       return (int64_t)CB->getValue();
-    else if (const ConstantSInt *CS = dyn_cast<ConstantSInt>(V))
-      return (uint64_t)CS->getValue();
-    else if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(V))
-      return CU->getValue();
+    else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
+      return CI->getRawValue();
 
   isValidConstant = false;
   return 0;
@@ -377,15 +375,11 @@ UltraSparcInstrInfo::ConstantMayNotFitInImmedField(const Constant* CV,
   if (isa<ConstantPointerNull>(CV))               // can always use %g0
     return false;
 
-  if (const ConstantUInt* U = dyn_cast<ConstantUInt>(CV))
-    /* Large unsigned longs may really just be small negative signed longs */
-    return (labs((int64_t) U->getValue()) > MaxConstantsTable[I->getOpcode()]);
-
-  if (const ConstantSInt* S = dyn_cast<ConstantSInt>(CV))
-    return (labs(S->getValue()) > MaxConstantsTable[I->getOpcode()]);
+  if (const ConstantInt* CI = dyn_cast<ConstantInt>(CV))
+    return labs((int64_t)CI->getRawValue()) > MaxConstantsTable[I->getOpcode()];
 
   if (isa<ConstantBool>(CV))
-    return (1 > MaxConstantsTable[I->getOpcode()]);
+    return 1 > MaxConstantsTable[I->getOpcode()];
 
   return true;
 }
index ec8313eda42d77c4d15d87769a913a162593185b..1562c8483d807a785f319c7c590cf32e1e7eafbf 100644 (file)
@@ -343,12 +343,7 @@ void ISel::copyConstantToRegister(MachineBasicBlock *MBB,
 
     if (Class == cLong) {
       // Copy the value into the register pair.
-      uint64_t Val;
-      if (C->getType()->isSigned())
-       Val = cast<ConstantSInt>(C)->getValue();
-      else
-       Val = cast<ConstantUInt>(C)->getValue();
-
+      uint64_t Val = cast<ConstantInt>(C)->getRawValue();
       BMI(MBB, IP, X86::MOVir32, 1, R).addZImm(Val & 0xFFFFFFFF);
       BMI(MBB, IP, X86::MOVir32, 1, R+1).addZImm(Val >> 32);
       return;
@@ -362,12 +357,9 @@ void ISel::copyConstantToRegister(MachineBasicBlock *MBB,
 
     if (C->getType() == Type::BoolTy) {
       BMI(MBB, IP, X86::MOVir8, 1, R).addZImm(C == ConstantBool::True);
-    } else if (C->getType()->isSigned()) {
-      ConstantSInt *CSI = cast<ConstantSInt>(C);
-      BMI(MBB, IP, IntegralOpcodeTab[Class], 1, R).addZImm(CSI->getValue());
     } else {
-      ConstantUInt *CUI = cast<ConstantUInt>(C);
-      BMI(MBB, IP, IntegralOpcodeTab[Class], 1, R).addZImm(CUI->getValue());
+      ConstantInt *CI = cast<ConstantInt>(C);
+      BMI(MBB, IP, IntegralOpcodeTab[Class], 1, R).addZImm(CI->getRawValue());
     }
   } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
     double Value = CFP->getValue();
@@ -585,11 +577,8 @@ bool ISel::EmitComparisonGetSignedness(unsigned OpNum, Value *Op0, Value *Op1) {
   // Special case handling of: cmp R, i
   if (Class == cByte || Class == cShort || Class == cInt)
     if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
-      uint64_t Op1v;
-      if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(CI))
-        Op1v = CSI->getValue();
-      else
-        Op1v = cast<ConstantUInt>(CI)->getValue();
+      uint64_t Op1v = cast<ConstantInt>(CI)->getRawValue();
+
       // Mask off any upper bits of the constant, if there are any...
       Op1v &= (1ULL << (8 << Class)) - 1;
 
@@ -1061,11 +1050,7 @@ void ISel::emitSimpleBinaryOperation(MachineBasicBlock *BB,
     assert(Class < 3 && "General code handles 64-bit integer types!");
     unsigned Opcode = OpcodeTab[OperatorClass][Class];
     unsigned Op0r = getReg(Op0, BB, IP);
-    uint64_t Op1v;
-    if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(Op1C))
-      Op1v = CSI->getValue();
-    else
-      Op1v = cast<ConstantUInt>(Op1C)->getValue();
+    uint64_t Op1v = cast<ConstantInt>(Op1C)->getRawValue();
 
     // Mask off any upper bits of the constant, if there are any...
     Op1v &= (1ULL << (8 << Class)) - 1;
@@ -2082,8 +2067,6 @@ void ISel::visitMallocInst(MallocInst &I) {
     unsigned Op1Reg = getReg(I.getOperand(0));
     MachineBasicBlock::iterator MBBI = BB->end();
     doMultiply(BB, MBBI, Arg, Type::UIntTy, Op0Reg, Op1Reg);
-              
-              
   }
 
   std::vector<ValueRecord> Args;
index c2f2bb8f794a5d9abf1e9b46a472ae3577a9bbac..08dbb6c7a5c4eff2caa4837095be3817d625e27e 100644 (file)
@@ -293,9 +293,7 @@ static std::string getAsCString(const ConstantArray *CVA) {
   const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
   Result = "\"";
   for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
-    unsigned char C = (ETy == Type::SByteTy) ?
-      (unsigned char)cast<ConstantSInt>(CVA->getOperand(i))->getValue() :
-      (unsigned char)cast<ConstantUInt>(CVA->getOperand(i))->getValue();
+    unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
 
     if (C == '"') {
       Result += "\\\"";
@@ -943,19 +941,14 @@ bool Printer::doInitialization(Module &M)
   return false; // success
 }
 
-static const Function *isConstantFunctionPointerRef (const Constant *C) {
-  const ConstantPointerRef *R = dyn_cast<ConstantPointerRef>(C);
-  if (R) {
-    const Function *F = dyn_cast<Function>(R->getValue());
-    if (F) {
+static const Function *isConstantFunctionPointerRef(const Constant *C) {
+  if (const ConstantPointerRef *R = dyn_cast<ConstantPointerRef>(C))
+    if (const Function *F = dyn_cast<Function>(R->getValue()))
       return F;
-    }
-  }
-  return NULL;
+  return 0;
 }
 
-bool Printer::doFinalization(Module &M)
-{
+bool Printer::doFinalization(Module &M) {
   // Print out module-level global variables here.
   for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) {
     std::string name(getValueName(I));
@@ -989,5 +982,3 @@ bool Printer::doFinalization(Module &M)
   MangledGlobals.clear();
   return false; // success
 }
-
-
index c2f2bb8f794a5d9abf1e9b46a472ae3577a9bbac..08dbb6c7a5c4eff2caa4837095be3817d625e27e 100644 (file)
@@ -293,9 +293,7 @@ static std::string getAsCString(const ConstantArray *CVA) {
   const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
   Result = "\"";
   for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
-    unsigned char C = (ETy == Type::SByteTy) ?
-      (unsigned char)cast<ConstantSInt>(CVA->getOperand(i))->getValue() :
-      (unsigned char)cast<ConstantUInt>(CVA->getOperand(i))->getValue();
+    unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
 
     if (C == '"') {
       Result += "\\\"";
@@ -943,19 +941,14 @@ bool Printer::doInitialization(Module &M)
   return false; // success
 }
 
-static const Function *isConstantFunctionPointerRef (const Constant *C) {
-  const ConstantPointerRef *R = dyn_cast<ConstantPointerRef>(C);
-  if (R) {
-    const Function *F = dyn_cast<Function>(R->getValue());
-    if (F) {
+static const Function *isConstantFunctionPointerRef(const Constant *C) {
+  if (const ConstantPointerRef *R = dyn_cast<ConstantPointerRef>(C))
+    if (const Function *F = dyn_cast<Function>(R->getValue()))
       return F;
-    }
-  }
-  return NULL;
+  return 0;
 }
 
-bool Printer::doFinalization(Module &M)
-{
+bool Printer::doFinalization(Module &M) {
   // Print out module-level global variables here.
   for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) {
     std::string name(getValueName(I));
@@ -989,5 +982,3 @@ bool Printer::doFinalization(Module &M)
   MangledGlobals.clear();
   return false; // success
 }
-
-
index ec8313eda42d77c4d15d87769a913a162593185b..1562c8483d807a785f319c7c590cf32e1e7eafbf 100644 (file)
@@ -343,12 +343,7 @@ void ISel::copyConstantToRegister(MachineBasicBlock *MBB,
 
     if (Class == cLong) {
       // Copy the value into the register pair.
-      uint64_t Val;
-      if (C->getType()->isSigned())
-       Val = cast<ConstantSInt>(C)->getValue();
-      else
-       Val = cast<ConstantUInt>(C)->getValue();
-
+      uint64_t Val = cast<ConstantInt>(C)->getRawValue();
       BMI(MBB, IP, X86::MOVir32, 1, R).addZImm(Val & 0xFFFFFFFF);
       BMI(MBB, IP, X86::MOVir32, 1, R+1).addZImm(Val >> 32);
       return;
@@ -362,12 +357,9 @@ void ISel::copyConstantToRegister(MachineBasicBlock *MBB,
 
     if (C->getType() == Type::BoolTy) {
       BMI(MBB, IP, X86::MOVir8, 1, R).addZImm(C == ConstantBool::True);
-    } else if (C->getType()->isSigned()) {
-      ConstantSInt *CSI = cast<ConstantSInt>(C);
-      BMI(MBB, IP, IntegralOpcodeTab[Class], 1, R).addZImm(CSI->getValue());
     } else {
-      ConstantUInt *CUI = cast<ConstantUInt>(C);
-      BMI(MBB, IP, IntegralOpcodeTab[Class], 1, R).addZImm(CUI->getValue());
+      ConstantInt *CI = cast<ConstantInt>(C);
+      BMI(MBB, IP, IntegralOpcodeTab[Class], 1, R).addZImm(CI->getRawValue());
     }
   } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
     double Value = CFP->getValue();
@@ -585,11 +577,8 @@ bool ISel::EmitComparisonGetSignedness(unsigned OpNum, Value *Op0, Value *Op1) {
   // Special case handling of: cmp R, i
   if (Class == cByte || Class == cShort || Class == cInt)
     if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
-      uint64_t Op1v;
-      if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(CI))
-        Op1v = CSI->getValue();
-      else
-        Op1v = cast<ConstantUInt>(CI)->getValue();
+      uint64_t Op1v = cast<ConstantInt>(CI)->getRawValue();
+
       // Mask off any upper bits of the constant, if there are any...
       Op1v &= (1ULL << (8 << Class)) - 1;
 
@@ -1061,11 +1050,7 @@ void ISel::emitSimpleBinaryOperation(MachineBasicBlock *BB,
     assert(Class < 3 && "General code handles 64-bit integer types!");
     unsigned Opcode = OpcodeTab[OperatorClass][Class];
     unsigned Op0r = getReg(Op0, BB, IP);
-    uint64_t Op1v;
-    if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(Op1C))
-      Op1v = CSI->getValue();
-    else
-      Op1v = cast<ConstantUInt>(Op1C)->getValue();
+    uint64_t Op1v = cast<ConstantInt>(Op1C)->getRawValue();
 
     // Mask off any upper bits of the constant, if there are any...
     Op1v &= (1ULL << (8 << Class)) - 1;
@@ -2082,8 +2067,6 @@ void ISel::visitMallocInst(MallocInst &I) {
     unsigned Op1Reg = getReg(I.getOperand(0));
     MachineBasicBlock::iterator MBBI = BB->end();
     doMultiply(BB, MBBI, Arg, Type::UIntTy, Op0Reg, Op1Reg);
-              
-              
   }
 
   std::vector<ValueRecord> Args;
index 0ecec730d90c9c0ab3f7fae5ec27929a852a8d74..33de49d391d4916fc38f34517ed00591991cc0ba 100644 (file)
@@ -370,8 +370,7 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) {
   if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) {
     if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
       const Type *Ty = CI->getType();
-      int64_t Val = Ty->isSigned() ?        cast<ConstantSInt>(CI)->getValue() :
-                                   (int64_t)cast<ConstantUInt>(CI)->getValue();
+      int64_t Val = (int64_t)cast<ConstantInt>(CI)->getRawValue();
       switch (Val) {
       case -1:                               // X * -1 -> -X
         return BinaryOperator::createNeg(Op0, I.getName());
index f0e51e589d7bfa5f375e992eeddba0f2704ee891..fceaccc89a507c21527894380359e109eb9e1a68 100644 (file)
@@ -103,11 +103,7 @@ bool SROA::runOnFunction(Function &F) {
       Instruction *User = cast<Instruction>(*I);
       if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(User)) {
         // We now know that the GEP is of the form: GEP <ptr>, 0, <cst>
-        uint64_t Idx;
-        if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(GEPI->getOperand(2)))
-          Idx = CSI->getValue();
-        else
-          Idx = cast<ConstantUInt>(GEPI->getOperand(2))->getValue();
+        uint64_t Idx = cast<ConstantInt>(GEPI->getOperand(2))->getRawValue();
         
         assert(Idx < ElementAllocas.size() && "Index out of range?");
         AllocaInst *AllocaToUse = ElementAllocas[Idx];
index 8cab4293496012515215a87b7dd62e13645ce985..5f87a38cc4c19d3b1775bdd25c1280ac139e4c39 100644 (file)
@@ -16,9 +16,7 @@
 #include <set>
 
 static inline int64_t getConstantValue(const ConstantInt *CPI) {
-  if (const ConstantSInt *CSI = dyn_cast<ConstantSInt>(CPI))
-    return CSI->getValue();
-  return (int64_t)cast<ConstantUInt>(CPI)->getValue();
+  return (int64_t)cast<ConstantInt>(CPI)->getRawValue();
 }
 
 
index 0c62d05d2b3781db6d819f9b191e17d2cc4b4baf..545349a6f5f04dfe014ae6e36cc0b7e7ddcbeff6 100644 (file)
@@ -274,9 +274,7 @@ static void WriteConstantInt(std::ostream &Out, const Constant *CV,
     if (isString) {
       Out << "c\"";
       for (unsigned i = 0; i < CA->getNumOperands(); ++i) {
-        unsigned char C = (ETy == Type::SByteTy) ?
-          (unsigned char)cast<ConstantSInt>(CA->getOperand(i))->getValue() :
-          (unsigned char)cast<ConstantUInt>(CA->getOperand(i))->getValue();
+        unsigned char C = cast<ConstantInt>(CA->getOperand(i))->getRawValue();
         
         if (isprint(C) && C != '"' && C != '\\') {
           Out << C;
index b7ef5e05110343674f9d3a8a73ee61a9af8000df..c9d8ae6fbbc930f5108c2b77bf17c12ed9eb992e 100644 (file)
@@ -126,10 +126,7 @@ uint64_t ConstantRange::getSetSize() const {
   // Simply subtract the bounds...
   Constant *Result = *(Constant*)Upper - *(Constant*)Lower;
   assert(Result && "Subtraction of constant integers not implemented?");
-  if (getType()->isSigned())
-    return (uint64_t)cast<ConstantSInt>(Result)->getValue();
-  else
-    return cast<ConstantUInt>(Result)->getValue();
+  return cast<ConstantInt>(Result)->getRawValue();
 }
 
 
index 935c2938c22ecb521cf3f5a4f3bf50518f48b6b6..7bbbdb32023493b018e593113246ad4bfedf6baf 100644 (file)
@@ -601,15 +601,12 @@ ConstantArray *ConstantArray::get(const std::string &Str) {
 // Otherwise, it asserts out.
 //
 std::string ConstantArray::getAsString() const {
+  assert((getType()->getElementType() == Type::UByteTy ||
+          getType()->getElementType() == Type::SByteTy) && "Not a string!");
+
   std::string Result;
-  if (getType()->getElementType() == Type::SByteTy)
-    for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
-      Result += (char)cast<ConstantSInt>(getOperand(i))->getValue();
-  else {
-    assert(getType()->getElementType() == Type::UByteTy && "Not a string!");
-    for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
-      Result += (char)cast<ConstantUInt>(getOperand(i))->getValue();
-  }
+  for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
+    Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue();
   return Result;
 }
 
index b7ef5e05110343674f9d3a8a73ee61a9af8000df..c9d8ae6fbbc930f5108c2b77bf17c12ed9eb992e 100644 (file)
@@ -126,10 +126,7 @@ uint64_t ConstantRange::getSetSize() const {
   // Simply subtract the bounds...
   Constant *Result = *(Constant*)Upper - *(Constant*)Lower;
   assert(Result && "Subtraction of constant integers not implemented?");
-  if (getType()->isSigned())
-    return (uint64_t)cast<ConstantSInt>(Result)->getValue();
-  else
-    return cast<ConstantUInt>(Result)->getValue();
+  return cast<ConstantInt>(Result)->getRawValue();
 }