blob: 7f163b8db2cda6006e2530a16a0edc538fb8049e [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 Lattner6fc3c522002-11-17 21:11:55 +00009#include "X86InstrBuilder.h"
Chris Lattner72614082002-10-25 22:55:53 +000010#include "llvm/Function.h"
11#include "llvm/iTerminators.h"
Brian Gaeke1749d632002-11-07 17:59:21 +000012#include "llvm/iOperators.h"
Brian Gaekea1719c92002-10-31 23:03:59 +000013#include "llvm/iOther.h"
Chris Lattner51b49a92002-11-02 19:45:49 +000014#include "llvm/iPHINode.h"
Chris Lattner6fc3c522002-11-17 21:11:55 +000015#include "llvm/iMemory.h"
Chris Lattner72614082002-10-25 22:55:53 +000016#include "llvm/Type.h"
Chris Lattnerc5291f52002-10-27 21:16:59 +000017#include "llvm/Constants.h"
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000018#include "llvm/Pass.h"
Chris Lattner341a9372002-10-29 17:43:55 +000019#include "llvm/CodeGen/MachineFunction.h"
Misha Brukmand2cc0172002-11-20 00:58:23 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
21#include "llvm/Target/TargetMachine.h"
Chris Lattner72614082002-10-25 22:55:53 +000022#include "llvm/Support/InstVisitor.h"
Misha Brukmand2cc0172002-11-20 00:58:23 +000023#include "llvm/Target/MRegisterInfo.h"
24#include <map>
Chris Lattner72614082002-10-25 22:55:53 +000025
Chris Lattner06925362002-11-17 21:56:38 +000026using namespace MOTy; // Get Use, Def, UseAndDef
27
Chris Lattner72614082002-10-25 22:55:53 +000028namespace {
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000029 struct ISel : public FunctionPass, InstVisitor<ISel> {
30 TargetMachine &TM;
Chris Lattner341a9372002-10-29 17:43:55 +000031 MachineFunction *F; // The function we are compiling into
32 MachineBasicBlock *BB; // The current MBB we are compiling
Chris Lattner72614082002-10-25 22:55:53 +000033
34 unsigned CurReg;
35 std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs
36
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000037 ISel(TargetMachine &tm)
38 : TM(tm), F(0), BB(0), CurReg(MRegisterInfo::FirstVirtualRegister) {}
Chris Lattner72614082002-10-25 22:55:53 +000039
40 /// runOnFunction - Top level implementation of instruction selection for
41 /// the entire function.
42 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000043 bool runOnFunction(Function &Fn) {
Chris Lattner36b36032002-10-29 23:40:58 +000044 F = &MachineFunction::construct(&Fn, TM);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000045 visit(Fn);
Chris Lattner72614082002-10-25 22:55:53 +000046 RegMap.clear();
Chris Lattner94e8ee22002-11-21 17:26:58 +000047 CurReg = MRegisterInfo::FirstVirtualRegister;
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000048 F = 0;
Chris Lattner72614082002-10-25 22:55:53 +000049 return false; // We never modify the LLVM itself.
50 }
51
52 /// visitBasicBlock - This method is called when we are visiting a new basic
Chris Lattner33f53b52002-10-29 20:48:56 +000053 /// block. This simply creates a new MachineBasicBlock to emit code into
54 /// and adds it to the current MachineFunction. Subsequent visit* for
55 /// instructions will be invoked for all instructions in the basic block.
Chris Lattner72614082002-10-25 22:55:53 +000056 ///
57 void visitBasicBlock(BasicBlock &LLVM_BB) {
Chris Lattner42c77862002-10-30 00:47:40 +000058 BB = new MachineBasicBlock(&LLVM_BB);
Chris Lattner72614082002-10-25 22:55:53 +000059 // FIXME: Use the auto-insert form when it's available
60 F->getBasicBlockList().push_back(BB);
61 }
62
63 // Visitation methods for various instructions. These methods simply emit
64 // fixed X86 code for each instruction.
65 //
66 void visitReturnInst(ReturnInst &RI);
Chris Lattner2df035b2002-11-02 19:27:56 +000067 void visitBranchInst(BranchInst &BI);
Chris Lattnere2954c82002-11-02 20:04:26 +000068
69 // Arithmetic operators
Chris Lattnerf01729e2002-11-02 20:54:46 +000070 void visitSimpleBinary(BinaryOperator &B, unsigned OpcodeClass);
Chris Lattner68aad932002-11-02 20:13:22 +000071 void visitAdd(BinaryOperator &B) { visitSimpleBinary(B, 0); }
72 void visitSub(BinaryOperator &B) { visitSimpleBinary(B, 1); }
Chris Lattnerca9671d2002-11-02 20:28:58 +000073 void visitMul(BinaryOperator &B);
Chris Lattnere2954c82002-11-02 20:04:26 +000074
Chris Lattnerf01729e2002-11-02 20:54:46 +000075 void visitDiv(BinaryOperator &B) { visitDivRem(B); }
76 void visitRem(BinaryOperator &B) { visitDivRem(B); }
77 void visitDivRem(BinaryOperator &B);
78
Chris Lattnere2954c82002-11-02 20:04:26 +000079 // Bitwise operators
Chris Lattner68aad932002-11-02 20:13:22 +000080 void visitAnd(BinaryOperator &B) { visitSimpleBinary(B, 2); }
81 void visitOr (BinaryOperator &B) { visitSimpleBinary(B, 3); }
82 void visitXor(BinaryOperator &B) { visitSimpleBinary(B, 4); }
Chris Lattnere2954c82002-11-02 20:04:26 +000083
84 // Binary comparison operators
Chris Lattner05093a52002-11-21 15:52:38 +000085 void visitSetCCInst(SetCondInst &I, unsigned OpNum);
86 void visitSetEQ(SetCondInst &I) { visitSetCCInst(I, 0); }
87 void visitSetNE(SetCondInst &I) { visitSetCCInst(I, 1); }
88 void visitSetLT(SetCondInst &I) { visitSetCCInst(I, 2); }
89 void visitSetGT(SetCondInst &I) { visitSetCCInst(I, 3); }
90 void visitSetLE(SetCondInst &I) { visitSetCCInst(I, 4); }
91 void visitSetGE(SetCondInst &I) { visitSetCCInst(I, 5); }
Chris Lattner6fc3c522002-11-17 21:11:55 +000092
93 // Memory Instructions
94 void visitLoadInst(LoadInst &I);
95 void visitStoreInst(StoreInst &I);
Chris Lattnere2954c82002-11-02 20:04:26 +000096
97 // Other operators
Brian Gaekea1719c92002-10-31 23:03:59 +000098 void visitShiftInst(ShiftInst &I);
Chris Lattnere2954c82002-11-02 20:04:26 +000099 void visitPHINode(PHINode &I);
Chris Lattner72614082002-10-25 22:55:53 +0000100
101 void visitInstruction(Instruction &I) {
102 std::cerr << "Cannot instruction select: " << I;
103 abort();
104 }
105
Chris Lattnerc5291f52002-10-27 21:16:59 +0000106
107 /// copyConstantToRegister - Output the instructions required to put the
108 /// specified constant into the specified register.
109 ///
110 void copyConstantToRegister(Constant *C, unsigned Reg);
111
Chris Lattner72614082002-10-25 22:55:53 +0000112 /// getReg - This method turns an LLVM value into a register number. This
113 /// is guaranteed to produce the same register number for a particular value
114 /// every time it is queried.
115 ///
116 unsigned getReg(Value &V) { return getReg(&V); } // Allow references
117 unsigned getReg(Value *V) {
118 unsigned &Reg = RegMap[V];
Misha Brukmand2cc0172002-11-20 00:58:23 +0000119 if (Reg == 0) {
Chris Lattner72614082002-10-25 22:55:53 +0000120 Reg = CurReg++;
Misha Brukmand2cc0172002-11-20 00:58:23 +0000121 RegMap[V] = Reg;
122
123 // Add the mapping of regnumber => reg class to MachineFunction
124 F->addRegMap(Reg,
125 TM.getRegisterInfo()->getRegClassForType(V->getType()));
126 }
Chris Lattner72614082002-10-25 22:55:53 +0000127
Chris Lattner6f8fd252002-10-27 21:23:43 +0000128 // If this operand is a constant, emit the code to copy the constant into
129 // the register here...
130 //
Chris Lattnerc5291f52002-10-27 21:16:59 +0000131 if (Constant *C = dyn_cast<Constant>(V))
132 copyConstantToRegister(C, Reg);
133
Chris Lattner72614082002-10-25 22:55:53 +0000134 return Reg;
135 }
Chris Lattner72614082002-10-25 22:55:53 +0000136 };
137}
138
Chris Lattner43189d12002-11-17 20:07:45 +0000139/// TypeClass - Used by the X86 backend to group LLVM types by their basic X86
140/// Representation.
141///
142enum TypeClass {
143 cByte, cShort, cInt, cLong, cFloat, cDouble
144};
145
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000146/// getClass - Turn a primitive type into a "class" number which is based on the
147/// size of the type, and whether or not it is floating point.
148///
Chris Lattner43189d12002-11-17 20:07:45 +0000149static inline TypeClass getClass(const Type *Ty) {
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000150 switch (Ty->getPrimitiveID()) {
151 case Type::SByteTyID:
Chris Lattner43189d12002-11-17 20:07:45 +0000152 case Type::UByteTyID: return cByte; // Byte operands are class #0
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000153 case Type::ShortTyID:
Chris Lattner43189d12002-11-17 20:07:45 +0000154 case Type::UShortTyID: return cShort; // Short operands are class #1
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000155 case Type::IntTyID:
156 case Type::UIntTyID:
Chris Lattner43189d12002-11-17 20:07:45 +0000157 case Type::PointerTyID: return cInt; // Int's and pointers are class #2
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000158
159 case Type::LongTyID:
Chris Lattner43189d12002-11-17 20:07:45 +0000160 case Type::ULongTyID: return cLong; // Longs are class #3
161 case Type::FloatTyID: return cFloat; // Float is class #4
162 case Type::DoubleTyID: return cDouble; // Doubles are class #5
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000163 default:
164 assert(0 && "Invalid type to getClass!");
Chris Lattner43189d12002-11-17 20:07:45 +0000165 return cByte; // not reached
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000166 }
167}
Chris Lattnerc5291f52002-10-27 21:16:59 +0000168
Chris Lattner06925362002-11-17 21:56:38 +0000169
Chris Lattnerc5291f52002-10-27 21:16:59 +0000170/// copyConstantToRegister - Output the instructions required to put the
171/// specified constant into the specified register.
172///
173void ISel::copyConstantToRegister(Constant *C, unsigned R) {
174 assert (!isa<ConstantExpr>(C) && "Constant expressions not yet handled!\n");
175
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000176 if (C->getType()->isIntegral()) {
177 unsigned Class = getClass(C->getType());
178 assert(Class != 3 && "Type not handled yet!");
179
180 static const unsigned IntegralOpcodeTab[] = {
181 X86::MOVir8, X86::MOVir16, X86::MOVir32
182 };
183
184 if (C->getType()->isSigned()) {
185 ConstantSInt *CSI = cast<ConstantSInt>(C);
186 BuildMI(BB, IntegralOpcodeTab[Class], 1, R).addSImm(CSI->getValue());
187 } else {
188 ConstantUInt *CUI = cast<ConstantUInt>(C);
189 BuildMI(BB, IntegralOpcodeTab[Class], 1, R).addZImm(CUI->getValue());
190 }
191 } else {
192 assert(0 && "Type not handled yet!");
Chris Lattnerc5291f52002-10-27 21:16:59 +0000193 }
194}
195
Chris Lattner06925362002-11-17 21:56:38 +0000196
Brian Gaeke1749d632002-11-07 17:59:21 +0000197/// SetCC instructions - Here we just emit boilerplate code to set a byte-sized
198/// register, then move it to wherever the result should be.
199/// We handle FP setcc instructions by pushing them, doing a
200/// compare-and-pop-twice, and then copying the concodes to the main
201/// processor's concodes (I didn't make this up, it's in the Intel manual)
202///
Chris Lattner05093a52002-11-21 15:52:38 +0000203void ISel::visitSetCCInst(SetCondInst &I, unsigned OpNum) {
Brian Gaeke1749d632002-11-07 17:59:21 +0000204 // The arguments are already supposed to be of the same type.
Chris Lattner05093a52002-11-21 15:52:38 +0000205 const Type *CompTy = I.getOperand(0)->getType();
206 unsigned reg1 = getReg(I.getOperand(0));
207 unsigned reg2 = getReg(I.getOperand(1));
208
209 unsigned Class = getClass(CompTy);
210 switch (Class) {
211 // Emit: cmp <var1>, <var2> (do the comparison). We can
212 // compare 8-bit with 8-bit, 16-bit with 16-bit, 32-bit with
213 // 32-bit.
214 case cByte:
215 BuildMI (BB, X86::CMPrr8, 2).addReg (reg1).addReg (reg2);
216 break;
217 case cShort:
218 BuildMI (BB, X86::CMPrr16, 2).addReg (reg1).addReg (reg2);
219 break;
220 case cInt:
221 BuildMI (BB, X86::CMPrr32, 2).addReg (reg1).addReg (reg2);
222 break;
223
224 // Push the variables on the stack with fldl opcodes.
225 // FIXME: assuming var1, var2 are in memory, if not, spill to
226 // stack first
227 case cFloat: // Floats
228 BuildMI (BB, X86::FLDr4, 1, X86::NoReg).addReg (reg1);
229 BuildMI (BB, X86::FLDr4, 1, X86::NoReg).addReg (reg2);
230 break;
231 case cDouble: // Doubles
232 BuildMI (BB, X86::FLDr8, 1, X86::NoReg).addReg (reg1);
233 BuildMI (BB, X86::FLDr8, 1, X86::NoReg).addReg (reg2);
234 break;
235 case cLong:
236 default:
237 visitInstruction(I);
238 }
239
240 if (CompTy->isFloatingPoint()) {
241 // (Non-trapping) compare and pop twice.
242 BuildMI (BB, X86::FUCOMPP, 0);
243 // Move fp status word (concodes) to ax.
244 BuildMI (BB, X86::FNSTSWr8, 1, X86::AX);
245 // Load real concodes from ax.
246 BuildMI (BB, X86::SAHF, 1).addReg(X86::AH);
247 }
248
Brian Gaeke1749d632002-11-07 17:59:21 +0000249 // Emit setOp instruction (extract concode; clobbers ax),
250 // using the following mapping:
251 // LLVM -> X86 signed X86 unsigned
252 // ----- ----- -----
253 // seteq -> sete sete
254 // setne -> setne setne
255 // setlt -> setl setb
256 // setgt -> setg seta
257 // setle -> setle setbe
258 // setge -> setge setae
Chris Lattner05093a52002-11-21 15:52:38 +0000259
260 static const unsigned OpcodeTab[2][6] = {
Chris Lattner4b4e9dd2002-11-21 16:19:42 +0000261 {X86::SETEr, X86::SETNEr, X86::SETBr, X86::SETAr, X86::SETBEr, X86::SETAEr},
262 {X86::SETEr, X86::SETNEr, X86::SETLr, X86::SETGr, X86::SETLEr, X86::SETGEr},
Chris Lattner05093a52002-11-21 15:52:38 +0000263 };
264
265 BuildMI(BB, OpcodeTab[CompTy->isSigned()][OpNum], 0, X86::AL);
266
Brian Gaeke1749d632002-11-07 17:59:21 +0000267 // Put it in the result using a move.
Chris Lattner05093a52002-11-21 15:52:38 +0000268 BuildMI (BB, X86::MOVrr8, 1, getReg(I)).addReg(X86::AL);
Brian Gaeke1749d632002-11-07 17:59:21 +0000269}
Chris Lattner51b49a92002-11-02 19:45:49 +0000270
Chris Lattnerc5291f52002-10-27 21:16:59 +0000271
Chris Lattner72614082002-10-25 22:55:53 +0000272/// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such,
273/// we have the following possibilities:
274///
275/// ret void: No return value, simply emit a 'ret' instruction
276/// ret sbyte, ubyte : Extend value into EAX and return
277/// ret short, ushort: Extend value into EAX and return
278/// ret int, uint : Move value into EAX and return
279/// ret pointer : Move value into EAX and return
Chris Lattner06925362002-11-17 21:56:38 +0000280/// ret long, ulong : Move value into EAX/EDX and return
281/// ret float/double : Top of FP stack
Chris Lattner72614082002-10-25 22:55:53 +0000282///
Chris Lattner6fc3c522002-11-17 21:11:55 +0000283void ISel::visitReturnInst (ReturnInst &I) {
Chris Lattner43189d12002-11-17 20:07:45 +0000284 if (I.getNumOperands() == 0) {
285 // Emit a 'ret' instruction
286 BuildMI(BB, X86::RET, 0);
287 return;
288 }
289
290 unsigned val = getReg(I.getOperand(0));
Chris Lattner6fc3c522002-11-17 21:11:55 +0000291 unsigned Class = getClass(I.getOperand(0)->getType());
Chris Lattner43189d12002-11-17 20:07:45 +0000292 bool isUnsigned = I.getOperand(0)->getType()->isUnsigned();
293 switch (Class) {
294 case cByte:
295 // ret sbyte, ubyte: Extend value into EAX and return
Chris Lattner6fc3c522002-11-17 21:11:55 +0000296 if (isUnsigned)
Chris Lattner43189d12002-11-17 20:07:45 +0000297 BuildMI (BB, X86::MOVZXr32r8, 1, X86::EAX).addReg (val);
Chris Lattner6fc3c522002-11-17 21:11:55 +0000298 else
Chris Lattner43189d12002-11-17 20:07:45 +0000299 BuildMI (BB, X86::MOVSXr32r8, 1, X86::EAX).addReg (val);
Chris Lattner43189d12002-11-17 20:07:45 +0000300 break;
301 case cShort:
302 // ret short, ushort: Extend value into EAX and return
Chris Lattner6fc3c522002-11-17 21:11:55 +0000303 if (isUnsigned)
Chris Lattner43189d12002-11-17 20:07:45 +0000304 BuildMI (BB, X86::MOVZXr32r16, 1, X86::EAX).addReg (val);
Chris Lattner6fc3c522002-11-17 21:11:55 +0000305 else
Chris Lattner43189d12002-11-17 20:07:45 +0000306 BuildMI (BB, X86::MOVSXr32r16, 1, X86::EAX).addReg (val);
Chris Lattner43189d12002-11-17 20:07:45 +0000307 break;
308 case cInt:
309 // ret int, uint, ptr: Move value into EAX and return
310 // MOV EAX, <val>
311 BuildMI(BB, X86::MOVrr32, 1, X86::EAX).addReg(val);
312 break;
313
314 // ret float/double: top of FP stack
315 // FLD <val>
316 case cFloat: // Floats
317 BuildMI(BB, X86::FLDr4, 1).addReg(val);
318 break;
319 case cDouble: // Doubles
320 BuildMI(BB, X86::FLDr8, 1).addReg(val);
321 break;
322 case cLong:
323 // ret long: use EAX(least significant 32 bits)/EDX (most
324 // significant 32)...uh, I think so Brain, but how do i call
325 // up the two parts of the value from inside this mouse
326 // cage? *zort*
327 default:
328 visitInstruction(I);
329 }
330
331 // Emit a 'ret' instruction
332 BuildMI(BB, X86::RET, 0);
Chris Lattner72614082002-10-25 22:55:53 +0000333}
334
Chris Lattner51b49a92002-11-02 19:45:49 +0000335/// visitBranchInst - Handle conditional and unconditional branches here. Note
336/// that since code layout is frozen at this point, that if we are trying to
337/// jump to a block that is the immediate successor of the current block, we can
338/// just make a fall-through. (but we don't currently).
339///
Brian Gaekec03a0cb2002-11-19 09:08:47 +0000340void
341ISel::visitBranchInst (BranchInst & BI)
342{
343 if (BI.isConditional ())
344 {
345 BasicBlock *ifTrue = BI.getSuccessor (0);
346 BasicBlock *ifFalse = BI.getSuccessor (1); // this is really unobvious
Chris Lattner2df035b2002-11-02 19:27:56 +0000347
Brian Gaekec03a0cb2002-11-19 09:08:47 +0000348 // simplest thing I can think of: compare condition with zero,
349 // followed by jump-if-equal to ifFalse, and jump-if-nonequal to
350 // ifTrue
351 unsigned int condReg = getReg (BI.getCondition ());
Chris Lattner97ad9e12002-11-21 01:59:50 +0000352 BuildMI (BB, X86::CMPri8, 2).addReg (condReg).addZImm (0);
Brian Gaekec03a0cb2002-11-19 09:08:47 +0000353 BuildMI (BB, X86::JNE, 1).addPCDisp (BI.getSuccessor (0));
354 BuildMI (BB, X86::JE, 1).addPCDisp (BI.getSuccessor (1));
355 }
356 else // unconditional branch
357 {
358 BuildMI (BB, X86::JMP, 1).addPCDisp (BI.getSuccessor (0));
359 }
Chris Lattner2df035b2002-11-02 19:27:56 +0000360}
361
362
Chris Lattner68aad932002-11-02 20:13:22 +0000363/// visitSimpleBinary - Implement simple binary operators for integral types...
364/// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or,
365/// 4 for Xor.
366///
367void ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) {
368 if (B.getType() == Type::BoolTy) // FIXME: Handle bools for logicals
Chris Lattnere2954c82002-11-02 20:04:26 +0000369 visitInstruction(B);
370
371 unsigned Class = getClass(B.getType());
372 if (Class > 2) // FIXME: Handle longs
373 visitInstruction(B);
374
375 static const unsigned OpcodeTab[][4] = {
Chris Lattner68aad932002-11-02 20:13:22 +0000376 // Arithmetic operators
377 { X86::ADDrr8, X86::ADDrr16, X86::ADDrr32, 0 }, // ADD
378 { X86::SUBrr8, X86::SUBrr16, X86::SUBrr32, 0 }, // SUB
379
380 // Bitwise operators
Chris Lattnere2954c82002-11-02 20:04:26 +0000381 { X86::ANDrr8, X86::ANDrr16, X86::ANDrr32, 0 }, // AND
382 { X86:: ORrr8, X86:: ORrr16, X86:: ORrr32, 0 }, // OR
383 { X86::XORrr8, X86::XORrr16, X86::XORrr32, 0 }, // XOR
384 };
385
386 unsigned Opcode = OpcodeTab[OperatorClass][Class];
387 unsigned Op0r = getReg(B.getOperand(0));
388 unsigned Op1r = getReg(B.getOperand(1));
389 BuildMI(BB, Opcode, 2, getReg(B)).addReg(Op0r).addReg(Op1r);
390}
391
Chris Lattnerca9671d2002-11-02 20:28:58 +0000392/// visitMul - Multiplies are not simple binary operators because they must deal
393/// with the EAX register explicitly.
394///
395void ISel::visitMul(BinaryOperator &I) {
396 unsigned Class = getClass(I.getType());
397 if (Class > 2) // FIXME: Handle longs
398 visitInstruction(I);
Chris Lattnere2954c82002-11-02 20:04:26 +0000399
Chris Lattnerca9671d2002-11-02 20:28:58 +0000400 static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX };
Chris Lattner06925362002-11-17 21:56:38 +0000401 static const unsigned Clobbers[] ={ X86::AH , X86::DX , X86::EDX };
Chris Lattnerca9671d2002-11-02 20:28:58 +0000402 static const unsigned MulOpcode[]={ X86::MULrr8, X86::MULrr16, X86::MULrr32 };
403 static const unsigned MovOpcode[]={ X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 };
404
Chris Lattner06925362002-11-17 21:56:38 +0000405 unsigned Reg = Regs[Class];
406 unsigned Clobber = Clobbers[Class];
407 unsigned Op0Reg = getReg(I.getOperand(0));
408 unsigned Op1Reg = getReg(I.getOperand(1));
Chris Lattnerca9671d2002-11-02 20:28:58 +0000409
410 // Put the first operand into one of the A registers...
411 BuildMI(BB, MovOpcode[Class], 1, Reg).addReg(Op0Reg);
412
Chris Lattner06925362002-11-17 21:56:38 +0000413 // Emit the appropriate multiply instruction...
Chris Lattner71e83ca2002-11-17 22:33:26 +0000414 BuildMI(BB, MulOpcode[Class], 3)
Chris Lattner06925362002-11-17 21:56:38 +0000415 .addReg(Reg, UseAndDef).addReg(Op1Reg).addClobber(Clobber);
Chris Lattnerca9671d2002-11-02 20:28:58 +0000416
417 // Put the result into the destination register...
418 BuildMI(BB, MovOpcode[Class], 1, getReg(I)).addReg(Reg);
Chris Lattnerf01729e2002-11-02 20:54:46 +0000419}
Chris Lattnerca9671d2002-11-02 20:28:58 +0000420
Chris Lattner06925362002-11-17 21:56:38 +0000421
Chris Lattnerf01729e2002-11-02 20:54:46 +0000422/// visitDivRem - Handle division and remainder instructions... these
423/// instruction both require the same instructions to be generated, they just
424/// select the result from a different register. Note that both of these
425/// instructions work differently for signed and unsigned operands.
426///
427void ISel::visitDivRem(BinaryOperator &I) {
428 unsigned Class = getClass(I.getType());
429 if (Class > 2) // FIXME: Handle longs
430 visitInstruction(I);
431
432 static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX };
433 static const unsigned MovOpcode[]={ X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 };
Brian Gaeke6559bb92002-11-14 22:32:30 +0000434 static const unsigned ExtOpcode[]={ X86::CBW , X86::CWD , X86::CDQ };
Chris Lattnerf01729e2002-11-02 20:54:46 +0000435 static const unsigned ClrOpcode[]={ X86::XORrr8, X86::XORrr16, X86::XORrr32 };
436 static const unsigned ExtRegs[] ={ X86::AH , X86::DX , X86::EDX };
437
438 static const unsigned DivOpcode[][4] = {
439 { X86::DIVrr8 , X86::DIVrr16 , X86::DIVrr32 , 0 }, // Unsigned division
440 { X86::IDIVrr8, X86::IDIVrr16, X86::IDIVrr32, 0 }, // Signed division
441 };
442
443 bool isSigned = I.getType()->isSigned();
444 unsigned Reg = Regs[Class];
445 unsigned ExtReg = ExtRegs[Class];
Chris Lattner6fc3c522002-11-17 21:11:55 +0000446 unsigned Op0Reg = getReg(I.getOperand(0));
Chris Lattnerf01729e2002-11-02 20:54:46 +0000447 unsigned Op1Reg = getReg(I.getOperand(1));
448
449 // Put the first operand into one of the A registers...
450 BuildMI(BB, MovOpcode[Class], 1, Reg).addReg(Op0Reg);
451
452 if (isSigned) {
453 // Emit a sign extension instruction...
454 BuildMI(BB, ExtOpcode[Class], 1, ExtReg).addReg(Reg);
455 } else {
456 // If unsigned, emit a zeroing instruction... (reg = xor reg, reg)
457 BuildMI(BB, ClrOpcode[Class], 2, ExtReg).addReg(ExtReg).addReg(ExtReg);
458 }
459
Chris Lattner06925362002-11-17 21:56:38 +0000460 // Emit the appropriate divide or remainder instruction...
461 BuildMI(BB, DivOpcode[isSigned][Class], 2)
462 .addReg(Reg, UseAndDef).addReg(ExtReg, UseAndDef).addReg(Op1Reg);
463
Chris Lattnerf01729e2002-11-02 20:54:46 +0000464 // Figure out which register we want to pick the result out of...
465 unsigned DestReg = (I.getOpcode() == Instruction::Div) ? Reg : ExtReg;
466
Chris Lattnerf01729e2002-11-02 20:54:46 +0000467 // Put the result into the destination register...
468 BuildMI(BB, MovOpcode[Class], 1, getReg(I)).addReg(DestReg);
Chris Lattnerca9671d2002-11-02 20:28:58 +0000469}
Chris Lattnere2954c82002-11-02 20:04:26 +0000470
Chris Lattner06925362002-11-17 21:56:38 +0000471
Brian Gaekea1719c92002-10-31 23:03:59 +0000472/// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here
473/// for constant immediate shift values, and for constant immediate
474/// shift values equal to 1. Even the general case is sort of special,
475/// because the shift amount has to be in CL, not just any old register.
476///
Chris Lattnerf01729e2002-11-02 20:54:46 +0000477void ISel::visitShiftInst (ShiftInst &I) {
478 unsigned Op0r = getReg (I.getOperand(0));
479 unsigned DestReg = getReg(I);
Chris Lattnere9913f22002-11-02 01:41:55 +0000480 bool isLeftShift = I.getOpcode() == Instruction::Shl;
481 bool isOperandSigned = I.getType()->isUnsigned();
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000482 unsigned OperandClass = getClass(I.getType());
483
484 if (OperandClass > 2)
485 visitInstruction(I); // Can't handle longs yet!
Chris Lattner796df732002-11-02 00:44:25 +0000486
Brian Gaekea1719c92002-10-31 23:03:59 +0000487 if (ConstantUInt *CUI = dyn_cast <ConstantUInt> (I.getOperand (1)))
488 {
Chris Lattner796df732002-11-02 00:44:25 +0000489 // The shift amount is constant, guaranteed to be a ubyte. Get its value.
490 assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?");
491 unsigned char shAmt = CUI->getValue();
492
Chris Lattnere9913f22002-11-02 01:41:55 +0000493 static const unsigned ConstantOperand[][4] = {
494 { X86::SHRir8, X86::SHRir16, X86::SHRir32, 0 }, // SHR
495 { X86::SARir8, X86::SARir16, X86::SARir32, 0 }, // SAR
496 { X86::SHLir8, X86::SHLir16, X86::SHLir32, 0 }, // SHL
497 { X86::SHLir8, X86::SHLir16, X86::SHLir32, 0 }, // SAL = SHL
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000498 };
499
Chris Lattnere9913f22002-11-02 01:41:55 +0000500 const unsigned *OpTab = // Figure out the operand table to use
501 ConstantOperand[isLeftShift*2+isOperandSigned];
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000502
Brian Gaekea1719c92002-10-31 23:03:59 +0000503 // Emit: <insn> reg, shamt (shift-by-immediate opcode "ir" form.)
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000504 BuildMI(BB, OpTab[OperandClass], 2, DestReg).addReg(Op0r).addZImm(shAmt);
Brian Gaekea1719c92002-10-31 23:03:59 +0000505 }
506 else
507 {
508 // The shift amount is non-constant.
509 //
510 // In fact, you can only shift with a variable shift amount if
511 // that amount is already in the CL register, so we have to put it
512 // there first.
513 //
Chris Lattnere9913f22002-11-02 01:41:55 +0000514
Brian Gaekea1719c92002-10-31 23:03:59 +0000515 // Emit: move cl, shiftAmount (put the shift amount in CL.)
Chris Lattnerca9671d2002-11-02 20:28:58 +0000516 BuildMI(BB, X86::MOVrr8, 1, X86::CL).addReg(getReg(I.getOperand(1)));
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000517
518 // This is a shift right (SHR).
Chris Lattnere9913f22002-11-02 01:41:55 +0000519 static const unsigned NonConstantOperand[][4] = {
520 { X86::SHRrr8, X86::SHRrr16, X86::SHRrr32, 0 }, // SHR
521 { X86::SARrr8, X86::SARrr16, X86::SARrr32, 0 }, // SAR
522 { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32, 0 }, // SHL
523 { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32, 0 }, // SAL = SHL
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000524 };
525
Chris Lattnere9913f22002-11-02 01:41:55 +0000526 const unsigned *OpTab = // Figure out the operand table to use
527 NonConstantOperand[isLeftShift*2+isOperandSigned];
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000528
Chris Lattnere9913f22002-11-02 01:41:55 +0000529 BuildMI(BB, OpTab[OperandClass], 2, DestReg).addReg(Op0r).addReg(X86::CL);
Brian Gaekea1719c92002-10-31 23:03:59 +0000530 }
531}
532
Chris Lattner06925362002-11-17 21:56:38 +0000533
Chris Lattner6fc3c522002-11-17 21:11:55 +0000534/// visitLoadInst - Implement LLVM load instructions in terms of the x86 'mov'
535/// instruction.
536///
537void ISel::visitLoadInst(LoadInst &I) {
538 unsigned Class = getClass(I.getType());
539 if (Class > 2) // FIXME: Handle longs and others...
540 visitInstruction(I);
541
542 static const unsigned Opcode[] = { X86::MOVmr8, X86::MOVmr16, X86::MOVmr32 };
543
544 unsigned AddressReg = getReg(I.getOperand(0));
545 addDirectMem(BuildMI(BB, Opcode[Class], 4, getReg(I)), AddressReg);
546}
547
Chris Lattner06925362002-11-17 21:56:38 +0000548
Chris Lattner6fc3c522002-11-17 21:11:55 +0000549/// visitStoreInst - Implement LLVM store instructions in terms of the x86 'mov'
550/// instruction.
551///
552void ISel::visitStoreInst(StoreInst &I) {
553 unsigned Class = getClass(I.getOperand(0)->getType());
554 if (Class > 2) // FIXME: Handle longs and others...
555 visitInstruction(I);
556
557 static const unsigned Opcode[] = { X86::MOVrm8, X86::MOVrm16, X86::MOVrm32 };
558
559 unsigned ValReg = getReg(I.getOperand(0));
560 unsigned AddressReg = getReg(I.getOperand(1));
561 addDirectMem(BuildMI(BB, Opcode[Class], 1+4), AddressReg).addReg(ValReg);
562}
563
564
Chris Lattnere2954c82002-11-02 20:04:26 +0000565/// visitPHINode - Turn an LLVM PHI node into an X86 PHI node...
566///
567void ISel::visitPHINode(PHINode &PN) {
568 MachineInstr *MI = BuildMI(BB, X86::PHI, PN.getNumOperands(), getReg(PN));
Chris Lattner72614082002-10-25 22:55:53 +0000569
Chris Lattnere2954c82002-11-02 20:04:26 +0000570 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
571 // FIXME: This will put constants after the PHI nodes in the block, which
572 // is invalid. They should be put inline into the PHI node eventually.
573 //
574 MI->addRegOperand(getReg(PN.getIncomingValue(i)));
575 MI->addPCDispOperand(PN.getIncomingBlock(i));
576 }
Chris Lattner72614082002-10-25 22:55:53 +0000577}
578
Brian Gaekea1719c92002-10-31 23:03:59 +0000579
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000580/// createSimpleX86InstructionSelector - This pass converts an LLVM function
581/// into a machine code representation is a very simple peep-hole fashion. The
Chris Lattner72614082002-10-25 22:55:53 +0000582/// generated code sucks but the implementation is nice and simple.
583///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000584Pass *createSimpleX86InstructionSelector(TargetMachine &TM) {
585 return new ISel(TM);
Chris Lattner72614082002-10-25 22:55:53 +0000586}