Add support for writing bytecode files with compactiontables for bytecode files.
[oota-llvm.git] / lib / Bytecode / Writer / InstructionWriter.cpp
index d5a2abf3fb3c679ebb3c89dc06164e6fc8453a4a..b93a812d52c4e9980189dc503c76932f3c7252bc 100644 (file)
@@ -1,4 +1,11 @@
 //===-- InstructionWriter.cpp - Functions for writing instructions --------===//
+// 
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// 
+//===----------------------------------------------------------------------===//
 //
 // This file implements the routines for encoding instruction opcodes to a 
 // bytecode stream.
 #include "llvm/Instructions.h"
 #include "Support/Statistic.h"
 #include <algorithm>
+using namespace llvm;
 
 static Statistic<> 
 NumInstrs("bytecodewriter", "Number of instructions");
+static Statistic<> 
+NumOversizedInstrs("bytecodewriter", "Number of oversized instructions");
+static Statistic<> 
+BytesOversizedInstrs("bytecodewriter", "Bytes of oversized instructions");
+
+static Statistic<> 
+NumHugeOperandInstrs("bytecodewriter", "Number of instructions with > 3 operands");
+static Statistic<> 
+NumOversized1OpInstrs("bytecodewriter", "Number of oversized 1 operand instrs");
+static Statistic<> 
+NumOversized2OpInstrs("bytecodewriter", "Number of oversized 2 operand instrs");
+static Statistic<>
+NumOversized3OpInstrs("bytecodewriter", "Number of oversized 3 operand instrs");
+
+static Statistic<>
+NumOversidedBecauseOfTypes("bytecodewriter", "Number of oversized instructions because of their type");
+
 
 typedef unsigned char uchar;
 
@@ -25,12 +50,16 @@ typedef unsigned char uchar;
 static void outputInstructionFormat0(const Instruction *I, unsigned Opcode,
                                     const SlotCalculator &Table,
                                     unsigned Type, std::deque<uchar> &Out) {
+  NumOversizedInstrs++;
+  BytesOversizedInstrs -= Out.size();
+
   // Opcode must have top two bits clear...
   output_vbr(Opcode << 2, Out);                  // Instruction Opcode ID
   output_vbr(Type, Out);                         // Result type
 
   unsigned NumArgs = I->getNumOperands();
-  output_vbr(NumArgs + (isa<CastInst>(I) || isa<VarArgInst>(I)), Out);
+  output_vbr(NumArgs + (isa<CastInst>(I) || isa<VANextInst>(I) ||
+                        isa<VAArgInst>(I)), Out);
 
   for (unsigned i = 0; i < NumArgs; ++i) {
     int Slot = Table.getSlot(I->getOperand(i));
@@ -38,13 +67,18 @@ static void outputInstructionFormat0(const Instruction *I, unsigned Opcode,
     output_vbr((unsigned)Slot, Out);
   }
 
-  if (isa<CastInst>(I) || isa<VarArgInst>(I)) {
+  if (isa<CastInst>(I) || isa<VAArgInst>(I)) {
     int Slot = Table.getSlot(I->getType());
-    assert(Slot != -1 && "Cast/VarArg return type unknown?");
+    assert(Slot != -1 && "Cast return type unknown?");
+    output_vbr((unsigned)Slot, Out);
+  } else if (const VANextInst *VAI = dyn_cast<VANextInst>(I)) {
+    int Slot = Table.getSlot(VAI->getArgType());
+    assert(Slot != -1 && "VarArg argument type unknown?");
     output_vbr((unsigned)Slot, Out);
   }
 
   align32(Out);    // We must maintain correct alignment!
+  BytesOversizedInstrs += Out.size();
 }
 
 
@@ -65,29 +99,37 @@ static void outputInstrVarArgsCall(const Instruction *I, unsigned Opcode,
   output_vbr(Opcode << 2, Out);                  // Instruction Opcode ID
   output_vbr(Type, Out);                         // Result type (varargs type)
 
-  unsigned NumArgs = I->getNumOperands();
-  output_vbr(NumArgs*2, Out);
-  // TODO: Don't need to emit types for the fixed types of the varargs function
-  // prototype...
+  const PointerType *PTy = cast<PointerType>(I->getOperand(0)->getType());
+  const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
+  unsigned NumParams = FTy->getNumParams();
+
+  unsigned NumFixedOperands;
+  if (isa<CallInst>(I)) {
+    // Output an operand for the callee and each fixed argument, then two for
+    // each variable argument.
+    NumFixedOperands = 1+NumParams;
+  } else {
+    assert(isa<InvokeInst>(I) && "Not call or invoke??");
+    // Output an operand for the callee and destinations, then two for each
+    // variable argument.
+    NumFixedOperands = 3+NumParams;
+  }
+  output_vbr(2 * I->getNumOperands()-NumFixedOperands, Out);
 
   // The type for the function has already been emitted in the type field of the
   // instruction.  Just emit the slot # now.
-  int Slot = Table.getSlot(I->getOperand(0));
-  assert(Slot >= 0 && "No slot number for value!?!?");      
-  output_vbr((unsigned)Slot, Out);
-
-  // Output a dummy field to fill Arg#2 in the reader that is currently unused
-  // for varargs calls.  This is a gross hack to make the code simpler, but we
-  // aren't really doing very small bytecode for varargs calls anyways.
-  // FIXME in the future: Smaller bytecode for varargs calls
-  output_vbr(0, Out);
+  for (unsigned i = 0; i != NumFixedOperands; ++i) {
+    int Slot = Table.getSlot(I->getOperand(i));
+    assert(Slot >= 0 && "No slot number for value!?!?");      
+    output_vbr((unsigned)Slot, Out);
+  }
 
-  for (unsigned i = 1; i < NumArgs; ++i) {
+  for (unsigned i = NumFixedOperands, e = I->getNumOperands(); i != e; ++i) {
     // Output Arg Type ID
-    Slot = Table.getSlot(I->getOperand(i)->getType());
+    int Slot = Table.getSlot(I->getOperand(i)->getType());
     assert(Slot >= 0 && "No slot number for value!?!?");      
     output_vbr((unsigned)Slot, Out);
-
+    
     // Output arg ID itself
     Slot = Table.getSlot(I->getOperand(i));
     assert(Slot >= 0 && "No slot number for value!?!?");      
@@ -160,7 +202,7 @@ static void outputInstructionFormat3(const Instruction *I, unsigned Opcode,
   output(Bits, Out);
 }
 
-void BytecodeWriter::processInstruction(const Instruction &I) {
+void BytecodeWriter::outputInstruction(const Instruction &I) {
   assert(I.getOpcode() < 62 && "Opcode too big???");
   unsigned Opcode = I.getOpcode();
 
@@ -174,9 +216,8 @@ void BytecodeWriter::processInstruction(const Instruction &I) {
   int MaxOpSlot = 0;
   int Slots[3]; Slots[0] = (1 << 12)-1;   // Marker to signify 0 operands
 
-  for (unsigned i = 0; i < NumOperands; ++i) {
-    const Value *Def = I.getOperand(i);
-    int slot = Table.getSlot(Def);
+  for (unsigned i = 0; i != NumOperands; ++i) {
+    int slot = Table.getSlot(I.getOperand(i));
     assert(slot != -1 && "Broken bytecode!");
     if (slot > MaxOpSlot) MaxOpSlot = slot;
     if (i < 3) Slots[i] = slot;
@@ -214,13 +255,18 @@ void BytecodeWriter::processInstruction(const Instruction &I) {
   if (Slot > MaxOpSlot) MaxOpSlot = Slot;
 
   // Handle the special case for cast...
-  if (isa<CastInst>(I) || isa<VarArgInst>(I)) {
+  if (isa<CastInst>(I) || isa<VAArgInst>(I)) {
     // Cast has to encode the destination type as the second argument in the
     // packet, or else we won't know what type to cast to!
     Slots[1] = Table.getSlot(I.getType());
     assert(Slots[1] != -1 && "Cast return type unknown?");
     if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
     NumOperands++;
+  } else if (const VANextInst *VANI = dyn_cast<VANextInst>(&I)) {
+    Slots[1] = Table.getSlot(VANI->getArgType());
+    assert(Slots[1] != -1 && "va_next return type unknown?");
+    if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
+    NumOperands++;
   } else if (const CallInst *CI = dyn_cast<CallInst>(&I)){// Handle VarArg calls
     const PointerType *Ty = cast<PointerType>(CI->getCalledValue()->getType());
     if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
@@ -249,6 +295,10 @@ void BytecodeWriter::processInstruction(const Instruction &I) {
       outputInstructionFormat1(&I, Opcode, Table, Slots, Type, Out);
       return;
     }
+    if (Type >= (1 << 12)-1)
+      NumOversidedBecauseOfTypes++;
+
+    NumOversized1OpInstrs++;
     break;
 
   case 2:
@@ -256,6 +306,9 @@ void BytecodeWriter::processInstruction(const Instruction &I) {
       outputInstructionFormat2(&I, Opcode, Table, Slots, Type, Out);
       return;
     }
+    if (Type >= (1 << 8)) 
+      NumOversidedBecauseOfTypes++;
+    NumOversized2OpInstrs++;
     break;
 
   case 3:
@@ -263,6 +316,12 @@ void BytecodeWriter::processInstruction(const Instruction &I) {
       outputInstructionFormat3(&I, Opcode, Table, Slots, Type, Out);
       return;
     }
+    if (Type >= (1 << 6)) 
+      NumOversidedBecauseOfTypes++;
+    NumOversized3OpInstrs++;
+    break;
+  default:
+    ++NumHugeOperandInstrs;
     break;
   }
 
@@ -270,3 +329,4 @@ void BytecodeWriter::processInstruction(const Instruction &I) {
   // operands or a large operand index that we are referring to.
   outputInstructionFormat0(&I, Opcode, Table, Type, Out);
 }
+