blob: d74bfd4912e9151fa33b4af6ae36aa794b46c96a [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"
Chris Lattner72614082002-10-25 22:55:53 +000020#include "llvm/Support/InstVisitor.h"
Chris Lattner72614082002-10-25 22:55:53 +000021
Chris Lattner06925362002-11-17 21:56:38 +000022using namespace MOTy; // Get Use, Def, UseAndDef
23
Chris Lattner72614082002-10-25 22:55:53 +000024namespace {
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000025 struct ISel : public FunctionPass, InstVisitor<ISel> {
26 TargetMachine &TM;
Chris Lattner341a9372002-10-29 17:43:55 +000027 MachineFunction *F; // The function we are compiling into
28 MachineBasicBlock *BB; // The current MBB we are compiling
Chris Lattner72614082002-10-25 22:55:53 +000029
30 unsigned CurReg;
31 std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs
32
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000033 ISel(TargetMachine &tm)
34 : TM(tm), F(0), BB(0), CurReg(MRegisterInfo::FirstVirtualRegister) {}
Chris Lattner72614082002-10-25 22:55:53 +000035
36 /// runOnFunction - Top level implementation of instruction selection for
37 /// the entire function.
38 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000039 bool runOnFunction(Function &Fn) {
Chris Lattner36b36032002-10-29 23:40:58 +000040 F = &MachineFunction::construct(&Fn, TM);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000041 visit(Fn);
Chris Lattner72614082002-10-25 22:55:53 +000042 RegMap.clear();
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000043 F = 0;
Chris Lattner72614082002-10-25 22:55:53 +000044 return false; // We never modify the LLVM itself.
45 }
46
47 /// visitBasicBlock - This method is called when we are visiting a new basic
Chris Lattner33f53b52002-10-29 20:48:56 +000048 /// block. This simply creates a new MachineBasicBlock to emit code into
49 /// and adds it to the current MachineFunction. Subsequent visit* for
50 /// instructions will be invoked for all instructions in the basic block.
Chris Lattner72614082002-10-25 22:55:53 +000051 ///
52 void visitBasicBlock(BasicBlock &LLVM_BB) {
Chris Lattner42c77862002-10-30 00:47:40 +000053 BB = new MachineBasicBlock(&LLVM_BB);
Chris Lattner72614082002-10-25 22:55:53 +000054 // FIXME: Use the auto-insert form when it's available
55 F->getBasicBlockList().push_back(BB);
56 }
57
58 // Visitation methods for various instructions. These methods simply emit
59 // fixed X86 code for each instruction.
60 //
61 void visitReturnInst(ReturnInst &RI);
Chris Lattner2df035b2002-11-02 19:27:56 +000062 void visitBranchInst(BranchInst &BI);
Chris Lattnere2954c82002-11-02 20:04:26 +000063
64 // Arithmetic operators
Chris Lattnerf01729e2002-11-02 20:54:46 +000065 void visitSimpleBinary(BinaryOperator &B, unsigned OpcodeClass);
Chris Lattner68aad932002-11-02 20:13:22 +000066 void visitAdd(BinaryOperator &B) { visitSimpleBinary(B, 0); }
67 void visitSub(BinaryOperator &B) { visitSimpleBinary(B, 1); }
Chris Lattnerca9671d2002-11-02 20:28:58 +000068 void visitMul(BinaryOperator &B);
Chris Lattnere2954c82002-11-02 20:04:26 +000069
Chris Lattnerf01729e2002-11-02 20:54:46 +000070 void visitDiv(BinaryOperator &B) { visitDivRem(B); }
71 void visitRem(BinaryOperator &B) { visitDivRem(B); }
72 void visitDivRem(BinaryOperator &B);
73
Chris Lattnere2954c82002-11-02 20:04:26 +000074 // Bitwise operators
Chris Lattner68aad932002-11-02 20:13:22 +000075 void visitAnd(BinaryOperator &B) { visitSimpleBinary(B, 2); }
76 void visitOr (BinaryOperator &B) { visitSimpleBinary(B, 3); }
77 void visitXor(BinaryOperator &B) { visitSimpleBinary(B, 4); }
Chris Lattnere2954c82002-11-02 20:04:26 +000078
79 // Binary comparison operators
Chris Lattner6fc3c522002-11-17 21:11:55 +000080 void visitSetCondInst(SetCondInst &I);
81
82 // Memory Instructions
83 void visitLoadInst(LoadInst &I);
84 void visitStoreInst(StoreInst &I);
Chris Lattnere2954c82002-11-02 20:04:26 +000085
86 // Other operators
Brian Gaekea1719c92002-10-31 23:03:59 +000087 void visitShiftInst(ShiftInst &I);
Chris Lattnere2954c82002-11-02 20:04:26 +000088 void visitPHINode(PHINode &I);
Chris Lattner72614082002-10-25 22:55:53 +000089
90 void visitInstruction(Instruction &I) {
91 std::cerr << "Cannot instruction select: " << I;
92 abort();
93 }
94
Chris Lattnerc5291f52002-10-27 21:16:59 +000095
96 /// copyConstantToRegister - Output the instructions required to put the
97 /// specified constant into the specified register.
98 ///
99 void copyConstantToRegister(Constant *C, unsigned Reg);
100
Chris Lattner72614082002-10-25 22:55:53 +0000101 /// getReg - This method turns an LLVM value into a register number. This
102 /// is guaranteed to produce the same register number for a particular value
103 /// every time it is queried.
104 ///
105 unsigned getReg(Value &V) { return getReg(&V); } // Allow references
106 unsigned getReg(Value *V) {
107 unsigned &Reg = RegMap[V];
108 if (Reg == 0)
109 Reg = CurReg++;
110
Chris Lattner6f8fd252002-10-27 21:23:43 +0000111 // If this operand is a constant, emit the code to copy the constant into
112 // the register here...
113 //
Chris Lattnerc5291f52002-10-27 21:16:59 +0000114 if (Constant *C = dyn_cast<Constant>(V))
115 copyConstantToRegister(C, Reg);
116
Chris Lattner72614082002-10-25 22:55:53 +0000117 return Reg;
118 }
Chris Lattner72614082002-10-25 22:55:53 +0000119 };
120}
121
Chris Lattner43189d12002-11-17 20:07:45 +0000122/// TypeClass - Used by the X86 backend to group LLVM types by their basic X86
123/// Representation.
124///
125enum TypeClass {
126 cByte, cShort, cInt, cLong, cFloat, cDouble
127};
128
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000129/// getClass - Turn a primitive type into a "class" number which is based on the
130/// size of the type, and whether or not it is floating point.
131///
Chris Lattner43189d12002-11-17 20:07:45 +0000132static inline TypeClass getClass(const Type *Ty) {
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000133 switch (Ty->getPrimitiveID()) {
134 case Type::SByteTyID:
Chris Lattner43189d12002-11-17 20:07:45 +0000135 case Type::UByteTyID: return cByte; // Byte operands are class #0
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000136 case Type::ShortTyID:
Chris Lattner43189d12002-11-17 20:07:45 +0000137 case Type::UShortTyID: return cShort; // Short operands are class #1
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000138 case Type::IntTyID:
139 case Type::UIntTyID:
Chris Lattner43189d12002-11-17 20:07:45 +0000140 case Type::PointerTyID: return cInt; // Int's and pointers are class #2
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000141
142 case Type::LongTyID:
Chris Lattner43189d12002-11-17 20:07:45 +0000143 case Type::ULongTyID: return cLong; // Longs are class #3
144 case Type::FloatTyID: return cFloat; // Float is class #4
145 case Type::DoubleTyID: return cDouble; // Doubles are class #5
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000146 default:
147 assert(0 && "Invalid type to getClass!");
Chris Lattner43189d12002-11-17 20:07:45 +0000148 return cByte; // not reached
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000149 }
150}
Chris Lattnerc5291f52002-10-27 21:16:59 +0000151
Chris Lattner06925362002-11-17 21:56:38 +0000152
Chris Lattnerc5291f52002-10-27 21:16:59 +0000153/// copyConstantToRegister - Output the instructions required to put the
154/// specified constant into the specified register.
155///
156void ISel::copyConstantToRegister(Constant *C, unsigned R) {
157 assert (!isa<ConstantExpr>(C) && "Constant expressions not yet handled!\n");
158
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000159 if (C->getType()->isIntegral()) {
160 unsigned Class = getClass(C->getType());
161 assert(Class != 3 && "Type not handled yet!");
162
163 static const unsigned IntegralOpcodeTab[] = {
164 X86::MOVir8, X86::MOVir16, X86::MOVir32
165 };
166
167 if (C->getType()->isSigned()) {
168 ConstantSInt *CSI = cast<ConstantSInt>(C);
169 BuildMI(BB, IntegralOpcodeTab[Class], 1, R).addSImm(CSI->getValue());
170 } else {
171 ConstantUInt *CUI = cast<ConstantUInt>(C);
172 BuildMI(BB, IntegralOpcodeTab[Class], 1, R).addZImm(CUI->getValue());
173 }
174 } else {
175 assert(0 && "Type not handled yet!");
Chris Lattnerc5291f52002-10-27 21:16:59 +0000176 }
177}
178
Chris Lattner06925362002-11-17 21:56:38 +0000179
Brian Gaeke1749d632002-11-07 17:59:21 +0000180/// SetCC instructions - Here we just emit boilerplate code to set a byte-sized
181/// register, then move it to wherever the result should be.
182/// We handle FP setcc instructions by pushing them, doing a
183/// compare-and-pop-twice, and then copying the concodes to the main
184/// processor's concodes (I didn't make this up, it's in the Intel manual)
185///
186void
187ISel::visitSetCondInst (SetCondInst & I)
188{
189 // The arguments are already supposed to be of the same type.
190 Value *var1 = I.getOperand (0);
191 Value *var2 = I.getOperand (1);
192 unsigned reg1 = getReg (var1);
193 unsigned reg2 = getReg (var2);
194 unsigned resultReg = getReg (I);
195 unsigned comparisonWidth = var1->getType ()->getPrimitiveSize ();
196 unsigned unsignedComparison = var1->getType ()->isUnsigned ();
197 unsigned resultWidth = I.getType ()->getPrimitiveSize ();
198 bool fpComparison = var1->getType ()->isFloatingPoint ();
199 if (fpComparison)
200 {
201 // Push the variables on the stack with fldl opcodes.
202 // FIXME: assuming var1, var2 are in memory, if not, spill to
203 // stack first
204 switch (comparisonWidth)
205 {
206 case 4:
207 BuildMI (BB, X86::FLDr4, 1, X86::NoReg).addReg (reg1);
208 break;
209 case 8:
210 BuildMI (BB, X86::FLDr8, 1, X86::NoReg).addReg (reg1);
211 break;
212 default:
213 visitInstruction (I);
214 break;
215 }
216 switch (comparisonWidth)
217 {
218 case 4:
219 BuildMI (BB, X86::FLDr4, 1, X86::NoReg).addReg (reg2);
220 break;
221 case 8:
222 BuildMI (BB, X86::FLDr8, 1, X86::NoReg).addReg (reg2);
223 break;
224 default:
225 visitInstruction (I);
226 break;
227 }
228 // (Non-trapping) compare and pop twice.
Brian Gaeke1749d632002-11-07 17:59:21 +0000229 BuildMI (BB, X86::FUCOMPP, 0);
230 // Move fp status word (concodes) to ax.
231 BuildMI (BB, X86::FNSTSWr8, 1, X86::AX);
232 // Load real concodes from ax.
Brian Gaeke6559bb92002-11-14 22:32:30 +0000233 BuildMI (BB, X86::SAHF, 1, X86::EFLAGS).addReg(X86::AH);
Brian Gaeke1749d632002-11-07 17:59:21 +0000234 }
235 else
236 { // integer comparison
237 // Emit: cmp <var1>, <var2> (do the comparison). We can
238 // compare 8-bit with 8-bit, 16-bit with 16-bit, 32-bit with
239 // 32-bit.
Brian Gaeke1749d632002-11-07 17:59:21 +0000240 switch (comparisonWidth)
241 {
242 case 1:
243 BuildMI (BB, X86::CMPrr8, 2,
Brian Gaeke6559bb92002-11-14 22:32:30 +0000244 X86::EFLAGS).addReg (reg1).addReg (reg2);
Brian Gaeke1749d632002-11-07 17:59:21 +0000245 break;
246 case 2:
247 BuildMI (BB, X86::CMPrr16, 2,
Brian Gaeke6559bb92002-11-14 22:32:30 +0000248 X86::EFLAGS).addReg (reg1).addReg (reg2);
Brian Gaeke1749d632002-11-07 17:59:21 +0000249 break;
250 case 4:
251 BuildMI (BB, X86::CMPrr32, 2,
Brian Gaeke6559bb92002-11-14 22:32:30 +0000252 X86::EFLAGS).addReg (reg1).addReg (reg2);
Brian Gaeke1749d632002-11-07 17:59:21 +0000253 break;
254 case 8:
255 default:
256 visitInstruction (I);
257 break;
258 }
259 }
260 // Emit setOp instruction (extract concode; clobbers ax),
261 // using the following mapping:
262 // LLVM -> X86 signed X86 unsigned
263 // ----- ----- -----
264 // seteq -> sete sete
265 // setne -> setne setne
266 // setlt -> setl setb
267 // setgt -> setg seta
268 // setle -> setle setbe
269 // setge -> setge setae
270 switch (I.getOpcode ())
271 {
272 case Instruction::SetEQ:
273 BuildMI (BB, X86::SETE, 0, X86::AL);
274 break;
275 case Instruction::SetGE:
276 if (unsignedComparison)
277 BuildMI (BB, X86::SETAE, 0, X86::AL);
278 else
279 BuildMI (BB, X86::SETGE, 0, X86::AL);
280 break;
281 case Instruction::SetGT:
282 if (unsignedComparison)
283 BuildMI (BB, X86::SETA, 0, X86::AL);
284 else
285 BuildMI (BB, X86::SETG, 0, X86::AL);
286 break;
287 case Instruction::SetLE:
288 if (unsignedComparison)
289 BuildMI (BB, X86::SETBE, 0, X86::AL);
290 else
291 BuildMI (BB, X86::SETLE, 0, X86::AL);
292 break;
293 case Instruction::SetLT:
294 if (unsignedComparison)
295 BuildMI (BB, X86::SETB, 0, X86::AL);
296 else
297 BuildMI (BB, X86::SETL, 0, X86::AL);
298 break;
299 case Instruction::SetNE:
300 BuildMI (BB, X86::SETNE, 0, X86::AL);
301 break;
302 default:
303 visitInstruction (I);
304 break;
305 }
306 // Put it in the result using a move.
307 switch (resultWidth)
308 {
309 case 1:
310 BuildMI (BB, X86::MOVrr8, 1, resultReg).addReg (X86::AL);
311 break;
Brian Gaeke1749d632002-11-07 17:59:21 +0000312 case 2:
Brian Gaeke6559bb92002-11-14 22:32:30 +0000313 BuildMI (BB, X86::MOVZXr16r8, 1, resultReg).addReg (X86::AL);
Brian Gaeke1749d632002-11-07 17:59:21 +0000314 break;
315 case 4:
Brian Gaeke6559bb92002-11-14 22:32:30 +0000316 BuildMI (BB, X86::MOVZXr32r8, 1, resultReg).addReg (X86::AL);
Brian Gaeke1749d632002-11-07 17:59:21 +0000317 break;
318 case 8:
319 default:
320 visitInstruction (I);
321 break;
322 }
323}
Chris Lattner51b49a92002-11-02 19:45:49 +0000324
Chris Lattnerc5291f52002-10-27 21:16:59 +0000325
Chris Lattner72614082002-10-25 22:55:53 +0000326/// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such,
327/// we have the following possibilities:
328///
329/// ret void: No return value, simply emit a 'ret' instruction
330/// ret sbyte, ubyte : Extend value into EAX and return
331/// ret short, ushort: Extend value into EAX and return
332/// ret int, uint : Move value into EAX and return
333/// ret pointer : Move value into EAX and return
Chris Lattner06925362002-11-17 21:56:38 +0000334/// ret long, ulong : Move value into EAX/EDX and return
335/// ret float/double : Top of FP stack
Chris Lattner72614082002-10-25 22:55:53 +0000336///
Chris Lattner6fc3c522002-11-17 21:11:55 +0000337void ISel::visitReturnInst (ReturnInst &I) {
Chris Lattner43189d12002-11-17 20:07:45 +0000338 if (I.getNumOperands() == 0) {
339 // Emit a 'ret' instruction
340 BuildMI(BB, X86::RET, 0);
341 return;
342 }
343
344 unsigned val = getReg(I.getOperand(0));
Chris Lattner6fc3c522002-11-17 21:11:55 +0000345 unsigned Class = getClass(I.getOperand(0)->getType());
Chris Lattner43189d12002-11-17 20:07:45 +0000346 bool isUnsigned = I.getOperand(0)->getType()->isUnsigned();
347 switch (Class) {
348 case cByte:
349 // ret sbyte, ubyte: Extend value into EAX and return
Chris Lattner6fc3c522002-11-17 21:11:55 +0000350 if (isUnsigned)
Chris Lattner43189d12002-11-17 20:07:45 +0000351 BuildMI (BB, X86::MOVZXr32r8, 1, X86::EAX).addReg (val);
Chris Lattner6fc3c522002-11-17 21:11:55 +0000352 else
Chris Lattner43189d12002-11-17 20:07:45 +0000353 BuildMI (BB, X86::MOVSXr32r8, 1, X86::EAX).addReg (val);
Chris Lattner43189d12002-11-17 20:07:45 +0000354 break;
355 case cShort:
356 // ret short, ushort: Extend value into EAX and return
Chris Lattner6fc3c522002-11-17 21:11:55 +0000357 if (isUnsigned)
Chris Lattner43189d12002-11-17 20:07:45 +0000358 BuildMI (BB, X86::MOVZXr32r16, 1, X86::EAX).addReg (val);
Chris Lattner6fc3c522002-11-17 21:11:55 +0000359 else
Chris Lattner43189d12002-11-17 20:07:45 +0000360 BuildMI (BB, X86::MOVSXr32r16, 1, X86::EAX).addReg (val);
Chris Lattner43189d12002-11-17 20:07:45 +0000361 break;
362 case cInt:
363 // ret int, uint, ptr: Move value into EAX and return
364 // MOV EAX, <val>
365 BuildMI(BB, X86::MOVrr32, 1, X86::EAX).addReg(val);
366 break;
367
368 // ret float/double: top of FP stack
369 // FLD <val>
370 case cFloat: // Floats
371 BuildMI(BB, X86::FLDr4, 1).addReg(val);
372 break;
373 case cDouble: // Doubles
374 BuildMI(BB, X86::FLDr8, 1).addReg(val);
375 break;
376 case cLong:
377 // ret long: use EAX(least significant 32 bits)/EDX (most
378 // significant 32)...uh, I think so Brain, but how do i call
379 // up the two parts of the value from inside this mouse
380 // cage? *zort*
381 default:
382 visitInstruction(I);
383 }
384
385 // Emit a 'ret' instruction
386 BuildMI(BB, X86::RET, 0);
Chris Lattner72614082002-10-25 22:55:53 +0000387}
388
Chris Lattner06925362002-11-17 21:56:38 +0000389
Chris Lattner51b49a92002-11-02 19:45:49 +0000390/// visitBranchInst - Handle conditional and unconditional branches here. Note
391/// that since code layout is frozen at this point, that if we are trying to
392/// jump to a block that is the immediate successor of the current block, we can
393/// just make a fall-through. (but we don't currently).
394///
Chris Lattner2df035b2002-11-02 19:27:56 +0000395void ISel::visitBranchInst(BranchInst &BI) {
396 if (BI.isConditional()) // Only handles unconditional branches so far...
397 visitInstruction(BI);
398
399 BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(0));
400}
401
402
Chris Lattner68aad932002-11-02 20:13:22 +0000403/// visitSimpleBinary - Implement simple binary operators for integral types...
404/// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or,
405/// 4 for Xor.
406///
407void ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) {
408 if (B.getType() == Type::BoolTy) // FIXME: Handle bools for logicals
Chris Lattnere2954c82002-11-02 20:04:26 +0000409 visitInstruction(B);
410
411 unsigned Class = getClass(B.getType());
412 if (Class > 2) // FIXME: Handle longs
413 visitInstruction(B);
414
415 static const unsigned OpcodeTab[][4] = {
Chris Lattner68aad932002-11-02 20:13:22 +0000416 // Arithmetic operators
417 { X86::ADDrr8, X86::ADDrr16, X86::ADDrr32, 0 }, // ADD
418 { X86::SUBrr8, X86::SUBrr16, X86::SUBrr32, 0 }, // SUB
419
420 // Bitwise operators
Chris Lattnere2954c82002-11-02 20:04:26 +0000421 { X86::ANDrr8, X86::ANDrr16, X86::ANDrr32, 0 }, // AND
422 { X86:: ORrr8, X86:: ORrr16, X86:: ORrr32, 0 }, // OR
423 { X86::XORrr8, X86::XORrr16, X86::XORrr32, 0 }, // XOR
424 };
425
426 unsigned Opcode = OpcodeTab[OperatorClass][Class];
427 unsigned Op0r = getReg(B.getOperand(0));
428 unsigned Op1r = getReg(B.getOperand(1));
429 BuildMI(BB, Opcode, 2, getReg(B)).addReg(Op0r).addReg(Op1r);
430}
431
Chris Lattnerca9671d2002-11-02 20:28:58 +0000432/// visitMul - Multiplies are not simple binary operators because they must deal
433/// with the EAX register explicitly.
434///
435void ISel::visitMul(BinaryOperator &I) {
436 unsigned Class = getClass(I.getType());
437 if (Class > 2) // FIXME: Handle longs
438 visitInstruction(I);
Chris Lattnere2954c82002-11-02 20:04:26 +0000439
Chris Lattnerca9671d2002-11-02 20:28:58 +0000440 static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX };
Chris Lattner06925362002-11-17 21:56:38 +0000441 static const unsigned Clobbers[] ={ X86::AH , X86::DX , X86::EDX };
Chris Lattnerca9671d2002-11-02 20:28:58 +0000442 static const unsigned MulOpcode[]={ X86::MULrr8, X86::MULrr16, X86::MULrr32 };
443 static const unsigned MovOpcode[]={ X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 };
444
Chris Lattner06925362002-11-17 21:56:38 +0000445 unsigned Reg = Regs[Class];
446 unsigned Clobber = Clobbers[Class];
447 unsigned Op0Reg = getReg(I.getOperand(0));
448 unsigned Op1Reg = getReg(I.getOperand(1));
Chris Lattnerca9671d2002-11-02 20:28:58 +0000449
450 // Put the first operand into one of the A registers...
451 BuildMI(BB, MovOpcode[Class], 1, Reg).addReg(Op0Reg);
452
Chris Lattner06925362002-11-17 21:56:38 +0000453 // Emit the appropriate multiply instruction...
Chris Lattner71e83ca2002-11-17 22:33:26 +0000454 BuildMI(BB, MulOpcode[Class], 3)
Chris Lattner06925362002-11-17 21:56:38 +0000455 .addReg(Reg, UseAndDef).addReg(Op1Reg).addClobber(Clobber);
Chris Lattnerca9671d2002-11-02 20:28:58 +0000456
457 // Put the result into the destination register...
458 BuildMI(BB, MovOpcode[Class], 1, getReg(I)).addReg(Reg);
Chris Lattnerf01729e2002-11-02 20:54:46 +0000459}
Chris Lattnerca9671d2002-11-02 20:28:58 +0000460
Chris Lattner06925362002-11-17 21:56:38 +0000461
Chris Lattnerf01729e2002-11-02 20:54:46 +0000462/// visitDivRem - Handle division and remainder instructions... these
463/// instruction both require the same instructions to be generated, they just
464/// select the result from a different register. Note that both of these
465/// instructions work differently for signed and unsigned operands.
466///
467void ISel::visitDivRem(BinaryOperator &I) {
468 unsigned Class = getClass(I.getType());
469 if (Class > 2) // FIXME: Handle longs
470 visitInstruction(I);
471
472 static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX };
473 static const unsigned MovOpcode[]={ X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 };
Brian Gaeke6559bb92002-11-14 22:32:30 +0000474 static const unsigned ExtOpcode[]={ X86::CBW , X86::CWD , X86::CDQ };
Chris Lattnerf01729e2002-11-02 20:54:46 +0000475 static const unsigned ClrOpcode[]={ X86::XORrr8, X86::XORrr16, X86::XORrr32 };
476 static const unsigned ExtRegs[] ={ X86::AH , X86::DX , X86::EDX };
477
478 static const unsigned DivOpcode[][4] = {
479 { X86::DIVrr8 , X86::DIVrr16 , X86::DIVrr32 , 0 }, // Unsigned division
480 { X86::IDIVrr8, X86::IDIVrr16, X86::IDIVrr32, 0 }, // Signed division
481 };
482
483 bool isSigned = I.getType()->isSigned();
484 unsigned Reg = Regs[Class];
485 unsigned ExtReg = ExtRegs[Class];
Chris Lattner6fc3c522002-11-17 21:11:55 +0000486 unsigned Op0Reg = getReg(I.getOperand(0));
Chris Lattnerf01729e2002-11-02 20:54:46 +0000487 unsigned Op1Reg = getReg(I.getOperand(1));
488
489 // Put the first operand into one of the A registers...
490 BuildMI(BB, MovOpcode[Class], 1, Reg).addReg(Op0Reg);
491
492 if (isSigned) {
493 // Emit a sign extension instruction...
494 BuildMI(BB, ExtOpcode[Class], 1, ExtReg).addReg(Reg);
495 } else {
496 // If unsigned, emit a zeroing instruction... (reg = xor reg, reg)
497 BuildMI(BB, ClrOpcode[Class], 2, ExtReg).addReg(ExtReg).addReg(ExtReg);
498 }
499
Chris Lattner06925362002-11-17 21:56:38 +0000500 // Emit the appropriate divide or remainder instruction...
501 BuildMI(BB, DivOpcode[isSigned][Class], 2)
502 .addReg(Reg, UseAndDef).addReg(ExtReg, UseAndDef).addReg(Op1Reg);
503
Chris Lattnerf01729e2002-11-02 20:54:46 +0000504 // Figure out which register we want to pick the result out of...
505 unsigned DestReg = (I.getOpcode() == Instruction::Div) ? Reg : ExtReg;
506
Chris Lattnerf01729e2002-11-02 20:54:46 +0000507 // Put the result into the destination register...
508 BuildMI(BB, MovOpcode[Class], 1, getReg(I)).addReg(DestReg);
Chris Lattnerca9671d2002-11-02 20:28:58 +0000509}
Chris Lattnere2954c82002-11-02 20:04:26 +0000510
Chris Lattner06925362002-11-17 21:56:38 +0000511
Brian Gaekea1719c92002-10-31 23:03:59 +0000512/// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here
513/// for constant immediate shift values, and for constant immediate
514/// shift values equal to 1. Even the general case is sort of special,
515/// because the shift amount has to be in CL, not just any old register.
516///
Chris Lattnerf01729e2002-11-02 20:54:46 +0000517void ISel::visitShiftInst (ShiftInst &I) {
518 unsigned Op0r = getReg (I.getOperand(0));
519 unsigned DestReg = getReg(I);
Chris Lattnere9913f22002-11-02 01:41:55 +0000520 bool isLeftShift = I.getOpcode() == Instruction::Shl;
521 bool isOperandSigned = I.getType()->isUnsigned();
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000522 unsigned OperandClass = getClass(I.getType());
523
524 if (OperandClass > 2)
525 visitInstruction(I); // Can't handle longs yet!
Chris Lattner796df732002-11-02 00:44:25 +0000526
Brian Gaekea1719c92002-10-31 23:03:59 +0000527 if (ConstantUInt *CUI = dyn_cast <ConstantUInt> (I.getOperand (1)))
528 {
Chris Lattner796df732002-11-02 00:44:25 +0000529 // The shift amount is constant, guaranteed to be a ubyte. Get its value.
530 assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?");
531 unsigned char shAmt = CUI->getValue();
532
Chris Lattnere9913f22002-11-02 01:41:55 +0000533 static const unsigned ConstantOperand[][4] = {
534 { X86::SHRir8, X86::SHRir16, X86::SHRir32, 0 }, // SHR
535 { X86::SARir8, X86::SARir16, X86::SARir32, 0 }, // SAR
536 { X86::SHLir8, X86::SHLir16, X86::SHLir32, 0 }, // SHL
537 { X86::SHLir8, X86::SHLir16, X86::SHLir32, 0 }, // SAL = SHL
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000538 };
539
Chris Lattnere9913f22002-11-02 01:41:55 +0000540 const unsigned *OpTab = // Figure out the operand table to use
541 ConstantOperand[isLeftShift*2+isOperandSigned];
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000542
Brian Gaekea1719c92002-10-31 23:03:59 +0000543 // Emit: <insn> reg, shamt (shift-by-immediate opcode "ir" form.)
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000544 BuildMI(BB, OpTab[OperandClass], 2, DestReg).addReg(Op0r).addZImm(shAmt);
Brian Gaekea1719c92002-10-31 23:03:59 +0000545 }
546 else
547 {
548 // The shift amount is non-constant.
549 //
550 // In fact, you can only shift with a variable shift amount if
551 // that amount is already in the CL register, so we have to put it
552 // there first.
553 //
Chris Lattnere9913f22002-11-02 01:41:55 +0000554
Brian Gaekea1719c92002-10-31 23:03:59 +0000555 // Emit: move cl, shiftAmount (put the shift amount in CL.)
Chris Lattnerca9671d2002-11-02 20:28:58 +0000556 BuildMI(BB, X86::MOVrr8, 1, X86::CL).addReg(getReg(I.getOperand(1)));
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000557
558 // This is a shift right (SHR).
Chris Lattnere9913f22002-11-02 01:41:55 +0000559 static const unsigned NonConstantOperand[][4] = {
560 { X86::SHRrr8, X86::SHRrr16, X86::SHRrr32, 0 }, // SHR
561 { X86::SARrr8, X86::SARrr16, X86::SARrr32, 0 }, // SAR
562 { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32, 0 }, // SHL
563 { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32, 0 }, // SAL = SHL
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000564 };
565
Chris Lattnere9913f22002-11-02 01:41:55 +0000566 const unsigned *OpTab = // Figure out the operand table to use
567 NonConstantOperand[isLeftShift*2+isOperandSigned];
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000568
Chris Lattnere9913f22002-11-02 01:41:55 +0000569 BuildMI(BB, OpTab[OperandClass], 2, DestReg).addReg(Op0r).addReg(X86::CL);
Brian Gaekea1719c92002-10-31 23:03:59 +0000570 }
571}
572
Chris Lattner06925362002-11-17 21:56:38 +0000573
Chris Lattner6fc3c522002-11-17 21:11:55 +0000574/// visitLoadInst - Implement LLVM load instructions in terms of the x86 'mov'
575/// instruction.
576///
577void ISel::visitLoadInst(LoadInst &I) {
578 unsigned Class = getClass(I.getType());
579 if (Class > 2) // FIXME: Handle longs and others...
580 visitInstruction(I);
581
582 static const unsigned Opcode[] = { X86::MOVmr8, X86::MOVmr16, X86::MOVmr32 };
583
584 unsigned AddressReg = getReg(I.getOperand(0));
585 addDirectMem(BuildMI(BB, Opcode[Class], 4, getReg(I)), AddressReg);
586}
587
Chris Lattner06925362002-11-17 21:56:38 +0000588
Chris Lattner6fc3c522002-11-17 21:11:55 +0000589/// visitStoreInst - Implement LLVM store instructions in terms of the x86 'mov'
590/// instruction.
591///
592void ISel::visitStoreInst(StoreInst &I) {
593 unsigned Class = getClass(I.getOperand(0)->getType());
594 if (Class > 2) // FIXME: Handle longs and others...
595 visitInstruction(I);
596
597 static const unsigned Opcode[] = { X86::MOVrm8, X86::MOVrm16, X86::MOVrm32 };
598
599 unsigned ValReg = getReg(I.getOperand(0));
600 unsigned AddressReg = getReg(I.getOperand(1));
601 addDirectMem(BuildMI(BB, Opcode[Class], 1+4), AddressReg).addReg(ValReg);
602}
603
604
Chris Lattnere2954c82002-11-02 20:04:26 +0000605/// visitPHINode - Turn an LLVM PHI node into an X86 PHI node...
606///
607void ISel::visitPHINode(PHINode &PN) {
608 MachineInstr *MI = BuildMI(BB, X86::PHI, PN.getNumOperands(), getReg(PN));
Chris Lattner72614082002-10-25 22:55:53 +0000609
Chris Lattnere2954c82002-11-02 20:04:26 +0000610 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
611 // FIXME: This will put constants after the PHI nodes in the block, which
612 // is invalid. They should be put inline into the PHI node eventually.
613 //
614 MI->addRegOperand(getReg(PN.getIncomingValue(i)));
615 MI->addPCDispOperand(PN.getIncomingBlock(i));
616 }
Chris Lattner72614082002-10-25 22:55:53 +0000617}
618
Brian Gaekea1719c92002-10-31 23:03:59 +0000619
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000620/// createSimpleX86InstructionSelector - This pass converts an LLVM function
621/// into a machine code representation is a very simple peep-hole fashion. The
Chris Lattner72614082002-10-25 22:55:53 +0000622/// generated code sucks but the implementation is nice and simple.
623///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000624Pass *createSimpleX86InstructionSelector(TargetMachine &TM) {
625 return new ISel(TM);
Chris Lattner72614082002-10-25 22:55:53 +0000626}