blob: fe03554827f8667a3115850cbd20c4e50c4e89f6 [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 Lattner00950542001-06-06 20:29:01 +000014#include "llvm/DerivedTypes.h"
Chris Lattneref9c23f2001-10-03 14:53:21 +000015#include "llvm/iOther.h"
Chris Lattner1b98c5c2001-10-13 06:48:38 +000016#include "llvm/iTerminators.h"
Chris Lattnera92f6962002-10-01 22:38:41 +000017#include "Support/Statistic.h"
Chris Lattner00950542001-06-06 20:29:01 +000018#include <algorithm>
19
Chris Lattnerce6ef112002-07-26 18:40:14 +000020static Statistic<>
Chris Lattnera92f6962002-10-01 22:38:41 +000021NumOversized("bytecodewriter", "Number of oversized instructions");
Chris Lattnerce6ef112002-07-26 18:40:14 +000022static Statistic<>
Chris Lattnera92f6962002-10-01 22:38:41 +000023NumNormal("bytecodewriter", "Number of normal instructions");
Chris Lattnerce6ef112002-07-26 18:40:14 +000024
Chris Lattner00950542001-06-06 20:29:01 +000025typedef unsigned char uchar;
26
27// outputInstructionFormat0 - Output those wierd instructions that have a large
28// number of operands or have large operands themselves...
29//
30// Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
31//
32static void outputInstructionFormat0(const Instruction *I,
33 const SlotCalculator &Table,
Chris Lattner697954c2002-01-20 22:54:45 +000034 unsigned Type, std::deque<uchar> &Out) {
Chris Lattner00950542001-06-06 20:29:01 +000035 // Opcode must have top two bits clear...
Chris Lattner2b9f6002001-10-23 03:21:10 +000036 output_vbr(I->getOpcode() << 2, Out); // Instruction Opcode ID
Chris Lattner00950542001-06-06 20:29:01 +000037 output_vbr(Type, Out); // Result type
38
Chris Lattnerc8b25d42001-07-07 08:36:50 +000039 unsigned NumArgs = I->getNumOperands();
Chris Lattner5ab1f872001-10-21 00:14:44 +000040 output_vbr(NumArgs + isa<CastInst>(I), Out);
Chris Lattner00950542001-06-06 20:29:01 +000041
Chris Lattnerc8b25d42001-07-07 08:36:50 +000042 for (unsigned i = 0; i < NumArgs; ++i) {
Chris Lattnere5a57ee2001-07-25 22:47:55 +000043 int Slot = Table.getValSlot(I->getOperand(i));
44 assert(Slot >= 0 && "No slot number for value!?!?");
45 output_vbr((unsigned)Slot, Out);
46 }
Chris Lattner5ab1f872001-10-21 00:14:44 +000047
48 if (isa<CastInst>(I)) {
49 int Slot = Table.getValSlot(I->getType());
50 assert(Slot != -1 && "Cast return type unknown?");
51 output_vbr((unsigned)Slot, Out);
52 }
53
Chris Lattnere5a57ee2001-07-25 22:47:55 +000054 align32(Out); // We must maintain correct alignment!
Chris Lattnerce6ef112002-07-26 18:40:14 +000055 ++NumOversized;
Chris Lattnere5a57ee2001-07-25 22:47:55 +000056}
57
58
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000059// outputInstrVarArgsCall - Output the obsurdly annoying varargs function calls.
Chris Lattnere5a57ee2001-07-25 22:47:55 +000060// This are more annoying than most because the signature of the call does not
61// tell us anything about the types of the arguments in the varargs portion.
62// Because of this, we encode (as type 0) all of the argument types explicitly
63// before the argument value. This really sucks, but you shouldn't be using
64// varargs functions in your code! *death to printf*!
65//
66// Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
67//
68static void outputInstrVarArgsCall(const Instruction *I,
69 const SlotCalculator &Table, unsigned Type,
Chris Lattner697954c2002-01-20 22:54:45 +000070 std::deque<uchar> &Out) {
Chris Lattner1b98c5c2001-10-13 06:48:38 +000071 assert(isa<CallInst>(I) || isa<InvokeInst>(I));
Chris Lattnere5a57ee2001-07-25 22:47:55 +000072 // Opcode must have top two bits clear...
Chris Lattner2b9f6002001-10-23 03:21:10 +000073 output_vbr(I->getOpcode() << 2, Out); // Instruction Opcode ID
Chris Lattnere5a57ee2001-07-25 22:47:55 +000074 output_vbr(Type, Out); // Result type (varargs type)
75
76 unsigned NumArgs = I->getNumOperands();
Chris Lattner1b98c5c2001-10-13 06:48:38 +000077 output_vbr(NumArgs*2, Out);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000078 // TODO: Don't need to emit types for the fixed types of the varargs function
Chris Lattner1b98c5c2001-10-13 06:48:38 +000079 // prototype...
Chris Lattnere5a57ee2001-07-25 22:47:55 +000080
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000081 // The type for the function has already been emitted in the type field of the
Chris Lattner1b98c5c2001-10-13 06:48:38 +000082 // instruction. Just emit the slot # now.
Chris Lattnere5a57ee2001-07-25 22:47:55 +000083 int Slot = Table.getValSlot(I->getOperand(0));
84 assert(Slot >= 0 && "No slot number for value!?!?");
85 output_vbr((unsigned)Slot, Out);
86
Chris Lattner1b98c5c2001-10-13 06:48:38 +000087 // Output a dummy field to fill Arg#2 in the reader that is currently unused
88 // for varargs calls. This is a gross hack to make the code simpler, but we
89 // aren't really doing very small bytecode for varargs calls anyways.
90 // FIXME in the future: Smaller bytecode for varargs calls
91 output_vbr(0, Out);
Chris Lattnere5a57ee2001-07-25 22:47:55 +000092
Chris Lattner1b98c5c2001-10-13 06:48:38 +000093 for (unsigned i = 1; i < NumArgs; ++i) {
Chris Lattnere5a57ee2001-07-25 22:47:55 +000094 // Output Arg Type ID
95 Slot = Table.getValSlot(I->getOperand(i)->getType());
96 assert(Slot >= 0 && "No slot number for value!?!?");
97 output_vbr((unsigned)Slot, Out);
98
99 // Output arg ID itself
100 Slot = Table.getValSlot(I->getOperand(i));
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000101 assert(Slot >= 0 && "No slot number for value!?!?");
Chris Lattner00950542001-06-06 20:29:01 +0000102 output_vbr((unsigned)Slot, Out);
103 }
104 align32(Out); // We must maintain correct alignment!
Chris Lattnerce6ef112002-07-26 18:40:14 +0000105 ++NumOversized;
Chris Lattner00950542001-06-06 20:29:01 +0000106}
107
108
109// outputInstructionFormat1 - Output one operand instructions, knowing that no
110// operand index is >= 2^12.
111//
112static void outputInstructionFormat1(const Instruction *I,
113 const SlotCalculator &Table, int *Slots,
Chris Lattner697954c2002-01-20 22:54:45 +0000114 unsigned Type, std::deque<uchar> &Out) {
Chris Lattner2b9f6002001-10-23 03:21:10 +0000115 unsigned Opcode = I->getOpcode(); // Instruction Opcode ID
Chris Lattner00950542001-06-06 20:29:01 +0000116
117 // bits Instruction format:
118 // --------------------------
Chris Lattner2b9f6002001-10-23 03:21:10 +0000119 // 01-00: Opcode type, fixed to 1.
120 // 07-02: Opcode
121 // 19-08: Resulting type plane
122 // 31-20: Operand #1 (if set to (2^12-1), then zero operands)
Chris Lattner00950542001-06-06 20:29:01 +0000123 //
Chris Lattner2b9f6002001-10-23 03:21:10 +0000124 unsigned Bits = 1 | (Opcode << 2) | (Type << 8) | (Slots[0] << 20);
Chris Lattner00950542001-06-06 20:29:01 +0000125 // cerr << "1 " << IType << " " << Type << " " << Slots[0] << endl;
Chris Lattner2b9f6002001-10-23 03:21:10 +0000126 output(Bits, Out);
Chris Lattnerce6ef112002-07-26 18:40:14 +0000127 ++NumNormal;
Chris Lattner00950542001-06-06 20:29:01 +0000128}
129
130
131// outputInstructionFormat2 - Output two operand instructions, knowing that no
132// operand index is >= 2^8.
133//
134static void outputInstructionFormat2(const Instruction *I,
135 const SlotCalculator &Table, int *Slots,
Chris Lattner697954c2002-01-20 22:54:45 +0000136 unsigned Type, std::deque<uchar> &Out) {
Chris Lattner2b9f6002001-10-23 03:21:10 +0000137 unsigned Opcode = I->getOpcode(); // Instruction Opcode ID
Chris Lattner00950542001-06-06 20:29:01 +0000138
139 // bits Instruction format:
140 // --------------------------
Chris Lattner2b9f6002001-10-23 03:21:10 +0000141 // 01-00: Opcode type, fixed to 2.
142 // 07-02: Opcode
143 // 15-08: Resulting type plane
144 // 23-16: Operand #1
145 // 31-24: Operand #2
Chris Lattner00950542001-06-06 20:29:01 +0000146 //
Chris Lattner2b9f6002001-10-23 03:21:10 +0000147 unsigned Bits = 2 | (Opcode << 2) | (Type << 8) |
148 (Slots[0] << 16) | (Slots[1] << 24);
Chris Lattner00950542001-06-06 20:29:01 +0000149 // cerr << "2 " << IType << " " << Type << " " << Slots[0] << " "
150 // << Slots[1] << endl;
Chris Lattner2b9f6002001-10-23 03:21:10 +0000151 output(Bits, Out);
Chris Lattnerce6ef112002-07-26 18:40:14 +0000152 ++NumNormal;
Chris Lattner00950542001-06-06 20:29:01 +0000153}
154
155
156// outputInstructionFormat3 - Output three operand instructions, knowing that no
157// operand index is >= 2^6.
158//
159static void outputInstructionFormat3(const Instruction *I,
160 const SlotCalculator &Table, int *Slots,
Chris Lattner697954c2002-01-20 22:54:45 +0000161 unsigned Type, std::deque<uchar> &Out) {
Chris Lattner2b9f6002001-10-23 03:21:10 +0000162 unsigned Opcode = I->getOpcode(); // Instruction Opcode ID
Chris Lattner00950542001-06-06 20:29:01 +0000163
164 // bits Instruction format:
165 // --------------------------
Chris Lattner2b9f6002001-10-23 03:21:10 +0000166 // 01-00: Opcode type, fixed to 3.
167 // 07-02: Opcode
168 // 13-08: Resulting type plane
169 // 19-14: Operand #1
170 // 25-20: Operand #2
171 // 31-26: Operand #3
Chris Lattner00950542001-06-06 20:29:01 +0000172 //
Chris Lattner2b9f6002001-10-23 03:21:10 +0000173 unsigned Bits = 3 | (Opcode << 2) | (Type << 8) |
174 (Slots[0] << 14) | (Slots[1] << 20) | (Slots[2] << 26);
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000175 //cerr << "3 " << IType << " " << Type << " " << Slots[0] << " "
176 // << Slots[1] << " " << Slots[2] << endl;
Chris Lattner2b9f6002001-10-23 03:21:10 +0000177 output(Bits, Out);
Chris Lattnerce6ef112002-07-26 18:40:14 +0000178 ++NumNormal;
Chris Lattner00950542001-06-06 20:29:01 +0000179}
180
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000181void BytecodeWriter::processInstruction(const Instruction &I) {
182 assert(I.getOpcode() < 64 && "Opcode too big???");
Chris Lattner00950542001-06-06 20:29:01 +0000183
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000184 unsigned NumOperands = I.getNumOperands();
Chris Lattner00950542001-06-06 20:29:01 +0000185 int MaxOpSlot = 0;
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000186 int Slots[3]; Slots[0] = (1 << 12)-1; // Marker to signify 0 operands
Chris Lattner00950542001-06-06 20:29:01 +0000187
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000188 for (unsigned i = 0; i < NumOperands; ++i) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000189 const Value *Def = I.getOperand(i);
Chris Lattner00950542001-06-06 20:29:01 +0000190 int slot = Table.getValSlot(Def);
191 assert(slot != -1 && "Broken bytecode!");
192 if (slot > MaxOpSlot) MaxOpSlot = slot;
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000193 if (i < 3) Slots[i] = slot;
Chris Lattner00950542001-06-06 20:29:01 +0000194 }
195
196 // Figure out which type to encode with the instruction. Typically we want
197 // the type of the first parameter, as opposed to the type of the instruction
198 // (for example, with setcc, we always know it returns bool, but the type of
199 // the first param is actually interesting). But if we have no arguments
200 // we take the type of the instruction itself.
201 //
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000202 const Type *Ty;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000203 switch (I.getOpcode()) {
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000204 case Instruction::Malloc:
205 case Instruction::Alloca:
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000206 Ty = I.getType(); // Malloc & Alloca ALWAYS want to encode the return type
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000207 break;
208 case Instruction::Store:
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000209 Ty = I.getOperand(1)->getType(); // Encode the pointer type...
Chris Lattner9b625032002-05-06 16:15:30 +0000210 assert(isa<PointerType>(Ty) && "Store to nonpointer type!?!?");
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000211 break;
212 default: // Otherwise use the default behavior...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000213 Ty = NumOperands ? I.getOperand(0)->getType() : I.getType();
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000214 break;
215 }
Chris Lattner00950542001-06-06 20:29:01 +0000216
217 unsigned Type;
218 int Slot = Table.getValSlot(Ty);
219 assert(Slot != -1 && "Type not available!!?!");
220 Type = (unsigned)Slot;
221
Chris Lattner7c501472001-07-28 17:51:21 +0000222 // Make sure that we take the type number into consideration. We don't want
223 // to overflow the field size for the instruction format we select.
224 //
225 if (Slot > MaxOpSlot) MaxOpSlot = Slot;
226
Chris Lattner09083092001-07-08 04:57:15 +0000227 // Handle the special case for cast...
Chris Lattner5ab1f872001-10-21 00:14:44 +0000228 if (isa<CastInst>(I)) {
Chris Lattner09083092001-07-08 04:57:15 +0000229 // Cast has to encode the destination type as the second argument in the
230 // packet, or else we won't know what type to cast to!
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000231 Slots[1] = Table.getValSlot(I.getType());
Chris Lattner09083092001-07-08 04:57:15 +0000232 assert(Slots[1] != -1 && "Cast return type unknown?");
233 if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
234 NumOperands++;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000235 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)){// Handle VarArg calls
Chris Lattner9fcccb02002-06-05 17:49:40 +0000236 const PointerType *Ty = cast<PointerType>(CI->getCalledValue()->getType());
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000237 if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000238 outputInstrVarArgsCall(CI, Table, Type, Out);
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000239 return;
240 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000241 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {// ... & Invokes
Chris Lattner9fcccb02002-06-05 17:49:40 +0000242 const PointerType *Ty = cast<PointerType>(II->getCalledValue()->getType());
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000243 if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000244 outputInstrVarArgsCall(II, Table, Type, Out);
Chris Lattneref9c23f2001-10-03 14:53:21 +0000245 return;
246 }
Chris Lattner09083092001-07-08 04:57:15 +0000247 }
Chris Lattner00950542001-06-06 20:29:01 +0000248
249 // Decide which instruction encoding to use. This is determined primarily by
250 // the number of operands, and secondarily by whether or not the max operand
251 // will fit into the instruction encoding. More operands == fewer bits per
252 // operand.
253 //
254 switch (NumOperands) {
255 case 0:
256 case 1:
257 if (MaxOpSlot < (1 << 12)-1) { // -1 because we use 4095 to indicate 0 ops
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000258 outputInstructionFormat1(&I, Table, Slots, Type, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000259 return;
Chris Lattner00950542001-06-06 20:29:01 +0000260 }
261 break;
262
263 case 2:
264 if (MaxOpSlot < (1 << 8)) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000265 outputInstructionFormat2(&I, Table, Slots, Type, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000266 return;
Chris Lattner00950542001-06-06 20:29:01 +0000267 }
268 break;
269
270 case 3:
271 if (MaxOpSlot < (1 << 6)) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000272 outputInstructionFormat3(&I, Table, Slots, Type, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000273 return;
Chris Lattner00950542001-06-06 20:29:01 +0000274 }
275 break;
276 }
277
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000278 // If we weren't handled before here, we either have a large number of
279 // operands or a large operand index that we are refering to.
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000280 outputInstructionFormat0(&I, Table, Type, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000281}