blob: 425ff9ed81c38664bd2febb8ad047cf73278c7d3 [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 Lattner51b49a92002-11-02 19:45:49 +000012#include "llvm/iPHINode.h"
Chris Lattner72614082002-10-25 22:55:53 +000013#include "llvm/Type.h"
Chris Lattnerc5291f52002-10-27 21:16:59 +000014#include "llvm/Constants.h"
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000015#include "llvm/Pass.h"
Chris Lattner341a9372002-10-29 17:43:55 +000016#include "llvm/CodeGen/MachineFunction.h"
17#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner72614082002-10-25 22:55:53 +000018#include "llvm/Support/InstVisitor.h"
19#include <map>
20
21namespace {
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000022 struct ISel : public FunctionPass, InstVisitor<ISel> {
23 TargetMachine &TM;
Chris Lattner341a9372002-10-29 17:43:55 +000024 MachineFunction *F; // The function we are compiling into
25 MachineBasicBlock *BB; // The current MBB we are compiling
Chris Lattner72614082002-10-25 22:55:53 +000026
27 unsigned CurReg;
28 std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs
29
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000030 ISel(TargetMachine &tm)
31 : TM(tm), F(0), BB(0), CurReg(MRegisterInfo::FirstVirtualRegister) {}
Chris Lattner72614082002-10-25 22:55:53 +000032
33 /// runOnFunction - Top level implementation of instruction selection for
34 /// the entire function.
35 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000036 bool runOnFunction(Function &Fn) {
Chris Lattner36b36032002-10-29 23:40:58 +000037 F = &MachineFunction::construct(&Fn, TM);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000038 visit(Fn);
Chris Lattner72614082002-10-25 22:55:53 +000039 RegMap.clear();
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000040 F = 0;
Chris Lattner72614082002-10-25 22:55:53 +000041 return false; // We never modify the LLVM itself.
42 }
43
44 /// visitBasicBlock - This method is called when we are visiting a new basic
Chris Lattner33f53b52002-10-29 20:48:56 +000045 /// block. This simply creates a new MachineBasicBlock to emit code into
46 /// and adds it to the current MachineFunction. Subsequent visit* for
47 /// instructions will be invoked for all instructions in the basic block.
Chris Lattner72614082002-10-25 22:55:53 +000048 ///
49 void visitBasicBlock(BasicBlock &LLVM_BB) {
Chris Lattner42c77862002-10-30 00:47:40 +000050 BB = new MachineBasicBlock(&LLVM_BB);
Chris Lattner72614082002-10-25 22:55:53 +000051 // FIXME: Use the auto-insert form when it's available
52 F->getBasicBlockList().push_back(BB);
53 }
54
55 // Visitation methods for various instructions. These methods simply emit
56 // fixed X86 code for each instruction.
57 //
58 void visitReturnInst(ReturnInst &RI);
Chris Lattner2df035b2002-11-02 19:27:56 +000059 void visitBranchInst(BranchInst &BI);
Chris Lattnere2954c82002-11-02 20:04:26 +000060
61 // Arithmetic operators
Chris Lattner72614082002-10-25 22:55:53 +000062 void visitAdd(BinaryOperator &B);
Chris Lattnere2954c82002-11-02 20:04:26 +000063
64 // Bitwise operators
65 void visitAnd(BinaryOperator &B) { visitBitwise(B, 0); }
66 void visitOr (BinaryOperator &B) { visitBitwise(B, 1); }
67 void visitXor(BinaryOperator &B) { visitBitwise(B, 2); }
68 void visitBitwise(BinaryOperator &B, unsigned OpcodeClass);
69
70 // Binary comparison operators
71
72 // Other operators
Brian Gaekea1719c92002-10-31 23:03:59 +000073 void visitShiftInst(ShiftInst &I);
Chris Lattnere2954c82002-11-02 20:04:26 +000074 void visitPHINode(PHINode &I);
Chris Lattner72614082002-10-25 22:55:53 +000075
76 void visitInstruction(Instruction &I) {
77 std::cerr << "Cannot instruction select: " << I;
78 abort();
79 }
80
Chris Lattnerc5291f52002-10-27 21:16:59 +000081
82 /// copyConstantToRegister - Output the instructions required to put the
83 /// specified constant into the specified register.
84 ///
85 void copyConstantToRegister(Constant *C, unsigned Reg);
86
Chris Lattner72614082002-10-25 22:55:53 +000087 /// getReg - This method turns an LLVM value into a register number. This
88 /// is guaranteed to produce the same register number for a particular value
89 /// every time it is queried.
90 ///
91 unsigned getReg(Value &V) { return getReg(&V); } // Allow references
92 unsigned getReg(Value *V) {
93 unsigned &Reg = RegMap[V];
94 if (Reg == 0)
95 Reg = CurReg++;
96
Chris Lattner6f8fd252002-10-27 21:23:43 +000097 // If this operand is a constant, emit the code to copy the constant into
98 // the register here...
99 //
Chris Lattnerc5291f52002-10-27 21:16:59 +0000100 if (Constant *C = dyn_cast<Constant>(V))
101 copyConstantToRegister(C, Reg);
102
Chris Lattner72614082002-10-25 22:55:53 +0000103 return Reg;
104 }
Chris Lattner72614082002-10-25 22:55:53 +0000105 };
106}
107
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000108/// getClass - Turn a primitive type into a "class" number which is based on the
109/// size of the type, and whether or not it is floating point.
110///
111static inline unsigned getClass(const Type *Ty) {
112 switch (Ty->getPrimitiveID()) {
113 case Type::SByteTyID:
114 case Type::UByteTyID: return 0; // Byte operands are class #0
115 case Type::ShortTyID:
116 case Type::UShortTyID: return 1; // Short operands are class #1
117 case Type::IntTyID:
118 case Type::UIntTyID:
119 case Type::PointerTyID: return 2; // Int's and pointers are class #2
120
121 case Type::LongTyID:
122 case Type::ULongTyID: return 3; // Longs are class #3
123 case Type::FloatTyID: return 4; // Float is class #4
124 case Type::DoubleTyID: return 5; // Doubles are class #5
125 default:
126 assert(0 && "Invalid type to getClass!");
127 return 0; // not reached
128 }
129}
Chris Lattnerc5291f52002-10-27 21:16:59 +0000130
131/// copyConstantToRegister - Output the instructions required to put the
132/// specified constant into the specified register.
133///
134void ISel::copyConstantToRegister(Constant *C, unsigned R) {
135 assert (!isa<ConstantExpr>(C) && "Constant expressions not yet handled!\n");
136
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000137 if (C->getType()->isIntegral()) {
138 unsigned Class = getClass(C->getType());
139 assert(Class != 3 && "Type not handled yet!");
140
141 static const unsigned IntegralOpcodeTab[] = {
142 X86::MOVir8, X86::MOVir16, X86::MOVir32
143 };
144
145 if (C->getType()->isSigned()) {
146 ConstantSInt *CSI = cast<ConstantSInt>(C);
147 BuildMI(BB, IntegralOpcodeTab[Class], 1, R).addSImm(CSI->getValue());
148 } else {
149 ConstantUInt *CUI = cast<ConstantUInt>(C);
150 BuildMI(BB, IntegralOpcodeTab[Class], 1, R).addZImm(CUI->getValue());
151 }
152 } else {
153 assert(0 && "Type not handled yet!");
Chris Lattnerc5291f52002-10-27 21:16:59 +0000154 }
155}
156
Chris Lattner51b49a92002-11-02 19:45:49 +0000157
Chris Lattnerc5291f52002-10-27 21:16:59 +0000158
Chris Lattner72614082002-10-25 22:55:53 +0000159/// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such,
160/// we have the following possibilities:
161///
162/// ret void: No return value, simply emit a 'ret' instruction
163/// ret sbyte, ubyte : Extend value into EAX and return
164/// ret short, ushort: Extend value into EAX and return
165/// ret int, uint : Move value into EAX and return
166/// ret pointer : Move value into EAX and return
167/// ret long, ulong : Move value into EAX/EDX (?) and return
168/// ret float/double : ? Top of FP stack? XMM0?
169///
170void ISel::visitReturnInst(ReturnInst &I) {
171 if (I.getNumOperands() != 0) { // Not 'ret void'?
172 // Move result into a hard register... then emit a ret
173 visitInstruction(I); // abort
174 }
175
176 // Emit a simple 'ret' instruction... appending it to the end of the basic
177 // block
Chris Lattner341a9372002-10-29 17:43:55 +0000178 BuildMI(BB, X86::RET, 0);
Chris Lattner72614082002-10-25 22:55:53 +0000179}
180
Chris Lattner51b49a92002-11-02 19:45:49 +0000181/// visitBranchInst - Handle conditional and unconditional branches here. Note
182/// that since code layout is frozen at this point, that if we are trying to
183/// jump to a block that is the immediate successor of the current block, we can
184/// just make a fall-through. (but we don't currently).
185///
Chris Lattner2df035b2002-11-02 19:27:56 +0000186void ISel::visitBranchInst(BranchInst &BI) {
187 if (BI.isConditional()) // Only handles unconditional branches so far...
188 visitInstruction(BI);
189
190 BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(0));
191}
192
193
Chris Lattnere2954c82002-11-02 20:04:26 +0000194
195/// 'add' instruction - Simply turn this into an x86 reg,reg add instruction.
196void ISel::visitAdd(BinaryOperator &B) {
197 unsigned Op0r = getReg(B.getOperand(0)), Op1r = getReg(B.getOperand(1));
198 unsigned DestReg = getReg(B);
199 unsigned Class = getClass(B.getType());
200
201 static const unsigned Opcodes[] = { X86::ADDrr8, X86::ADDrr16, X86::ADDrr32 };
202
203 if (Class >= sizeof(Opcodes)/sizeof(Opcodes[0]))
204 visitInstruction(B); // Not handled class yet...
205
206 BuildMI(BB, Opcodes[Class], 2, DestReg).addReg(Op0r).addReg(Op1r);
207
208 // For Longs: Here we have a pair of operands each occupying a pair of
209 // registers. We need to do an ADDrr32 of the least-significant pair
210 // immediately followed by an ADCrr32 (Add with Carry) of the most-significant
211 // pair. I don't know how we are representing these multi-register arguments.
212}
213
214/// visitBitwise - Implement the three bitwise operators for integral types...
215/// OperatorClass is one of: 0 for And, 1 for Or, 2 for Xor.
216void ISel::visitBitwise(BinaryOperator &B, unsigned OperatorClass) {
217 if (B.getType() == Type::BoolTy) // FIXME: Handle bools
218 visitInstruction(B);
219
220 unsigned Class = getClass(B.getType());
221 if (Class > 2) // FIXME: Handle longs
222 visitInstruction(B);
223
224 static const unsigned OpcodeTab[][4] = {
225 { X86::ANDrr8, X86::ANDrr16, X86::ANDrr32, 0 }, // AND
226 { X86:: ORrr8, X86:: ORrr16, X86:: ORrr32, 0 }, // OR
227 { X86::XORrr8, X86::XORrr16, X86::XORrr32, 0 }, // XOR
228 };
229
230 unsigned Opcode = OpcodeTab[OperatorClass][Class];
231 unsigned Op0r = getReg(B.getOperand(0));
232 unsigned Op1r = getReg(B.getOperand(1));
233 BuildMI(BB, Opcode, 2, getReg(B)).addReg(Op0r).addReg(Op1r);
234}
235
236
237
Brian Gaekea1719c92002-10-31 23:03:59 +0000238/// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here
239/// for constant immediate shift values, and for constant immediate
240/// shift values equal to 1. Even the general case is sort of special,
241/// because the shift amount has to be in CL, not just any old register.
242///
243void
244ISel::visitShiftInst (ShiftInst & I)
245{
246 unsigned Op0r = getReg (I.getOperand (0));
247 unsigned DestReg = getReg (I);
Chris Lattnere9913f22002-11-02 01:41:55 +0000248 bool isLeftShift = I.getOpcode() == Instruction::Shl;
249 bool isOperandSigned = I.getType()->isUnsigned();
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000250 unsigned OperandClass = getClass(I.getType());
251
252 if (OperandClass > 2)
253 visitInstruction(I); // Can't handle longs yet!
Chris Lattner796df732002-11-02 00:44:25 +0000254
Brian Gaekea1719c92002-10-31 23:03:59 +0000255 if (ConstantUInt *CUI = dyn_cast <ConstantUInt> (I.getOperand (1)))
256 {
Chris Lattner796df732002-11-02 00:44:25 +0000257 // The shift amount is constant, guaranteed to be a ubyte. Get its value.
258 assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?");
259 unsigned char shAmt = CUI->getValue();
260
Chris Lattnere9913f22002-11-02 01:41:55 +0000261 static const unsigned ConstantOperand[][4] = {
262 { X86::SHRir8, X86::SHRir16, X86::SHRir32, 0 }, // SHR
263 { X86::SARir8, X86::SARir16, X86::SARir32, 0 }, // SAR
264 { X86::SHLir8, X86::SHLir16, X86::SHLir32, 0 }, // SHL
265 { X86::SHLir8, X86::SHLir16, X86::SHLir32, 0 }, // SAL = SHL
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000266 };
267
Chris Lattnere9913f22002-11-02 01:41:55 +0000268 const unsigned *OpTab = // Figure out the operand table to use
269 ConstantOperand[isLeftShift*2+isOperandSigned];
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000270
Brian Gaekea1719c92002-10-31 23:03:59 +0000271 // Emit: <insn> reg, shamt (shift-by-immediate opcode "ir" form.)
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000272 BuildMI(BB, OpTab[OperandClass], 2, DestReg).addReg(Op0r).addZImm(shAmt);
Brian Gaekea1719c92002-10-31 23:03:59 +0000273 }
274 else
275 {
276 // The shift amount is non-constant.
277 //
278 // In fact, you can only shift with a variable shift amount if
279 // that amount is already in the CL register, so we have to put it
280 // there first.
281 //
Chris Lattnere9913f22002-11-02 01:41:55 +0000282
Brian Gaekea1719c92002-10-31 23:03:59 +0000283 // Emit: move cl, shiftAmount (put the shift amount in CL.)
Chris Lattnere9913f22002-11-02 01:41:55 +0000284 BuildMI (BB, X86::MOVrr8, 2, X86::CL).addReg(getReg(I.getOperand(1)));
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000285
286 // This is a shift right (SHR).
Chris Lattnere9913f22002-11-02 01:41:55 +0000287 static const unsigned NonConstantOperand[][4] = {
288 { X86::SHRrr8, X86::SHRrr16, X86::SHRrr32, 0 }, // SHR
289 { X86::SARrr8, X86::SARrr16, X86::SARrr32, 0 }, // SAR
290 { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32, 0 }, // SHL
291 { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32, 0 }, // SAL = SHL
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000292 };
293
Chris Lattnere9913f22002-11-02 01:41:55 +0000294 const unsigned *OpTab = // Figure out the operand table to use
295 NonConstantOperand[isLeftShift*2+isOperandSigned];
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000296
Chris Lattnere9913f22002-11-02 01:41:55 +0000297 BuildMI(BB, OpTab[OperandClass], 2, DestReg).addReg(Op0r).addReg(X86::CL);
Brian Gaekea1719c92002-10-31 23:03:59 +0000298 }
299}
300
Chris Lattnere2954c82002-11-02 20:04:26 +0000301/// visitPHINode - Turn an LLVM PHI node into an X86 PHI node...
302///
303void ISel::visitPHINode(PHINode &PN) {
304 MachineInstr *MI = BuildMI(BB, X86::PHI, PN.getNumOperands(), getReg(PN));
Chris Lattner72614082002-10-25 22:55:53 +0000305
Chris Lattnere2954c82002-11-02 20:04:26 +0000306 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
307 // FIXME: This will put constants after the PHI nodes in the block, which
308 // is invalid. They should be put inline into the PHI node eventually.
309 //
310 MI->addRegOperand(getReg(PN.getIncomingValue(i)));
311 MI->addPCDispOperand(PN.getIncomingBlock(i));
312 }
Chris Lattner72614082002-10-25 22:55:53 +0000313}
314
Brian Gaekea1719c92002-10-31 23:03:59 +0000315
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000316/// createSimpleX86InstructionSelector - This pass converts an LLVM function
317/// into a machine code representation is a very simple peep-hole fashion. The
Chris Lattner72614082002-10-25 22:55:53 +0000318/// generated code sucks but the implementation is nice and simple.
319///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000320Pass *createSimpleX86InstructionSelector(TargetMachine &TM) {
321 return new ISel(TM);
Chris Lattner72614082002-10-25 22:55:53 +0000322}