implement the 'string constant' optimization. This shrinks kc.bit from
authorChris Lattner <sabre@nondot.org>
Sun, 6 May 2007 00:35:24 +0000 (00:35 +0000)
committerChris Lattner <sabre@nondot.org>
Sun, 6 May 2007 00:35:24 +0000 (00:35 +0000)
2878544 to 2815788

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

include/llvm/Bitcode/LLVMBitCodes.h
lib/Bitcode/Reader/BitcodeReader.cpp
lib/Bitcode/Writer/BitcodeWriter.cpp
lib/Bitcode/Writer/ValueEnumerator.cpp

index e00a71153d57b04ea95efece5d305ab35caade80..9850db4fcd28ba1a9d5d12628ba610fb505dcb43 100644 (file)
@@ -106,14 +106,15 @@ namespace bitc {
     CST_CODE_WIDE_INTEGER  =  5,  // WIDE_INTEGER:  [n x intval]
     CST_CODE_FLOAT         =  6,  // FLOAT:         [fpval]
     CST_CODE_AGGREGATE     =  7,  // AGGREGATE:     [n x value number]
-    CST_CODE_CE_BINOP      =  8,  // CE_BINOP:      [opcode, opval, opval]
-    CST_CODE_CE_CAST       =  9,  // CE_CAST:       [opcode, opty, opval]
-    CST_CODE_CE_GEP        = 10,  // CE_GEP:        [n x operands]
-    CST_CODE_CE_SELECT     = 11,  // CE_SELECT:     [opval, opval, opval]
-    CST_CODE_CE_EXTRACTELT = 12,  // CE_EXTRACTELT: [opty, opval, opval]
-    CST_CODE_CE_INSERTELT  = 13,  // CE_INSERTELT:  [opval, opval, opval]
-    CST_CODE_CE_SHUFFLEVEC = 14,  // CE_SHUFFLEVEC: [opval, opval, opval]
-    CST_CODE_CE_CMP        = 15   // CE_CMP:        [opty, opval, opval, pred]
+    CST_CODE_STRING        =  8,  // STRING:        [values]
+    CST_CODE_CE_BINOP      =  9,  // CE_BINOP:      [opcode, opval, opval]
+    CST_CODE_CE_CAST       = 10,  // CE_CAST:       [opcode, opty, opval]
+    CST_CODE_CE_GEP        = 11,  // CE_GEP:        [n x operands]
+    CST_CODE_CE_SELECT     = 12,  // CE_SELECT:     [opval, opval, opval]
+    CST_CODE_CE_EXTRACTELT = 13,  // CE_EXTRACTELT: [opty, opval, opval]
+    CST_CODE_CE_INSERTELT  = 14,  // CE_INSERTELT:  [opval, opval, opval]
+    CST_CODE_CE_SHUFFLEVEC = 15,  // CE_SHUFFLEVEC: [opval, opval, opval]
+    CST_CODE_CE_CMP        = 16   // CE_CMP:        [opty, opval, opval, pred]
   };
   
   /// CastOpcodes - These are values used in the bitcode files to encode which
index 5a7c84da9318da1f69f1a93fb75a07767be3feb4..b1a001e1af47d5314d64018e8d9c52be37d51e48 100644 (file)
@@ -642,7 +642,21 @@ bool BitcodeReader::ParseConstants() {
       }
       break;
     }
+    case bitc::CST_CODE_STRING: { // STRING: [values]
+      if (Record.empty())
+        return Error("Invalid CST_AGGREGATE record");
 
+      const ArrayType *ATy = cast<ArrayType>(CurTy);
+      const Type *EltTy = ATy->getElementType();
+      
+      unsigned Size = Record.size();
+      std::vector<Constant*> Elts;
+      
+      for (unsigned i = 0; i != Size; ++i)
+        Elts.push_back(ConstantInt::get(EltTy, Record[i]));
+      V = ConstantArray::get(ATy, Elts);
+      break;
+    }
     case bitc::CST_CODE_CE_BINOP: {  // CE_BINOP: [opcode, opval, opval]
       if (Record.size() < 3) return Error("Invalid CE_BINOP record");
       int Opc = GetDecodedBinaryOpcode(Record[0], CurTy);
index 7198bc0060f476860b4994db07d8e0b1a5582975..a29f23ddfc1a76727abd33d6c8e5e5fb9872eb89 100644 (file)
@@ -484,6 +484,12 @@ static void WriteConstants(unsigned FirstVal, unsigned LastVal,
         assert (CFP->getType() == Type::DoubleTy && "Unknown FP type!");
         Record.push_back(DoubleToBits((double)CFP->getValue()));
       }
+    } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) {
+      // Emit constant strings specially.
+      Code = bitc::CST_CODE_STRING;
+      for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
+        Record.push_back(cast<ConstantInt>(C->getOperand(i))->getZExtValue());
+      
     } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(V) ||
                isa<ConstantVector>(V)) {
       Code = bitc::CST_CODE_AGGREGATE;
index 6b753b23865a95da45bb9546e1aa3b503a3e5761..ae9e67c8266b85a30aab77f9faa08442ab50fa8e 100644 (file)
@@ -12,6 +12,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "ValueEnumerator.h"
+#include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/Module.h"
 #include "llvm/TypeSymbolTable.h"
@@ -65,8 +66,6 @@ ValueEnumerator::ValueEnumerator(const Module *M) {
        I != E; ++I)
     EnumerateValue(I->getAliasee());
   
-  // FIXME: Implement the 'string constant' optimization.
-
   // Enumerate types used by the type symbol table.
   EnumerateTypeSymbolTable(M->getTypeSymbolTable());
 
@@ -105,8 +104,6 @@ ValueEnumerator::ValueEnumerator(const Module *M) {
   // Now that we rearranged the type table, rebuild TypeMap.
   for (unsigned i = 0, e = Types.size(); i != e; ++i)
     TypeMap[Types[i].first] = i+1;
-  
-  // FIXME: Sort value tables by frequency.
 }
 
 // Optimize constant ordering.
@@ -176,6 +173,10 @@ void ValueEnumerator::EnumerateValue(const Value *V) {
   if (const Constant *C = dyn_cast<Constant>(V)) {
     if (isa<GlobalValue>(C)) {
       // Initializers for globals are handled explicitly elsewhere.
+    } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) {
+      // Do not enumerate the initializers for an array of simple characters.
+      // The initializers just polute the value table, and we emit the strings
+      // specially.
     } else {
       // This makes sure that if a constant has uses (for example an array of
       // const ints), that they are inserted also.