blob: f0ffc679ed75a7a8ae2cf624a53a47a99d0a4f32 [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 Lattner51b49a92002-11-02 19:45:49 +0000389/// visitBranchInst - Handle conditional and unconditional branches here. Note
390/// that since code layout is frozen at this point, that if we are trying to
391/// jump to a block that is the immediate successor of the current block, we can
392/// just make a fall-through. (but we don't currently).
393///
Brian Gaekec03a0cb2002-11-19 09:08:47 +0000394void
395ISel::visitBranchInst (BranchInst & BI)
396{
397 if (BI.isConditional ())
398 {
399 BasicBlock *ifTrue = BI.getSuccessor (0);
400 BasicBlock *ifFalse = BI.getSuccessor (1); // this is really unobvious
Chris Lattner2df035b2002-11-02 19:27:56 +0000401
Brian Gaekec03a0cb2002-11-19 09:08:47 +0000402 // simplest thing I can think of: compare condition with zero,
403 // followed by jump-if-equal to ifFalse, and jump-if-nonequal to
404 // ifTrue
405 unsigned int condReg = getReg (BI.getCondition ());
406 BuildMI (BB, X86::CMPri8, 2, X86::EFLAGS).addReg (condReg).addZImm (0);
407 BuildMI (BB, X86::JNE, 1).addPCDisp (BI.getSuccessor (0));
408 BuildMI (BB, X86::JE, 1).addPCDisp (BI.getSuccessor (1));
409 }
410 else // unconditional branch
411 {
412 BuildMI (BB, X86::JMP, 1).addPCDisp (BI.getSuccessor (0));
413 }
Chris Lattner2df035b2002-11-02 19:27:56 +0000414}
415
416
Chris Lattner68aad932002-11-02 20:13:22 +0000417/// visitSimpleBinary - Implement simple binary operators for integral types...
418/// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or,
419/// 4 for Xor.
420///
421void ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) {
422 if (B.getType() == Type::BoolTy) // FIXME: Handle bools for logicals
Chris Lattnere2954c82002-11-02 20:04:26 +0000423 visitInstruction(B);
424
425 unsigned Class = getClass(B.getType());
426 if (Class > 2) // FIXME: Handle longs
427 visitInstruction(B);
428
429 static const unsigned OpcodeTab[][4] = {
Chris Lattner68aad932002-11-02 20:13:22 +0000430 // Arithmetic operators
431 { X86::ADDrr8, X86::ADDrr16, X86::ADDrr32, 0 }, // ADD
432 { X86::SUBrr8, X86::SUBrr16, X86::SUBrr32, 0 }, // SUB
433
434 // Bitwise operators
Chris Lattnere2954c82002-11-02 20:04:26 +0000435 { X86::ANDrr8, X86::ANDrr16, X86::ANDrr32, 0 }, // AND
436 { X86:: ORrr8, X86:: ORrr16, X86:: ORrr32, 0 }, // OR
437 { X86::XORrr8, X86::XORrr16, X86::XORrr32, 0 }, // XOR
438 };
439
440 unsigned Opcode = OpcodeTab[OperatorClass][Class];
441 unsigned Op0r = getReg(B.getOperand(0));
442 unsigned Op1r = getReg(B.getOperand(1));
443 BuildMI(BB, Opcode, 2, getReg(B)).addReg(Op0r).addReg(Op1r);
444}
445
Chris Lattnerca9671d2002-11-02 20:28:58 +0000446/// visitMul - Multiplies are not simple binary operators because they must deal
447/// with the EAX register explicitly.
448///
449void ISel::visitMul(BinaryOperator &I) {
450 unsigned Class = getClass(I.getType());
451 if (Class > 2) // FIXME: Handle longs
452 visitInstruction(I);
Chris Lattnere2954c82002-11-02 20:04:26 +0000453
Chris Lattnerca9671d2002-11-02 20:28:58 +0000454 static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX };
Chris Lattner06925362002-11-17 21:56:38 +0000455 static const unsigned Clobbers[] ={ X86::AH , X86::DX , X86::EDX };
Chris Lattnerca9671d2002-11-02 20:28:58 +0000456 static const unsigned MulOpcode[]={ X86::MULrr8, X86::MULrr16, X86::MULrr32 };
457 static const unsigned MovOpcode[]={ X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 };
458
Chris Lattner06925362002-11-17 21:56:38 +0000459 unsigned Reg = Regs[Class];
460 unsigned Clobber = Clobbers[Class];
461 unsigned Op0Reg = getReg(I.getOperand(0));
462 unsigned Op1Reg = getReg(I.getOperand(1));
Chris Lattnerca9671d2002-11-02 20:28:58 +0000463
464 // Put the first operand into one of the A registers...
465 BuildMI(BB, MovOpcode[Class], 1, Reg).addReg(Op0Reg);
466
Chris Lattner06925362002-11-17 21:56:38 +0000467 // Emit the appropriate multiply instruction...
Chris Lattner71e83ca2002-11-17 22:33:26 +0000468 BuildMI(BB, MulOpcode[Class], 3)
Chris Lattner06925362002-11-17 21:56:38 +0000469 .addReg(Reg, UseAndDef).addReg(Op1Reg).addClobber(Clobber);
Chris Lattnerca9671d2002-11-02 20:28:58 +0000470
471 // Put the result into the destination register...
472 BuildMI(BB, MovOpcode[Class], 1, getReg(I)).addReg(Reg);
Chris Lattnerf01729e2002-11-02 20:54:46 +0000473}
Chris Lattnerca9671d2002-11-02 20:28:58 +0000474
Chris Lattner06925362002-11-17 21:56:38 +0000475
Chris Lattnerf01729e2002-11-02 20:54:46 +0000476/// visitDivRem - Handle division and remainder instructions... these
477/// instruction both require the same instructions to be generated, they just
478/// select the result from a different register. Note that both of these
479/// instructions work differently for signed and unsigned operands.
480///
481void ISel::visitDivRem(BinaryOperator &I) {
482 unsigned Class = getClass(I.getType());
483 if (Class > 2) // FIXME: Handle longs
484 visitInstruction(I);
485
486 static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX };
487 static const unsigned MovOpcode[]={ X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 };
Brian Gaeke6559bb92002-11-14 22:32:30 +0000488 static const unsigned ExtOpcode[]={ X86::CBW , X86::CWD , X86::CDQ };
Chris Lattnerf01729e2002-11-02 20:54:46 +0000489 static const unsigned ClrOpcode[]={ X86::XORrr8, X86::XORrr16, X86::XORrr32 };
490 static const unsigned ExtRegs[] ={ X86::AH , X86::DX , X86::EDX };
491
492 static const unsigned DivOpcode[][4] = {
493 { X86::DIVrr8 , X86::DIVrr16 , X86::DIVrr32 , 0 }, // Unsigned division
494 { X86::IDIVrr8, X86::IDIVrr16, X86::IDIVrr32, 0 }, // Signed division
495 };
496
497 bool isSigned = I.getType()->isSigned();
498 unsigned Reg = Regs[Class];
499 unsigned ExtReg = ExtRegs[Class];
Chris Lattner6fc3c522002-11-17 21:11:55 +0000500 unsigned Op0Reg = getReg(I.getOperand(0));
Chris Lattnerf01729e2002-11-02 20:54:46 +0000501 unsigned Op1Reg = getReg(I.getOperand(1));
502
503 // Put the first operand into one of the A registers...
504 BuildMI(BB, MovOpcode[Class], 1, Reg).addReg(Op0Reg);
505
506 if (isSigned) {
507 // Emit a sign extension instruction...
508 BuildMI(BB, ExtOpcode[Class], 1, ExtReg).addReg(Reg);
509 } else {
510 // If unsigned, emit a zeroing instruction... (reg = xor reg, reg)
511 BuildMI(BB, ClrOpcode[Class], 2, ExtReg).addReg(ExtReg).addReg(ExtReg);
512 }
513
Chris Lattner06925362002-11-17 21:56:38 +0000514 // Emit the appropriate divide or remainder instruction...
515 BuildMI(BB, DivOpcode[isSigned][Class], 2)
516 .addReg(Reg, UseAndDef).addReg(ExtReg, UseAndDef).addReg(Op1Reg);
517
Chris Lattnerf01729e2002-11-02 20:54:46 +0000518 // Figure out which register we want to pick the result out of...
519 unsigned DestReg = (I.getOpcode() == Instruction::Div) ? Reg : ExtReg;
520
Chris Lattnerf01729e2002-11-02 20:54:46 +0000521 // Put the result into the destination register...
522 BuildMI(BB, MovOpcode[Class], 1, getReg(I)).addReg(DestReg);
Chris Lattnerca9671d2002-11-02 20:28:58 +0000523}
Chris Lattnere2954c82002-11-02 20:04:26 +0000524
Chris Lattner06925362002-11-17 21:56:38 +0000525
Brian Gaekea1719c92002-10-31 23:03:59 +0000526/// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here
527/// for constant immediate shift values, and for constant immediate
528/// shift values equal to 1. Even the general case is sort of special,
529/// because the shift amount has to be in CL, not just any old register.
530///
Chris Lattnerf01729e2002-11-02 20:54:46 +0000531void ISel::visitShiftInst (ShiftInst &I) {
532 unsigned Op0r = getReg (I.getOperand(0));
533 unsigned DestReg = getReg(I);
Chris Lattnere9913f22002-11-02 01:41:55 +0000534 bool isLeftShift = I.getOpcode() == Instruction::Shl;
535 bool isOperandSigned = I.getType()->isUnsigned();
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000536 unsigned OperandClass = getClass(I.getType());
537
538 if (OperandClass > 2)
539 visitInstruction(I); // Can't handle longs yet!
Chris Lattner796df732002-11-02 00:44:25 +0000540
Brian Gaekea1719c92002-10-31 23:03:59 +0000541 if (ConstantUInt *CUI = dyn_cast <ConstantUInt> (I.getOperand (1)))
542 {
Chris Lattner796df732002-11-02 00:44:25 +0000543 // The shift amount is constant, guaranteed to be a ubyte. Get its value.
544 assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?");
545 unsigned char shAmt = CUI->getValue();
546
Chris Lattnere9913f22002-11-02 01:41:55 +0000547 static const unsigned ConstantOperand[][4] = {
548 { X86::SHRir8, X86::SHRir16, X86::SHRir32, 0 }, // SHR
549 { X86::SARir8, X86::SARir16, X86::SARir32, 0 }, // SAR
550 { X86::SHLir8, X86::SHLir16, X86::SHLir32, 0 }, // SHL
551 { X86::SHLir8, X86::SHLir16, X86::SHLir32, 0 }, // SAL = SHL
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000552 };
553
Chris Lattnere9913f22002-11-02 01:41:55 +0000554 const unsigned *OpTab = // Figure out the operand table to use
555 ConstantOperand[isLeftShift*2+isOperandSigned];
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000556
Brian Gaekea1719c92002-10-31 23:03:59 +0000557 // Emit: <insn> reg, shamt (shift-by-immediate opcode "ir" form.)
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000558 BuildMI(BB, OpTab[OperandClass], 2, DestReg).addReg(Op0r).addZImm(shAmt);
Brian Gaekea1719c92002-10-31 23:03:59 +0000559 }
560 else
561 {
562 // The shift amount is non-constant.
563 //
564 // In fact, you can only shift with a variable shift amount if
565 // that amount is already in the CL register, so we have to put it
566 // there first.
567 //
Chris Lattnere9913f22002-11-02 01:41:55 +0000568
Brian Gaekea1719c92002-10-31 23:03:59 +0000569 // Emit: move cl, shiftAmount (put the shift amount in CL.)
Chris Lattnerca9671d2002-11-02 20:28:58 +0000570 BuildMI(BB, X86::MOVrr8, 1, X86::CL).addReg(getReg(I.getOperand(1)));
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000571
572 // This is a shift right (SHR).
Chris Lattnere9913f22002-11-02 01:41:55 +0000573 static const unsigned NonConstantOperand[][4] = {
574 { X86::SHRrr8, X86::SHRrr16, X86::SHRrr32, 0 }, // SHR
575 { X86::SARrr8, X86::SARrr16, X86::SARrr32, 0 }, // SAR
576 { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32, 0 }, // SHL
577 { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32, 0 }, // SAL = SHL
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000578 };
579
Chris Lattnere9913f22002-11-02 01:41:55 +0000580 const unsigned *OpTab = // Figure out the operand table to use
581 NonConstantOperand[isLeftShift*2+isOperandSigned];
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000582
Chris Lattnere9913f22002-11-02 01:41:55 +0000583 BuildMI(BB, OpTab[OperandClass], 2, DestReg).addReg(Op0r).addReg(X86::CL);
Brian Gaekea1719c92002-10-31 23:03:59 +0000584 }
585}
586
Chris Lattner06925362002-11-17 21:56:38 +0000587
Chris Lattner6fc3c522002-11-17 21:11:55 +0000588/// visitLoadInst - Implement LLVM load instructions in terms of the x86 'mov'
589/// instruction.
590///
591void ISel::visitLoadInst(LoadInst &I) {
592 unsigned Class = getClass(I.getType());
593 if (Class > 2) // FIXME: Handle longs and others...
594 visitInstruction(I);
595
596 static const unsigned Opcode[] = { X86::MOVmr8, X86::MOVmr16, X86::MOVmr32 };
597
598 unsigned AddressReg = getReg(I.getOperand(0));
599 addDirectMem(BuildMI(BB, Opcode[Class], 4, getReg(I)), AddressReg);
600}
601
Chris Lattner06925362002-11-17 21:56:38 +0000602
Chris Lattner6fc3c522002-11-17 21:11:55 +0000603/// visitStoreInst - Implement LLVM store instructions in terms of the x86 'mov'
604/// instruction.
605///
606void ISel::visitStoreInst(StoreInst &I) {
607 unsigned Class = getClass(I.getOperand(0)->getType());
608 if (Class > 2) // FIXME: Handle longs and others...
609 visitInstruction(I);
610
611 static const unsigned Opcode[] = { X86::MOVrm8, X86::MOVrm16, X86::MOVrm32 };
612
613 unsigned ValReg = getReg(I.getOperand(0));
614 unsigned AddressReg = getReg(I.getOperand(1));
615 addDirectMem(BuildMI(BB, Opcode[Class], 1+4), AddressReg).addReg(ValReg);
616}
617
618
Chris Lattnere2954c82002-11-02 20:04:26 +0000619/// visitPHINode - Turn an LLVM PHI node into an X86 PHI node...
620///
621void ISel::visitPHINode(PHINode &PN) {
622 MachineInstr *MI = BuildMI(BB, X86::PHI, PN.getNumOperands(), getReg(PN));
Chris Lattner72614082002-10-25 22:55:53 +0000623
Chris Lattnere2954c82002-11-02 20:04:26 +0000624 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
625 // FIXME: This will put constants after the PHI nodes in the block, which
626 // is invalid. They should be put inline into the PHI node eventually.
627 //
628 MI->addRegOperand(getReg(PN.getIncomingValue(i)));
629 MI->addPCDispOperand(PN.getIncomingBlock(i));
630 }
Chris Lattner72614082002-10-25 22:55:53 +0000631}
632
Brian Gaekea1719c92002-10-31 23:03:59 +0000633
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000634/// createSimpleX86InstructionSelector - This pass converts an LLVM function
635/// into a machine code representation is a very simple peep-hole fashion. The
Chris Lattner72614082002-10-25 22:55:53 +0000636/// generated code sucks but the implementation is nice and simple.
637///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000638Pass *createSimpleX86InstructionSelector(TargetMachine &TM) {
639 return new ISel(TM);
Chris Lattner72614082002-10-25 22:55:53 +0000640}