blob: d0d80e3226c5575e200a42e0f8adc4c2953e4d5d [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 Lattnerb4f68ed2002-10-29 22:37:54 +000013#include "llvm/Pass.h"
Chris Lattner341a9372002-10-29 17:43:55 +000014#include "llvm/CodeGen/MachineFunction.h"
15#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner72614082002-10-25 22:55:53 +000016#include "llvm/Support/InstVisitor.h"
17#include <map>
18
19namespace {
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000020 struct ISel : public FunctionPass, InstVisitor<ISel> {
21 TargetMachine &TM;
Chris Lattner341a9372002-10-29 17:43:55 +000022 MachineFunction *F; // The function we are compiling into
23 MachineBasicBlock *BB; // The current MBB we are compiling
Chris Lattner72614082002-10-25 22:55:53 +000024
25 unsigned CurReg;
26 std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs
27
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000028 ISel(TargetMachine &tm)
29 : TM(tm), F(0), BB(0), CurReg(MRegisterInfo::FirstVirtualRegister) {}
Chris Lattner72614082002-10-25 22:55:53 +000030
31 /// runOnFunction - Top level implementation of instruction selection for
32 /// the entire function.
33 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000034 bool runOnFunction(Function &Fn) {
Chris Lattner36b36032002-10-29 23:40:58 +000035 F = &MachineFunction::construct(&Fn, TM);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000036 visit(Fn);
Chris Lattner72614082002-10-25 22:55:53 +000037 RegMap.clear();
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000038 F = 0;
Chris Lattner72614082002-10-25 22:55:53 +000039 return false; // We never modify the LLVM itself.
40 }
41
42 /// visitBasicBlock - This method is called when we are visiting a new basic
Chris Lattner33f53b52002-10-29 20:48:56 +000043 /// block. This simply creates a new MachineBasicBlock to emit code into
44 /// and adds it to the current MachineFunction. Subsequent visit* for
45 /// instructions will be invoked for all instructions in the basic block.
Chris Lattner72614082002-10-25 22:55:53 +000046 ///
47 void visitBasicBlock(BasicBlock &LLVM_BB) {
Chris Lattner42c77862002-10-30 00:47:40 +000048 BB = new MachineBasicBlock(&LLVM_BB);
Chris Lattner72614082002-10-25 22:55:53 +000049 // FIXME: Use the auto-insert form when it's available
50 F->getBasicBlockList().push_back(BB);
51 }
52
53 // Visitation methods for various instructions. These methods simply emit
54 // fixed X86 code for each instruction.
55 //
56 void visitReturnInst(ReturnInst &RI);
57 void visitAdd(BinaryOperator &B);
58
59 void visitInstruction(Instruction &I) {
60 std::cerr << "Cannot instruction select: " << I;
61 abort();
62 }
63
Chris Lattnerc5291f52002-10-27 21:16:59 +000064
65 /// copyConstantToRegister - Output the instructions required to put the
66 /// specified constant into the specified register.
67 ///
68 void copyConstantToRegister(Constant *C, unsigned Reg);
69
Chris Lattner72614082002-10-25 22:55:53 +000070 /// getReg - This method turns an LLVM value into a register number. This
71 /// is guaranteed to produce the same register number for a particular value
72 /// every time it is queried.
73 ///
74 unsigned getReg(Value &V) { return getReg(&V); } // Allow references
75 unsigned getReg(Value *V) {
76 unsigned &Reg = RegMap[V];
77 if (Reg == 0)
78 Reg = CurReg++;
79
Chris Lattner6f8fd252002-10-27 21:23:43 +000080 // If this operand is a constant, emit the code to copy the constant into
81 // the register here...
82 //
Chris Lattnerc5291f52002-10-27 21:16:59 +000083 if (Constant *C = dyn_cast<Constant>(V))
84 copyConstantToRegister(C, Reg);
85
Chris Lattner72614082002-10-25 22:55:53 +000086 return Reg;
87 }
Chris Lattner72614082002-10-25 22:55:53 +000088 };
89}
90
Chris Lattnerc5291f52002-10-27 21:16:59 +000091
92/// copyConstantToRegister - Output the instructions required to put the
93/// specified constant into the specified register.
94///
95void ISel::copyConstantToRegister(Constant *C, unsigned R) {
96 assert (!isa<ConstantExpr>(C) && "Constant expressions not yet handled!\n");
97
98 switch (C->getType()->getPrimitiveID()) {
99 case Type::SByteTyID:
Chris Lattner8548ee72002-10-30 01:49:01 +0000100 BuildMI(BB, X86::MOVir8, 1, R).addSImm(cast<ConstantSInt>(C)->getValue());
Chris Lattnerc5291f52002-10-27 21:16:59 +0000101 break;
102 case Type::UByteTyID:
Chris Lattner8548ee72002-10-30 01:49:01 +0000103 BuildMI(BB, X86::MOVir8, 1, R).addZImm(cast<ConstantUInt>(C)->getValue());
Chris Lattnerc5291f52002-10-27 21:16:59 +0000104 break;
105 case Type::ShortTyID:
Chris Lattner8548ee72002-10-30 01:49:01 +0000106 BuildMI(BB, X86::MOVir16, 1, R).addSImm(cast<ConstantSInt>(C)->getValue());
Chris Lattnerc5291f52002-10-27 21:16:59 +0000107 break;
108 case Type::UShortTyID:
Chris Lattner8548ee72002-10-30 01:49:01 +0000109 BuildMI(BB, X86::MOVir16, 1, R).addZImm(cast<ConstantUInt>(C)->getValue());
Chris Lattnerc5291f52002-10-27 21:16:59 +0000110 break;
111 case Type::IntTyID:
Chris Lattner8548ee72002-10-30 01:49:01 +0000112 BuildMI(BB, X86::MOVir32, 1, R).addSImm(cast<ConstantSInt>(C)->getValue());
Chris Lattnerc5291f52002-10-27 21:16:59 +0000113 break;
114 case Type::UIntTyID:
Chris Lattner8548ee72002-10-30 01:49:01 +0000115 BuildMI(BB, X86::MOVir32, 1, R).addZImm(cast<ConstantUInt>(C)->getValue());
Chris Lattnerc5291f52002-10-27 21:16:59 +0000116 break;
117 default: assert(0 && "Type not handled yet!");
118 }
119}
120
121
Chris Lattner72614082002-10-25 22:55:53 +0000122/// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such,
123/// we have the following possibilities:
124///
125/// ret void: No return value, simply emit a 'ret' instruction
126/// ret sbyte, ubyte : Extend value into EAX and return
127/// ret short, ushort: Extend value into EAX and return
128/// ret int, uint : Move value into EAX and return
129/// ret pointer : Move value into EAX and return
130/// ret long, ulong : Move value into EAX/EDX (?) and return
131/// ret float/double : ? Top of FP stack? XMM0?
132///
133void ISel::visitReturnInst(ReturnInst &I) {
134 if (I.getNumOperands() != 0) { // Not 'ret void'?
135 // Move result into a hard register... then emit a ret
136 visitInstruction(I); // abort
137 }
138
139 // Emit a simple 'ret' instruction... appending it to the end of the basic
140 // block
Chris Lattner341a9372002-10-29 17:43:55 +0000141 BuildMI(BB, X86::RET, 0);
Chris Lattner72614082002-10-25 22:55:53 +0000142}
143
144
145/// 'add' instruction - Simply turn this into an x86 reg,reg add instruction.
146void ISel::visitAdd(BinaryOperator &B) {
147 unsigned Op0r = getReg(B.getOperand(0)), Op1r = getReg(B.getOperand(1));
148 unsigned DestReg = getReg(B);
149
150 switch (B.getType()->getPrimitiveSize()) {
151 case 1: // UByte, SByte
Chris Lattner8548ee72002-10-30 01:49:01 +0000152 BuildMI(BB, X86::ADDrr8, 2, DestReg).addReg(Op0r).addReg(Op1r);
Chris Lattner72614082002-10-25 22:55:53 +0000153 break;
154 case 2: // UShort, Short
Chris Lattner8548ee72002-10-30 01:49:01 +0000155 BuildMI(BB, X86::ADDrr16, 2, DestReg).addReg(Op0r).addReg(Op1r);
Chris Lattner72614082002-10-25 22:55:53 +0000156 break;
157 case 4: // UInt, Int
Chris Lattner8548ee72002-10-30 01:49:01 +0000158 BuildMI(BB, X86::ADDrr32, 2, DestReg).addReg(Op0r).addReg(Op1r);
Chris Lattner72614082002-10-25 22:55:53 +0000159 break;
160
161 case 8: // ULong, Long
162 default:
163 visitInstruction(B); // abort
164 }
165}
166
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000167/// createSimpleX86InstructionSelector - This pass converts an LLVM function
168/// into a machine code representation is a very simple peep-hole fashion. The
Chris Lattner72614082002-10-25 22:55:53 +0000169/// generated code sucks but the implementation is nice and simple.
170///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000171Pass *createSimpleX86InstructionSelector(TargetMachine &TM) {
172 return new ISel(TM);
Chris Lattner72614082002-10-25 22:55:53 +0000173}