blob: b93a812d52c4e9980189dc503c76932f3c7252bc [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 Lattnera92f6962002-10-01 22:38:41 +000019#include "Support/Statistic.h"
Chris Lattner00950542001-06-06 20:29:01 +000020#include <algorithm>
Chris Lattner44f549b2004-01-10 18:49:43 +000021using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000022
Chris Lattnerce6ef112002-07-26 18:40:14 +000023static Statistic<>
Chris Lattnera407ba12003-01-21 20:13:49 +000024NumInstrs("bytecodewriter", "Number of instructions");
Chris Lattnerd6942d72004-01-14 16:54:21 +000025static Statistic<>
26NumOversizedInstrs("bytecodewriter", "Number of oversized instructions");
27static Statistic<>
28BytesOversizedInstrs("bytecodewriter", "Bytes of oversized instructions");
29
30static Statistic<>
31NumHugeOperandInstrs("bytecodewriter", "Number of instructions with > 3 operands");
32static Statistic<>
33NumOversized1OpInstrs("bytecodewriter", "Number of oversized 1 operand instrs");
34static Statistic<>
35NumOversized2OpInstrs("bytecodewriter", "Number of oversized 2 operand instrs");
36static Statistic<>
37NumOversized3OpInstrs("bytecodewriter", "Number of oversized 3 operand instrs");
38
39static Statistic<>
40NumOversidedBecauseOfTypes("bytecodewriter", "Number of oversized instructions because of their type");
41
Chris Lattnerce6ef112002-07-26 18:40:14 +000042
Chris Lattner00950542001-06-06 20:29:01 +000043typedef unsigned char uchar;
44
45// outputInstructionFormat0 - Output those wierd instructions that have a large
46// number of operands or have large operands themselves...
47//
48// Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
49//
Chris Lattner0fe56f42003-09-08 17:58:37 +000050static void outputInstructionFormat0(const Instruction *I, unsigned Opcode,
Chris Lattner00950542001-06-06 20:29:01 +000051 const SlotCalculator &Table,
Chris Lattner697954c2002-01-20 22:54:45 +000052 unsigned Type, std::deque<uchar> &Out) {
Chris Lattnerd6942d72004-01-14 16:54:21 +000053 NumOversizedInstrs++;
54 BytesOversizedInstrs -= Out.size();
55
Chris Lattner00950542001-06-06 20:29:01 +000056 // Opcode must have top two bits clear...
Chris Lattner0fe56f42003-09-08 17:58:37 +000057 output_vbr(Opcode << 2, Out); // Instruction Opcode ID
Chris Lattner00950542001-06-06 20:29:01 +000058 output_vbr(Type, Out); // Result type
59
Chris Lattnerc8b25d42001-07-07 08:36:50 +000060 unsigned NumArgs = I->getNumOperands();
Chris Lattnereff112c2003-10-18 05:54:48 +000061 output_vbr(NumArgs + (isa<CastInst>(I) || isa<VANextInst>(I) ||
62 isa<VAArgInst>(I)), Out);
Chris Lattner00950542001-06-06 20:29:01 +000063
Chris Lattnerc8b25d42001-07-07 08:36:50 +000064 for (unsigned i = 0; i < NumArgs; ++i) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +000065 int Slot = Table.getSlot(I->getOperand(i));
Chris Lattnere5a57ee2001-07-25 22:47:55 +000066 assert(Slot >= 0 && "No slot number for value!?!?");
67 output_vbr((unsigned)Slot, Out);
68 }
Chris Lattner5ab1f872001-10-21 00:14:44 +000069
Chris Lattnereff112c2003-10-18 05:54:48 +000070 if (isa<CastInst>(I) || isa<VAArgInst>(I)) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +000071 int Slot = Table.getSlot(I->getType());
Chris Lattnereff112c2003-10-18 05:54:48 +000072 assert(Slot != -1 && "Cast return type unknown?");
73 output_vbr((unsigned)Slot, Out);
74 } else if (const VANextInst *VAI = dyn_cast<VANextInst>(I)) {
75 int Slot = Table.getSlot(VAI->getArgType());
76 assert(Slot != -1 && "VarArg argument type unknown?");
Chris Lattner5ab1f872001-10-21 00:14:44 +000077 output_vbr((unsigned)Slot, Out);
78 }
79
Chris Lattnere5a57ee2001-07-25 22:47:55 +000080 align32(Out); // We must maintain correct alignment!
Chris Lattnerd6942d72004-01-14 16:54:21 +000081 BytesOversizedInstrs += Out.size();
Chris Lattnere5a57ee2001-07-25 22:47:55 +000082}
83
84
Misha Brukman37f92e22003-09-11 22:34:13 +000085// outputInstrVarArgsCall - Output the absurdly annoying varargs function calls.
Chris Lattnere5a57ee2001-07-25 22:47:55 +000086// This are more annoying than most because the signature of the call does not
87// tell us anything about the types of the arguments in the varargs portion.
88// Because of this, we encode (as type 0) all of the argument types explicitly
89// before the argument value. This really sucks, but you shouldn't be using
90// varargs functions in your code! *death to printf*!
91//
92// Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
93//
Chris Lattner0fe56f42003-09-08 17:58:37 +000094static void outputInstrVarArgsCall(const Instruction *I, unsigned Opcode,
Chris Lattnere5a57ee2001-07-25 22:47:55 +000095 const SlotCalculator &Table, unsigned Type,
Chris Lattner697954c2002-01-20 22:54:45 +000096 std::deque<uchar> &Out) {
Chris Lattner1b98c5c2001-10-13 06:48:38 +000097 assert(isa<CallInst>(I) || isa<InvokeInst>(I));
Chris Lattnere5a57ee2001-07-25 22:47:55 +000098 // Opcode must have top two bits clear...
Chris Lattner0fe56f42003-09-08 17:58:37 +000099 output_vbr(Opcode << 2, Out); // Instruction Opcode ID
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000100 output_vbr(Type, Out); // Result type (varargs type)
101
Chris Lattnereff112c2003-10-18 05:54:48 +0000102 const PointerType *PTy = cast<PointerType>(I->getOperand(0)->getType());
103 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
104 unsigned NumParams = FTy->getNumParams();
105
106 unsigned NumFixedOperands;
107 if (isa<CallInst>(I)) {
108 // Output an operand for the callee and each fixed argument, then two for
109 // each variable argument.
110 NumFixedOperands = 1+NumParams;
111 } else {
112 assert(isa<InvokeInst>(I) && "Not call or invoke??");
113 // Output an operand for the callee and destinations, then two for each
114 // variable argument.
115 NumFixedOperands = 3+NumParams;
116 }
117 output_vbr(2 * I->getNumOperands()-NumFixedOperands, Out);
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000118
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000119 // The type for the function has already been emitted in the type field of the
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000120 // instruction. Just emit the slot # now.
Chris Lattnereff112c2003-10-18 05:54:48 +0000121 for (unsigned i = 0; i != NumFixedOperands; ++i) {
122 int Slot = Table.getSlot(I->getOperand(i));
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000123 assert(Slot >= 0 && "No slot number for value!?!?");
124 output_vbr((unsigned)Slot, Out);
Chris Lattnereff112c2003-10-18 05:54:48 +0000125 }
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000126
Chris Lattnereff112c2003-10-18 05:54:48 +0000127 for (unsigned i = NumFixedOperands, e = I->getNumOperands(); i != e; ++i) {
128 // Output Arg Type ID
129 int Slot = Table.getSlot(I->getOperand(i)->getType());
130 assert(Slot >= 0 && "No slot number for value!?!?");
131 output_vbr((unsigned)Slot, Out);
132
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000133 // Output arg ID itself
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000134 Slot = Table.getSlot(I->getOperand(i));
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000135 assert(Slot >= 0 && "No slot number for value!?!?");
Chris Lattner00950542001-06-06 20:29:01 +0000136 output_vbr((unsigned)Slot, Out);
137 }
138 align32(Out); // We must maintain correct alignment!
139}
140
141
142// outputInstructionFormat1 - Output one operand instructions, knowing that no
143// operand index is >= 2^12.
144//
Chris Lattner0fe56f42003-09-08 17:58:37 +0000145static void outputInstructionFormat1(const Instruction *I, unsigned Opcode,
Chris Lattner00950542001-06-06 20:29:01 +0000146 const SlotCalculator &Table, int *Slots,
Chris Lattner697954c2002-01-20 22:54:45 +0000147 unsigned Type, std::deque<uchar> &Out) {
Chris Lattner00950542001-06-06 20:29:01 +0000148 // bits Instruction format:
149 // --------------------------
Chris Lattner2b9f6002001-10-23 03:21:10 +0000150 // 01-00: Opcode type, fixed to 1.
151 // 07-02: Opcode
152 // 19-08: Resulting type plane
153 // 31-20: Operand #1 (if set to (2^12-1), then zero operands)
Chris Lattner00950542001-06-06 20:29:01 +0000154 //
Chris Lattner2b9f6002001-10-23 03:21:10 +0000155 unsigned Bits = 1 | (Opcode << 2) | (Type << 8) | (Slots[0] << 20);
Chris Lattner00950542001-06-06 20:29:01 +0000156 // cerr << "1 " << IType << " " << Type << " " << Slots[0] << endl;
Chris Lattner2b9f6002001-10-23 03:21:10 +0000157 output(Bits, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000158}
159
160
161// outputInstructionFormat2 - Output two operand instructions, knowing that no
162// operand index is >= 2^8.
163//
Chris Lattner0fe56f42003-09-08 17:58:37 +0000164static void outputInstructionFormat2(const Instruction *I, unsigned Opcode,
Chris Lattner00950542001-06-06 20:29:01 +0000165 const SlotCalculator &Table, int *Slots,
Chris Lattner697954c2002-01-20 22:54:45 +0000166 unsigned Type, std::deque<uchar> &Out) {
Chris Lattner00950542001-06-06 20:29:01 +0000167 // bits Instruction format:
168 // --------------------------
Chris Lattner2b9f6002001-10-23 03:21:10 +0000169 // 01-00: Opcode type, fixed to 2.
170 // 07-02: Opcode
171 // 15-08: Resulting type plane
172 // 23-16: Operand #1
173 // 31-24: Operand #2
Chris Lattner00950542001-06-06 20:29:01 +0000174 //
Chris Lattner2b9f6002001-10-23 03:21:10 +0000175 unsigned Bits = 2 | (Opcode << 2) | (Type << 8) |
176 (Slots[0] << 16) | (Slots[1] << 24);
Chris Lattner00950542001-06-06 20:29:01 +0000177 // cerr << "2 " << IType << " " << Type << " " << Slots[0] << " "
178 // << Slots[1] << endl;
Chris Lattner2b9f6002001-10-23 03:21:10 +0000179 output(Bits, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000180}
181
182
183// outputInstructionFormat3 - Output three operand instructions, knowing that no
184// operand index is >= 2^6.
185//
Chris Lattner0fe56f42003-09-08 17:58:37 +0000186static void outputInstructionFormat3(const Instruction *I, unsigned Opcode,
Chris Lattner00950542001-06-06 20:29:01 +0000187 const SlotCalculator &Table, int *Slots,
Chris Lattner697954c2002-01-20 22:54:45 +0000188 unsigned Type, std::deque<uchar> &Out) {
Chris Lattner00950542001-06-06 20:29:01 +0000189 // bits Instruction format:
190 // --------------------------
Chris Lattner2b9f6002001-10-23 03:21:10 +0000191 // 01-00: Opcode type, fixed to 3.
192 // 07-02: Opcode
193 // 13-08: Resulting type plane
194 // 19-14: Operand #1
195 // 25-20: Operand #2
196 // 31-26: Operand #3
Chris Lattner00950542001-06-06 20:29:01 +0000197 //
Chris Lattner2b9f6002001-10-23 03:21:10 +0000198 unsigned Bits = 3 | (Opcode << 2) | (Type << 8) |
199 (Slots[0] << 14) | (Slots[1] << 20) | (Slots[2] << 26);
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000200 //cerr << "3 " << IType << " " << Type << " " << Slots[0] << " "
201 // << Slots[1] << " " << Slots[2] << endl;
Chris Lattner2b9f6002001-10-23 03:21:10 +0000202 output(Bits, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000203}
204
Chris Lattnercf3e67f2004-01-18 21:08:52 +0000205void BytecodeWriter::outputInstruction(const Instruction &I) {
Chris Lattner0fe56f42003-09-08 17:58:37 +0000206 assert(I.getOpcode() < 62 && "Opcode too big???");
207 unsigned Opcode = I.getOpcode();
208
209 // Encode 'volatile load' as 62 and 'volatile store' as 63.
210 if (isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile())
211 Opcode = 62;
212 if (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())
213 Opcode = 63;
Chris Lattner00950542001-06-06 20:29:01 +0000214
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000215 unsigned NumOperands = I.getNumOperands();
Chris Lattner00950542001-06-06 20:29:01 +0000216 int MaxOpSlot = 0;
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000217 int Slots[3]; Slots[0] = (1 << 12)-1; // Marker to signify 0 operands
Chris Lattner00950542001-06-06 20:29:01 +0000218
Chris Lattnercf3e67f2004-01-18 21:08:52 +0000219 for (unsigned i = 0; i != NumOperands; ++i) {
220 int slot = Table.getSlot(I.getOperand(i));
Chris Lattner00950542001-06-06 20:29:01 +0000221 assert(slot != -1 && "Broken bytecode!");
222 if (slot > MaxOpSlot) MaxOpSlot = slot;
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000223 if (i < 3) Slots[i] = slot;
Chris Lattner00950542001-06-06 20:29:01 +0000224 }
225
226 // Figure out which type to encode with the instruction. Typically we want
227 // the type of the first parameter, as opposed to the type of the instruction
228 // (for example, with setcc, we always know it returns bool, but the type of
229 // the first param is actually interesting). But if we have no arguments
230 // we take the type of the instruction itself.
231 //
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000232 const Type *Ty;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000233 switch (I.getOpcode()) {
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000234 case Instruction::Malloc:
235 case Instruction::Alloca:
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000236 Ty = I.getType(); // Malloc & Alloca ALWAYS want to encode the return type
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000237 break;
238 case Instruction::Store:
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000239 Ty = I.getOperand(1)->getType(); // Encode the pointer type...
Chris Lattner9b625032002-05-06 16:15:30 +0000240 assert(isa<PointerType>(Ty) && "Store to nonpointer type!?!?");
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000241 break;
242 default: // Otherwise use the default behavior...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000243 Ty = NumOperands ? I.getOperand(0)->getType() : I.getType();
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000244 break;
245 }
Chris Lattner00950542001-06-06 20:29:01 +0000246
247 unsigned Type;
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000248 int Slot = Table.getSlot(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000249 assert(Slot != -1 && "Type not available!!?!");
250 Type = (unsigned)Slot;
251
Chris Lattner7c501472001-07-28 17:51:21 +0000252 // Make sure that we take the type number into consideration. We don't want
253 // to overflow the field size for the instruction format we select.
254 //
255 if (Slot > MaxOpSlot) MaxOpSlot = Slot;
256
Chris Lattner09083092001-07-08 04:57:15 +0000257 // Handle the special case for cast...
Chris Lattnereff112c2003-10-18 05:54:48 +0000258 if (isa<CastInst>(I) || isa<VAArgInst>(I)) {
Chris Lattner09083092001-07-08 04:57:15 +0000259 // Cast has to encode the destination type as the second argument in the
260 // packet, or else we won't know what type to cast to!
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000261 Slots[1] = Table.getSlot(I.getType());
Chris Lattner09083092001-07-08 04:57:15 +0000262 assert(Slots[1] != -1 && "Cast return type unknown?");
263 if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
264 NumOperands++;
Chris Lattnereff112c2003-10-18 05:54:48 +0000265 } else if (const VANextInst *VANI = dyn_cast<VANextInst>(&I)) {
266 Slots[1] = Table.getSlot(VANI->getArgType());
267 assert(Slots[1] != -1 && "va_next return type unknown?");
268 if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
269 NumOperands++;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000270 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)){// Handle VarArg calls
Chris Lattner9fcccb02002-06-05 17:49:40 +0000271 const PointerType *Ty = cast<PointerType>(CI->getCalledValue()->getType());
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000272 if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
Chris Lattner0fe56f42003-09-08 17:58:37 +0000273 outputInstrVarArgsCall(CI, Opcode, Table, Type, Out);
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000274 return;
275 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000276 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {// ... & Invokes
Chris Lattner9fcccb02002-06-05 17:49:40 +0000277 const PointerType *Ty = cast<PointerType>(II->getCalledValue()->getType());
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000278 if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
Chris Lattner0fe56f42003-09-08 17:58:37 +0000279 outputInstrVarArgsCall(II, Opcode, Table, Type, Out);
Chris Lattneref9c23f2001-10-03 14:53:21 +0000280 return;
281 }
Chris Lattner09083092001-07-08 04:57:15 +0000282 }
Chris Lattner00950542001-06-06 20:29:01 +0000283
Chris Lattnera407ba12003-01-21 20:13:49 +0000284 ++NumInstrs;
285
Chris Lattner00950542001-06-06 20:29:01 +0000286 // Decide which instruction encoding to use. This is determined primarily by
287 // the number of operands, and secondarily by whether or not the max operand
288 // will fit into the instruction encoding. More operands == fewer bits per
289 // operand.
290 //
291 switch (NumOperands) {
292 case 0:
293 case 1:
294 if (MaxOpSlot < (1 << 12)-1) { // -1 because we use 4095 to indicate 0 ops
Chris Lattner0fe56f42003-09-08 17:58:37 +0000295 outputInstructionFormat1(&I, Opcode, Table, Slots, Type, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000296 return;
Chris Lattner00950542001-06-06 20:29:01 +0000297 }
Chris Lattnerd6942d72004-01-14 16:54:21 +0000298 if (Type >= (1 << 12)-1)
299 NumOversidedBecauseOfTypes++;
300
301 NumOversized1OpInstrs++;
Chris Lattner00950542001-06-06 20:29:01 +0000302 break;
303
304 case 2:
305 if (MaxOpSlot < (1 << 8)) {
Chris Lattner0fe56f42003-09-08 17:58:37 +0000306 outputInstructionFormat2(&I, Opcode, Table, Slots, Type, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000307 return;
Chris Lattner00950542001-06-06 20:29:01 +0000308 }
Chris Lattnerd6942d72004-01-14 16:54:21 +0000309 if (Type >= (1 << 8))
310 NumOversidedBecauseOfTypes++;
311 NumOversized2OpInstrs++;
Chris Lattner00950542001-06-06 20:29:01 +0000312 break;
313
314 case 3:
315 if (MaxOpSlot < (1 << 6)) {
Chris Lattner0fe56f42003-09-08 17:58:37 +0000316 outputInstructionFormat3(&I, Opcode, Table, Slots, Type, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000317 return;
Chris Lattner00950542001-06-06 20:29:01 +0000318 }
Chris Lattnerd6942d72004-01-14 16:54:21 +0000319 if (Type >= (1 << 6))
320 NumOversidedBecauseOfTypes++;
321 NumOversized3OpInstrs++;
322 break;
323 default:
324 ++NumHugeOperandInstrs;
Chris Lattner00950542001-06-06 20:29:01 +0000325 break;
326 }
327
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000328 // If we weren't handled before here, we either have a large number of
Misha Brukman37f92e22003-09-11 22:34:13 +0000329 // operands or a large operand index that we are referring to.
Chris Lattner0fe56f42003-09-08 17:58:37 +0000330 outputInstructionFormat0(&I, Opcode, Table, Type, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000331}
Brian Gaeked0fde302003-11-11 22:41:34 +0000332