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 "X86InstrInfo.h"
9 #include "llvm/Function.h"
10 #include "llvm/iTerminators.h"
11 #include "llvm/Type.h"
12 #include "llvm/Constants.h"
13 #include "llvm/CodeGen/MachineFunction.h"
14 #include "llvm/CodeGen/MachineInstrBuilder.h"
15 #include "llvm/Support/InstVisitor.h"
19 struct ISel : public InstVisitor<ISel> { // eventually will be a FunctionPass
20 MachineFunction *F; // The function we are compiling into
21 MachineBasicBlock *BB; // The current MBB we are compiling
24 std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs
26 ISel(MachineFunction *f)
27 : F(f), BB(0), CurReg(MRegisterInfo::FirstVirtualRegister) {}
29 /// runOnFunction - Top level implementation of instruction selection for
30 /// the entire function.
32 bool runOnFunction(Function &F) {
35 return false; // We never modify the LLVM itself.
38 /// visitBasicBlock - This method is called when we are visiting a new basic
39 /// block. This simply creates a new MachineBasicBlock to emit code into
40 /// and adds it to the current MachineFunction. Subsequent visit* for
41 /// instructions will be invoked for all instructions in the basic block.
43 void visitBasicBlock(BasicBlock &LLVM_BB) {
44 BB = new MachineBasicBlock();
45 // FIXME: Use the auto-insert form when it's available
46 F->getBasicBlockList().push_back(BB);
49 // Visitation methods for various instructions. These methods simply emit
50 // fixed X86 code for each instruction.
52 void visitReturnInst(ReturnInst &RI);
53 void visitAdd(BinaryOperator &B);
55 void visitInstruction(Instruction &I) {
56 std::cerr << "Cannot instruction select: " << I;
61 /// copyConstantToRegister - Output the instructions required to put the
62 /// specified constant into the specified register.
64 void copyConstantToRegister(Constant *C, unsigned Reg);
66 /// getReg - This method turns an LLVM value into a register number. This
67 /// is guaranteed to produce the same register number for a particular value
68 /// every time it is queried.
70 unsigned getReg(Value &V) { return getReg(&V); } // Allow references
71 unsigned getReg(Value *V) {
72 unsigned &Reg = RegMap[V];
76 // If this operand is a constant, emit the code to copy the constant into
77 // the register here...
79 if (Constant *C = dyn_cast<Constant>(V))
80 copyConstantToRegister(C, Reg);
89 /// copyConstantToRegister - Output the instructions required to put the
90 /// specified constant into the specified register.
92 void ISel::copyConstantToRegister(Constant *C, unsigned R) {
93 assert (!isa<ConstantExpr>(C) && "Constant expressions not yet handled!\n");
95 switch (C->getType()->getPrimitiveID()) {
97 BuildMI(BB, X86::MOVir8, R).addSImm(cast<ConstantSInt>(C)->getValue());
100 BuildMI(BB, X86::MOVir8, R).addZImm(cast<ConstantUInt>(C)->getValue());
102 case Type::ShortTyID:
103 BuildMI(BB, X86::MOVir16, R).addSImm(cast<ConstantSInt>(C)->getValue());
105 case Type::UShortTyID:
106 BuildMI(BB, X86::MOVir16, R).addZImm(cast<ConstantUInt>(C)->getValue());
109 BuildMI(BB, X86::MOVir32, R).addSImm(cast<ConstantSInt>(C)->getValue());
112 BuildMI(BB, X86::MOVir32, R).addZImm(cast<ConstantUInt>(C)->getValue());
114 default: assert(0 && "Type not handled yet!");
119 /// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such,
120 /// we have the following possibilities:
122 /// ret void: No return value, simply emit a 'ret' instruction
123 /// ret sbyte, ubyte : Extend value into EAX and return
124 /// ret short, ushort: Extend value into EAX and return
125 /// ret int, uint : Move value into EAX and return
126 /// ret pointer : Move value into EAX and return
127 /// ret long, ulong : Move value into EAX/EDX (?) and return
128 /// ret float/double : ? Top of FP stack? XMM0?
130 void ISel::visitReturnInst(ReturnInst &I) {
131 if (I.getNumOperands() != 0) { // Not 'ret void'?
132 // Move result into a hard register... then emit a ret
133 visitInstruction(I); // abort
136 // Emit a simple 'ret' instruction... appending it to the end of the basic
138 BuildMI(BB, X86::RET, 0);
142 /// 'add' instruction - Simply turn this into an x86 reg,reg add instruction.
143 void ISel::visitAdd(BinaryOperator &B) {
144 unsigned Op0r = getReg(B.getOperand(0)), Op1r = getReg(B.getOperand(1));
145 unsigned DestReg = getReg(B);
147 switch (B.getType()->getPrimitiveSize()) {
148 case 1: // UByte, SByte
149 BuildMI(BB, X86::ADDrr8, DestReg).addReg(Op0r).addReg(Op1r);
151 case 2: // UShort, Short
152 BuildMI(BB, X86::ADDrr16, DestReg).addReg(Op0r).addReg(Op1r);
155 BuildMI(BB, X86::ADDrr32, DestReg).addReg(Op0r).addReg(Op1r);
158 case 8: // ULong, Long
160 visitInstruction(B); // abort
166 /// X86SimpleInstructionSelection - This function converts an LLVM function into
167 /// a machine code representation is a very simple peep-hole fashion. The
168 /// generated code sucks but the implementation is nice and simple.
170 MachineFunction *X86SimpleInstructionSelection(Function &F, TargetMachine &TM) {
171 MachineFunction *Result = new MachineFunction(&F, TM);
172 ISel(Result).runOnFunction(F);