blob: 4b845996bfb70404fb7dfe4ec8dbb89a75afb442 [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 Lattner00950542001-06-06 20:29:01 +000023typedef unsigned char uchar;
24
25// outputInstructionFormat0 - Output those wierd instructions that have a large
26// number of operands or have large operands themselves...
27//
28// Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
29//
Chris Lattner0fe56f42003-09-08 17:58:37 +000030static void outputInstructionFormat0(const Instruction *I, unsigned Opcode,
Chris Lattner00950542001-06-06 20:29:01 +000031 const SlotCalculator &Table,
Chris Lattner697954c2002-01-20 22:54:45 +000032 unsigned Type, std::deque<uchar> &Out) {
Chris Lattner00950542001-06-06 20:29:01 +000033 // Opcode must have top two bits clear...
Chris Lattner0fe56f42003-09-08 17:58:37 +000034 output_vbr(Opcode << 2, Out); // Instruction Opcode ID
Chris Lattner00950542001-06-06 20:29:01 +000035 output_vbr(Type, Out); // Result type
36
Chris Lattnerc8b25d42001-07-07 08:36:50 +000037 unsigned NumArgs = I->getNumOperands();
Chris Lattnereff112c2003-10-18 05:54:48 +000038 output_vbr(NumArgs + (isa<CastInst>(I) || isa<VANextInst>(I) ||
39 isa<VAArgInst>(I)), Out);
Chris Lattner00950542001-06-06 20:29:01 +000040
Chris Lattnerc8b25d42001-07-07 08:36:50 +000041 for (unsigned i = 0; i < NumArgs; ++i) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +000042 int Slot = Table.getSlot(I->getOperand(i));
Chris Lattnere5a57ee2001-07-25 22:47:55 +000043 assert(Slot >= 0 && "No slot number for value!?!?");
44 output_vbr((unsigned)Slot, Out);
45 }
Chris Lattner5ab1f872001-10-21 00:14:44 +000046
Chris Lattnereff112c2003-10-18 05:54:48 +000047 if (isa<CastInst>(I) || isa<VAArgInst>(I)) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +000048 int Slot = Table.getSlot(I->getType());
Chris Lattnereff112c2003-10-18 05:54:48 +000049 assert(Slot != -1 && "Cast return type unknown?");
50 output_vbr((unsigned)Slot, Out);
51 } else if (const VANextInst *VAI = dyn_cast<VANextInst>(I)) {
52 int Slot = Table.getSlot(VAI->getArgType());
53 assert(Slot != -1 && "VarArg argument type unknown?");
Chris Lattner5ab1f872001-10-21 00:14:44 +000054 output_vbr((unsigned)Slot, Out);
55 }
56
Chris Lattnere5a57ee2001-07-25 22:47:55 +000057 align32(Out); // We must maintain correct alignment!
58}
59
60
Misha Brukman37f92e22003-09-11 22:34:13 +000061// outputInstrVarArgsCall - Output the absurdly 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//
Chris Lattner0fe56f42003-09-08 17:58:37 +000070static void outputInstrVarArgsCall(const Instruction *I, unsigned Opcode,
Chris Lattnere5a57ee2001-07-25 22:47:55 +000071 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 Lattner0fe56f42003-09-08 17:58:37 +000075 output_vbr(Opcode << 2, Out); // Instruction Opcode ID
Chris Lattnere5a57ee2001-07-25 22:47:55 +000076 output_vbr(Type, Out); // Result type (varargs type)
77
Chris Lattnereff112c2003-10-18 05:54:48 +000078 const PointerType *PTy = cast<PointerType>(I->getOperand(0)->getType());
79 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
80 unsigned NumParams = FTy->getNumParams();
81
82 unsigned NumFixedOperands;
83 if (isa<CallInst>(I)) {
84 // Output an operand for the callee and each fixed argument, then two for
85 // each variable argument.
86 NumFixedOperands = 1+NumParams;
87 } else {
88 assert(isa<InvokeInst>(I) && "Not call or invoke??");
89 // Output an operand for the callee and destinations, then two for each
90 // variable argument.
91 NumFixedOperands = 3+NumParams;
92 }
93 output_vbr(2 * I->getNumOperands()-NumFixedOperands, Out);
Chris Lattnere5a57ee2001-07-25 22:47:55 +000094
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000095 // The type for the function has already been emitted in the type field of the
Chris Lattner1b98c5c2001-10-13 06:48:38 +000096 // instruction. Just emit the slot # now.
Chris Lattnereff112c2003-10-18 05:54:48 +000097 for (unsigned i = 0; i != NumFixedOperands; ++i) {
98 int Slot = Table.getSlot(I->getOperand(i));
Chris Lattnere5a57ee2001-07-25 22:47:55 +000099 assert(Slot >= 0 && "No slot number for value!?!?");
100 output_vbr((unsigned)Slot, Out);
Chris Lattnereff112c2003-10-18 05:54:48 +0000101 }
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000102
Chris Lattnereff112c2003-10-18 05:54:48 +0000103 for (unsigned i = NumFixedOperands, e = I->getNumOperands(); i != e; ++i) {
104 // Output Arg Type ID
105 int Slot = Table.getSlot(I->getOperand(i)->getType());
106 assert(Slot >= 0 && "No slot number for value!?!?");
107 output_vbr((unsigned)Slot, Out);
108
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000109 // Output arg ID itself
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000110 Slot = Table.getSlot(I->getOperand(i));
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000111 assert(Slot >= 0 && "No slot number for value!?!?");
Chris Lattner00950542001-06-06 20:29:01 +0000112 output_vbr((unsigned)Slot, Out);
113 }
114 align32(Out); // We must maintain correct alignment!
115}
116
117
118// outputInstructionFormat1 - Output one operand instructions, knowing that no
119// operand index is >= 2^12.
120//
Chris Lattner0fe56f42003-09-08 17:58:37 +0000121static void outputInstructionFormat1(const Instruction *I, unsigned Opcode,
Chris Lattner00950542001-06-06 20:29:01 +0000122 const SlotCalculator &Table, int *Slots,
Chris Lattner697954c2002-01-20 22:54:45 +0000123 unsigned Type, std::deque<uchar> &Out) {
Chris Lattner00950542001-06-06 20:29:01 +0000124 // bits Instruction format:
125 // --------------------------
Chris Lattner2b9f6002001-10-23 03:21:10 +0000126 // 01-00: Opcode type, fixed to 1.
127 // 07-02: Opcode
128 // 19-08: Resulting type plane
129 // 31-20: Operand #1 (if set to (2^12-1), then zero operands)
Chris Lattner00950542001-06-06 20:29:01 +0000130 //
Chris Lattner2b9f6002001-10-23 03:21:10 +0000131 unsigned Bits = 1 | (Opcode << 2) | (Type << 8) | (Slots[0] << 20);
Chris Lattner00950542001-06-06 20:29:01 +0000132 // cerr << "1 " << IType << " " << Type << " " << Slots[0] << endl;
Chris Lattner2b9f6002001-10-23 03:21:10 +0000133 output(Bits, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000134}
135
136
137// outputInstructionFormat2 - Output two operand instructions, knowing that no
138// operand index is >= 2^8.
139//
Chris Lattner0fe56f42003-09-08 17:58:37 +0000140static void outputInstructionFormat2(const Instruction *I, unsigned Opcode,
Chris Lattner00950542001-06-06 20:29:01 +0000141 const SlotCalculator &Table, int *Slots,
Chris Lattner697954c2002-01-20 22:54:45 +0000142 unsigned Type, std::deque<uchar> &Out) {
Chris Lattner00950542001-06-06 20:29:01 +0000143 // bits Instruction format:
144 // --------------------------
Chris Lattner2b9f6002001-10-23 03:21:10 +0000145 // 01-00: Opcode type, fixed to 2.
146 // 07-02: Opcode
147 // 15-08: Resulting type plane
148 // 23-16: Operand #1
149 // 31-24: Operand #2
Chris Lattner00950542001-06-06 20:29:01 +0000150 //
Chris Lattner2b9f6002001-10-23 03:21:10 +0000151 unsigned Bits = 2 | (Opcode << 2) | (Type << 8) |
152 (Slots[0] << 16) | (Slots[1] << 24);
Chris Lattner00950542001-06-06 20:29:01 +0000153 // cerr << "2 " << IType << " " << Type << " " << Slots[0] << " "
154 // << Slots[1] << endl;
Chris Lattner2b9f6002001-10-23 03:21:10 +0000155 output(Bits, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000156}
157
158
159// outputInstructionFormat3 - Output three operand instructions, knowing that no
160// operand index is >= 2^6.
161//
Chris Lattner0fe56f42003-09-08 17:58:37 +0000162static void outputInstructionFormat3(const Instruction *I, unsigned Opcode,
Chris Lattner00950542001-06-06 20:29:01 +0000163 const SlotCalculator &Table, int *Slots,
Chris Lattner697954c2002-01-20 22:54:45 +0000164 unsigned Type, std::deque<uchar> &Out) {
Chris Lattner00950542001-06-06 20:29:01 +0000165 // bits Instruction format:
166 // --------------------------
Chris Lattner2b9f6002001-10-23 03:21:10 +0000167 // 01-00: Opcode type, fixed to 3.
168 // 07-02: Opcode
169 // 13-08: Resulting type plane
170 // 19-14: Operand #1
171 // 25-20: Operand #2
172 // 31-26: Operand #3
Chris Lattner00950542001-06-06 20:29:01 +0000173 //
Chris Lattner2b9f6002001-10-23 03:21:10 +0000174 unsigned Bits = 3 | (Opcode << 2) | (Type << 8) |
175 (Slots[0] << 14) | (Slots[1] << 20) | (Slots[2] << 26);
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000176 //cerr << "3 " << IType << " " << Type << " " << Slots[0] << " "
177 // << Slots[1] << " " << Slots[2] << endl;
Chris Lattner2b9f6002001-10-23 03:21:10 +0000178 output(Bits, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000179}
180
Chris Lattnercf3e67f2004-01-18 21:08:52 +0000181void BytecodeWriter::outputInstruction(const Instruction &I) {
Chris Lattner0fe56f42003-09-08 17:58:37 +0000182 assert(I.getOpcode() < 62 && "Opcode too big???");
183 unsigned Opcode = I.getOpcode();
184
185 // Encode 'volatile load' as 62 and 'volatile store' as 63.
186 if (isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile())
187 Opcode = 62;
188 if (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())
189 Opcode = 63;
Chris Lattner00950542001-06-06 20:29:01 +0000190
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000191 unsigned NumOperands = I.getNumOperands();
Chris Lattner00950542001-06-06 20:29:01 +0000192 int MaxOpSlot = 0;
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000193 int Slots[3]; Slots[0] = (1 << 12)-1; // Marker to signify 0 operands
Chris Lattner00950542001-06-06 20:29:01 +0000194
Chris Lattnercf3e67f2004-01-18 21:08:52 +0000195 for (unsigned i = 0; i != NumOperands; ++i) {
196 int slot = Table.getSlot(I.getOperand(i));
Chris Lattner00950542001-06-06 20:29:01 +0000197 assert(slot != -1 && "Broken bytecode!");
198 if (slot > MaxOpSlot) MaxOpSlot = slot;
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000199 if (i < 3) Slots[i] = slot;
Chris Lattner00950542001-06-06 20:29:01 +0000200 }
201
202 // Figure out which type to encode with the instruction. Typically we want
203 // the type of the first parameter, as opposed to the type of the instruction
204 // (for example, with setcc, we always know it returns bool, but the type of
205 // the first param is actually interesting). But if we have no arguments
206 // we take the type of the instruction itself.
207 //
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000208 const Type *Ty;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000209 switch (I.getOpcode()) {
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000210 case Instruction::Malloc:
211 case Instruction::Alloca:
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000212 Ty = I.getType(); // Malloc & Alloca ALWAYS want to encode the return type
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000213 break;
214 case Instruction::Store:
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000215 Ty = I.getOperand(1)->getType(); // Encode the pointer type...
Chris Lattner9b625032002-05-06 16:15:30 +0000216 assert(isa<PointerType>(Ty) && "Store to nonpointer type!?!?");
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000217 break;
218 default: // Otherwise use the default behavior...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000219 Ty = NumOperands ? I.getOperand(0)->getType() : I.getType();
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000220 break;
221 }
Chris Lattner00950542001-06-06 20:29:01 +0000222
223 unsigned Type;
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000224 int Slot = Table.getSlot(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000225 assert(Slot != -1 && "Type not available!!?!");
226 Type = (unsigned)Slot;
227
Chris Lattner7c501472001-07-28 17:51:21 +0000228 // Make sure that we take the type number into consideration. We don't want
229 // to overflow the field size for the instruction format we select.
230 //
231 if (Slot > MaxOpSlot) MaxOpSlot = Slot;
232
Chris Lattner09083092001-07-08 04:57:15 +0000233 // Handle the special case for cast...
Chris Lattnereff112c2003-10-18 05:54:48 +0000234 if (isa<CastInst>(I) || isa<VAArgInst>(I)) {
Chris Lattner09083092001-07-08 04:57:15 +0000235 // Cast has to encode the destination type as the second argument in the
236 // packet, or else we won't know what type to cast to!
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000237 Slots[1] = Table.getSlot(I.getType());
Chris Lattner09083092001-07-08 04:57:15 +0000238 assert(Slots[1] != -1 && "Cast return type unknown?");
239 if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
240 NumOperands++;
Chris Lattnereff112c2003-10-18 05:54:48 +0000241 } else if (const VANextInst *VANI = dyn_cast<VANextInst>(&I)) {
242 Slots[1] = Table.getSlot(VANI->getArgType());
243 assert(Slots[1] != -1 && "va_next return type unknown?");
244 if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
245 NumOperands++;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000246 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)){// Handle VarArg calls
Chris Lattner9fcccb02002-06-05 17:49:40 +0000247 const PointerType *Ty = cast<PointerType>(CI->getCalledValue()->getType());
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000248 if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
Chris Lattner0fe56f42003-09-08 17:58:37 +0000249 outputInstrVarArgsCall(CI, Opcode, Table, Type, Out);
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000250 return;
251 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000252 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {// ... & Invokes
Chris Lattner9fcccb02002-06-05 17:49:40 +0000253 const PointerType *Ty = cast<PointerType>(II->getCalledValue()->getType());
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000254 if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
Chris Lattner0fe56f42003-09-08 17:58:37 +0000255 outputInstrVarArgsCall(II, Opcode, Table, Type, Out);
Chris Lattneref9c23f2001-10-03 14:53:21 +0000256 return;
257 }
Chris Lattner09083092001-07-08 04:57:15 +0000258 }
Chris Lattner00950542001-06-06 20:29:01 +0000259
260 // Decide which instruction encoding to use. This is determined primarily by
261 // the number of operands, and secondarily by whether or not the max operand
262 // will fit into the instruction encoding. More operands == fewer bits per
263 // operand.
264 //
265 switch (NumOperands) {
266 case 0:
267 case 1:
268 if (MaxOpSlot < (1 << 12)-1) { // -1 because we use 4095 to indicate 0 ops
Chris Lattner0fe56f42003-09-08 17:58:37 +0000269 outputInstructionFormat1(&I, Opcode, Table, Slots, Type, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000270 return;
Chris Lattner00950542001-06-06 20:29:01 +0000271 }
272 break;
273
274 case 2:
275 if (MaxOpSlot < (1 << 8)) {
Chris Lattner0fe56f42003-09-08 17:58:37 +0000276 outputInstructionFormat2(&I, Opcode, Table, Slots, Type, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000277 return;
Chris Lattner00950542001-06-06 20:29:01 +0000278 }
279 break;
280
281 case 3:
282 if (MaxOpSlot < (1 << 6)) {
Chris Lattner0fe56f42003-09-08 17:58:37 +0000283 outputInstructionFormat3(&I, Opcode, Table, Slots, Type, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000284 return;
Chris Lattner00950542001-06-06 20:29:01 +0000285 }
Chris Lattnerd6942d72004-01-14 16:54:21 +0000286 break;
287 default:
Chris Lattner00950542001-06-06 20:29:01 +0000288 break;
289 }
290
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000291 // If we weren't handled before here, we either have a large number of
Misha Brukman37f92e22003-09-11 22:34:13 +0000292 // operands or a large operand index that we are referring to.
Chris Lattner0fe56f42003-09-08 17:58:37 +0000293 outputInstructionFormat0(&I, Opcode, Table, Type, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000294}
Brian Gaeked0fde302003-11-11 22:41:34 +0000295