blob: 361f4595c3adfead336bc2af34aacf560710cc63 [file] [log] [blame]
Chris Lattner72614082002-10-25 22:55:53 +00001//===-- InstSelectSimple.cpp - A simple instruction selector for x86 ------===//
2//
3// This file defines a simple peephole instruction selector for the x86 platform
4//
5//===----------------------------------------------------------------------===//
6
7#include "X86.h"
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"
15#include <map>
16
17namespace {
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
21
22 unsigned CurReg;
23 std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs
24
25 ISel(MFunction *f)
26 : F(f), BB(0), CurReg(MRegisterInfo::FirstVirtualRegister) {}
27
28 /// runOnFunction - Top level implementation of instruction selection for
29 /// the entire function.
30 ///
31 bool runOnFunction(Function &F) {
32 visit(F);
33 RegMap.clear();
34 return false; // We never modify the LLVM itself.
35 }
36
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.
41 ///
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);
46 }
47
48 // Visitation methods for various instructions. These methods simply emit
49 // fixed X86 code for each instruction.
50 //
51 void visitReturnInst(ReturnInst &RI);
52 void visitAdd(BinaryOperator &B);
53
54 void visitInstruction(Instruction &I) {
55 std::cerr << "Cannot instruction select: " << I;
56 abort();
57 }
58
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.
62 ///
63 unsigned getReg(Value &V) { return getReg(&V); } // Allow references
64 unsigned getReg(Value *V) {
65 unsigned &Reg = RegMap[V];
66 if (Reg == 0)
67 Reg = CurReg++;
68
69 // FIXME: Constants should be thrown into registers here and appended to
70 // the end of the current basic block!
71
72 return Reg;
73 }
74
75 };
76}
77
78/// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such,
79/// we have the following possibilities:
80///
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?
88///
89void 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
93 }
94
95 // Emit a simple 'ret' instruction... appending it to the end of the basic
96 // block
97 new MInstruction(BB, X86::RET);
98}
99
100
101/// 'add' instruction - Simply turn this into an x86 reg,reg add instruction.
102void ISel::visitAdd(BinaryOperator &B) {
103 unsigned Op0r = getReg(B.getOperand(0)), Op1r = getReg(B.getOperand(1));
104 unsigned DestReg = getReg(B);
105
106 switch (B.getType()->getPrimitiveSize()) {
107 case 1: // UByte, SByte
108 BuildMInst(BB, X86::ADDrr8, DestReg).addReg(Op0r).addReg(Op1r);
109 break;
110 case 2: // UShort, Short
111 BuildMInst(BB, X86::ADDrr16, DestReg).addReg(Op0r).addReg(Op1r);
112 break;
113 case 4: // UInt, Int
114 BuildMInst(BB, X86::ADDrr32, DestReg).addReg(Op0r).addReg(Op1r);
115 break;
116
117 case 8: // ULong, Long
118 default:
119 visitInstruction(B); // abort
120 }
121}
122
123
124
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.
128///
129MFunction *X86SimpleInstructionSelection(Function &F) {
130 MFunction *Result = new MFunction();
131 ISel(Result).runOnFunction(F);
132 return Result;
133}