blob: bc5268c9435e074bedb0c70450c8ecaeae336466 [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 Lattnerce6ef112002-07-26 18:40:14 +000025
Chris Lattner00950542001-06-06 20:29:01 +000026typedef unsigned char uchar;
27
28// outputInstructionFormat0 - Output those wierd instructions that have a large
29// number of operands or have large operands themselves...
30//
31// Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
32//
Chris Lattner0fe56f42003-09-08 17:58:37 +000033static void outputInstructionFormat0(const Instruction *I, unsigned Opcode,
Chris Lattner00950542001-06-06 20:29:01 +000034 const SlotCalculator &Table,
Chris Lattner697954c2002-01-20 22:54:45 +000035 unsigned Type, std::deque<uchar> &Out) {
Chris Lattner00950542001-06-06 20:29:01 +000036 // Opcode must have top two bits clear...
Chris Lattner0fe56f42003-09-08 17:58:37 +000037 output_vbr(Opcode << 2, Out); // Instruction Opcode ID
Chris Lattner00950542001-06-06 20:29:01 +000038 output_vbr(Type, Out); // Result type
39
Chris Lattnerc8b25d42001-07-07 08:36:50 +000040 unsigned NumArgs = I->getNumOperands();
Chris Lattnereff112c2003-10-18 05:54:48 +000041 output_vbr(NumArgs + (isa<CastInst>(I) || isa<VANextInst>(I) ||
42 isa<VAArgInst>(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) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +000045 int Slot = Table.getSlot(I->getOperand(i));
Chris Lattnere5a57ee2001-07-25 22:47:55 +000046 assert(Slot >= 0 && "No slot number for value!?!?");
47 output_vbr((unsigned)Slot, Out);
48 }
Chris Lattner5ab1f872001-10-21 00:14:44 +000049
Chris Lattnereff112c2003-10-18 05:54:48 +000050 if (isa<CastInst>(I) || isa<VAArgInst>(I)) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +000051 int Slot = Table.getSlot(I->getType());
Chris Lattnereff112c2003-10-18 05:54:48 +000052 assert(Slot != -1 && "Cast return type unknown?");
53 output_vbr((unsigned)Slot, Out);
54 } else if (const VANextInst *VAI = dyn_cast<VANextInst>(I)) {
55 int Slot = Table.getSlot(VAI->getArgType());
56 assert(Slot != -1 && "VarArg argument type unknown?");
Chris Lattner5ab1f872001-10-21 00:14:44 +000057 output_vbr((unsigned)Slot, Out);
58 }
59
Chris Lattnere5a57ee2001-07-25 22:47:55 +000060 align32(Out); // We must maintain correct alignment!
61}
62
63
Misha Brukman37f92e22003-09-11 22:34:13 +000064// outputInstrVarArgsCall - Output the absurdly annoying varargs function calls.
Chris Lattnere5a57ee2001-07-25 22:47:55 +000065// This are more annoying than most because the signature of the call does not
66// tell us anything about the types of the arguments in the varargs portion.
67// Because of this, we encode (as type 0) all of the argument types explicitly
68// before the argument value. This really sucks, but you shouldn't be using
69// varargs functions in your code! *death to printf*!
70//
71// Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
72//
Chris Lattner0fe56f42003-09-08 17:58:37 +000073static void outputInstrVarArgsCall(const Instruction *I, unsigned Opcode,
Chris Lattnere5a57ee2001-07-25 22:47:55 +000074 const SlotCalculator &Table, unsigned Type,
Chris Lattner697954c2002-01-20 22:54:45 +000075 std::deque<uchar> &Out) {
Chris Lattner1b98c5c2001-10-13 06:48:38 +000076 assert(isa<CallInst>(I) || isa<InvokeInst>(I));
Chris Lattnere5a57ee2001-07-25 22:47:55 +000077 // Opcode must have top two bits clear...
Chris Lattner0fe56f42003-09-08 17:58:37 +000078 output_vbr(Opcode << 2, Out); // Instruction Opcode ID
Chris Lattnere5a57ee2001-07-25 22:47:55 +000079 output_vbr(Type, Out); // Result type (varargs type)
80
Chris Lattnereff112c2003-10-18 05:54:48 +000081 const PointerType *PTy = cast<PointerType>(I->getOperand(0)->getType());
82 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
83 unsigned NumParams = FTy->getNumParams();
84
85 unsigned NumFixedOperands;
86 if (isa<CallInst>(I)) {
87 // Output an operand for the callee and each fixed argument, then two for
88 // each variable argument.
89 NumFixedOperands = 1+NumParams;
90 } else {
91 assert(isa<InvokeInst>(I) && "Not call or invoke??");
92 // Output an operand for the callee and destinations, then two for each
93 // variable argument.
94 NumFixedOperands = 3+NumParams;
95 }
96 output_vbr(2 * I->getNumOperands()-NumFixedOperands, Out);
Chris Lattnere5a57ee2001-07-25 22:47:55 +000097
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000098 // The type for the function has already been emitted in the type field of the
Chris Lattner1b98c5c2001-10-13 06:48:38 +000099 // instruction. Just emit the slot # now.
Chris Lattnereff112c2003-10-18 05:54:48 +0000100 for (unsigned i = 0; i != NumFixedOperands; ++i) {
101 int Slot = Table.getSlot(I->getOperand(i));
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000102 assert(Slot >= 0 && "No slot number for value!?!?");
103 output_vbr((unsigned)Slot, Out);
Chris Lattnereff112c2003-10-18 05:54:48 +0000104 }
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000105
Chris Lattnereff112c2003-10-18 05:54:48 +0000106 for (unsigned i = NumFixedOperands, e = I->getNumOperands(); i != e; ++i) {
107 // Output Arg Type ID
108 int Slot = Table.getSlot(I->getOperand(i)->getType());
109 assert(Slot >= 0 && "No slot number for value!?!?");
110 output_vbr((unsigned)Slot, Out);
111
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000112 // Output arg ID itself
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000113 Slot = Table.getSlot(I->getOperand(i));
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000114 assert(Slot >= 0 && "No slot number for value!?!?");
Chris Lattner00950542001-06-06 20:29:01 +0000115 output_vbr((unsigned)Slot, Out);
116 }
117 align32(Out); // We must maintain correct alignment!
118}
119
120
121// outputInstructionFormat1 - Output one operand instructions, knowing that no
122// operand index is >= 2^12.
123//
Chris Lattner0fe56f42003-09-08 17:58:37 +0000124static void outputInstructionFormat1(const Instruction *I, unsigned Opcode,
Chris Lattner00950542001-06-06 20:29:01 +0000125 const SlotCalculator &Table, int *Slots,
Chris Lattner697954c2002-01-20 22:54:45 +0000126 unsigned Type, std::deque<uchar> &Out) {
Chris Lattner00950542001-06-06 20:29:01 +0000127 // bits Instruction format:
128 // --------------------------
Chris Lattner2b9f6002001-10-23 03:21:10 +0000129 // 01-00: Opcode type, fixed to 1.
130 // 07-02: Opcode
131 // 19-08: Resulting type plane
132 // 31-20: Operand #1 (if set to (2^12-1), then zero operands)
Chris Lattner00950542001-06-06 20:29:01 +0000133 //
Chris Lattner2b9f6002001-10-23 03:21:10 +0000134 unsigned Bits = 1 | (Opcode << 2) | (Type << 8) | (Slots[0] << 20);
Chris Lattner00950542001-06-06 20:29:01 +0000135 // cerr << "1 " << IType << " " << Type << " " << Slots[0] << endl;
Chris Lattner2b9f6002001-10-23 03:21:10 +0000136 output(Bits, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000137}
138
139
140// outputInstructionFormat2 - Output two operand instructions, knowing that no
141// operand index is >= 2^8.
142//
Chris Lattner0fe56f42003-09-08 17:58:37 +0000143static void outputInstructionFormat2(const Instruction *I, unsigned Opcode,
Chris Lattner00950542001-06-06 20:29:01 +0000144 const SlotCalculator &Table, int *Slots,
Chris Lattner697954c2002-01-20 22:54:45 +0000145 unsigned Type, std::deque<uchar> &Out) {
Chris Lattner00950542001-06-06 20:29:01 +0000146 // bits Instruction format:
147 // --------------------------
Chris Lattner2b9f6002001-10-23 03:21:10 +0000148 // 01-00: Opcode type, fixed to 2.
149 // 07-02: Opcode
150 // 15-08: Resulting type plane
151 // 23-16: Operand #1
152 // 31-24: Operand #2
Chris Lattner00950542001-06-06 20:29:01 +0000153 //
Chris Lattner2b9f6002001-10-23 03:21:10 +0000154 unsigned Bits = 2 | (Opcode << 2) | (Type << 8) |
155 (Slots[0] << 16) | (Slots[1] << 24);
Chris Lattner00950542001-06-06 20:29:01 +0000156 // cerr << "2 " << IType << " " << Type << " " << Slots[0] << " "
157 // << Slots[1] << endl;
Chris Lattner2b9f6002001-10-23 03:21:10 +0000158 output(Bits, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000159}
160
161
162// outputInstructionFormat3 - Output three operand instructions, knowing that no
163// operand index is >= 2^6.
164//
Chris Lattner0fe56f42003-09-08 17:58:37 +0000165static void outputInstructionFormat3(const Instruction *I, unsigned Opcode,
Chris Lattner00950542001-06-06 20:29:01 +0000166 const SlotCalculator &Table, int *Slots,
Chris Lattner697954c2002-01-20 22:54:45 +0000167 unsigned Type, std::deque<uchar> &Out) {
Chris Lattner00950542001-06-06 20:29:01 +0000168 // bits Instruction format:
169 // --------------------------
Chris Lattner2b9f6002001-10-23 03:21:10 +0000170 // 01-00: Opcode type, fixed to 3.
171 // 07-02: Opcode
172 // 13-08: Resulting type plane
173 // 19-14: Operand #1
174 // 25-20: Operand #2
175 // 31-26: Operand #3
Chris Lattner00950542001-06-06 20:29:01 +0000176 //
Chris Lattner2b9f6002001-10-23 03:21:10 +0000177 unsigned Bits = 3 | (Opcode << 2) | (Type << 8) |
178 (Slots[0] << 14) | (Slots[1] << 20) | (Slots[2] << 26);
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000179 //cerr << "3 " << IType << " " << Type << " " << Slots[0] << " "
180 // << Slots[1] << " " << Slots[2] << endl;
Chris Lattner2b9f6002001-10-23 03:21:10 +0000181 output(Bits, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000182}
183
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000184void BytecodeWriter::processInstruction(const Instruction &I) {
Chris Lattner0fe56f42003-09-08 17:58:37 +0000185 assert(I.getOpcode() < 62 && "Opcode too big???");
186 unsigned Opcode = I.getOpcode();
187
188 // Encode 'volatile load' as 62 and 'volatile store' as 63.
189 if (isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile())
190 Opcode = 62;
191 if (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())
192 Opcode = 63;
Chris Lattner00950542001-06-06 20:29:01 +0000193
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000194 unsigned NumOperands = I.getNumOperands();
Chris Lattner00950542001-06-06 20:29:01 +0000195 int MaxOpSlot = 0;
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000196 int Slots[3]; Slots[0] = (1 << 12)-1; // Marker to signify 0 operands
Chris Lattner00950542001-06-06 20:29:01 +0000197
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000198 for (unsigned i = 0; i < NumOperands; ++i) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000199 const Value *Def = I.getOperand(i);
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000200 int slot = Table.getSlot(Def);
Chris Lattner00950542001-06-06 20:29:01 +0000201 assert(slot != -1 && "Broken bytecode!");
202 if (slot > MaxOpSlot) MaxOpSlot = slot;
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000203 if (i < 3) Slots[i] = slot;
Chris Lattner00950542001-06-06 20:29:01 +0000204 }
205
206 // Figure out which type to encode with the instruction. Typically we want
207 // the type of the first parameter, as opposed to the type of the instruction
208 // (for example, with setcc, we always know it returns bool, but the type of
209 // the first param is actually interesting). But if we have no arguments
210 // we take the type of the instruction itself.
211 //
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000212 const Type *Ty;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000213 switch (I.getOpcode()) {
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000214 case Instruction::Malloc:
215 case Instruction::Alloca:
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000216 Ty = I.getType(); // Malloc & Alloca ALWAYS want to encode the return type
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000217 break;
218 case Instruction::Store:
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000219 Ty = I.getOperand(1)->getType(); // Encode the pointer type...
Chris Lattner9b625032002-05-06 16:15:30 +0000220 assert(isa<PointerType>(Ty) && "Store to nonpointer type!?!?");
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000221 break;
222 default: // Otherwise use the default behavior...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000223 Ty = NumOperands ? I.getOperand(0)->getType() : I.getType();
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000224 break;
225 }
Chris Lattner00950542001-06-06 20:29:01 +0000226
227 unsigned Type;
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000228 int Slot = Table.getSlot(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000229 assert(Slot != -1 && "Type not available!!?!");
230 Type = (unsigned)Slot;
231
Chris Lattner7c501472001-07-28 17:51:21 +0000232 // Make sure that we take the type number into consideration. We don't want
233 // to overflow the field size for the instruction format we select.
234 //
235 if (Slot > MaxOpSlot) MaxOpSlot = Slot;
236
Chris Lattner09083092001-07-08 04:57:15 +0000237 // Handle the special case for cast...
Chris Lattnereff112c2003-10-18 05:54:48 +0000238 if (isa<CastInst>(I) || isa<VAArgInst>(I)) {
Chris Lattner09083092001-07-08 04:57:15 +0000239 // Cast has to encode the destination type as the second argument in the
240 // packet, or else we won't know what type to cast to!
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000241 Slots[1] = Table.getSlot(I.getType());
Chris Lattner09083092001-07-08 04:57:15 +0000242 assert(Slots[1] != -1 && "Cast return type unknown?");
243 if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
244 NumOperands++;
Chris Lattnereff112c2003-10-18 05:54:48 +0000245 } else if (const VANextInst *VANI = dyn_cast<VANextInst>(&I)) {
246 Slots[1] = Table.getSlot(VANI->getArgType());
247 assert(Slots[1] != -1 && "va_next return type unknown?");
248 if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
249 NumOperands++;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000250 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)){// Handle VarArg calls
Chris Lattner9fcccb02002-06-05 17:49:40 +0000251 const PointerType *Ty = cast<PointerType>(CI->getCalledValue()->getType());
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000252 if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
Chris Lattner0fe56f42003-09-08 17:58:37 +0000253 outputInstrVarArgsCall(CI, Opcode, Table, Type, Out);
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000254 return;
255 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000256 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {// ... & Invokes
Chris Lattner9fcccb02002-06-05 17:49:40 +0000257 const PointerType *Ty = cast<PointerType>(II->getCalledValue()->getType());
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000258 if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
Chris Lattner0fe56f42003-09-08 17:58:37 +0000259 outputInstrVarArgsCall(II, Opcode, Table, Type, Out);
Chris Lattneref9c23f2001-10-03 14:53:21 +0000260 return;
261 }
Chris Lattner09083092001-07-08 04:57:15 +0000262 }
Chris Lattner00950542001-06-06 20:29:01 +0000263
Chris Lattnera407ba12003-01-21 20:13:49 +0000264 ++NumInstrs;
265
Chris Lattner00950542001-06-06 20:29:01 +0000266 // Decide which instruction encoding to use. This is determined primarily by
267 // the number of operands, and secondarily by whether or not the max operand
268 // will fit into the instruction encoding. More operands == fewer bits per
269 // operand.
270 //
271 switch (NumOperands) {
272 case 0:
273 case 1:
274 if (MaxOpSlot < (1 << 12)-1) { // -1 because we use 4095 to indicate 0 ops
Chris Lattner0fe56f42003-09-08 17:58:37 +0000275 outputInstructionFormat1(&I, Opcode, Table, Slots, Type, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000276 return;
Chris Lattner00950542001-06-06 20:29:01 +0000277 }
278 break;
279
280 case 2:
281 if (MaxOpSlot < (1 << 8)) {
Chris Lattner0fe56f42003-09-08 17:58:37 +0000282 outputInstructionFormat2(&I, Opcode, Table, Slots, Type, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000283 return;
Chris Lattner00950542001-06-06 20:29:01 +0000284 }
285 break;
286
287 case 3:
288 if (MaxOpSlot < (1 << 6)) {
Chris Lattner0fe56f42003-09-08 17:58:37 +0000289 outputInstructionFormat3(&I, Opcode, Table, Slots, Type, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000290 return;
Chris Lattner00950542001-06-06 20:29:01 +0000291 }
292 break;
293 }
294
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000295 // If we weren't handled before here, we either have a large number of
Misha Brukman37f92e22003-09-11 22:34:13 +0000296 // operands or a large operand index that we are referring to.
Chris Lattner0fe56f42003-09-08 17:58:37 +0000297 outputInstructionFormat0(&I, Opcode, Table, Type, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000298}
Brian Gaeked0fde302003-11-11 22:41:34 +0000299