1 //===-- WriteInst.cpp - Functions for writing instructions -------*- C++ -*--=//
3 // This file implements the routines for encoding instruction opcodes to a
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
10 //===----------------------------------------------------------------------===//
12 #include "WriterInternals.h"
13 #include "llvm/Module.h"
14 #include "llvm/DerivedTypes.h"
15 #include "llvm/iOther.h"
16 #include "llvm/iTerminators.h"
17 #include "Support/StatisticReporter.h"
21 NumOversized("bytecodewriter\t- Number of oversized instructions");
23 NumNormal("bytecodewriter\t- Number of normal instructions");
25 typedef unsigned char uchar;
27 // outputInstructionFormat0 - Output those wierd instructions that have a large
28 // number of operands or have large operands themselves...
30 // Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
32 static void outputInstructionFormat0(const Instruction *I,
33 const SlotCalculator &Table,
34 unsigned Type, std::deque<uchar> &Out) {
35 // Opcode must have top two bits clear...
36 output_vbr(I->getOpcode() << 2, Out); // Instruction Opcode ID
37 output_vbr(Type, Out); // Result type
39 unsigned NumArgs = I->getNumOperands();
40 output_vbr(NumArgs + isa<CastInst>(I), Out);
42 for (unsigned i = 0; i < NumArgs; ++i) {
43 int Slot = Table.getValSlot(I->getOperand(i));
44 assert(Slot >= 0 && "No slot number for value!?!?");
45 output_vbr((unsigned)Slot, Out);
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);
54 align32(Out); // We must maintain correct alignment!
59 // outputInstrVarArgsCall - Output the obsurdly annoying varargs function calls.
60 // 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*!
66 // Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
68 static void outputInstrVarArgsCall(const Instruction *I,
69 const SlotCalculator &Table, unsigned Type,
70 std::deque<uchar> &Out) {
71 assert(isa<CallInst>(I) || isa<InvokeInst>(I));
72 // Opcode must have top two bits clear...
73 output_vbr(I->getOpcode() << 2, Out); // Instruction Opcode ID
74 output_vbr(Type, Out); // Result type (varargs type)
76 unsigned NumArgs = I->getNumOperands();
77 output_vbr(NumArgs*2, Out);
78 // TODO: Don't need to emit types for the fixed types of the varargs function
81 // The type for the function has already been emitted in the type field of the
82 // instruction. Just emit the slot # now.
83 int Slot = Table.getValSlot(I->getOperand(0));
84 assert(Slot >= 0 && "No slot number for value!?!?");
85 output_vbr((unsigned)Slot, Out);
87 // 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
93 for (unsigned i = 1; i < NumArgs; ++i) {
95 Slot = Table.getValSlot(I->getOperand(i)->getType());
96 assert(Slot >= 0 && "No slot number for value!?!?");
97 output_vbr((unsigned)Slot, Out);
99 // Output arg ID itself
100 Slot = Table.getValSlot(I->getOperand(i));
101 assert(Slot >= 0 && "No slot number for value!?!?");
102 output_vbr((unsigned)Slot, Out);
104 align32(Out); // We must maintain correct alignment!
109 // outputInstructionFormat1 - Output one operand instructions, knowing that no
110 // operand index is >= 2^12.
112 static void outputInstructionFormat1(const Instruction *I,
113 const SlotCalculator &Table, int *Slots,
114 unsigned Type, std::deque<uchar> &Out) {
115 unsigned Opcode = I->getOpcode(); // Instruction Opcode ID
117 // bits Instruction format:
118 // --------------------------
119 // 01-00: Opcode type, fixed to 1.
121 // 19-08: Resulting type plane
122 // 31-20: Operand #1 (if set to (2^12-1), then zero operands)
124 unsigned Bits = 1 | (Opcode << 2) | (Type << 8) | (Slots[0] << 20);
125 // cerr << "1 " << IType << " " << Type << " " << Slots[0] << endl;
131 // outputInstructionFormat2 - Output two operand instructions, knowing that no
132 // operand index is >= 2^8.
134 static void outputInstructionFormat2(const Instruction *I,
135 const SlotCalculator &Table, int *Slots,
136 unsigned Type, std::deque<uchar> &Out) {
137 unsigned Opcode = I->getOpcode(); // Instruction Opcode ID
139 // bits Instruction format:
140 // --------------------------
141 // 01-00: Opcode type, fixed to 2.
143 // 15-08: Resulting type plane
147 unsigned Bits = 2 | (Opcode << 2) | (Type << 8) |
148 (Slots[0] << 16) | (Slots[1] << 24);
149 // cerr << "2 " << IType << " " << Type << " " << Slots[0] << " "
150 // << Slots[1] << endl;
156 // outputInstructionFormat3 - Output three operand instructions, knowing that no
157 // operand index is >= 2^6.
159 static void outputInstructionFormat3(const Instruction *I,
160 const SlotCalculator &Table, int *Slots,
161 unsigned Type, std::deque<uchar> &Out) {
162 unsigned Opcode = I->getOpcode(); // Instruction Opcode ID
164 // bits Instruction format:
165 // --------------------------
166 // 01-00: Opcode type, fixed to 3.
168 // 13-08: Resulting type plane
173 unsigned Bits = 3 | (Opcode << 2) | (Type << 8) |
174 (Slots[0] << 14) | (Slots[1] << 20) | (Slots[2] << 26);
175 //cerr << "3 " << IType << " " << Type << " " << Slots[0] << " "
176 // << Slots[1] << " " << Slots[2] << endl;
181 void BytecodeWriter::processInstruction(const Instruction &I) {
182 assert(I.getOpcode() < 64 && "Opcode too big???");
184 unsigned NumOperands = I.getNumOperands();
186 int Slots[3]; Slots[0] = (1 << 12)-1; // Marker to signify 0 operands
188 for (unsigned i = 0; i < NumOperands; ++i) {
189 const Value *Def = I.getOperand(i);
190 int slot = Table.getValSlot(Def);
191 assert(slot != -1 && "Broken bytecode!");
192 if (slot > MaxOpSlot) MaxOpSlot = slot;
193 if (i < 3) Slots[i] = slot;
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.
203 switch (I.getOpcode()) {
204 case Instruction::Malloc:
205 case Instruction::Alloca:
206 Ty = I.getType(); // Malloc & Alloca ALWAYS want to encode the return type
208 case Instruction::Store:
209 Ty = I.getOperand(1)->getType(); // Encode the pointer type...
210 assert(isa<PointerType>(Ty) && "Store to nonpointer type!?!?");
212 default: // Otherwise use the default behavior...
213 Ty = NumOperands ? I.getOperand(0)->getType() : I.getType();
218 int Slot = Table.getValSlot(Ty);
219 assert(Slot != -1 && "Type not available!!?!");
220 Type = (unsigned)Slot;
222 // 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.
225 if (Slot > MaxOpSlot) MaxOpSlot = Slot;
227 // Handle the special case for cast...
228 if (isa<CastInst>(I)) {
229 // 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!
231 Slots[1] = Table.getValSlot(I.getType());
232 assert(Slots[1] != -1 && "Cast return type unknown?");
233 if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
235 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)){// Handle VarArg calls
236 const PointerType *Ty = cast<PointerType>(CI->getCalledValue()->getType());
237 if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
238 outputInstrVarArgsCall(CI, Table, Type, Out);
241 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {// ... & Invokes
242 const PointerType *Ty = cast<PointerType>(II->getCalledValue()->getType());
243 if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
244 outputInstrVarArgsCall(II, Table, Type, Out);
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
254 switch (NumOperands) {
257 if (MaxOpSlot < (1 << 12)-1) { // -1 because we use 4095 to indicate 0 ops
258 outputInstructionFormat1(&I, Table, Slots, Type, Out);
264 if (MaxOpSlot < (1 << 8)) {
265 outputInstructionFormat2(&I, Table, Slots, Type, Out);
271 if (MaxOpSlot < (1 << 6)) {
272 outputInstructionFormat3(&I, Table, Slots, Type, Out);
278 // 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.
280 outputInstructionFormat0(&I, Table, Type, Out);