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/Function.h"
15 #include "llvm/BasicBlock.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/iOther.h"
18 #include "llvm/iTerminators.h"
19 #include "Support/StatisticReporter.h"
23 NumOversized("bytecodewriter\t- Number of oversized instructions");
25 NumNormal("bytecodewriter\t- Number of normal instructions");
27 typedef unsigned char uchar;
29 // outputInstructionFormat0 - Output those wierd instructions that have a large
30 // number of operands or have large operands themselves...
32 // Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
34 static void outputInstructionFormat0(const Instruction *I,
35 const SlotCalculator &Table,
36 unsigned Type, std::deque<uchar> &Out) {
37 // Opcode must have top two bits clear...
38 output_vbr(I->getOpcode() << 2, Out); // Instruction Opcode ID
39 output_vbr(Type, Out); // Result type
41 unsigned NumArgs = I->getNumOperands();
42 output_vbr(NumArgs + isa<CastInst>(I), Out);
44 for (unsigned i = 0; i < NumArgs; ++i) {
45 int Slot = Table.getValSlot(I->getOperand(i));
46 assert(Slot >= 0 && "No slot number for value!?!?");
47 output_vbr((unsigned)Slot, Out);
50 if (isa<CastInst>(I)) {
51 int Slot = Table.getValSlot(I->getType());
52 assert(Slot != -1 && "Cast return type unknown?");
53 output_vbr((unsigned)Slot, Out);
56 align32(Out); // We must maintain correct alignment!
61 // outputInstrVarArgsCall - Output the obsurdly annoying varargs function calls.
62 // 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*!
68 // Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
70 static void outputInstrVarArgsCall(const Instruction *I,
71 const SlotCalculator &Table, unsigned Type,
72 std::deque<uchar> &Out) {
73 assert(isa<CallInst>(I) || isa<InvokeInst>(I));
74 // Opcode must have top two bits clear...
75 output_vbr(I->getOpcode() << 2, Out); // Instruction Opcode ID
76 output_vbr(Type, Out); // Result type (varargs type)
78 unsigned NumArgs = I->getNumOperands();
79 output_vbr(NumArgs*2, Out);
80 // TODO: Don't need to emit types for the fixed types of the varargs function
83 // The type for the function has already been emitted in the type field of the
84 // instruction. Just emit the slot # now.
85 int Slot = Table.getValSlot(I->getOperand(0));
86 assert(Slot >= 0 && "No slot number for value!?!?");
87 output_vbr((unsigned)Slot, Out);
89 // Output a dummy field to fill Arg#2 in the reader that is currently unused
90 // for varargs calls. This is a gross hack to make the code simpler, but we
91 // aren't really doing very small bytecode for varargs calls anyways.
92 // FIXME in the future: Smaller bytecode for varargs calls
95 for (unsigned i = 1; i < NumArgs; ++i) {
97 Slot = Table.getValSlot(I->getOperand(i)->getType());
98 assert(Slot >= 0 && "No slot number for value!?!?");
99 output_vbr((unsigned)Slot, Out);
101 // Output arg ID itself
102 Slot = Table.getValSlot(I->getOperand(i));
103 assert(Slot >= 0 && "No slot number for value!?!?");
104 output_vbr((unsigned)Slot, Out);
106 align32(Out); // We must maintain correct alignment!
111 // outputInstructionFormat1 - Output one operand instructions, knowing that no
112 // operand index is >= 2^12.
114 static void outputInstructionFormat1(const Instruction *I,
115 const SlotCalculator &Table, int *Slots,
116 unsigned Type, std::deque<uchar> &Out) {
117 unsigned Opcode = I->getOpcode(); // Instruction Opcode ID
119 // bits Instruction format:
120 // --------------------------
121 // 01-00: Opcode type, fixed to 1.
123 // 19-08: Resulting type plane
124 // 31-20: Operand #1 (if set to (2^12-1), then zero operands)
126 unsigned Bits = 1 | (Opcode << 2) | (Type << 8) | (Slots[0] << 20);
127 // cerr << "1 " << IType << " " << Type << " " << Slots[0] << endl;
133 // outputInstructionFormat2 - Output two operand instructions, knowing that no
134 // operand index is >= 2^8.
136 static void outputInstructionFormat2(const Instruction *I,
137 const SlotCalculator &Table, int *Slots,
138 unsigned Type, std::deque<uchar> &Out) {
139 unsigned Opcode = I->getOpcode(); // Instruction Opcode ID
141 // bits Instruction format:
142 // --------------------------
143 // 01-00: Opcode type, fixed to 2.
145 // 15-08: Resulting type plane
149 unsigned Bits = 2 | (Opcode << 2) | (Type << 8) |
150 (Slots[0] << 16) | (Slots[1] << 24);
151 // cerr << "2 " << IType << " " << Type << " " << Slots[0] << " "
152 // << Slots[1] << endl;
158 // outputInstructionFormat3 - Output three operand instructions, knowing that no
159 // operand index is >= 2^6.
161 static void outputInstructionFormat3(const Instruction *I,
162 const SlotCalculator &Table, int *Slots,
163 unsigned Type, std::deque<uchar> &Out) {
164 unsigned Opcode = I->getOpcode(); // Instruction Opcode ID
166 // bits Instruction format:
167 // --------------------------
168 // 01-00: Opcode type, fixed to 3.
170 // 13-08: Resulting type plane
175 unsigned Bits = 3 | (Opcode << 2) | (Type << 8) |
176 (Slots[0] << 14) | (Slots[1] << 20) | (Slots[2] << 26);
177 //cerr << "3 " << IType << " " << Type << " " << Slots[0] << " "
178 // << Slots[1] << " " << Slots[2] << endl;
183 void BytecodeWriter::processInstruction(const Instruction &I) {
184 assert(I.getOpcode() < 64 && "Opcode too big???");
186 unsigned NumOperands = I.getNumOperands();
188 int Slots[3]; Slots[0] = (1 << 12)-1; // Marker to signify 0 operands
190 for (unsigned i = 0; i < NumOperands; ++i) {
191 const Value *Def = I.getOperand(i);
192 int slot = Table.getValSlot(Def);
193 assert(slot != -1 && "Broken bytecode!");
194 if (slot > MaxOpSlot) MaxOpSlot = slot;
195 if (i < 3) Slots[i] = slot;
198 // Figure out which type to encode with the instruction. Typically we want
199 // the type of the first parameter, as opposed to the type of the instruction
200 // (for example, with setcc, we always know it returns bool, but the type of
201 // the first param is actually interesting). But if we have no arguments
202 // we take the type of the instruction itself.
205 switch (I.getOpcode()) {
206 case Instruction::Malloc:
207 case Instruction::Alloca:
208 Ty = I.getType(); // Malloc & Alloca ALWAYS want to encode the return type
210 case Instruction::Store:
211 Ty = I.getOperand(1)->getType(); // Encode the pointer type...
212 assert(isa<PointerType>(Ty) && "Store to nonpointer type!?!?");
214 default: // Otherwise use the default behavior...
215 Ty = NumOperands ? I.getOperand(0)->getType() : I.getType();
220 int Slot = Table.getValSlot(Ty);
221 assert(Slot != -1 && "Type not available!!?!");
222 Type = (unsigned)Slot;
224 // Make sure that we take the type number into consideration. We don't want
225 // to overflow the field size for the instruction format we select.
227 if (Slot > MaxOpSlot) MaxOpSlot = Slot;
229 // Handle the special case for cast...
230 if (isa<CastInst>(I)) {
231 // Cast has to encode the destination type as the second argument in the
232 // packet, or else we won't know what type to cast to!
233 Slots[1] = Table.getValSlot(I.getType());
234 assert(Slots[1] != -1 && "Cast return type unknown?");
235 if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
237 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)){// Handle VarArg calls
238 const PointerType *Ty = cast<PointerType>(CI->getCalledValue()->getType());
239 if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
240 outputInstrVarArgsCall(CI, Table, Type, Out);
243 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {// ... & Invokes
244 const PointerType *Ty = cast<PointerType>(II->getCalledValue()->getType());
245 if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
246 outputInstrVarArgsCall(II, Table, Type, Out);
251 // Decide which instruction encoding to use. This is determined primarily by
252 // the number of operands, and secondarily by whether or not the max operand
253 // will fit into the instruction encoding. More operands == fewer bits per
256 switch (NumOperands) {
259 if (MaxOpSlot < (1 << 12)-1) { // -1 because we use 4095 to indicate 0 ops
260 outputInstructionFormat1(&I, Table, Slots, Type, Out);
266 if (MaxOpSlot < (1 << 8)) {
267 outputInstructionFormat2(&I, Table, Slots, Type, Out);
273 if (MaxOpSlot < (1 << 6)) {
274 outputInstructionFormat3(&I, Table, Slots, Type, Out);
280 // If we weren't handled before here, we either have a large number of
281 // operands or a large operand index that we are refering to.
282 outputInstructionFormat0(&I, Table, Type, Out);