blob: 9e063510dfe5c39f33161686a2156ed3eabdcdba [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- InstructionWriter.cpp - Functions for writing instructions --------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements the routines for encoding instruction opcodes to a
11// bytecode stream.
12//
Chris Lattner00950542001-06-06 20:29:01 +000013//===----------------------------------------------------------------------===//
14
15#include "WriterInternals.h"
16#include "llvm/Module.h"
Chris Lattner00950542001-06-06 20:29:01 +000017#include "llvm/DerivedTypes.h"
Chris Lattner0fe56f42003-09-08 17:58:37 +000018#include "llvm/Instructions.h"
Chris Lattner5fa428f2004-04-05 01:27:26 +000019#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattnera92f6962002-10-01 22:38:41 +000020#include "Support/Statistic.h"
Chris Lattner00950542001-06-06 20:29:01 +000021#include <algorithm>
Chris Lattner44f549b2004-01-10 18:49:43 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Chris Lattner00950542001-06-06 20:29:01 +000024typedef unsigned char uchar;
25
26// outputInstructionFormat0 - Output those wierd instructions that have a large
27// number of operands or have large operands themselves...
28//
29// Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
30//
Chris Lattner0fe56f42003-09-08 17:58:37 +000031static void outputInstructionFormat0(const Instruction *I, unsigned Opcode,
Chris Lattner00950542001-06-06 20:29:01 +000032 const SlotCalculator &Table,
Chris Lattner697954c2002-01-20 22:54:45 +000033 unsigned Type, std::deque<uchar> &Out) {
Chris Lattner00950542001-06-06 20:29:01 +000034 // Opcode must have top two bits clear...
Chris Lattner0fe56f42003-09-08 17:58:37 +000035 output_vbr(Opcode << 2, Out); // Instruction Opcode ID
Chris Lattner00950542001-06-06 20:29:01 +000036 output_vbr(Type, Out); // Result type
37
Chris Lattnerc8b25d42001-07-07 08:36:50 +000038 unsigned NumArgs = I->getNumOperands();
Chris Lattnereff112c2003-10-18 05:54:48 +000039 output_vbr(NumArgs + (isa<CastInst>(I) || isa<VANextInst>(I) ||
40 isa<VAArgInst>(I)), Out);
Chris Lattner00950542001-06-06 20:29:01 +000041
Chris Lattner5fa428f2004-04-05 01:27:26 +000042 if (!isa<GetElementPtrInst>(&I)) {
43 for (unsigned i = 0; i < NumArgs; ++i) {
44 int Slot = Table.getSlot(I->getOperand(i));
45 assert(Slot >= 0 && "No slot number for value!?!?");
46 output_vbr((unsigned)Slot, Out);
47 }
Chris Lattner5ab1f872001-10-21 00:14:44 +000048
Chris Lattner5fa428f2004-04-05 01:27:26 +000049 if (isa<CastInst>(I) || isa<VAArgInst>(I)) {
50 int Slot = Table.getSlot(I->getType());
51 assert(Slot != -1 && "Cast return type unknown?");
52 output_vbr((unsigned)Slot, Out);
53 } else if (const VANextInst *VAI = dyn_cast<VANextInst>(I)) {
54 int Slot = Table.getSlot(VAI->getArgType());
55 assert(Slot != -1 && "VarArg argument type unknown?");
56 output_vbr((unsigned)Slot, Out);
57 }
58
59 } else {
60 int Slot = Table.getSlot(I->getOperand(0));
61 assert(Slot >= 0 && "No slot number for value!?!?");
62 output_vbr(unsigned(Slot), Out);
63
64 // We need to encode the type of sequential type indices into their slot #
65 unsigned Idx = 1;
66 for (gep_type_iterator TI = gep_type_begin(I), E = gep_type_end(I);
67 Idx != NumArgs; ++TI, ++Idx) {
68 Slot = Table.getSlot(I->getOperand(Idx));
69 assert(Slot >= 0 && "No slot number for value!?!?");
70
71 if (isa<SequentialType>(*TI)) {
72 unsigned IdxId;
73 switch (I->getOperand(Idx)->getType()->getPrimitiveID()) {
74 default: assert(0 && "Unknown index type!");
75 case Type::UIntTyID: IdxId = 0; break;
76 case Type::IntTyID: IdxId = 1; break;
77 case Type::ULongTyID: IdxId = 2; break;
78 case Type::LongTyID: IdxId = 3; break;
79 }
80 Slot = (Slot << 2) | IdxId;
81 }
82 output_vbr(unsigned(Slot), Out);
83 }
Chris Lattner5ab1f872001-10-21 00:14:44 +000084 }
85
Chris Lattnere5a57ee2001-07-25 22:47:55 +000086 align32(Out); // We must maintain correct alignment!
87}
88
89
Misha Brukman37f92e22003-09-11 22:34:13 +000090// outputInstrVarArgsCall - Output the absurdly annoying varargs function calls.
Chris Lattnere5a57ee2001-07-25 22:47:55 +000091// This are more annoying than most because the signature of the call does not
92// tell us anything about the types of the arguments in the varargs portion.
93// Because of this, we encode (as type 0) all of the argument types explicitly
94// before the argument value. This really sucks, but you shouldn't be using
95// varargs functions in your code! *death to printf*!
96//
97// Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
98//
Chris Lattner0fe56f42003-09-08 17:58:37 +000099static void outputInstrVarArgsCall(const Instruction *I, unsigned Opcode,
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000100 const SlotCalculator &Table, unsigned Type,
Chris Lattner697954c2002-01-20 22:54:45 +0000101 std::deque<uchar> &Out) {
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000102 assert(isa<CallInst>(I) || isa<InvokeInst>(I));
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000103 // Opcode must have top two bits clear...
Chris Lattner0fe56f42003-09-08 17:58:37 +0000104 output_vbr(Opcode << 2, Out); // Instruction Opcode ID
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000105 output_vbr(Type, Out); // Result type (varargs type)
106
Chris Lattnereff112c2003-10-18 05:54:48 +0000107 const PointerType *PTy = cast<PointerType>(I->getOperand(0)->getType());
108 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
109 unsigned NumParams = FTy->getNumParams();
110
111 unsigned NumFixedOperands;
112 if (isa<CallInst>(I)) {
113 // Output an operand for the callee and each fixed argument, then two for
114 // each variable argument.
115 NumFixedOperands = 1+NumParams;
116 } else {
117 assert(isa<InvokeInst>(I) && "Not call or invoke??");
118 // Output an operand for the callee and destinations, then two for each
119 // variable argument.
120 NumFixedOperands = 3+NumParams;
121 }
122 output_vbr(2 * I->getNumOperands()-NumFixedOperands, Out);
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000123
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000124 // The type for the function has already been emitted in the type field of the
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000125 // instruction. Just emit the slot # now.
Chris Lattnereff112c2003-10-18 05:54:48 +0000126 for (unsigned i = 0; i != NumFixedOperands; ++i) {
127 int Slot = Table.getSlot(I->getOperand(i));
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000128 assert(Slot >= 0 && "No slot number for value!?!?");
129 output_vbr((unsigned)Slot, Out);
Chris Lattnereff112c2003-10-18 05:54:48 +0000130 }
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000131
Chris Lattnereff112c2003-10-18 05:54:48 +0000132 for (unsigned i = NumFixedOperands, e = I->getNumOperands(); i != e; ++i) {
133 // Output Arg Type ID
134 int Slot = Table.getSlot(I->getOperand(i)->getType());
135 assert(Slot >= 0 && "No slot number for value!?!?");
136 output_vbr((unsigned)Slot, Out);
137
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000138 // Output arg ID itself
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000139 Slot = Table.getSlot(I->getOperand(i));
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000140 assert(Slot >= 0 && "No slot number for value!?!?");
Chris Lattner00950542001-06-06 20:29:01 +0000141 output_vbr((unsigned)Slot, Out);
142 }
143 align32(Out); // We must maintain correct alignment!
144}
145
146
147// outputInstructionFormat1 - Output one operand instructions, knowing that no
148// operand index is >= 2^12.
149//
Chris Lattner0fe56f42003-09-08 17:58:37 +0000150static void outputInstructionFormat1(const Instruction *I, unsigned Opcode,
Chris Lattner5fa428f2004-04-05 01:27:26 +0000151 const SlotCalculator &Table,
152 unsigned *Slots, unsigned Type,
153 std::deque<uchar> &Out) {
Chris Lattner00950542001-06-06 20:29:01 +0000154 // bits Instruction format:
155 // --------------------------
Chris Lattner2b9f6002001-10-23 03:21:10 +0000156 // 01-00: Opcode type, fixed to 1.
157 // 07-02: Opcode
158 // 19-08: Resulting type plane
159 // 31-20: Operand #1 (if set to (2^12-1), then zero operands)
Chris Lattner00950542001-06-06 20:29:01 +0000160 //
Chris Lattner2b9f6002001-10-23 03:21:10 +0000161 unsigned Bits = 1 | (Opcode << 2) | (Type << 8) | (Slots[0] << 20);
Chris Lattner00950542001-06-06 20:29:01 +0000162 // cerr << "1 " << IType << " " << Type << " " << Slots[0] << endl;
Chris Lattner2b9f6002001-10-23 03:21:10 +0000163 output(Bits, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000164}
165
166
167// outputInstructionFormat2 - Output two operand instructions, knowing that no
168// operand index is >= 2^8.
169//
Chris Lattner0fe56f42003-09-08 17:58:37 +0000170static void outputInstructionFormat2(const Instruction *I, unsigned Opcode,
Chris Lattner5fa428f2004-04-05 01:27:26 +0000171 const SlotCalculator &Table,
172 unsigned *Slots, unsigned Type,
173 std::deque<uchar> &Out) {
Chris Lattner00950542001-06-06 20:29:01 +0000174 // bits Instruction format:
175 // --------------------------
Chris Lattner2b9f6002001-10-23 03:21:10 +0000176 // 01-00: Opcode type, fixed to 2.
177 // 07-02: Opcode
178 // 15-08: Resulting type plane
179 // 23-16: Operand #1
180 // 31-24: Operand #2
Chris Lattner00950542001-06-06 20:29:01 +0000181 //
Chris Lattner2b9f6002001-10-23 03:21:10 +0000182 unsigned Bits = 2 | (Opcode << 2) | (Type << 8) |
183 (Slots[0] << 16) | (Slots[1] << 24);
Chris Lattner00950542001-06-06 20:29:01 +0000184 // cerr << "2 " << IType << " " << Type << " " << Slots[0] << " "
185 // << Slots[1] << endl;
Chris Lattner2b9f6002001-10-23 03:21:10 +0000186 output(Bits, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000187}
188
189
190// outputInstructionFormat3 - Output three operand instructions, knowing that no
191// operand index is >= 2^6.
192//
Chris Lattner0fe56f42003-09-08 17:58:37 +0000193static void outputInstructionFormat3(const Instruction *I, unsigned Opcode,
Chris Lattner5fa428f2004-04-05 01:27:26 +0000194 const SlotCalculator &Table,
195 unsigned *Slots, unsigned Type,
196 std::deque<uchar> &Out) {
Chris Lattner00950542001-06-06 20:29:01 +0000197 // bits Instruction format:
198 // --------------------------
Chris Lattner2b9f6002001-10-23 03:21:10 +0000199 // 01-00: Opcode type, fixed to 3.
200 // 07-02: Opcode
201 // 13-08: Resulting type plane
202 // 19-14: Operand #1
203 // 25-20: Operand #2
204 // 31-26: Operand #3
Chris Lattner00950542001-06-06 20:29:01 +0000205 //
Chris Lattner2b9f6002001-10-23 03:21:10 +0000206 unsigned Bits = 3 | (Opcode << 2) | (Type << 8) |
207 (Slots[0] << 14) | (Slots[1] << 20) | (Slots[2] << 26);
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000208 //cerr << "3 " << IType << " " << Type << " " << Slots[0] << " "
209 // << Slots[1] << " " << Slots[2] << endl;
Chris Lattner2b9f6002001-10-23 03:21:10 +0000210 output(Bits, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000211}
212
Chris Lattnercf3e67f2004-01-18 21:08:52 +0000213void BytecodeWriter::outputInstruction(const Instruction &I) {
Chris Lattner0fe56f42003-09-08 17:58:37 +0000214 assert(I.getOpcode() < 62 && "Opcode too big???");
215 unsigned Opcode = I.getOpcode();
Chris Lattner5fa428f2004-04-05 01:27:26 +0000216 unsigned NumOperands = I.getNumOperands();
Chris Lattner0fe56f42003-09-08 17:58:37 +0000217
218 // Encode 'volatile load' as 62 and 'volatile store' as 63.
219 if (isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile())
220 Opcode = 62;
221 if (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())
222 Opcode = 63;
Chris Lattner00950542001-06-06 20:29:01 +0000223
Chris Lattner00950542001-06-06 20:29:01 +0000224 // Figure out which type to encode with the instruction. Typically we want
225 // the type of the first parameter, as opposed to the type of the instruction
226 // (for example, with setcc, we always know it returns bool, but the type of
227 // the first param is actually interesting). But if we have no arguments
228 // we take the type of the instruction itself.
229 //
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000230 const Type *Ty;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000231 switch (I.getOpcode()) {
Chris Lattner46b787e2004-03-12 05:52:01 +0000232 case Instruction::Select:
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000233 case Instruction::Malloc:
234 case Instruction::Alloca:
Chris Lattner46b787e2004-03-12 05:52:01 +0000235 Ty = I.getType(); // These ALWAYS want to encode the return type
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000236 break;
237 case Instruction::Store:
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000238 Ty = I.getOperand(1)->getType(); // Encode the pointer type...
Chris Lattner9b625032002-05-06 16:15:30 +0000239 assert(isa<PointerType>(Ty) && "Store to nonpointer type!?!?");
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000240 break;
241 default: // Otherwise use the default behavior...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000242 Ty = NumOperands ? I.getOperand(0)->getType() : I.getType();
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000243 break;
244 }
Chris Lattner00950542001-06-06 20:29:01 +0000245
246 unsigned Type;
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000247 int Slot = Table.getSlot(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000248 assert(Slot != -1 && "Type not available!!?!");
249 Type = (unsigned)Slot;
250
Chris Lattner5fa428f2004-04-05 01:27:26 +0000251 // Varargs calls and invokes are encoded entirely different from any other
252 // instructions.
253 if (const CallInst *CI = dyn_cast<CallInst>(&I)){
254 const PointerType *Ty =cast<PointerType>(CI->getCalledValue()->getType());
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000255 if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
Chris Lattner0fe56f42003-09-08 17:58:37 +0000256 outputInstrVarArgsCall(CI, Opcode, Table, Type, Out);
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000257 return;
258 }
Chris Lattner5fa428f2004-04-05 01:27:26 +0000259 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
260 const PointerType *Ty =cast<PointerType>(II->getCalledValue()->getType());
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000261 if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
Chris Lattner0fe56f42003-09-08 17:58:37 +0000262 outputInstrVarArgsCall(II, Opcode, Table, Type, Out);
Chris Lattneref9c23f2001-10-03 14:53:21 +0000263 return;
264 }
Chris Lattner09083092001-07-08 04:57:15 +0000265 }
Chris Lattner00950542001-06-06 20:29:01 +0000266
Chris Lattner5fa428f2004-04-05 01:27:26 +0000267 if (NumOperands <= 3) {
268 // Make sure that we take the type number into consideration. We don't want
269 // to overflow the field size for the instruction format we select.
270 //
271 unsigned MaxOpSlot = Type;
272 unsigned Slots[3]; Slots[0] = (1 << 12)-1; // Marker to signify 0 operands
273
274 for (unsigned i = 0; i != NumOperands; ++i) {
275 int slot = Table.getSlot(I.getOperand(i));
276 assert(slot != -1 && "Broken bytecode!");
277 if (unsigned(slot) > MaxOpSlot) MaxOpSlot = unsigned(slot);
278 Slots[i] = unsigned(slot);
Chris Lattner00950542001-06-06 20:29:01 +0000279 }
Chris Lattner00950542001-06-06 20:29:01 +0000280
Chris Lattner5fa428f2004-04-05 01:27:26 +0000281 // Handle the special cases for various instructions...
282 if (isa<CastInst>(I) || isa<VAArgInst>(I)) {
283 // Cast has to encode the destination type as the second argument in the
284 // packet, or else we won't know what type to cast to!
285 Slots[1] = Table.getSlot(I.getType());
286 assert(Slots[1] != ~0U && "Cast return type unknown?");
287 if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
288 NumOperands++;
289 } else if (const VANextInst *VANI = dyn_cast<VANextInst>(&I)) {
290 Slots[1] = Table.getSlot(VANI->getArgType());
291 assert(Slots[1] != ~0U && "va_next return type unknown?");
292 if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
293 NumOperands++;
294 } else if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I)) {
295 // We need to encode the type of sequential type indices into their slot #
296 unsigned Idx = 1;
297 for (gep_type_iterator I = gep_type_begin(GEP), E = gep_type_end(GEP);
298 I != E; ++I, ++Idx)
299 if (isa<SequentialType>(*I)) {
300 unsigned IdxId;
301 switch (GEP->getOperand(Idx)->getType()->getPrimitiveID()) {
302 default: assert(0 && "Unknown index type!");
303 case Type::UIntTyID: IdxId = 0; break;
304 case Type::IntTyID: IdxId = 1; break;
305 case Type::ULongTyID: IdxId = 2; break;
306 case Type::LongTyID: IdxId = 3; break;
307 }
308 Slots[Idx] = (Slots[Idx] << 2) | IdxId;
309 if (Slots[Idx] > MaxOpSlot) MaxOpSlot = Slots[Idx];
310 }
Chris Lattner00950542001-06-06 20:29:01 +0000311 }
Chris Lattner00950542001-06-06 20:29:01 +0000312
Chris Lattner5fa428f2004-04-05 01:27:26 +0000313 // Decide which instruction encoding to use. This is determined primarily
314 // by the number of operands, and secondarily by whether or not the max
315 // operand will fit into the instruction encoding. More operands == fewer
316 // bits per operand.
317 //
318 switch (NumOperands) {
319 case 0:
320 case 1:
321 if (MaxOpSlot < (1 << 12)-1) { // -1 because we use 4095 to indicate 0 ops
322 outputInstructionFormat1(&I, Opcode, Table, Slots, Type, Out);
323 return;
324 }
325 break;
326
327 case 2:
328 if (MaxOpSlot < (1 << 8)) {
329 outputInstructionFormat2(&I, Opcode, Table, Slots, Type, Out);
330 return;
331 }
332 break;
333
334 case 3:
335 if (MaxOpSlot < (1 << 6)) {
336 outputInstructionFormat3(&I, Opcode, Table, Slots, Type, Out);
337 return;
338 }
339 break;
340 default:
341 break;
Chris Lattner00950542001-06-06 20:29:01 +0000342 }
Chris Lattner00950542001-06-06 20:29:01 +0000343 }
344
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000345 // If we weren't handled before here, we either have a large number of
Misha Brukman37f92e22003-09-11 22:34:13 +0000346 // operands or a large operand index that we are referring to.
Chris Lattner0fe56f42003-09-08 17:58:37 +0000347 outputInstructionFormat0(&I, Opcode, Table, Type, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000348}