blob: fdc5c5c1435c045266518a47d3b7f7f153993c31 [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- 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"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000014#include "llvm/Function.h"
Chris Lattner00950542001-06-06 20:29:01 +000015#include "llvm/BasicBlock.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include "llvm/DerivedTypes.h"
Chris Lattneref9c23f2001-10-03 14:53:21 +000017#include "llvm/iOther.h"
Chris Lattner1b98c5c2001-10-13 06:48:38 +000018#include "llvm/iTerminators.h"
Chris Lattnerce6ef112002-07-26 18:40:14 +000019#include "Support/StatisticReporter.h"
Chris Lattner00950542001-06-06 20:29:01 +000020#include <algorithm>
21
Chris Lattnerce6ef112002-07-26 18:40:14 +000022static Statistic<>
23NumOversized("bytecodewriter\t- Number of oversized instructions");
24static Statistic<>
25NumNormal("bytecodewriter\t- Number of normal instructions");
26
Chris Lattner00950542001-06-06 20:29:01 +000027typedef 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//
34static void outputInstructionFormat0(const Instruction *I,
35 const SlotCalculator &Table,
Chris Lattner697954c2002-01-20 22:54:45 +000036 unsigned Type, std::deque<uchar> &Out) {
Chris Lattner00950542001-06-06 20:29:01 +000037 // Opcode must have top two bits clear...
Chris Lattner2b9f6002001-10-23 03:21:10 +000038 output_vbr(I->getOpcode() << 2, Out); // Instruction Opcode ID
Chris Lattner00950542001-06-06 20:29:01 +000039 output_vbr(Type, Out); // Result type
40
Chris Lattnerc8b25d42001-07-07 08:36:50 +000041 unsigned NumArgs = I->getNumOperands();
Chris Lattner5ab1f872001-10-21 00:14:44 +000042 output_vbr(NumArgs + isa<CastInst>(I), Out);
Chris Lattner00950542001-06-06 20:29:01 +000043
Chris Lattnerc8b25d42001-07-07 08:36:50 +000044 for (unsigned i = 0; i < NumArgs; ++i) {
Chris Lattnere5a57ee2001-07-25 22:47:55 +000045 int Slot = Table.getValSlot(I->getOperand(i));
46 assert(Slot >= 0 && "No slot number for value!?!?");
47 output_vbr((unsigned)Slot, Out);
48 }
Chris Lattner5ab1f872001-10-21 00:14:44 +000049
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
Chris Lattnere5a57ee2001-07-25 22:47:55 +000056 align32(Out); // We must maintain correct alignment!
Chris Lattnerce6ef112002-07-26 18:40:14 +000057 ++NumOversized;
Chris Lattnere5a57ee2001-07-25 22:47:55 +000058}
59
60
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000061// outputInstrVarArgsCall - Output the obsurdly annoying varargs function calls.
Chris Lattnere5a57ee2001-07-25 22:47:55 +000062// 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//
70static void outputInstrVarArgsCall(const Instruction *I,
71 const SlotCalculator &Table, unsigned Type,
Chris Lattner697954c2002-01-20 22:54:45 +000072 std::deque<uchar> &Out) {
Chris Lattner1b98c5c2001-10-13 06:48:38 +000073 assert(isa<CallInst>(I) || isa<InvokeInst>(I));
Chris Lattnere5a57ee2001-07-25 22:47:55 +000074 // Opcode must have top two bits clear...
Chris Lattner2b9f6002001-10-23 03:21:10 +000075 output_vbr(I->getOpcode() << 2, Out); // Instruction Opcode ID
Chris Lattnere5a57ee2001-07-25 22:47:55 +000076 output_vbr(Type, Out); // Result type (varargs type)
77
78 unsigned NumArgs = I->getNumOperands();
Chris Lattner1b98c5c2001-10-13 06:48:38 +000079 output_vbr(NumArgs*2, Out);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000080 // TODO: Don't need to emit types for the fixed types of the varargs function
Chris Lattner1b98c5c2001-10-13 06:48:38 +000081 // prototype...
Chris Lattnere5a57ee2001-07-25 22:47:55 +000082
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000083 // The type for the function has already been emitted in the type field of the
Chris Lattner1b98c5c2001-10-13 06:48:38 +000084 // instruction. Just emit the slot # now.
Chris Lattnere5a57ee2001-07-25 22:47:55 +000085 int Slot = Table.getValSlot(I->getOperand(0));
86 assert(Slot >= 0 && "No slot number for value!?!?");
87 output_vbr((unsigned)Slot, Out);
88
Chris Lattner1b98c5c2001-10-13 06:48:38 +000089 // 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);
Chris Lattnere5a57ee2001-07-25 22:47:55 +000094
Chris Lattner1b98c5c2001-10-13 06:48:38 +000095 for (unsigned i = 1; i < NumArgs; ++i) {
Chris Lattnere5a57ee2001-07-25 22:47:55 +000096 // 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));
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000103 assert(Slot >= 0 && "No slot number for value!?!?");
Chris Lattner00950542001-06-06 20:29:01 +0000104 output_vbr((unsigned)Slot, Out);
105 }
106 align32(Out); // We must maintain correct alignment!
Chris Lattnerce6ef112002-07-26 18:40:14 +0000107 ++NumOversized;
Chris Lattner00950542001-06-06 20:29:01 +0000108}
109
110
111// outputInstructionFormat1 - Output one operand instructions, knowing that no
112// operand index is >= 2^12.
113//
114static void outputInstructionFormat1(const Instruction *I,
115 const SlotCalculator &Table, int *Slots,
Chris Lattner697954c2002-01-20 22:54:45 +0000116 unsigned Type, std::deque<uchar> &Out) {
Chris Lattner2b9f6002001-10-23 03:21:10 +0000117 unsigned Opcode = I->getOpcode(); // Instruction Opcode ID
Chris Lattner00950542001-06-06 20:29:01 +0000118
119 // bits Instruction format:
120 // --------------------------
Chris Lattner2b9f6002001-10-23 03:21:10 +0000121 // 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)
Chris Lattner00950542001-06-06 20:29:01 +0000125 //
Chris Lattner2b9f6002001-10-23 03:21:10 +0000126 unsigned Bits = 1 | (Opcode << 2) | (Type << 8) | (Slots[0] << 20);
Chris Lattner00950542001-06-06 20:29:01 +0000127 // cerr << "1 " << IType << " " << Type << " " << Slots[0] << endl;
Chris Lattner2b9f6002001-10-23 03:21:10 +0000128 output(Bits, Out);
Chris Lattnerce6ef112002-07-26 18:40:14 +0000129 ++NumNormal;
Chris Lattner00950542001-06-06 20:29:01 +0000130}
131
132
133// outputInstructionFormat2 - Output two operand instructions, knowing that no
134// operand index is >= 2^8.
135//
136static void outputInstructionFormat2(const Instruction *I,
137 const SlotCalculator &Table, int *Slots,
Chris Lattner697954c2002-01-20 22:54:45 +0000138 unsigned Type, std::deque<uchar> &Out) {
Chris Lattner2b9f6002001-10-23 03:21:10 +0000139 unsigned Opcode = I->getOpcode(); // Instruction Opcode ID
Chris Lattner00950542001-06-06 20:29:01 +0000140
141 // bits Instruction format:
142 // --------------------------
Chris Lattner2b9f6002001-10-23 03:21:10 +0000143 // 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
Chris Lattner00950542001-06-06 20:29:01 +0000148 //
Chris Lattner2b9f6002001-10-23 03:21:10 +0000149 unsigned Bits = 2 | (Opcode << 2) | (Type << 8) |
150 (Slots[0] << 16) | (Slots[1] << 24);
Chris Lattner00950542001-06-06 20:29:01 +0000151 // cerr << "2 " << IType << " " << Type << " " << Slots[0] << " "
152 // << Slots[1] << endl;
Chris Lattner2b9f6002001-10-23 03:21:10 +0000153 output(Bits, Out);
Chris Lattnerce6ef112002-07-26 18:40:14 +0000154 ++NumNormal;
Chris Lattner00950542001-06-06 20:29:01 +0000155}
156
157
158// outputInstructionFormat3 - Output three operand instructions, knowing that no
159// operand index is >= 2^6.
160//
161static void outputInstructionFormat3(const Instruction *I,
162 const SlotCalculator &Table, int *Slots,
Chris Lattner697954c2002-01-20 22:54:45 +0000163 unsigned Type, std::deque<uchar> &Out) {
Chris Lattner2b9f6002001-10-23 03:21:10 +0000164 unsigned Opcode = I->getOpcode(); // Instruction Opcode ID
Chris Lattner00950542001-06-06 20:29:01 +0000165
166 // bits Instruction format:
167 // --------------------------
Chris Lattner2b9f6002001-10-23 03:21:10 +0000168 // 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
Chris Lattner00950542001-06-06 20:29:01 +0000174 //
Chris Lattner2b9f6002001-10-23 03:21:10 +0000175 unsigned Bits = 3 | (Opcode << 2) | (Type << 8) |
176 (Slots[0] << 14) | (Slots[1] << 20) | (Slots[2] << 26);
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000177 //cerr << "3 " << IType << " " << Type << " " << Slots[0] << " "
178 // << Slots[1] << " " << Slots[2] << endl;
Chris Lattner2b9f6002001-10-23 03:21:10 +0000179 output(Bits, Out);
Chris Lattnerce6ef112002-07-26 18:40:14 +0000180 ++NumNormal;
Chris Lattner00950542001-06-06 20:29:01 +0000181}
182
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000183void BytecodeWriter::processInstruction(const Instruction &I) {
184 assert(I.getOpcode() < 64 && "Opcode too big???");
Chris Lattner00950542001-06-06 20:29:01 +0000185
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000186 unsigned NumOperands = I.getNumOperands();
Chris Lattner00950542001-06-06 20:29:01 +0000187 int MaxOpSlot = 0;
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000188 int Slots[3]; Slots[0] = (1 << 12)-1; // Marker to signify 0 operands
Chris Lattner00950542001-06-06 20:29:01 +0000189
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000190 for (unsigned i = 0; i < NumOperands; ++i) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000191 const Value *Def = I.getOperand(i);
Chris Lattner00950542001-06-06 20:29:01 +0000192 int slot = Table.getValSlot(Def);
193 assert(slot != -1 && "Broken bytecode!");
194 if (slot > MaxOpSlot) MaxOpSlot = slot;
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000195 if (i < 3) Slots[i] = slot;
Chris Lattner00950542001-06-06 20:29:01 +0000196 }
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 //
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000204 const Type *Ty;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000205 switch (I.getOpcode()) {
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000206 case Instruction::Malloc:
207 case Instruction::Alloca:
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000208 Ty = I.getType(); // Malloc & Alloca ALWAYS want to encode the return type
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000209 break;
210 case Instruction::Store:
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000211 Ty = I.getOperand(1)->getType(); // Encode the pointer type...
Chris Lattner9b625032002-05-06 16:15:30 +0000212 assert(isa<PointerType>(Ty) && "Store to nonpointer type!?!?");
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000213 break;
214 default: // Otherwise use the default behavior...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000215 Ty = NumOperands ? I.getOperand(0)->getType() : I.getType();
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000216 break;
217 }
Chris Lattner00950542001-06-06 20:29:01 +0000218
219 unsigned Type;
220 int Slot = Table.getValSlot(Ty);
221 assert(Slot != -1 && "Type not available!!?!");
222 Type = (unsigned)Slot;
223
Chris Lattner7c501472001-07-28 17:51:21 +0000224 // 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
Chris Lattner09083092001-07-08 04:57:15 +0000229 // Handle the special case for cast...
Chris Lattner5ab1f872001-10-21 00:14:44 +0000230 if (isa<CastInst>(I)) {
Chris Lattner09083092001-07-08 04:57:15 +0000231 // 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!
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000233 Slots[1] = Table.getValSlot(I.getType());
Chris Lattner09083092001-07-08 04:57:15 +0000234 assert(Slots[1] != -1 && "Cast return type unknown?");
235 if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
236 NumOperands++;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000237 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)){// Handle VarArg calls
Chris Lattner9fcccb02002-06-05 17:49:40 +0000238 const PointerType *Ty = cast<PointerType>(CI->getCalledValue()->getType());
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000239 if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000240 outputInstrVarArgsCall(CI, Table, Type, Out);
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000241 return;
242 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000243 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {// ... & Invokes
Chris Lattner9fcccb02002-06-05 17:49:40 +0000244 const PointerType *Ty = cast<PointerType>(II->getCalledValue()->getType());
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000245 if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000246 outputInstrVarArgsCall(II, Table, Type, Out);
Chris Lattneref9c23f2001-10-03 14:53:21 +0000247 return;
248 }
Chris Lattner09083092001-07-08 04:57:15 +0000249 }
Chris Lattner00950542001-06-06 20:29:01 +0000250
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
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000260 outputInstructionFormat1(&I, Table, Slots, Type, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000261 return;
Chris Lattner00950542001-06-06 20:29:01 +0000262 }
263 break;
264
265 case 2:
266 if (MaxOpSlot < (1 << 8)) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000267 outputInstructionFormat2(&I, Table, Slots, Type, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000268 return;
Chris Lattner00950542001-06-06 20:29:01 +0000269 }
270 break;
271
272 case 3:
273 if (MaxOpSlot < (1 << 6)) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000274 outputInstructionFormat3(&I, Table, Slots, Type, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000275 return;
Chris Lattner00950542001-06-06 20:29:01 +0000276 }
277 break;
278 }
279
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000280 // 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.
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000282 outputInstructionFormat0(&I, Table, Type, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000283}