blob: 4491fdf792fb3d38510db19fb0f118910edf8177 [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"
Chris Lattner055c9652002-10-29 21:05:24 +00008#include "X86InstrInfo.h"
Chris Lattner72614082002-10-25 22:55:53 +00009#include "llvm/Function.h"
10#include "llvm/iTerminators.h"
11#include "llvm/Type.h"
Chris Lattnerc5291f52002-10-27 21:16:59 +000012#include "llvm/Constants.h"
Chris Lattner341a9372002-10-29 17:43:55 +000013#include "llvm/CodeGen/MachineFunction.h"
14#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner72614082002-10-25 22:55:53 +000015#include "llvm/Support/InstVisitor.h"
16#include <map>
17
18namespace {
Chris Lattner341a9372002-10-29 17:43:55 +000019 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
Chris Lattner72614082002-10-25 22:55:53 +000022
23 unsigned CurReg;
24 std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs
25
Chris Lattner341a9372002-10-29 17:43:55 +000026 ISel(MachineFunction *f)
Chris Lattner72614082002-10-25 22:55:53 +000027 : F(f), BB(0), CurReg(MRegisterInfo::FirstVirtualRegister) {}
28
29 /// runOnFunction - Top level implementation of instruction selection for
30 /// the entire function.
31 ///
32 bool runOnFunction(Function &F) {
33 visit(F);
34 RegMap.clear();
35 return false; // We never modify the LLVM itself.
36 }
37
38 /// visitBasicBlock - This method is called when we are visiting a new basic
Chris Lattner33f53b52002-10-29 20:48:56 +000039 /// 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.
Chris Lattner72614082002-10-25 22:55:53 +000042 ///
43 void visitBasicBlock(BasicBlock &LLVM_BB) {
Chris Lattner341a9372002-10-29 17:43:55 +000044 BB = new MachineBasicBlock();
Chris Lattner72614082002-10-25 22:55:53 +000045 // FIXME: Use the auto-insert form when it's available
46 F->getBasicBlockList().push_back(BB);
47 }
48
49 // Visitation methods for various instructions. These methods simply emit
50 // fixed X86 code for each instruction.
51 //
52 void visitReturnInst(ReturnInst &RI);
53 void visitAdd(BinaryOperator &B);
54
55 void visitInstruction(Instruction &I) {
56 std::cerr << "Cannot instruction select: " << I;
57 abort();
58 }
59
Chris Lattnerc5291f52002-10-27 21:16:59 +000060
61 /// copyConstantToRegister - Output the instructions required to put the
62 /// specified constant into the specified register.
63 ///
64 void copyConstantToRegister(Constant *C, unsigned Reg);
65
Chris Lattner72614082002-10-25 22:55:53 +000066 /// 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.
69 ///
70 unsigned getReg(Value &V) { return getReg(&V); } // Allow references
71 unsigned getReg(Value *V) {
72 unsigned &Reg = RegMap[V];
73 if (Reg == 0)
74 Reg = CurReg++;
75
Chris Lattner6f8fd252002-10-27 21:23:43 +000076 // If this operand is a constant, emit the code to copy the constant into
77 // the register here...
78 //
Chris Lattnerc5291f52002-10-27 21:16:59 +000079 if (Constant *C = dyn_cast<Constant>(V))
80 copyConstantToRegister(C, Reg);
81
Chris Lattner72614082002-10-25 22:55:53 +000082 return Reg;
83 }
84
85 };
86}
87
Chris Lattnerc5291f52002-10-27 21:16:59 +000088
89/// copyConstantToRegister - Output the instructions required to put the
90/// specified constant into the specified register.
91///
92void ISel::copyConstantToRegister(Constant *C, unsigned R) {
93 assert (!isa<ConstantExpr>(C) && "Constant expressions not yet handled!\n");
94
95 switch (C->getType()->getPrimitiveID()) {
96 case Type::SByteTyID:
Chris Lattner341a9372002-10-29 17:43:55 +000097 BuildMI(BB, X86::MOVir8, R).addSImm(cast<ConstantSInt>(C)->getValue());
Chris Lattnerc5291f52002-10-27 21:16:59 +000098 break;
99 case Type::UByteTyID:
Chris Lattner341a9372002-10-29 17:43:55 +0000100 BuildMI(BB, X86::MOVir8, R).addZImm(cast<ConstantUInt>(C)->getValue());
Chris Lattnerc5291f52002-10-27 21:16:59 +0000101 break;
102 case Type::ShortTyID:
Chris Lattner341a9372002-10-29 17:43:55 +0000103 BuildMI(BB, X86::MOVir16, R).addSImm(cast<ConstantSInt>(C)->getValue());
Chris Lattnerc5291f52002-10-27 21:16:59 +0000104 break;
105 case Type::UShortTyID:
Chris Lattner341a9372002-10-29 17:43:55 +0000106 BuildMI(BB, X86::MOVir16, R).addZImm(cast<ConstantUInt>(C)->getValue());
Chris Lattnerc5291f52002-10-27 21:16:59 +0000107 break;
108 case Type::IntTyID:
Chris Lattner341a9372002-10-29 17:43:55 +0000109 BuildMI(BB, X86::MOVir32, R).addSImm(cast<ConstantSInt>(C)->getValue());
Chris Lattnerc5291f52002-10-27 21:16:59 +0000110 break;
111 case Type::UIntTyID:
Chris Lattner341a9372002-10-29 17:43:55 +0000112 BuildMI(BB, X86::MOVir32, R).addZImm(cast<ConstantUInt>(C)->getValue());
Chris Lattnerc5291f52002-10-27 21:16:59 +0000113 break;
114 default: assert(0 && "Type not handled yet!");
115 }
116}
117
118
Chris Lattner72614082002-10-25 22:55:53 +0000119/// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such,
120/// we have the following possibilities:
121///
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?
129///
130void 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
134 }
135
136 // Emit a simple 'ret' instruction... appending it to the end of the basic
137 // block
Chris Lattner341a9372002-10-29 17:43:55 +0000138 BuildMI(BB, X86::RET, 0);
Chris Lattner72614082002-10-25 22:55:53 +0000139}
140
141
142/// 'add' instruction - Simply turn this into an x86 reg,reg add instruction.
143void ISel::visitAdd(BinaryOperator &B) {
144 unsigned Op0r = getReg(B.getOperand(0)), Op1r = getReg(B.getOperand(1));
145 unsigned DestReg = getReg(B);
146
147 switch (B.getType()->getPrimitiveSize()) {
148 case 1: // UByte, SByte
Chris Lattner341a9372002-10-29 17:43:55 +0000149 BuildMI(BB, X86::ADDrr8, DestReg).addReg(Op0r).addReg(Op1r);
Chris Lattner72614082002-10-25 22:55:53 +0000150 break;
151 case 2: // UShort, Short
Chris Lattner341a9372002-10-29 17:43:55 +0000152 BuildMI(BB, X86::ADDrr16, DestReg).addReg(Op0r).addReg(Op1r);
Chris Lattner72614082002-10-25 22:55:53 +0000153 break;
154 case 4: // UInt, Int
Chris Lattner341a9372002-10-29 17:43:55 +0000155 BuildMI(BB, X86::ADDrr32, DestReg).addReg(Op0r).addReg(Op1r);
Chris Lattner72614082002-10-25 22:55:53 +0000156 break;
157
158 case 8: // ULong, Long
159 default:
160 visitInstruction(B); // abort
161 }
162}
163
164
165
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.
169///
Chris Lattner341a9372002-10-29 17:43:55 +0000170MachineFunction *X86SimpleInstructionSelection(Function &F, TargetMachine &TM) {
171 MachineFunction *Result = new MachineFunction(&F, TM);
Chris Lattner72614082002-10-25 22:55:53 +0000172 ISel(Result).runOnFunction(F);
173 return Result;
174}