From c07736a397012499e337c994f7f952b07c709544 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Wed, 23 Jul 2003 15:22:26 +0000 Subject: Simplify code by using ConstantInt::getRawValue instead of checking to see 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 --- lib/Analysis/ConstantRange.cpp | 5 +---- lib/Support/ConstantRange.cpp | 5 +---- lib/Target/CBackend/CBackend.cpp | 4 +--- lib/Target/CBackend/Writer.cpp | 4 +--- lib/Target/SparcV9/SparcV9AsmPrinter.cpp | 4 +--- lib/Target/SparcV9/SparcV9InstrInfo.cpp | 16 +++++--------- lib/Target/X86/InstSelectSimple.cpp | 29 ++++++-------------------- lib/Target/X86/Printer.cpp | 21 ++++++------------- lib/Target/X86/X86AsmPrinter.cpp | 21 ++++++------------- lib/Target/X86/X86ISelSimple.cpp | 29 ++++++-------------------- lib/Transforms/Scalar/InstructionCombining.cpp | 3 +-- lib/Transforms/Scalar/ScalarReplAggregates.cpp | 6 +----- lib/Transforms/TransformInternals.h | 4 +--- lib/VMCore/AsmWriter.cpp | 4 +--- lib/VMCore/ConstantRange.cpp | 5 +---- lib/VMCore/Constants.cpp | 13 +++++------- support/lib/Support/ConstantRange.cpp | 5 +---- 17 files changed, 45 insertions(+), 133 deletions(-) diff --git a/lib/Analysis/ConstantRange.cpp b/lib/Analysis/ConstantRange.cpp index b7ef5e0..c9d8ae6 100644 --- a/lib/Analysis/ConstantRange.cpp +++ b/lib/Analysis/ConstantRange.cpp @@ -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(Result)->getValue(); - else - return cast(Result)->getValue(); + return cast(Result)->getRawValue(); } diff --git a/lib/Support/ConstantRange.cpp b/lib/Support/ConstantRange.cpp index b7ef5e0..c9d8ae6 100644 --- a/lib/Support/ConstantRange.cpp +++ b/lib/Support/ConstantRange.cpp @@ -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(Result)->getValue(); - else - return cast(Result)->getValue(); + return cast(Result)->getRawValue(); } diff --git a/lib/Target/CBackend/CBackend.cpp b/lib/Target/CBackend/CBackend.cpp index 6d6f145..c15c23e 100644 --- a/lib/Target/CBackend/CBackend.cpp +++ b/lib/Target/CBackend/CBackend.cpp @@ -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(CPA->getOperand(i))->getValue() : - (unsigned char)cast(CPA->getOperand(i))->getValue(); + unsigned char C = cast(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 diff --git a/lib/Target/CBackend/Writer.cpp b/lib/Target/CBackend/Writer.cpp index 6d6f145..c15c23e 100644 --- a/lib/Target/CBackend/Writer.cpp +++ b/lib/Target/CBackend/Writer.cpp @@ -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(CPA->getOperand(i))->getValue() : - (unsigned char)cast(CPA->getOperand(i))->getValue(); + unsigned char C = cast(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 diff --git a/lib/Target/SparcV9/SparcV9AsmPrinter.cpp b/lib/Target/SparcV9/SparcV9AsmPrinter.cpp index 6080e99..019d447 100644 --- a/lib/Target/SparcV9/SparcV9AsmPrinter.cpp +++ b/lib/Target/SparcV9/SparcV9AsmPrinter.cpp @@ -585,9 +585,7 @@ static string getAsCString(const ConstantArray *CVA) { const Type *ETy = cast(CVA->getType())->getElementType(); Result = "\""; for (unsigned i = 0; i < CVA->getNumOperands(); ++i) { - unsigned char C = (ETy == Type::SByteTy) ? - (unsigned char)cast(CVA->getOperand(i))->getValue() : - (unsigned char)cast(CVA->getOperand(i))->getValue(); + unsigned char C = cast(CVA->getOperand(i))->getRawValue(); if (C == '"') { Result += "\\\""; diff --git a/lib/Target/SparcV9/SparcV9InstrInfo.cpp b/lib/Target/SparcV9/SparcV9InstrInfo.cpp index 75bea0d..39d6dcc 100644 --- a/lib/Target/SparcV9/SparcV9InstrInfo.cpp +++ b/lib/Target/SparcV9/SparcV9InstrInfo.cpp @@ -43,10 +43,8 @@ GetConstantValueAsUnsignedInt(const Value *V, if (isa(V)) if (const ConstantBool *CB = dyn_cast(V)) return (int64_t)CB->getValue(); - else if (const ConstantSInt *CS = dyn_cast(V)) - return (uint64_t)CS->getValue(); - else if (const ConstantUInt *CU = dyn_cast(V)) - return CU->getValue(); + else if (const ConstantInt *CI = dyn_cast(V)) + return CI->getRawValue(); isValidConstant = false; return 0; @@ -377,15 +375,11 @@ UltraSparcInstrInfo::ConstantMayNotFitInImmedField(const Constant* CV, if (isa(CV)) // can always use %g0 return false; - if (const ConstantUInt* U = dyn_cast(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(CV)) - return (labs(S->getValue()) > MaxConstantsTable[I->getOpcode()]); + if (const ConstantInt* CI = dyn_cast(CV)) + return labs((int64_t)CI->getRawValue()) > MaxConstantsTable[I->getOpcode()]; if (isa(CV)) - return (1 > MaxConstantsTable[I->getOpcode()]); + return 1 > MaxConstantsTable[I->getOpcode()]; return true; } diff --git a/lib/Target/X86/InstSelectSimple.cpp b/lib/Target/X86/InstSelectSimple.cpp index ec8313e..1562c84 100644 --- a/lib/Target/X86/InstSelectSimple.cpp +++ b/lib/Target/X86/InstSelectSimple.cpp @@ -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(C)->getValue(); - else - Val = cast(C)->getValue(); - + uint64_t Val = cast(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(C); - BMI(MBB, IP, IntegralOpcodeTab[Class], 1, R).addZImm(CSI->getValue()); } else { - ConstantUInt *CUI = cast(C); - BMI(MBB, IP, IntegralOpcodeTab[Class], 1, R).addZImm(CUI->getValue()); + ConstantInt *CI = cast(C); + BMI(MBB, IP, IntegralOpcodeTab[Class], 1, R).addZImm(CI->getRawValue()); } } else if (ConstantFP *CFP = dyn_cast(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(Op1)) { - uint64_t Op1v; - if (ConstantSInt *CSI = dyn_cast(CI)) - Op1v = CSI->getValue(); - else - Op1v = cast(CI)->getValue(); + uint64_t Op1v = cast(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(Op1C)) - Op1v = CSI->getValue(); - else - Op1v = cast(Op1C)->getValue(); + uint64_t Op1v = cast(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 Args; diff --git a/lib/Target/X86/Printer.cpp b/lib/Target/X86/Printer.cpp index c2f2bb8..08dbb6c 100644 --- a/lib/Target/X86/Printer.cpp +++ b/lib/Target/X86/Printer.cpp @@ -293,9 +293,7 @@ static std::string getAsCString(const ConstantArray *CVA) { const Type *ETy = cast(CVA->getType())->getElementType(); Result = "\""; for (unsigned i = 0; i < CVA->getNumOperands(); ++i) { - unsigned char C = (ETy == Type::SByteTy) ? - (unsigned char)cast(CVA->getOperand(i))->getValue() : - (unsigned char)cast(CVA->getOperand(i))->getValue(); + unsigned char C = cast(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(C); - if (R) { - const Function *F = dyn_cast(R->getValue()); - if (F) { +static const Function *isConstantFunctionPointerRef(const Constant *C) { + if (const ConstantPointerRef *R = dyn_cast(C)) + if (const Function *F = dyn_cast(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 } - - diff --git a/lib/Target/X86/X86AsmPrinter.cpp b/lib/Target/X86/X86AsmPrinter.cpp index c2f2bb8..08dbb6c 100644 --- a/lib/Target/X86/X86AsmPrinter.cpp +++ b/lib/Target/X86/X86AsmPrinter.cpp @@ -293,9 +293,7 @@ static std::string getAsCString(const ConstantArray *CVA) { const Type *ETy = cast(CVA->getType())->getElementType(); Result = "\""; for (unsigned i = 0; i < CVA->getNumOperands(); ++i) { - unsigned char C = (ETy == Type::SByteTy) ? - (unsigned char)cast(CVA->getOperand(i))->getValue() : - (unsigned char)cast(CVA->getOperand(i))->getValue(); + unsigned char C = cast(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(C); - if (R) { - const Function *F = dyn_cast(R->getValue()); - if (F) { +static const Function *isConstantFunctionPointerRef(const Constant *C) { + if (const ConstantPointerRef *R = dyn_cast(C)) + if (const Function *F = dyn_cast(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 } - - diff --git a/lib/Target/X86/X86ISelSimple.cpp b/lib/Target/X86/X86ISelSimple.cpp index ec8313e..1562c84 100644 --- a/lib/Target/X86/X86ISelSimple.cpp +++ b/lib/Target/X86/X86ISelSimple.cpp @@ -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(C)->getValue(); - else - Val = cast(C)->getValue(); - + uint64_t Val = cast(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(C); - BMI(MBB, IP, IntegralOpcodeTab[Class], 1, R).addZImm(CSI->getValue()); } else { - ConstantUInt *CUI = cast(C); - BMI(MBB, IP, IntegralOpcodeTab[Class], 1, R).addZImm(CUI->getValue()); + ConstantInt *CI = cast(C); + BMI(MBB, IP, IntegralOpcodeTab[Class], 1, R).addZImm(CI->getRawValue()); } } else if (ConstantFP *CFP = dyn_cast(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(Op1)) { - uint64_t Op1v; - if (ConstantSInt *CSI = dyn_cast(CI)) - Op1v = CSI->getValue(); - else - Op1v = cast(CI)->getValue(); + uint64_t Op1v = cast(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(Op1C)) - Op1v = CSI->getValue(); - else - Op1v = cast(Op1C)->getValue(); + uint64_t Op1v = cast(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 Args; diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index 0ecec73..33de49d 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -370,8 +370,7 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) { if (Constant *Op1 = dyn_cast(I.getOperand(1))) { if (ConstantInt *CI = dyn_cast(Op1)) { const Type *Ty = CI->getType(); - int64_t Val = Ty->isSigned() ? cast(CI)->getValue() : - (int64_t)cast(CI)->getValue(); + int64_t Val = (int64_t)cast(CI)->getRawValue(); switch (Val) { case -1: // X * -1 -> -X return BinaryOperator::createNeg(Op0, I.getName()); diff --git a/lib/Transforms/Scalar/ScalarReplAggregates.cpp b/lib/Transforms/Scalar/ScalarReplAggregates.cpp index f0e51e58..fceaccc 100644 --- a/lib/Transforms/Scalar/ScalarReplAggregates.cpp +++ b/lib/Transforms/Scalar/ScalarReplAggregates.cpp @@ -103,11 +103,7 @@ bool SROA::runOnFunction(Function &F) { Instruction *User = cast(*I); if (GetElementPtrInst *GEPI = dyn_cast(User)) { // We now know that the GEP is of the form: GEP , 0, - uint64_t Idx; - if (ConstantSInt *CSI = dyn_cast(GEPI->getOperand(2))) - Idx = CSI->getValue(); - else - Idx = cast(GEPI->getOperand(2))->getValue(); + uint64_t Idx = cast(GEPI->getOperand(2))->getRawValue(); assert(Idx < ElementAllocas.size() && "Index out of range?"); AllocaInst *AllocaToUse = ElementAllocas[Idx]; diff --git a/lib/Transforms/TransformInternals.h b/lib/Transforms/TransformInternals.h index 8cab429..5f87a38 100644 --- a/lib/Transforms/TransformInternals.h +++ b/lib/Transforms/TransformInternals.h @@ -16,9 +16,7 @@ #include static inline int64_t getConstantValue(const ConstantInt *CPI) { - if (const ConstantSInt *CSI = dyn_cast(CPI)) - return CSI->getValue(); - return (int64_t)cast(CPI)->getValue(); + return (int64_t)cast(CPI)->getRawValue(); } diff --git a/lib/VMCore/AsmWriter.cpp b/lib/VMCore/AsmWriter.cpp index 0c62d05..545349a 100644 --- a/lib/VMCore/AsmWriter.cpp +++ b/lib/VMCore/AsmWriter.cpp @@ -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(CA->getOperand(i))->getValue() : - (unsigned char)cast(CA->getOperand(i))->getValue(); + unsigned char C = cast(CA->getOperand(i))->getRawValue(); if (isprint(C) && C != '"' && C != '\\') { Out << C; diff --git a/lib/VMCore/ConstantRange.cpp b/lib/VMCore/ConstantRange.cpp index b7ef5e0..c9d8ae6 100644 --- a/lib/VMCore/ConstantRange.cpp +++ b/lib/VMCore/ConstantRange.cpp @@ -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(Result)->getValue(); - else - return cast(Result)->getValue(); + return cast(Result)->getRawValue(); } diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp index 935c293..7bbbdb3 100644 --- a/lib/VMCore/Constants.cpp +++ b/lib/VMCore/Constants.cpp @@ -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(getOperand(i))->getValue(); - else { - assert(getType()->getElementType() == Type::UByteTy && "Not a string!"); - for (unsigned i = 0, e = getNumOperands(); i != e; ++i) - Result += (char)cast(getOperand(i))->getValue(); - } + for (unsigned i = 0, e = getNumOperands(); i != e; ++i) + Result += (char)cast(getOperand(i))->getRawValue(); return Result; } diff --git a/support/lib/Support/ConstantRange.cpp b/support/lib/Support/ConstantRange.cpp index b7ef5e0..c9d8ae6 100644 --- a/support/lib/Support/ConstantRange.cpp +++ b/support/lib/Support/ConstantRange.cpp @@ -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(Result)->getValue(); - else - return cast(Result)->getValue(); + return cast(Result)->getRawValue(); } -- cgit v1.1