blob: 253f3ddcb0474af5498d5c07d26c9dac54f2a3d3 [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"
Brian Gaekea1719c92002-10-31 23:03:59 +000011#include "llvm/iOther.h"
Chris Lattner72614082002-10-25 22:55:53 +000012#include "llvm/Type.h"
Chris Lattnerc5291f52002-10-27 21:16:59 +000013#include "llvm/Constants.h"
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000014#include "llvm/Pass.h"
Chris Lattner341a9372002-10-29 17:43:55 +000015#include "llvm/CodeGen/MachineFunction.h"
16#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner72614082002-10-25 22:55:53 +000017#include "llvm/Support/InstVisitor.h"
18#include <map>
19
20namespace {
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000021 struct ISel : public FunctionPass, InstVisitor<ISel> {
22 TargetMachine &TM;
Chris Lattner341a9372002-10-29 17:43:55 +000023 MachineFunction *F; // The function we are compiling into
24 MachineBasicBlock *BB; // The current MBB we are compiling
Chris Lattner72614082002-10-25 22:55:53 +000025
26 unsigned CurReg;
27 std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs
28
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000029 ISel(TargetMachine &tm)
30 : TM(tm), F(0), BB(0), CurReg(MRegisterInfo::FirstVirtualRegister) {}
Chris Lattner72614082002-10-25 22:55:53 +000031
32 /// runOnFunction - Top level implementation of instruction selection for
33 /// the entire function.
34 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000035 bool runOnFunction(Function &Fn) {
Chris Lattner36b36032002-10-29 23:40:58 +000036 F = &MachineFunction::construct(&Fn, TM);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000037 visit(Fn);
Chris Lattner72614082002-10-25 22:55:53 +000038 RegMap.clear();
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000039 F = 0;
Chris Lattner72614082002-10-25 22:55:53 +000040 return false; // We never modify the LLVM itself.
41 }
42
43 /// visitBasicBlock - This method is called when we are visiting a new basic
Chris Lattner33f53b52002-10-29 20:48:56 +000044 /// block. This simply creates a new MachineBasicBlock to emit code into
45 /// and adds it to the current MachineFunction. Subsequent visit* for
46 /// instructions will be invoked for all instructions in the basic block.
Chris Lattner72614082002-10-25 22:55:53 +000047 ///
48 void visitBasicBlock(BasicBlock &LLVM_BB) {
Chris Lattner42c77862002-10-30 00:47:40 +000049 BB = new MachineBasicBlock(&LLVM_BB);
Chris Lattner72614082002-10-25 22:55:53 +000050 // FIXME: Use the auto-insert form when it's available
51 F->getBasicBlockList().push_back(BB);
52 }
53
54 // Visitation methods for various instructions. These methods simply emit
55 // fixed X86 code for each instruction.
56 //
57 void visitReturnInst(ReturnInst &RI);
Chris Lattner2df035b2002-11-02 19:27:56 +000058 void visitBranchInst(BranchInst &BI);
Chris Lattner72614082002-10-25 22:55:53 +000059 void visitAdd(BinaryOperator &B);
Brian Gaekea1719c92002-10-31 23:03:59 +000060 void visitShiftInst(ShiftInst &I);
Chris Lattner72614082002-10-25 22:55:53 +000061
62 void visitInstruction(Instruction &I) {
63 std::cerr << "Cannot instruction select: " << I;
64 abort();
65 }
66
Chris Lattnerc5291f52002-10-27 21:16:59 +000067
68 /// copyConstantToRegister - Output the instructions required to put the
69 /// specified constant into the specified register.
70 ///
71 void copyConstantToRegister(Constant *C, unsigned Reg);
72
Chris Lattner72614082002-10-25 22:55:53 +000073 /// getReg - This method turns an LLVM value into a register number. This
74 /// is guaranteed to produce the same register number for a particular value
75 /// every time it is queried.
76 ///
77 unsigned getReg(Value &V) { return getReg(&V); } // Allow references
78 unsigned getReg(Value *V) {
79 unsigned &Reg = RegMap[V];
80 if (Reg == 0)
81 Reg = CurReg++;
82
Chris Lattner6f8fd252002-10-27 21:23:43 +000083 // If this operand is a constant, emit the code to copy the constant into
84 // the register here...
85 //
Chris Lattnerc5291f52002-10-27 21:16:59 +000086 if (Constant *C = dyn_cast<Constant>(V))
87 copyConstantToRegister(C, Reg);
88
Chris Lattner72614082002-10-25 22:55:53 +000089 return Reg;
90 }
Chris Lattner72614082002-10-25 22:55:53 +000091 };
92}
93
Chris Lattnerb1761fc2002-11-02 01:15:18 +000094/// getClass - Turn a primitive type into a "class" number which is based on the
95/// size of the type, and whether or not it is floating point.
96///
97static inline unsigned getClass(const Type *Ty) {
98 switch (Ty->getPrimitiveID()) {
99 case Type::SByteTyID:
100 case Type::UByteTyID: return 0; // Byte operands are class #0
101 case Type::ShortTyID:
102 case Type::UShortTyID: return 1; // Short operands are class #1
103 case Type::IntTyID:
104 case Type::UIntTyID:
105 case Type::PointerTyID: return 2; // Int's and pointers are class #2
106
107 case Type::LongTyID:
108 case Type::ULongTyID: return 3; // Longs are class #3
109 case Type::FloatTyID: return 4; // Float is class #4
110 case Type::DoubleTyID: return 5; // Doubles are class #5
111 default:
112 assert(0 && "Invalid type to getClass!");
113 return 0; // not reached
114 }
115}
Chris Lattnerc5291f52002-10-27 21:16:59 +0000116
117/// copyConstantToRegister - Output the instructions required to put the
118/// specified constant into the specified register.
119///
120void ISel::copyConstantToRegister(Constant *C, unsigned R) {
121 assert (!isa<ConstantExpr>(C) && "Constant expressions not yet handled!\n");
122
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000123 if (C->getType()->isIntegral()) {
124 unsigned Class = getClass(C->getType());
125 assert(Class != 3 && "Type not handled yet!");
126
127 static const unsigned IntegralOpcodeTab[] = {
128 X86::MOVir8, X86::MOVir16, X86::MOVir32
129 };
130
131 if (C->getType()->isSigned()) {
132 ConstantSInt *CSI = cast<ConstantSInt>(C);
133 BuildMI(BB, IntegralOpcodeTab[Class], 1, R).addSImm(CSI->getValue());
134 } else {
135 ConstantUInt *CUI = cast<ConstantUInt>(C);
136 BuildMI(BB, IntegralOpcodeTab[Class], 1, R).addZImm(CUI->getValue());
137 }
138 } else {
139 assert(0 && "Type not handled yet!");
Chris Lattnerc5291f52002-10-27 21:16:59 +0000140 }
141}
142
143
Chris Lattner72614082002-10-25 22:55:53 +0000144/// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such,
145/// we have the following possibilities:
146///
147/// ret void: No return value, simply emit a 'ret' instruction
148/// ret sbyte, ubyte : Extend value into EAX and return
149/// ret short, ushort: Extend value into EAX and return
150/// ret int, uint : Move value into EAX and return
151/// ret pointer : Move value into EAX and return
152/// ret long, ulong : Move value into EAX/EDX (?) and return
153/// ret float/double : ? Top of FP stack? XMM0?
154///
155void ISel::visitReturnInst(ReturnInst &I) {
156 if (I.getNumOperands() != 0) { // Not 'ret void'?
157 // Move result into a hard register... then emit a ret
158 visitInstruction(I); // abort
159 }
160
161 // Emit a simple 'ret' instruction... appending it to the end of the basic
162 // block
Chris Lattner341a9372002-10-29 17:43:55 +0000163 BuildMI(BB, X86::RET, 0);
Chris Lattner72614082002-10-25 22:55:53 +0000164}
165
Chris Lattner2df035b2002-11-02 19:27:56 +0000166void ISel::visitBranchInst(BranchInst &BI) {
167 if (BI.isConditional()) // Only handles unconditional branches so far...
168 visitInstruction(BI);
169
170 BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(0));
171}
172
173
Brian Gaekea1719c92002-10-31 23:03:59 +0000174/// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here
175/// for constant immediate shift values, and for constant immediate
176/// shift values equal to 1. Even the general case is sort of special,
177/// because the shift amount has to be in CL, not just any old register.
178///
179void
180ISel::visitShiftInst (ShiftInst & I)
181{
182 unsigned Op0r = getReg (I.getOperand (0));
183 unsigned DestReg = getReg (I);
Chris Lattnere9913f22002-11-02 01:41:55 +0000184 bool isLeftShift = I.getOpcode() == Instruction::Shl;
185 bool isOperandSigned = I.getType()->isUnsigned();
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000186 unsigned OperandClass = getClass(I.getType());
187
188 if (OperandClass > 2)
189 visitInstruction(I); // Can't handle longs yet!
Chris Lattner796df732002-11-02 00:44:25 +0000190
Brian Gaekea1719c92002-10-31 23:03:59 +0000191 if (ConstantUInt *CUI = dyn_cast <ConstantUInt> (I.getOperand (1)))
192 {
Chris Lattner796df732002-11-02 00:44:25 +0000193 // The shift amount is constant, guaranteed to be a ubyte. Get its value.
194 assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?");
195 unsigned char shAmt = CUI->getValue();
196
Chris Lattnere9913f22002-11-02 01:41:55 +0000197 static const unsigned ConstantOperand[][4] = {
198 { X86::SHRir8, X86::SHRir16, X86::SHRir32, 0 }, // SHR
199 { X86::SARir8, X86::SARir16, X86::SARir32, 0 }, // SAR
200 { X86::SHLir8, X86::SHLir16, X86::SHLir32, 0 }, // SHL
201 { X86::SHLir8, X86::SHLir16, X86::SHLir32, 0 }, // SAL = SHL
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000202 };
203
Chris Lattnere9913f22002-11-02 01:41:55 +0000204 const unsigned *OpTab = // Figure out the operand table to use
205 ConstantOperand[isLeftShift*2+isOperandSigned];
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000206
Brian Gaekea1719c92002-10-31 23:03:59 +0000207 // Emit: <insn> reg, shamt (shift-by-immediate opcode "ir" form.)
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000208 BuildMI(BB, OpTab[OperandClass], 2, DestReg).addReg(Op0r).addZImm(shAmt);
Brian Gaekea1719c92002-10-31 23:03:59 +0000209 }
210 else
211 {
212 // The shift amount is non-constant.
213 //
214 // In fact, you can only shift with a variable shift amount if
215 // that amount is already in the CL register, so we have to put it
216 // there first.
217 //
Chris Lattnere9913f22002-11-02 01:41:55 +0000218
Brian Gaekea1719c92002-10-31 23:03:59 +0000219 // Emit: move cl, shiftAmount (put the shift amount in CL.)
Chris Lattnere9913f22002-11-02 01:41:55 +0000220 BuildMI (BB, X86::MOVrr8, 2, X86::CL).addReg(getReg(I.getOperand(1)));
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000221
222 // This is a shift right (SHR).
Chris Lattnere9913f22002-11-02 01:41:55 +0000223 static const unsigned NonConstantOperand[][4] = {
224 { X86::SHRrr8, X86::SHRrr16, X86::SHRrr32, 0 }, // SHR
225 { X86::SARrr8, X86::SARrr16, X86::SARrr32, 0 }, // SAR
226 { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32, 0 }, // SHL
227 { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32, 0 }, // SAL = SHL
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000228 };
229
Chris Lattnere9913f22002-11-02 01:41:55 +0000230 const unsigned *OpTab = // Figure out the operand table to use
231 NonConstantOperand[isLeftShift*2+isOperandSigned];
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000232
Chris Lattnere9913f22002-11-02 01:41:55 +0000233 BuildMI(BB, OpTab[OperandClass], 2, DestReg).addReg(Op0r).addReg(X86::CL);
Brian Gaekea1719c92002-10-31 23:03:59 +0000234 }
235}
236
Chris Lattner72614082002-10-25 22:55:53 +0000237
238/// 'add' instruction - Simply turn this into an x86 reg,reg add instruction.
239void ISel::visitAdd(BinaryOperator &B) {
240 unsigned Op0r = getReg(B.getOperand(0)), Op1r = getReg(B.getOperand(1));
241 unsigned DestReg = getReg(B);
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000242 unsigned Class = getClass(B.getType());
Chris Lattner72614082002-10-25 22:55:53 +0000243
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000244 static const unsigned Opcodes[] = { X86::ADDrr8, X86::ADDrr16, X86::ADDrr32 };
245
246 if (Class >= sizeof(Opcodes)/sizeof(Opcodes[0]))
247 visitInstruction(B); // Not handled class yet...
248
249 BuildMI(BB, Opcodes[Class], 2, DestReg).addReg(Op0r).addReg(Op1r);
250
251 // For Longs: Here we have a pair of operands each occupying a pair of
252 // registers. We need to do an ADDrr32 of the least-significant pair
253 // immediately followed by an ADCrr32 (Add with Carry) of the most-significant
254 // pair. I don't know how we are representing these multi-register arguments.
Chris Lattner72614082002-10-25 22:55:53 +0000255}
256
Brian Gaekea1719c92002-10-31 23:03:59 +0000257
258
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000259/// createSimpleX86InstructionSelector - This pass converts an LLVM function
260/// into a machine code representation is a very simple peep-hole fashion. The
Chris Lattner72614082002-10-25 22:55:53 +0000261/// generated code sucks but the implementation is nice and simple.
262///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000263Pass *createSimpleX86InstructionSelector(TargetMachine &TM) {
264 return new ISel(TM);
Chris Lattner72614082002-10-25 22:55:53 +0000265}