1 //===-- InstSelectSimple.cpp - A simple instruction selector for x86 ------===//
3 // This file defines a simple peephole instruction selector for the x86 platform
5 //===----------------------------------------------------------------------===//
8 #include "X86InstructionInfo.h"
9 #include "llvm/Function.h"
10 #include "llvm/iTerminators.h"
11 #include "llvm/Type.h"
12 #include "llvm/CodeGen/MFunction.h"
13 #include "llvm/CodeGen/MInstBuilder.h"
14 #include "llvm/Support/InstVisitor.h"
18 struct ISel : public InstVisitor<ISel> { // eventually will be a FunctionPass
19 MFunction *F; // The function we are compiling into
20 MBasicBlock *BB; // The current basic block we are compiling
23 std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs
26 : F(f), BB(0), CurReg(MRegisterInfo::FirstVirtualRegister) {}
28 /// runOnFunction - Top level implementation of instruction selection for
29 /// the entire function.
31 bool runOnFunction(Function &F) {
34 return false; // We never modify the LLVM itself.
37 /// visitBasicBlock - This method is called when we are visiting a new basic
38 /// block. This simply creates a new MBasicBlock to emit code into and adds
39 /// it to the current MFunction. Subsequent visit* for instructions will be
40 /// invoked for all instructions in the basic block.
42 void visitBasicBlock(BasicBlock &LLVM_BB) {
43 BB = new MBasicBlock();
44 // FIXME: Use the auto-insert form when it's available
45 F->getBasicBlockList().push_back(BB);
48 // Visitation methods for various instructions. These methods simply emit
49 // fixed X86 code for each instruction.
51 void visitReturnInst(ReturnInst &RI);
52 void visitAdd(BinaryOperator &B);
54 void visitInstruction(Instruction &I) {
55 std::cerr << "Cannot instruction select: " << I;
59 /// getReg - This method turns an LLVM value into a register number. This
60 /// is guaranteed to produce the same register number for a particular value
61 /// every time it is queried.
63 unsigned getReg(Value &V) { return getReg(&V); } // Allow references
64 unsigned getReg(Value *V) {
65 unsigned &Reg = RegMap[V];
69 // FIXME: Constants should be thrown into registers here and appended to
70 // the end of the current basic block!
78 /// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such,
79 /// we have the following possibilities:
81 /// ret void: No return value, simply emit a 'ret' instruction
82 /// ret sbyte, ubyte : Extend value into EAX and return
83 /// ret short, ushort: Extend value into EAX and return
84 /// ret int, uint : Move value into EAX and return
85 /// ret pointer : Move value into EAX and return
86 /// ret long, ulong : Move value into EAX/EDX (?) and return
87 /// ret float/double : ? Top of FP stack? XMM0?
89 void ISel::visitReturnInst(ReturnInst &I) {
90 if (I.getNumOperands() != 0) { // Not 'ret void'?
91 // Move result into a hard register... then emit a ret
92 visitInstruction(I); // abort
95 // Emit a simple 'ret' instruction... appending it to the end of the basic
97 new MInstruction(BB, X86::RET);
101 /// 'add' instruction - Simply turn this into an x86 reg,reg add instruction.
102 void ISel::visitAdd(BinaryOperator &B) {
103 unsigned Op0r = getReg(B.getOperand(0)), Op1r = getReg(B.getOperand(1));
104 unsigned DestReg = getReg(B);
106 switch (B.getType()->getPrimitiveSize()) {
107 case 1: // UByte, SByte
108 BuildMInst(BB, X86::ADDrr8, DestReg).addReg(Op0r).addReg(Op1r);
110 case 2: // UShort, Short
111 BuildMInst(BB, X86::ADDrr16, DestReg).addReg(Op0r).addReg(Op1r);
114 BuildMInst(BB, X86::ADDrr32, DestReg).addReg(Op0r).addReg(Op1r);
117 case 8: // ULong, Long
119 visitInstruction(B); // abort
125 /// X86SimpleInstructionSelection - This function converts an LLVM function into
126 /// a machine code representation is a very simple peep-hole fashion. The
127 /// generated code sucks but the implementation is nice and simple.
129 MFunction *X86SimpleInstructionSelection(Function &F) {
130 MFunction *Result = new MFunction();
131 ISel(Result).runOnFunction(F);