blob: f2540a520dd7511c8970c2280c26662ecb320bc4 [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 Gaeke1749d632002-11-07 17:59:21 +000011#include "llvm/iOperators.h"
Brian Gaekea1719c92002-10-31 23:03:59 +000012#include "llvm/iOther.h"
Chris Lattner51b49a92002-11-02 19:45:49 +000013#include "llvm/iPHINode.h"
Chris Lattner72614082002-10-25 22:55:53 +000014#include "llvm/Type.h"
Chris Lattnerc5291f52002-10-27 21:16:59 +000015#include "llvm/Constants.h"
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000016#include "llvm/Pass.h"
Chris Lattner341a9372002-10-29 17:43:55 +000017#include "llvm/CodeGen/MachineFunction.h"
18#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner72614082002-10-25 22:55:53 +000019#include "llvm/Support/InstVisitor.h"
20#include <map>
21
22namespace {
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000023 struct ISel : public FunctionPass, InstVisitor<ISel> {
24 TargetMachine &TM;
Chris Lattner341a9372002-10-29 17:43:55 +000025 MachineFunction *F; // The function we are compiling into
26 MachineBasicBlock *BB; // The current MBB we are compiling
Chris Lattner72614082002-10-25 22:55:53 +000027
28 unsigned CurReg;
29 std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs
30
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000031 ISel(TargetMachine &tm)
32 : TM(tm), F(0), BB(0), CurReg(MRegisterInfo::FirstVirtualRegister) {}
Chris Lattner72614082002-10-25 22:55:53 +000033
34 /// runOnFunction - Top level implementation of instruction selection for
35 /// the entire function.
36 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000037 bool runOnFunction(Function &Fn) {
Chris Lattner36b36032002-10-29 23:40:58 +000038 F = &MachineFunction::construct(&Fn, TM);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000039 visit(Fn);
Chris Lattner72614082002-10-25 22:55:53 +000040 RegMap.clear();
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000041 F = 0;
Chris Lattner72614082002-10-25 22:55:53 +000042 return false; // We never modify the LLVM itself.
43 }
44
45 /// visitBasicBlock - This method is called when we are visiting a new basic
Chris Lattner33f53b52002-10-29 20:48:56 +000046 /// block. This simply creates a new MachineBasicBlock to emit code into
47 /// and adds it to the current MachineFunction. Subsequent visit* for
48 /// instructions will be invoked for all instructions in the basic block.
Chris Lattner72614082002-10-25 22:55:53 +000049 ///
50 void visitBasicBlock(BasicBlock &LLVM_BB) {
Chris Lattner42c77862002-10-30 00:47:40 +000051 BB = new MachineBasicBlock(&LLVM_BB);
Chris Lattner72614082002-10-25 22:55:53 +000052 // FIXME: Use the auto-insert form when it's available
53 F->getBasicBlockList().push_back(BB);
54 }
55
56 // Visitation methods for various instructions. These methods simply emit
57 // fixed X86 code for each instruction.
58 //
59 void visitReturnInst(ReturnInst &RI);
Chris Lattner2df035b2002-11-02 19:27:56 +000060 void visitBranchInst(BranchInst &BI);
Chris Lattnere2954c82002-11-02 20:04:26 +000061
62 // Arithmetic operators
Chris Lattnerf01729e2002-11-02 20:54:46 +000063 void visitSimpleBinary(BinaryOperator &B, unsigned OpcodeClass);
Chris Lattner68aad932002-11-02 20:13:22 +000064 void visitAdd(BinaryOperator &B) { visitSimpleBinary(B, 0); }
65 void visitSub(BinaryOperator &B) { visitSimpleBinary(B, 1); }
Chris Lattnerca9671d2002-11-02 20:28:58 +000066 void visitMul(BinaryOperator &B);
Chris Lattnere2954c82002-11-02 20:04:26 +000067
Chris Lattnerf01729e2002-11-02 20:54:46 +000068 void visitDiv(BinaryOperator &B) { visitDivRem(B); }
69 void visitRem(BinaryOperator &B) { visitDivRem(B); }
70 void visitDivRem(BinaryOperator &B);
71
Chris Lattnere2954c82002-11-02 20:04:26 +000072 // Bitwise operators
Chris Lattner68aad932002-11-02 20:13:22 +000073 void visitAnd(BinaryOperator &B) { visitSimpleBinary(B, 2); }
74 void visitOr (BinaryOperator &B) { visitSimpleBinary(B, 3); }
75 void visitXor(BinaryOperator &B) { visitSimpleBinary(B, 4); }
Chris Lattnere2954c82002-11-02 20:04:26 +000076
77 // Binary comparison operators
78
79 // Other operators
Brian Gaekea1719c92002-10-31 23:03:59 +000080 void visitShiftInst(ShiftInst &I);
Brian Gaeke1749d632002-11-07 17:59:21 +000081 void visitSetCondInst(SetCondInst &I);
Chris Lattnere2954c82002-11-02 20:04:26 +000082 void visitPHINode(PHINode &I);
Chris Lattner72614082002-10-25 22:55:53 +000083
84 void visitInstruction(Instruction &I) {
85 std::cerr << "Cannot instruction select: " << I;
86 abort();
87 }
88
Chris Lattnerc5291f52002-10-27 21:16:59 +000089
90 /// copyConstantToRegister - Output the instructions required to put the
91 /// specified constant into the specified register.
92 ///
93 void copyConstantToRegister(Constant *C, unsigned Reg);
94
Chris Lattner72614082002-10-25 22:55:53 +000095 /// getReg - This method turns an LLVM value into a register number. This
96 /// is guaranteed to produce the same register number for a particular value
97 /// every time it is queried.
98 ///
99 unsigned getReg(Value &V) { return getReg(&V); } // Allow references
100 unsigned getReg(Value *V) {
101 unsigned &Reg = RegMap[V];
102 if (Reg == 0)
103 Reg = CurReg++;
104
Chris Lattner6f8fd252002-10-27 21:23:43 +0000105 // If this operand is a constant, emit the code to copy the constant into
106 // the register here...
107 //
Chris Lattnerc5291f52002-10-27 21:16:59 +0000108 if (Constant *C = dyn_cast<Constant>(V))
109 copyConstantToRegister(C, Reg);
110
Chris Lattner72614082002-10-25 22:55:53 +0000111 return Reg;
112 }
Chris Lattner72614082002-10-25 22:55:53 +0000113 };
114}
115
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000116/// getClass - Turn a primitive type into a "class" number which is based on the
117/// size of the type, and whether or not it is floating point.
118///
119static inline unsigned getClass(const Type *Ty) {
120 switch (Ty->getPrimitiveID()) {
121 case Type::SByteTyID:
122 case Type::UByteTyID: return 0; // Byte operands are class #0
123 case Type::ShortTyID:
124 case Type::UShortTyID: return 1; // Short operands are class #1
125 case Type::IntTyID:
126 case Type::UIntTyID:
127 case Type::PointerTyID: return 2; // Int's and pointers are class #2
128
129 case Type::LongTyID:
130 case Type::ULongTyID: return 3; // Longs are class #3
131 case Type::FloatTyID: return 4; // Float is class #4
132 case Type::DoubleTyID: return 5; // Doubles are class #5
133 default:
134 assert(0 && "Invalid type to getClass!");
135 return 0; // not reached
136 }
137}
Chris Lattnerc5291f52002-10-27 21:16:59 +0000138
139/// copyConstantToRegister - Output the instructions required to put the
140/// specified constant into the specified register.
141///
142void ISel::copyConstantToRegister(Constant *C, unsigned R) {
143 assert (!isa<ConstantExpr>(C) && "Constant expressions not yet handled!\n");
144
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000145 if (C->getType()->isIntegral()) {
146 unsigned Class = getClass(C->getType());
147 assert(Class != 3 && "Type not handled yet!");
148
149 static const unsigned IntegralOpcodeTab[] = {
150 X86::MOVir8, X86::MOVir16, X86::MOVir32
151 };
152
153 if (C->getType()->isSigned()) {
154 ConstantSInt *CSI = cast<ConstantSInt>(C);
155 BuildMI(BB, IntegralOpcodeTab[Class], 1, R).addSImm(CSI->getValue());
156 } else {
157 ConstantUInt *CUI = cast<ConstantUInt>(C);
158 BuildMI(BB, IntegralOpcodeTab[Class], 1, R).addZImm(CUI->getValue());
159 }
160 } else {
161 assert(0 && "Type not handled yet!");
Chris Lattnerc5291f52002-10-27 21:16:59 +0000162 }
163}
164
Brian Gaeke1749d632002-11-07 17:59:21 +0000165/// SetCC instructions - Here we just emit boilerplate code to set a byte-sized
166/// register, then move it to wherever the result should be.
167/// We handle FP setcc instructions by pushing them, doing a
168/// compare-and-pop-twice, and then copying the concodes to the main
169/// processor's concodes (I didn't make this up, it's in the Intel manual)
170///
171void
172ISel::visitSetCondInst (SetCondInst & I)
173{
174 // The arguments are already supposed to be of the same type.
175 Value *var1 = I.getOperand (0);
176 Value *var2 = I.getOperand (1);
177 unsigned reg1 = getReg (var1);
178 unsigned reg2 = getReg (var2);
179 unsigned resultReg = getReg (I);
180 unsigned comparisonWidth = var1->getType ()->getPrimitiveSize ();
181 unsigned unsignedComparison = var1->getType ()->isUnsigned ();
182 unsigned resultWidth = I.getType ()->getPrimitiveSize ();
183 bool fpComparison = var1->getType ()->isFloatingPoint ();
184 if (fpComparison)
185 {
186 // Push the variables on the stack with fldl opcodes.
187 // FIXME: assuming var1, var2 are in memory, if not, spill to
188 // stack first
189 switch (comparisonWidth)
190 {
191 case 4:
192 BuildMI (BB, X86::FLDr4, 1, X86::NoReg).addReg (reg1);
193 break;
194 case 8:
195 BuildMI (BB, X86::FLDr8, 1, X86::NoReg).addReg (reg1);
196 break;
197 default:
198 visitInstruction (I);
199 break;
200 }
201 switch (comparisonWidth)
202 {
203 case 4:
204 BuildMI (BB, X86::FLDr4, 1, X86::NoReg).addReg (reg2);
205 break;
206 case 8:
207 BuildMI (BB, X86::FLDr8, 1, X86::NoReg).addReg (reg2);
208 break;
209 default:
210 visitInstruction (I);
211 break;
212 }
213 // (Non-trapping) compare and pop twice.
214 // FIXME: Result of comparison -> condition codes, not a register.
215 BuildMI (BB, X86::FUCOMPP, 0);
216 // Move fp status word (concodes) to ax.
217 BuildMI (BB, X86::FNSTSWr8, 1, X86::AX);
218 // Load real concodes from ax.
219 // FIXME: Once again, flags are not modeled.
220 BuildMI (BB, X86::SAHF, 0);
221 }
222 else
223 { // integer comparison
224 // Emit: cmp <var1>, <var2> (do the comparison). We can
225 // compare 8-bit with 8-bit, 16-bit with 16-bit, 32-bit with
226 // 32-bit.
227 // FIXME: Result of comparison -> condition codes, not a register.
228 switch (comparisonWidth)
229 {
230 case 1:
231 BuildMI (BB, X86::CMPrr8, 2,
232 X86::NoReg).addReg (reg1).addReg (reg2);
233 break;
234 case 2:
235 BuildMI (BB, X86::CMPrr16, 2,
236 X86::NoReg).addReg (reg1).addReg (reg2);
237 break;
238 case 4:
239 BuildMI (BB, X86::CMPrr32, 2,
240 X86::NoReg).addReg (reg1).addReg (reg2);
241 break;
242 case 8:
243 default:
244 visitInstruction (I);
245 break;
246 }
247 }
248 // Emit setOp instruction (extract concode; clobbers ax),
249 // using the following mapping:
250 // LLVM -> X86 signed X86 unsigned
251 // ----- ----- -----
252 // seteq -> sete sete
253 // setne -> setne setne
254 // setlt -> setl setb
255 // setgt -> setg seta
256 // setle -> setle setbe
257 // setge -> setge setae
258 switch (I.getOpcode ())
259 {
260 case Instruction::SetEQ:
261 BuildMI (BB, X86::SETE, 0, X86::AL);
262 break;
263 case Instruction::SetGE:
264 if (unsignedComparison)
265 BuildMI (BB, X86::SETAE, 0, X86::AL);
266 else
267 BuildMI (BB, X86::SETGE, 0, X86::AL);
268 break;
269 case Instruction::SetGT:
270 if (unsignedComparison)
271 BuildMI (BB, X86::SETA, 0, X86::AL);
272 else
273 BuildMI (BB, X86::SETG, 0, X86::AL);
274 break;
275 case Instruction::SetLE:
276 if (unsignedComparison)
277 BuildMI (BB, X86::SETBE, 0, X86::AL);
278 else
279 BuildMI (BB, X86::SETLE, 0, X86::AL);
280 break;
281 case Instruction::SetLT:
282 if (unsignedComparison)
283 BuildMI (BB, X86::SETB, 0, X86::AL);
284 else
285 BuildMI (BB, X86::SETL, 0, X86::AL);
286 break;
287 case Instruction::SetNE:
288 BuildMI (BB, X86::SETNE, 0, X86::AL);
289 break;
290 default:
291 visitInstruction (I);
292 break;
293 }
294 // Put it in the result using a move.
295 switch (resultWidth)
296 {
297 case 1:
298 BuildMI (BB, X86::MOVrr8, 1, resultReg).addReg (X86::AL);
299 break;
300 // FIXME: What to do about implicit destination registers?
301 // E.g., you don't specify it, but CBW is more like AX = CBW(AL).
302 case 2:
303 BuildMI (BB, X86::CBW, 0, X86::AX);
304 BuildMI (BB, X86::MOVrr16, 1, resultReg).addReg (X86::AX);
305 break;
306 case 4:
307 BuildMI (BB, X86::CWDE, 0, X86::EAX);
308 BuildMI (BB, X86::MOVrr32, 1, resultReg).addReg (X86::EAX);
309 break;
310 case 8:
311 default:
312 visitInstruction (I);
313 break;
314 }
315}
Chris Lattner51b49a92002-11-02 19:45:49 +0000316
Chris Lattnerc5291f52002-10-27 21:16:59 +0000317
Chris Lattner72614082002-10-25 22:55:53 +0000318/// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such,
319/// we have the following possibilities:
320///
321/// ret void: No return value, simply emit a 'ret' instruction
322/// ret sbyte, ubyte : Extend value into EAX and return
323/// ret short, ushort: Extend value into EAX and return
324/// ret int, uint : Move value into EAX and return
325/// ret pointer : Move value into EAX and return
326/// ret long, ulong : Move value into EAX/EDX (?) and return
327/// ret float/double : ? Top of FP stack? XMM0?
328///
Brian Gaeke20abb6b2002-11-11 19:37:09 +0000329void
330ISel::visitReturnInst (ReturnInst & I)
331{
332 if (I.getNumOperands () == 1)
333 {
334 unsigned val = getReg (I.getOperand (0));
335 unsigned operandSize =
336 I.getOperand (0)->getType ()->getPrimitiveSize ();
337 bool isFP = I.getOperand (0)->getType ()->isFloatingPoint ();
338 if (isFP)
339 {
340 // ret float/double: top of FP stack
341 // FLD <val>
342 switch (operandSize)
343 {
344 case 4:
345 BuildMI (BB, X86::FLDr4, 1, X86::NoReg).addReg (val);
346 break;
347 case 8:
348 BuildMI (BB, X86::FLDr8, 1, X86::NoReg).addReg (val);
349 break;
350 default:
351 visitInstruction (I);
352 break;
353 }
354 }
355 else
356 {
357 switch (operandSize)
358 {
359 case 1:
360 // ret sbyte, ubyte: Extend value into EAX and return
361 // MOV AL, <val>
362 // CBW
363 BuildMI (BB, X86::MOVrr8, 1, X86::AL).addReg (val);
364 BuildMI (BB, X86::CBW, 0);
365 break;
366 case 2:
367 // ret short, ushort: Extend value into EAX and return
368 // MOV AX, <val>
369 // CWDE
370 BuildMI (BB, X86::MOVrr16, 1, X86::AX).addReg (val);
371 BuildMI (BB, X86::CWDE, 0);
372 break;
373 case 4:
374 // ret int, uint, ptr: Move value into EAX and return
375 // MOV EAX, <val>
376 BuildMI (BB, X86::MOVrr32, 1, X86::EAX).addReg (val);
377 break;
378 case 8:
379 // ret long: use EAX(least significant 32 bits)/EDX (most
380 // significant 32)...uh, I think so Brain, but how do i call
381 // up the two parts of the value from inside this mouse
382 // cage? *zort*
383 default:
384 // abort
385 visitInstruction (I);
386 break;
387 }
388 }
389 }
390 // Emit a 'leave' and a 'ret'
391 BuildMI (BB, X86::LEAVE, 0);
392 BuildMI (BB, X86::RET, 0);
Chris Lattner72614082002-10-25 22:55:53 +0000393}
394
Chris Lattner51b49a92002-11-02 19:45:49 +0000395/// visitBranchInst - Handle conditional and unconditional branches here. Note
396/// that since code layout is frozen at this point, that if we are trying to
397/// jump to a block that is the immediate successor of the current block, we can
398/// just make a fall-through. (but we don't currently).
399///
Chris Lattner2df035b2002-11-02 19:27:56 +0000400void ISel::visitBranchInst(BranchInst &BI) {
401 if (BI.isConditional()) // Only handles unconditional branches so far...
402 visitInstruction(BI);
403
404 BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(0));
405}
406
407
Chris Lattner68aad932002-11-02 20:13:22 +0000408/// visitSimpleBinary - Implement simple binary operators for integral types...
409/// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or,
410/// 4 for Xor.
411///
412void ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) {
413 if (B.getType() == Type::BoolTy) // FIXME: Handle bools for logicals
Chris Lattnere2954c82002-11-02 20:04:26 +0000414 visitInstruction(B);
415
416 unsigned Class = getClass(B.getType());
417 if (Class > 2) // FIXME: Handle longs
418 visitInstruction(B);
419
420 static const unsigned OpcodeTab[][4] = {
Chris Lattner68aad932002-11-02 20:13:22 +0000421 // Arithmetic operators
422 { X86::ADDrr8, X86::ADDrr16, X86::ADDrr32, 0 }, // ADD
423 { X86::SUBrr8, X86::SUBrr16, X86::SUBrr32, 0 }, // SUB
424
425 // Bitwise operators
Chris Lattnere2954c82002-11-02 20:04:26 +0000426 { X86::ANDrr8, X86::ANDrr16, X86::ANDrr32, 0 }, // AND
427 { X86:: ORrr8, X86:: ORrr16, X86:: ORrr32, 0 }, // OR
428 { X86::XORrr8, X86::XORrr16, X86::XORrr32, 0 }, // XOR
429 };
430
431 unsigned Opcode = OpcodeTab[OperatorClass][Class];
432 unsigned Op0r = getReg(B.getOperand(0));
433 unsigned Op1r = getReg(B.getOperand(1));
434 BuildMI(BB, Opcode, 2, getReg(B)).addReg(Op0r).addReg(Op1r);
435}
436
Chris Lattnerca9671d2002-11-02 20:28:58 +0000437/// visitMul - Multiplies are not simple binary operators because they must deal
438/// with the EAX register explicitly.
439///
440void ISel::visitMul(BinaryOperator &I) {
441 unsigned Class = getClass(I.getType());
442 if (Class > 2) // FIXME: Handle longs
443 visitInstruction(I);
Chris Lattnere2954c82002-11-02 20:04:26 +0000444
Chris Lattnerca9671d2002-11-02 20:28:58 +0000445 static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX };
446 static const unsigned MulOpcode[]={ X86::MULrr8, X86::MULrr16, X86::MULrr32 };
447 static const unsigned MovOpcode[]={ X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 };
448
449 unsigned Reg = Regs[Class];
450 unsigned Op0Reg = getReg(I.getOperand(1));
451 unsigned Op1Reg = getReg(I.getOperand(1));
452
453 // Put the first operand into one of the A registers...
454 BuildMI(BB, MovOpcode[Class], 1, Reg).addReg(Op0Reg);
455
456 // Emit the appropriate multiple instruction...
457 // FIXME: We need to mark that this modified AH, DX, or EDX also!!
458 BuildMI(BB, MulOpcode[Class], 2, Reg).addReg(Reg).addReg(Op1Reg);
459
460 // Put the result into the destination register...
461 BuildMI(BB, MovOpcode[Class], 1, getReg(I)).addReg(Reg);
Chris Lattnerf01729e2002-11-02 20:54:46 +0000462}
Chris Lattnerca9671d2002-11-02 20:28:58 +0000463
Chris Lattnerf01729e2002-11-02 20:54:46 +0000464/// visitDivRem - Handle division and remainder instructions... these
465/// instruction both require the same instructions to be generated, they just
466/// select the result from a different register. Note that both of these
467/// instructions work differently for signed and unsigned operands.
468///
469void ISel::visitDivRem(BinaryOperator &I) {
470 unsigned Class = getClass(I.getType());
471 if (Class > 2) // FIXME: Handle longs
472 visitInstruction(I);
473
474 static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX };
475 static const unsigned MovOpcode[]={ X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 };
476 static const unsigned ExtOpcode[]={ X86::CBW , X86::CWD , X86::CWQ };
477 static const unsigned ClrOpcode[]={ X86::XORrr8, X86::XORrr16, X86::XORrr32 };
478 static const unsigned ExtRegs[] ={ X86::AH , X86::DX , X86::EDX };
479
480 static const unsigned DivOpcode[][4] = {
481 { X86::DIVrr8 , X86::DIVrr16 , X86::DIVrr32 , 0 }, // Unsigned division
482 { X86::IDIVrr8, X86::IDIVrr16, X86::IDIVrr32, 0 }, // Signed division
483 };
484
485 bool isSigned = I.getType()->isSigned();
486 unsigned Reg = Regs[Class];
487 unsigned ExtReg = ExtRegs[Class];
488 unsigned Op0Reg = getReg(I.getOperand(1));
489 unsigned Op1Reg = getReg(I.getOperand(1));
490
491 // Put the first operand into one of the A registers...
492 BuildMI(BB, MovOpcode[Class], 1, Reg).addReg(Op0Reg);
493
494 if (isSigned) {
495 // Emit a sign extension instruction...
496 BuildMI(BB, ExtOpcode[Class], 1, ExtReg).addReg(Reg);
497 } else {
498 // If unsigned, emit a zeroing instruction... (reg = xor reg, reg)
499 BuildMI(BB, ClrOpcode[Class], 2, ExtReg).addReg(ExtReg).addReg(ExtReg);
500 }
501
502 // Figure out which register we want to pick the result out of...
503 unsigned DestReg = (I.getOpcode() == Instruction::Div) ? Reg : ExtReg;
504
505 // Emit the appropriate multiple instruction...
506 // FIXME: We need to mark that this modified AH, DX, or EDX also!!
507 BuildMI(BB,DivOpcode[isSigned][Class], 2, DestReg).addReg(Reg).addReg(Op1Reg);
508
509 // Put the result into the destination register...
510 BuildMI(BB, MovOpcode[Class], 1, getReg(I)).addReg(DestReg);
Chris Lattnerca9671d2002-11-02 20:28:58 +0000511}
Chris Lattnere2954c82002-11-02 20:04:26 +0000512
Brian Gaekea1719c92002-10-31 23:03:59 +0000513/// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here
514/// for constant immediate shift values, and for constant immediate
515/// shift values equal to 1. Even the general case is sort of special,
516/// because the shift amount has to be in CL, not just any old register.
517///
Chris Lattnerf01729e2002-11-02 20:54:46 +0000518void ISel::visitShiftInst (ShiftInst &I) {
519 unsigned Op0r = getReg (I.getOperand(0));
520 unsigned DestReg = getReg(I);
Chris Lattnere9913f22002-11-02 01:41:55 +0000521 bool isLeftShift = I.getOpcode() == Instruction::Shl;
522 bool isOperandSigned = I.getType()->isUnsigned();
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000523 unsigned OperandClass = getClass(I.getType());
524
525 if (OperandClass > 2)
526 visitInstruction(I); // Can't handle longs yet!
Chris Lattner796df732002-11-02 00:44:25 +0000527
Brian Gaekea1719c92002-10-31 23:03:59 +0000528 if (ConstantUInt *CUI = dyn_cast <ConstantUInt> (I.getOperand (1)))
529 {
Chris Lattner796df732002-11-02 00:44:25 +0000530 // The shift amount is constant, guaranteed to be a ubyte. Get its value.
531 assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?");
532 unsigned char shAmt = CUI->getValue();
533
Chris Lattnere9913f22002-11-02 01:41:55 +0000534 static const unsigned ConstantOperand[][4] = {
535 { X86::SHRir8, X86::SHRir16, X86::SHRir32, 0 }, // SHR
536 { X86::SARir8, X86::SARir16, X86::SARir32, 0 }, // SAR
537 { X86::SHLir8, X86::SHLir16, X86::SHLir32, 0 }, // SHL
538 { X86::SHLir8, X86::SHLir16, X86::SHLir32, 0 }, // SAL = SHL
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000539 };
540
Chris Lattnere9913f22002-11-02 01:41:55 +0000541 const unsigned *OpTab = // Figure out the operand table to use
542 ConstantOperand[isLeftShift*2+isOperandSigned];
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000543
Brian Gaekea1719c92002-10-31 23:03:59 +0000544 // Emit: <insn> reg, shamt (shift-by-immediate opcode "ir" form.)
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000545 BuildMI(BB, OpTab[OperandClass], 2, DestReg).addReg(Op0r).addZImm(shAmt);
Brian Gaekea1719c92002-10-31 23:03:59 +0000546 }
547 else
548 {
549 // The shift amount is non-constant.
550 //
551 // In fact, you can only shift with a variable shift amount if
552 // that amount is already in the CL register, so we have to put it
553 // there first.
554 //
Chris Lattnere9913f22002-11-02 01:41:55 +0000555
Brian Gaekea1719c92002-10-31 23:03:59 +0000556 // Emit: move cl, shiftAmount (put the shift amount in CL.)
Chris Lattnerca9671d2002-11-02 20:28:58 +0000557 BuildMI(BB, X86::MOVrr8, 1, X86::CL).addReg(getReg(I.getOperand(1)));
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000558
559 // This is a shift right (SHR).
Chris Lattnere9913f22002-11-02 01:41:55 +0000560 static const unsigned NonConstantOperand[][4] = {
561 { X86::SHRrr8, X86::SHRrr16, X86::SHRrr32, 0 }, // SHR
562 { X86::SARrr8, X86::SARrr16, X86::SARrr32, 0 }, // SAR
563 { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32, 0 }, // SHL
564 { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32, 0 }, // SAL = SHL
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000565 };
566
Chris Lattnere9913f22002-11-02 01:41:55 +0000567 const unsigned *OpTab = // Figure out the operand table to use
568 NonConstantOperand[isLeftShift*2+isOperandSigned];
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000569
Chris Lattnere9913f22002-11-02 01:41:55 +0000570 BuildMI(BB, OpTab[OperandClass], 2, DestReg).addReg(Op0r).addReg(X86::CL);
Brian Gaekea1719c92002-10-31 23:03:59 +0000571 }
572}
573
Chris Lattnere2954c82002-11-02 20:04:26 +0000574/// visitPHINode - Turn an LLVM PHI node into an X86 PHI node...
575///
576void ISel::visitPHINode(PHINode &PN) {
577 MachineInstr *MI = BuildMI(BB, X86::PHI, PN.getNumOperands(), getReg(PN));
Chris Lattner72614082002-10-25 22:55:53 +0000578
Chris Lattnere2954c82002-11-02 20:04:26 +0000579 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
580 // FIXME: This will put constants after the PHI nodes in the block, which
581 // is invalid. They should be put inline into the PHI node eventually.
582 //
583 MI->addRegOperand(getReg(PN.getIncomingValue(i)));
584 MI->addPCDispOperand(PN.getIncomingBlock(i));
585 }
Chris Lattner72614082002-10-25 22:55:53 +0000586}
587
Brian Gaekea1719c92002-10-31 23:03:59 +0000588
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000589/// createSimpleX86InstructionSelector - This pass converts an LLVM function
590/// into a machine code representation is a very simple peep-hole fashion. The
Chris Lattner72614082002-10-25 22:55:53 +0000591/// generated code sucks but the implementation is nice and simple.
592///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000593Pass *createSimpleX86InstructionSelector(TargetMachine &TM) {
594 return new ISel(TM);
Chris Lattner72614082002-10-25 22:55:53 +0000595}