From 63f97201dc9dcebbe84d1b73113166c64212b4b8 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Fri, 17 Oct 2008 01:33:43 +0000 Subject: Fun x86 encoding tricks: when adding an immediate value of 128, use a SUB instruction instead of an ADD, because -128 can be encoded in an 8-bit signed immediate field, while +128 can't be. This avoids the need for a 32-bit immediate field in this case. A similar optimization applies to 64-bit adds with 0x80000000, with the 32-bit signed immediate field. To support this, teach tablegen how to handle 64-bit constants. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@57663 91177308-0d34-0410-b5e6-96231b3b80d8 --- utils/TableGen/DAGISelEmitter.cpp | 3 ++- utils/TableGen/Record.cpp | 24 ++++++++++++------------ utils/TableGen/Record.h | 14 +++++++------- utils/TableGen/RegisterInfoEmitter.cpp | 4 ++-- utils/TableGen/TGLexer.cpp | 12 ++++++++++++ utils/TableGen/TGLexer.h | 4 ++-- utils/TableGen/TGParser.cpp | 6 +++--- 7 files changed, 40 insertions(+), 27 deletions(-) (limited to 'utils') diff --git a/utils/TableGen/DAGISelEmitter.cpp b/utils/TableGen/DAGISelEmitter.cpp index 6ca5657..78239ec 100644 --- a/utils/TableGen/DAGISelEmitter.cpp +++ b/utils/TableGen/DAGISelEmitter.cpp @@ -734,7 +734,8 @@ public: emitCode("int64_t CN"+utostr(CTmp)+" = cast("+ RootName + ")->getSExtValue();"); - emitCheck("CN" + utostr(CTmp) + " == " +itostr(II->getValue())); + emitCheck("CN" + utostr(CTmp) + " == " + "INT64_C(" +itostr(II->getValue()) + ")"); } else { #ifndef NDEBUG Child->dump(); diff --git a/utils/TableGen/Record.cpp b/utils/TableGen/Record.cpp index 06d9bf2..e173cae 100644 --- a/utils/TableGen/Record.cpp +++ b/utils/TableGen/Record.cpp @@ -35,7 +35,7 @@ bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const { } Init *BitRecTy::convertValue(IntInit *II) { - int Val = II->getValue(); + int64_t Val = II->getValue(); if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit! return new BitInit(Val != 0); @@ -116,7 +116,7 @@ Init *IntRecTy::convertValue(BitInit *BI) { } Init *IntRecTy::convertValue(BitsInit *BI) { - int Result = 0; + int64_t Result = 0; for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) if (BitInit *Bit = dynamic_cast(BI->getBit(i))) { Result |= Bit->getValue() << i; @@ -262,7 +262,7 @@ std::string BitsInit::getAsString() const { bool BitsInit::printInHex(std::ostream &OS) const { // First, attempt to convert the value into an integer value... - int Result = 0; + int64_t Result = 0; for (unsigned i = 0, e = getNumBits(); i != e; ++i) if (BitInit *Bit = dynamic_cast(getBit(i))) { Result |= Bit->getValue() << i; @@ -338,11 +338,11 @@ Init *IntInit::convertInitializerBitRange(const std::vector &Bits) { BitsInit *BI = new BitsInit(Bits.size()); for (unsigned i = 0, e = Bits.size(); i != e; ++i) { - if (Bits[i] >= 32) { + if (Bits[i] >= 64) { delete BI; return 0; } - BI->setBit(i, new BitInit(Value & (1 << Bits[i]))); + BI->setBit(i, new BitInit(Value & (INT64_C(1) << Bits[i]))); } return BI; } @@ -443,13 +443,13 @@ Init *BinOpInit::Fold() { IntInit *LHSi = dynamic_cast(LHS); IntInit *RHSi = dynamic_cast(RHS); if (LHSi && RHSi) { - int LHSv = LHSi->getValue(), RHSv = RHSi->getValue(); - int Result; + int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue(); + int64_t Result; switch (getOpcode()) { default: assert(0 && "Bad opcode!"); case SHL: Result = LHSv << RHSv; break; case SRA: Result = LHSv >> RHSv; break; - case SRL: Result = (unsigned)LHSv >> (unsigned)RHSv; break; + case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break; } return new IntInit(Result); } @@ -861,10 +861,10 @@ Record::getValueAsListOfDefs(const std::string &FieldName) const { } /// getValueAsInt - This method looks up the specified field and returns its -/// value as an int, throwing an exception if the field does not exist or if +/// value as an int64_t, throwing an exception if the field does not exist or if /// the value is not the right type. /// -int Record::getValueAsInt(const std::string &FieldName) const { +int64_t Record::getValueAsInt(const std::string &FieldName) const { const RecordVal *R = getValue(FieldName); if (R == 0 || R->getValue() == 0) throw "Record `" + getName() + "' does not have a field named `" + @@ -880,10 +880,10 @@ int Record::getValueAsInt(const std::string &FieldName) const { /// its value as a vector of integers, throwing an exception if the field does /// not exist or if the value is not the right type. /// -std::vector +std::vector Record::getValueAsListOfInts(const std::string &FieldName) const { ListInit *List = getValueAsListInit(FieldName); - std::vector Ints; + std::vector Ints; for (unsigned i = 0; i < List->getSize(); i++) { if (IntInit *II = dynamic_cast(List->getElement(i))) { Ints.push_back(II->getValue()); diff --git a/utils/TableGen/Record.h b/utils/TableGen/Record.h index 90e246e..928fd5c 100644 --- a/utils/TableGen/Record.h +++ b/utils/TableGen/Record.h @@ -565,11 +565,11 @@ public: /// IntInit - 7 - Represent an initalization by a literal integer value. /// class IntInit : public Init { - int Value; + int64_t Value; public: - explicit IntInit(int V) : Value(V) {} + explicit IntInit(int64_t V) : Value(V) {} - int getValue() const { return Value; } + int64_t getValue() const { return Value; } virtual Init *convertInitializerTo(RecTy *Ty) { return Ty->convertValue(this); @@ -1082,7 +1082,7 @@ public: /// its value as a vector of integers, throwing an exception if the field does /// not exist or if the value is not the right type. /// - std::vector getValueAsListOfInts(const std::string &FieldName) const; + std::vector getValueAsListOfInts(const std::string &FieldName) const; /// getValueAsDef - This method looks up the specified field and returns its /// value as a Record, throwing an exception if the field does not exist or if @@ -1097,10 +1097,10 @@ public: bool getValueAsBit(const std::string &FieldName) const; /// getValueAsInt - This method looks up the specified field and returns its - /// value as an int, throwing an exception if the field does not exist or if - /// the value is not the right type. + /// value as an int64_t, throwing an exception if the field does not exist or + /// if the value is not the right type. /// - int getValueAsInt(const std::string &FieldName) const; + int64_t getValueAsInt(const std::string &FieldName) const; /// getValueAsDag - This method looks up the specified field and returns its /// value as an Dag, throwing an exception if the field does not exist or if diff --git a/utils/TableGen/RegisterInfoEmitter.cpp b/utils/TableGen/RegisterInfoEmitter.cpp index 55eb754..06f43d5 100644 --- a/utils/TableGen/RegisterInfoEmitter.cpp +++ b/utils/TableGen/RegisterInfoEmitter.cpp @@ -422,7 +422,7 @@ void RegisterInfoEmitter::run(std::ostream &OS) { std::map, LessRecord> RegisterSuperRegs; std::map, LessRecord> RegisterAliases; std::map > > SubRegVectors; - typedef std::map, LessRecord> DwarfRegNumsMapTy; + typedef std::map, LessRecord> DwarfRegNumsMapTy; DwarfRegNumsMapTy DwarfRegNums; const std::vector &Regs = Target.getRegisters(); @@ -685,7 +685,7 @@ void RegisterInfoEmitter::run(std::ostream &OS) { unsigned maxLength = 0; for (unsigned i = 0, e = Registers.size(); i != e; ++i) { Record *Reg = Registers[i].TheDef; - std::vector RegNums = Reg->getValueAsListOfInts("DwarfNumbers"); + std::vector RegNums = Reg->getValueAsListOfInts("DwarfNumbers"); maxLength = std::max((size_t)maxLength, RegNums.size()); if (DwarfRegNums.count(Reg)) cerr << "Warning: DWARF numbers for register " << getQualifiedName(Reg) diff --git a/utils/TableGen/TGLexer.cpp b/utils/TableGen/TGLexer.cpp index e7465de..0d83e7c 100644 --- a/utils/TableGen/TGLexer.cpp +++ b/utils/TableGen/TGLexer.cpp @@ -20,6 +20,7 @@ #include #include #include +#include using namespace llvm; TGLexer::TGLexer(MemoryBuffer *StartBuf) : CurLineNo(1), CurBuf(StartBuf) { @@ -343,7 +344,18 @@ tgtok::TokKind TGLexer::LexNumber() { if (CurPtr == NumStart) return ReturnError(CurPtr-2, "Invalid hexadecimal number"); + errno = 0; CurIntVal = strtoll(NumStart, 0, 16); + if (errno == EINVAL) + return ReturnError(CurPtr-2, "Invalid hexadecimal number"); + if (errno == ERANGE) { + errno = 0; + CurIntVal = (int64_t)strtoull(NumStart, 0, 16); + if (errno == EINVAL) + return ReturnError(CurPtr-2, "Invalid hexadecimal number"); + if (errno == ERANGE) + return ReturnError(CurPtr-2, "Hexadecimal number out of range"); + } return tgtok::IntVal; } else if (CurPtr[0] == 'b') { ++CurPtr; diff --git a/utils/TableGen/TGLexer.h b/utils/TableGen/TGLexer.h index b4fa97c..2c5a852 100644 --- a/utils/TableGen/TGLexer.h +++ b/utils/TableGen/TGLexer.h @@ -62,7 +62,7 @@ class TGLexer { const char *TokStart; tgtok::TokKind CurCode; std::string CurStrVal; // This is valid for ID, STRVAL, VARNAME, CODEFRAGMENT - int CurIntVal; // This is valid for INTVAL. + int64_t CurIntVal; // This is valid for INTVAL. /// IncludeRec / IncludeStack - This captures the current set of include /// directives we are nested within. @@ -98,7 +98,7 @@ public: "This token doesn't have a string value"); return CurStrVal; } - int getCurIntVal() const { + int64_t getCurIntVal() const { assert(CurCode == tgtok::IntVal && "This token isn't an integer"); return CurIntVal; } diff --git a/utils/TableGen/TGParser.cpp b/utils/TableGen/TGParser.cpp index 68a1cba..4ff108d 100644 --- a/utils/TableGen/TGParser.cpp +++ b/utils/TableGen/TGParser.cpp @@ -294,8 +294,8 @@ bool TGParser::ParseRangePiece(std::vector &Ranges) { TokError("expected integer or bitrange"); return true; } - int Start = Lex.getCurIntVal(); - int End; + int64_t Start = Lex.getCurIntVal(); + int64_t End; if (Start < 0) return TokError("invalid range, cannot be negative"); @@ -426,7 +426,7 @@ RecTy *TGParser::ParseType() { TokError("expected integer in bits type"); return 0; } - unsigned Val = Lex.getCurIntVal(); + uint64_t Val = Lex.getCurIntVal(); if (Lex.Lex() != tgtok::greater) { // Eat count. TokError("expected '>' at end of bits type"); return 0; -- cgit v1.1