*** empty log message ***
[oota-llvm.git] / lib / Bytecode / Writer / InstructionWriter.cpp
1 //===-- WriteInst.cpp - Functions for writing instructions -------*- C++ -*--=//
2 //
3 // This file implements the routines for encoding instruction opcodes to a 
4 // bytecode stream.
5 //
6 // Note that the performance of this library is not terribly important, because
7 // it shouldn't be used by JIT type applications... so it is not a huge focus
8 // at least.  :)
9 //
10 //===----------------------------------------------------------------------===//
11
12 #include "WriterInternals.h"
13 #include "llvm/Module.h"
14 #include "llvm/Function.h"
15 #include "llvm/BasicBlock.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/iOther.h"
18 #include "llvm/iTerminators.h"
19 #include "Support/StatisticReporter.h"
20 #include <algorithm>
21
22 static Statistic<> 
23 NumOversized("bytecodewriter\t- Number of oversized instructions");
24 static Statistic<> 
25 NumNormal("bytecodewriter\t- Number of normal instructions");
26
27 typedef unsigned char uchar;
28
29 // outputInstructionFormat0 - Output those wierd instructions that have a large
30 // number of operands or have large operands themselves...
31 //
32 // Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
33 //
34 static void outputInstructionFormat0(const Instruction *I,
35                                      const SlotCalculator &Table,
36                                      unsigned Type, std::deque<uchar> &Out) {
37   // Opcode must have top two bits clear...
38   output_vbr(I->getOpcode() << 2, Out);          // Instruction Opcode ID
39   output_vbr(Type, Out);                         // Result type
40
41   unsigned NumArgs = I->getNumOperands();
42   output_vbr(NumArgs + isa<CastInst>(I), Out);
43
44   for (unsigned i = 0; i < NumArgs; ++i) {
45     int Slot = Table.getValSlot(I->getOperand(i));
46     assert(Slot >= 0 && "No slot number for value!?!?");      
47     output_vbr((unsigned)Slot, Out);
48   }
49
50   if (isa<CastInst>(I)) {
51     int Slot = Table.getValSlot(I->getType());
52     assert(Slot != -1 && "Cast return type unknown?");
53     output_vbr((unsigned)Slot, Out);
54   }
55
56   align32(Out);    // We must maintain correct alignment!
57   ++NumOversized;
58 }
59
60
61 // outputInstrVarArgsCall - Output the obsurdly annoying varargs function calls.
62 // This are more annoying than most because the signature of the call does not
63 // tell us anything about the types of the arguments in the varargs portion.
64 // Because of this, we encode (as type 0) all of the argument types explicitly
65 // before the argument value.  This really sucks, but you shouldn't be using
66 // varargs functions in your code! *death to printf*!
67 //
68 // Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
69 //
70 static void outputInstrVarArgsCall(const Instruction *I,
71                                    const SlotCalculator &Table, unsigned Type,
72                                    std::deque<uchar> &Out) {
73   assert(isa<CallInst>(I) || isa<InvokeInst>(I));
74   // Opcode must have top two bits clear...
75   output_vbr(I->getOpcode() << 2, Out);          // Instruction Opcode ID
76   output_vbr(Type, Out);                         // Result type (varargs type)
77
78   unsigned NumArgs = I->getNumOperands();
79   output_vbr(NumArgs*2, Out);
80   // TODO: Don't need to emit types for the fixed types of the varargs function
81   // prototype...
82
83   // The type for the function has already been emitted in the type field of the
84   // instruction.  Just emit the slot # now.
85   int Slot = Table.getValSlot(I->getOperand(0));
86   assert(Slot >= 0 && "No slot number for value!?!?");      
87   output_vbr((unsigned)Slot, Out);
88
89   // Output a dummy field to fill Arg#2 in the reader that is currently unused
90   // for varargs calls.  This is a gross hack to make the code simpler, but we
91   // aren't really doing very small bytecode for varargs calls anyways.
92   // FIXME in the future: Smaller bytecode for varargs calls
93   output_vbr(0, Out);
94
95   for (unsigned i = 1; i < NumArgs; ++i) {
96     // Output Arg Type ID
97     Slot = Table.getValSlot(I->getOperand(i)->getType());
98     assert(Slot >= 0 && "No slot number for value!?!?");      
99     output_vbr((unsigned)Slot, Out);
100
101     // Output arg ID itself
102     Slot = Table.getValSlot(I->getOperand(i));
103     assert(Slot >= 0 && "No slot number for value!?!?");      
104     output_vbr((unsigned)Slot, Out);
105   }
106   align32(Out);    // We must maintain correct alignment!
107   ++NumOversized;
108 }
109
110
111 // outputInstructionFormat1 - Output one operand instructions, knowing that no
112 // operand index is >= 2^12.
113 //
114 static void outputInstructionFormat1(const Instruction *I, 
115                                      const SlotCalculator &Table, int *Slots,
116                                      unsigned Type, std::deque<uchar> &Out) {
117   unsigned Opcode = I->getOpcode();      // Instruction Opcode ID
118   
119   // bits   Instruction format:
120   // --------------------------
121   // 01-00: Opcode type, fixed to 1.
122   // 07-02: Opcode
123   // 19-08: Resulting type plane
124   // 31-20: Operand #1 (if set to (2^12-1), then zero operands)
125   //
126   unsigned Bits = 1 | (Opcode << 2) | (Type << 8) | (Slots[0] << 20);
127   //  cerr << "1 " << IType << " " << Type << " " << Slots[0] << endl;
128   output(Bits, Out);
129   ++NumNormal;
130 }
131
132
133 // outputInstructionFormat2 - Output two operand instructions, knowing that no
134 // operand index is >= 2^8.
135 //
136 static void outputInstructionFormat2(const Instruction *I, 
137                                      const SlotCalculator &Table, int *Slots,
138                                      unsigned Type, std::deque<uchar> &Out) {
139   unsigned Opcode = I->getOpcode();      // Instruction Opcode ID
140
141   // bits   Instruction format:
142   // --------------------------
143   // 01-00: Opcode type, fixed to 2.
144   // 07-02: Opcode
145   // 15-08: Resulting type plane
146   // 23-16: Operand #1
147   // 31-24: Operand #2  
148   //
149   unsigned Bits = 2 | (Opcode << 2) | (Type << 8) |
150                     (Slots[0] << 16) | (Slots[1] << 24);
151   //  cerr << "2 " << IType << " " << Type << " " << Slots[0] << " " 
152   //       << Slots[1] << endl;
153   output(Bits, Out);
154   ++NumNormal;
155 }
156
157
158 // outputInstructionFormat3 - Output three operand instructions, knowing that no
159 // operand index is >= 2^6.
160 //
161 static void outputInstructionFormat3(const Instruction *I, 
162                                      const SlotCalculator &Table, int *Slots,
163                                      unsigned Type, std::deque<uchar> &Out) {
164   unsigned Opcode = I->getOpcode();      // Instruction Opcode ID
165
166   // bits   Instruction format:
167   // --------------------------
168   // 01-00: Opcode type, fixed to 3.
169   // 07-02: Opcode
170   // 13-08: Resulting type plane
171   // 19-14: Operand #1
172   // 25-20: Operand #2
173   // 31-26: Operand #3
174   //
175   unsigned Bits = 3 | (Opcode << 2) | (Type << 8) |
176           (Slots[0] << 14) | (Slots[1] << 20) | (Slots[2] << 26);
177   //cerr << "3 " << IType << " " << Type << " " << Slots[0] << " " 
178   //     << Slots[1] << " " << Slots[2] << endl;
179   output(Bits, Out);
180   ++NumNormal;
181 }
182
183 void BytecodeWriter::processInstruction(const Instruction &I) {
184   assert(I.getOpcode() < 64 && "Opcode too big???");
185
186   unsigned NumOperands = I.getNumOperands();
187   int MaxOpSlot = 0;
188   int Slots[3]; Slots[0] = (1 << 12)-1;   // Marker to signify 0 operands
189
190   for (unsigned i = 0; i < NumOperands; ++i) {
191     const Value *Def = I.getOperand(i);
192     int slot = Table.getValSlot(Def);
193     assert(slot != -1 && "Broken bytecode!");
194     if (slot > MaxOpSlot) MaxOpSlot = slot;
195     if (i < 3) Slots[i] = slot;
196   }
197
198   // Figure out which type to encode with the instruction.  Typically we want
199   // the type of the first parameter, as opposed to the type of the instruction
200   // (for example, with setcc, we always know it returns bool, but the type of
201   // the first param is actually interesting).  But if we have no arguments
202   // we take the type of the instruction itself.  
203   //
204   const Type *Ty;
205   switch (I.getOpcode()) {
206   case Instruction::Malloc:
207   case Instruction::Alloca:
208     Ty = I.getType();  // Malloc & Alloca ALWAYS want to encode the return type
209     break;
210   case Instruction::Store:
211     Ty = I.getOperand(1)->getType();  // Encode the pointer type...
212     assert(isa<PointerType>(Ty) && "Store to nonpointer type!?!?");
213     break;
214   default:              // Otherwise use the default behavior...
215     Ty = NumOperands ? I.getOperand(0)->getType() : I.getType();
216     break;
217   }
218
219   unsigned Type;
220   int Slot = Table.getValSlot(Ty);
221   assert(Slot != -1 && "Type not available!!?!");
222   Type = (unsigned)Slot;
223
224   // Make sure that we take the type number into consideration.  We don't want
225   // to overflow the field size for the instruction format we select.
226   //
227   if (Slot > MaxOpSlot) MaxOpSlot = Slot;
228
229   // Handle the special case for cast...
230   if (isa<CastInst>(I)) {
231     // Cast has to encode the destination type as the second argument in the
232     // packet, or else we won't know what type to cast to!
233     Slots[1] = Table.getValSlot(I.getType());
234     assert(Slots[1] != -1 && "Cast return type unknown?");
235     if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
236     NumOperands++;
237   } else if (const CallInst *CI = dyn_cast<CallInst>(&I)){// Handle VarArg calls
238     const PointerType *Ty = cast<PointerType>(CI->getCalledValue()->getType());
239     if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
240       outputInstrVarArgsCall(CI, Table, Type, Out);
241       return;
242     }
243   } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {// ...  & Invokes
244     const PointerType *Ty = cast<PointerType>(II->getCalledValue()->getType());
245     if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
246       outputInstrVarArgsCall(II, Table, Type, Out);
247       return;
248     }
249   }
250
251   // Decide which instruction encoding to use.  This is determined primarily by
252   // the number of operands, and secondarily by whether or not the max operand
253   // will fit into the instruction encoding.  More operands == fewer bits per
254   // operand.
255   //
256   switch (NumOperands) {
257   case 0:
258   case 1:
259     if (MaxOpSlot < (1 << 12)-1) { // -1 because we use 4095 to indicate 0 ops
260       outputInstructionFormat1(&I, Table, Slots, Type, Out);
261       return;
262     }
263     break;
264
265   case 2:
266     if (MaxOpSlot < (1 << 8)) {
267       outputInstructionFormat2(&I, Table, Slots, Type, Out);
268       return;
269     }
270     break;
271
272   case 3:
273     if (MaxOpSlot < (1 << 6)) {
274       outputInstructionFormat3(&I, Table, Slots, Type, Out);
275       return;
276     }
277     break;
278   }
279
280   // If we weren't handled before here, we either have a large number of
281   // operands or a large operand index that we are refering to.
282   outputInstructionFormat0(&I, Table, Type, Out);
283 }