blob: 8cbe969843d4300f7b683f06d51264e1d4b47881 [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 Lattnerf01729e2002-11-02 20:54:46 +000062 void visitSimpleBinary(BinaryOperator &B, unsigned OpcodeClass);
Chris Lattner68aad932002-11-02 20:13:22 +000063 void visitAdd(BinaryOperator &B) { visitSimpleBinary(B, 0); }
64 void visitSub(BinaryOperator &B) { visitSimpleBinary(B, 1); }
Chris Lattnerca9671d2002-11-02 20:28:58 +000065 void visitMul(BinaryOperator &B);
Chris Lattnere2954c82002-11-02 20:04:26 +000066
Chris Lattnerf01729e2002-11-02 20:54:46 +000067 void visitDiv(BinaryOperator &B) { visitDivRem(B); }
68 void visitRem(BinaryOperator &B) { visitDivRem(B); }
69 void visitDivRem(BinaryOperator &B);
70
Chris Lattnere2954c82002-11-02 20:04:26 +000071 // Bitwise operators
Chris Lattner68aad932002-11-02 20:13:22 +000072 void visitAnd(BinaryOperator &B) { visitSimpleBinary(B, 2); }
73 void visitOr (BinaryOperator &B) { visitSimpleBinary(B, 3); }
74 void visitXor(BinaryOperator &B) { visitSimpleBinary(B, 4); }
Chris Lattnere2954c82002-11-02 20:04:26 +000075
76 // Binary comparison operators
77
78 // Other operators
Brian Gaekea1719c92002-10-31 23:03:59 +000079 void visitShiftInst(ShiftInst &I);
Chris Lattnere2954c82002-11-02 20:04:26 +000080 void visitPHINode(PHINode &I);
Chris Lattner72614082002-10-25 22:55:53 +000081
82 void visitInstruction(Instruction &I) {
83 std::cerr << "Cannot instruction select: " << I;
84 abort();
85 }
86
Chris Lattnerc5291f52002-10-27 21:16:59 +000087
88 /// copyConstantToRegister - Output the instructions required to put the
89 /// specified constant into the specified register.
90 ///
91 void copyConstantToRegister(Constant *C, unsigned Reg);
92
Chris Lattner72614082002-10-25 22:55:53 +000093 /// getReg - This method turns an LLVM value into a register number. This
94 /// is guaranteed to produce the same register number for a particular value
95 /// every time it is queried.
96 ///
97 unsigned getReg(Value &V) { return getReg(&V); } // Allow references
98 unsigned getReg(Value *V) {
99 unsigned &Reg = RegMap[V];
100 if (Reg == 0)
101 Reg = CurReg++;
102
Chris Lattner6f8fd252002-10-27 21:23:43 +0000103 // If this operand is a constant, emit the code to copy the constant into
104 // the register here...
105 //
Chris Lattnerc5291f52002-10-27 21:16:59 +0000106 if (Constant *C = dyn_cast<Constant>(V))
107 copyConstantToRegister(C, Reg);
108
Chris Lattner72614082002-10-25 22:55:53 +0000109 return Reg;
110 }
Chris Lattner72614082002-10-25 22:55:53 +0000111 };
112}
113
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000114/// getClass - Turn a primitive type into a "class" number which is based on the
115/// size of the type, and whether or not it is floating point.
116///
117static inline unsigned getClass(const Type *Ty) {
118 switch (Ty->getPrimitiveID()) {
119 case Type::SByteTyID:
120 case Type::UByteTyID: return 0; // Byte operands are class #0
121 case Type::ShortTyID:
122 case Type::UShortTyID: return 1; // Short operands are class #1
123 case Type::IntTyID:
124 case Type::UIntTyID:
125 case Type::PointerTyID: return 2; // Int's and pointers are class #2
126
127 case Type::LongTyID:
128 case Type::ULongTyID: return 3; // Longs are class #3
129 case Type::FloatTyID: return 4; // Float is class #4
130 case Type::DoubleTyID: return 5; // Doubles are class #5
131 default:
132 assert(0 && "Invalid type to getClass!");
133 return 0; // not reached
134 }
135}
Chris Lattnerc5291f52002-10-27 21:16:59 +0000136
137/// copyConstantToRegister - Output the instructions required to put the
138/// specified constant into the specified register.
139///
140void ISel::copyConstantToRegister(Constant *C, unsigned R) {
141 assert (!isa<ConstantExpr>(C) && "Constant expressions not yet handled!\n");
142
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000143 if (C->getType()->isIntegral()) {
144 unsigned Class = getClass(C->getType());
145 assert(Class != 3 && "Type not handled yet!");
146
147 static const unsigned IntegralOpcodeTab[] = {
148 X86::MOVir8, X86::MOVir16, X86::MOVir32
149 };
150
151 if (C->getType()->isSigned()) {
152 ConstantSInt *CSI = cast<ConstantSInt>(C);
153 BuildMI(BB, IntegralOpcodeTab[Class], 1, R).addSImm(CSI->getValue());
154 } else {
155 ConstantUInt *CUI = cast<ConstantUInt>(C);
156 BuildMI(BB, IntegralOpcodeTab[Class], 1, R).addZImm(CUI->getValue());
157 }
158 } else {
159 assert(0 && "Type not handled yet!");
Chris Lattnerc5291f52002-10-27 21:16:59 +0000160 }
161}
162
Chris Lattner51b49a92002-11-02 19:45:49 +0000163
Chris Lattnerc5291f52002-10-27 21:16:59 +0000164
Chris Lattner72614082002-10-25 22:55:53 +0000165/// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such,
166/// we have the following possibilities:
167///
168/// ret void: No return value, simply emit a 'ret' instruction
169/// ret sbyte, ubyte : Extend value into EAX and return
170/// ret short, ushort: Extend value into EAX and return
171/// ret int, uint : Move value into EAX and return
172/// ret pointer : Move value into EAX and return
173/// ret long, ulong : Move value into EAX/EDX (?) and return
174/// ret float/double : ? Top of FP stack? XMM0?
175///
176void ISel::visitReturnInst(ReturnInst &I) {
177 if (I.getNumOperands() != 0) { // Not 'ret void'?
178 // Move result into a hard register... then emit a ret
179 visitInstruction(I); // abort
180 }
181
182 // Emit a simple 'ret' instruction... appending it to the end of the basic
183 // block
Chris Lattner341a9372002-10-29 17:43:55 +0000184 BuildMI(BB, X86::RET, 0);
Chris Lattner72614082002-10-25 22:55:53 +0000185}
186
Chris Lattner51b49a92002-11-02 19:45:49 +0000187/// visitBranchInst - Handle conditional and unconditional branches here. Note
188/// that since code layout is frozen at this point, that if we are trying to
189/// jump to a block that is the immediate successor of the current block, we can
190/// just make a fall-through. (but we don't currently).
191///
Chris Lattner2df035b2002-11-02 19:27:56 +0000192void ISel::visitBranchInst(BranchInst &BI) {
193 if (BI.isConditional()) // Only handles unconditional branches so far...
194 visitInstruction(BI);
195
196 BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(0));
197}
198
199
Chris Lattner68aad932002-11-02 20:13:22 +0000200/// visitSimpleBinary - Implement simple binary operators for integral types...
201/// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or,
202/// 4 for Xor.
203///
204void ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) {
205 if (B.getType() == Type::BoolTy) // FIXME: Handle bools for logicals
Chris Lattnere2954c82002-11-02 20:04:26 +0000206 visitInstruction(B);
207
208 unsigned Class = getClass(B.getType());
209 if (Class > 2) // FIXME: Handle longs
210 visitInstruction(B);
211
212 static const unsigned OpcodeTab[][4] = {
Chris Lattner68aad932002-11-02 20:13:22 +0000213 // Arithmetic operators
214 { X86::ADDrr8, X86::ADDrr16, X86::ADDrr32, 0 }, // ADD
215 { X86::SUBrr8, X86::SUBrr16, X86::SUBrr32, 0 }, // SUB
216
217 // Bitwise operators
Chris Lattnere2954c82002-11-02 20:04:26 +0000218 { X86::ANDrr8, X86::ANDrr16, X86::ANDrr32, 0 }, // AND
219 { X86:: ORrr8, X86:: ORrr16, X86:: ORrr32, 0 }, // OR
220 { X86::XORrr8, X86::XORrr16, X86::XORrr32, 0 }, // XOR
221 };
222
223 unsigned Opcode = OpcodeTab[OperatorClass][Class];
224 unsigned Op0r = getReg(B.getOperand(0));
225 unsigned Op1r = getReg(B.getOperand(1));
226 BuildMI(BB, Opcode, 2, getReg(B)).addReg(Op0r).addReg(Op1r);
227}
228
Chris Lattnerca9671d2002-11-02 20:28:58 +0000229/// visitMul - Multiplies are not simple binary operators because they must deal
230/// with the EAX register explicitly.
231///
232void ISel::visitMul(BinaryOperator &I) {
233 unsigned Class = getClass(I.getType());
234 if (Class > 2) // FIXME: Handle longs
235 visitInstruction(I);
Chris Lattnere2954c82002-11-02 20:04:26 +0000236
Chris Lattnerca9671d2002-11-02 20:28:58 +0000237 static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX };
238 static const unsigned MulOpcode[]={ X86::MULrr8, X86::MULrr16, X86::MULrr32 };
239 static const unsigned MovOpcode[]={ X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 };
240
241 unsigned Reg = Regs[Class];
242 unsigned Op0Reg = getReg(I.getOperand(1));
243 unsigned Op1Reg = getReg(I.getOperand(1));
244
245 // Put the first operand into one of the A registers...
246 BuildMI(BB, MovOpcode[Class], 1, Reg).addReg(Op0Reg);
247
248 // Emit the appropriate multiple instruction...
249 // FIXME: We need to mark that this modified AH, DX, or EDX also!!
250 BuildMI(BB, MulOpcode[Class], 2, Reg).addReg(Reg).addReg(Op1Reg);
251
252 // Put the result into the destination register...
253 BuildMI(BB, MovOpcode[Class], 1, getReg(I)).addReg(Reg);
Chris Lattnerf01729e2002-11-02 20:54:46 +0000254}
Chris Lattnerca9671d2002-11-02 20:28:58 +0000255
Chris Lattnerf01729e2002-11-02 20:54:46 +0000256/// visitDivRem - Handle division and remainder instructions... these
257/// instruction both require the same instructions to be generated, they just
258/// select the result from a different register. Note that both of these
259/// instructions work differently for signed and unsigned operands.
260///
261void ISel::visitDivRem(BinaryOperator &I) {
262 unsigned Class = getClass(I.getType());
263 if (Class > 2) // FIXME: Handle longs
264 visitInstruction(I);
265
266 static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX };
267 static const unsigned MovOpcode[]={ X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 };
268 static const unsigned ExtOpcode[]={ X86::CBW , X86::CWD , X86::CWQ };
269 static const unsigned ClrOpcode[]={ X86::XORrr8, X86::XORrr16, X86::XORrr32 };
270 static const unsigned ExtRegs[] ={ X86::AH , X86::DX , X86::EDX };
271
272 static const unsigned DivOpcode[][4] = {
273 { X86::DIVrr8 , X86::DIVrr16 , X86::DIVrr32 , 0 }, // Unsigned division
274 { X86::IDIVrr8, X86::IDIVrr16, X86::IDIVrr32, 0 }, // Signed division
275 };
276
277 bool isSigned = I.getType()->isSigned();
278 unsigned Reg = Regs[Class];
279 unsigned ExtReg = ExtRegs[Class];
280 unsigned Op0Reg = getReg(I.getOperand(1));
281 unsigned Op1Reg = getReg(I.getOperand(1));
282
283 // Put the first operand into one of the A registers...
284 BuildMI(BB, MovOpcode[Class], 1, Reg).addReg(Op0Reg);
285
286 if (isSigned) {
287 // Emit a sign extension instruction...
288 BuildMI(BB, ExtOpcode[Class], 1, ExtReg).addReg(Reg);
289 } else {
290 // If unsigned, emit a zeroing instruction... (reg = xor reg, reg)
291 BuildMI(BB, ClrOpcode[Class], 2, ExtReg).addReg(ExtReg).addReg(ExtReg);
292 }
293
294 // Figure out which register we want to pick the result out of...
295 unsigned DestReg = (I.getOpcode() == Instruction::Div) ? Reg : ExtReg;
296
297 // Emit the appropriate multiple instruction...
298 // FIXME: We need to mark that this modified AH, DX, or EDX also!!
299 BuildMI(BB,DivOpcode[isSigned][Class], 2, DestReg).addReg(Reg).addReg(Op1Reg);
300
301 // Put the result into the destination register...
302 BuildMI(BB, MovOpcode[Class], 1, getReg(I)).addReg(DestReg);
Chris Lattnerca9671d2002-11-02 20:28:58 +0000303}
Chris Lattnere2954c82002-11-02 20:04:26 +0000304
Brian Gaekea1719c92002-10-31 23:03:59 +0000305/// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here
306/// for constant immediate shift values, and for constant immediate
307/// shift values equal to 1. Even the general case is sort of special,
308/// because the shift amount has to be in CL, not just any old register.
309///
Chris Lattnerf01729e2002-11-02 20:54:46 +0000310void ISel::visitShiftInst (ShiftInst &I) {
311 unsigned Op0r = getReg (I.getOperand(0));
312 unsigned DestReg = getReg(I);
Chris Lattnere9913f22002-11-02 01:41:55 +0000313 bool isLeftShift = I.getOpcode() == Instruction::Shl;
314 bool isOperandSigned = I.getType()->isUnsigned();
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000315 unsigned OperandClass = getClass(I.getType());
316
317 if (OperandClass > 2)
318 visitInstruction(I); // Can't handle longs yet!
Chris Lattner796df732002-11-02 00:44:25 +0000319
Brian Gaekea1719c92002-10-31 23:03:59 +0000320 if (ConstantUInt *CUI = dyn_cast <ConstantUInt> (I.getOperand (1)))
321 {
Chris Lattner796df732002-11-02 00:44:25 +0000322 // The shift amount is constant, guaranteed to be a ubyte. Get its value.
323 assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?");
324 unsigned char shAmt = CUI->getValue();
325
Chris Lattnere9913f22002-11-02 01:41:55 +0000326 static const unsigned ConstantOperand[][4] = {
327 { X86::SHRir8, X86::SHRir16, X86::SHRir32, 0 }, // SHR
328 { X86::SARir8, X86::SARir16, X86::SARir32, 0 }, // SAR
329 { X86::SHLir8, X86::SHLir16, X86::SHLir32, 0 }, // SHL
330 { X86::SHLir8, X86::SHLir16, X86::SHLir32, 0 }, // SAL = SHL
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000331 };
332
Chris Lattnere9913f22002-11-02 01:41:55 +0000333 const unsigned *OpTab = // Figure out the operand table to use
334 ConstantOperand[isLeftShift*2+isOperandSigned];
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000335
Brian Gaekea1719c92002-10-31 23:03:59 +0000336 // Emit: <insn> reg, shamt (shift-by-immediate opcode "ir" form.)
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000337 BuildMI(BB, OpTab[OperandClass], 2, DestReg).addReg(Op0r).addZImm(shAmt);
Brian Gaekea1719c92002-10-31 23:03:59 +0000338 }
339 else
340 {
341 // The shift amount is non-constant.
342 //
343 // In fact, you can only shift with a variable shift amount if
344 // that amount is already in the CL register, so we have to put it
345 // there first.
346 //
Chris Lattnere9913f22002-11-02 01:41:55 +0000347
Brian Gaekea1719c92002-10-31 23:03:59 +0000348 // Emit: move cl, shiftAmount (put the shift amount in CL.)
Chris Lattnerca9671d2002-11-02 20:28:58 +0000349 BuildMI(BB, X86::MOVrr8, 1, X86::CL).addReg(getReg(I.getOperand(1)));
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000350
351 // This is a shift right (SHR).
Chris Lattnere9913f22002-11-02 01:41:55 +0000352 static const unsigned NonConstantOperand[][4] = {
353 { X86::SHRrr8, X86::SHRrr16, X86::SHRrr32, 0 }, // SHR
354 { X86::SARrr8, X86::SARrr16, X86::SARrr32, 0 }, // SAR
355 { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32, 0 }, // SHL
356 { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32, 0 }, // SAL = SHL
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000357 };
358
Chris Lattnere9913f22002-11-02 01:41:55 +0000359 const unsigned *OpTab = // Figure out the operand table to use
360 NonConstantOperand[isLeftShift*2+isOperandSigned];
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000361
Chris Lattnere9913f22002-11-02 01:41:55 +0000362 BuildMI(BB, OpTab[OperandClass], 2, DestReg).addReg(Op0r).addReg(X86::CL);
Brian Gaekea1719c92002-10-31 23:03:59 +0000363 }
364}
365
Chris Lattnere2954c82002-11-02 20:04:26 +0000366/// visitPHINode - Turn an LLVM PHI node into an X86 PHI node...
367///
368void ISel::visitPHINode(PHINode &PN) {
369 MachineInstr *MI = BuildMI(BB, X86::PHI, PN.getNumOperands(), getReg(PN));
Chris Lattner72614082002-10-25 22:55:53 +0000370
Chris Lattnere2954c82002-11-02 20:04:26 +0000371 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
372 // FIXME: This will put constants after the PHI nodes in the block, which
373 // is invalid. They should be put inline into the PHI node eventually.
374 //
375 MI->addRegOperand(getReg(PN.getIncomingValue(i)));
376 MI->addPCDispOperand(PN.getIncomingBlock(i));
377 }
Chris Lattner72614082002-10-25 22:55:53 +0000378}
379
Brian Gaekea1719c92002-10-31 23:03:59 +0000380
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000381/// createSimpleX86InstructionSelector - This pass converts an LLVM function
382/// into a machine code representation is a very simple peep-hole fashion. The
Chris Lattner72614082002-10-25 22:55:53 +0000383/// generated code sucks but the implementation is nice and simple.
384///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000385Pass *createSimpleX86InstructionSelector(TargetMachine &TM) {
386 return new ISel(TM);
Chris Lattner72614082002-10-25 22:55:53 +0000387}