1 //===-- InstSelectSimple.cpp - A simple instruction selector for x86 ------===//
3 // This file defines a simple peephole instruction selector for the x86 platform
5 //===----------------------------------------------------------------------===//
8 #include "X86InstrInfo.h"
9 #include "X86InstrBuilder.h"
10 #include "llvm/Function.h"
11 #include "llvm/iTerminators.h"
12 #include "llvm/iOperators.h"
13 #include "llvm/iOther.h"
14 #include "llvm/iPHINode.h"
15 #include "llvm/iMemory.h"
16 #include "llvm/Type.h"
17 #include "llvm/Constants.h"
18 #include "llvm/Pass.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/Target/TargetMachine.h"
22 #include "llvm/Support/InstVisitor.h"
23 #include "llvm/Target/MRegisterInfo.h"
26 using namespace MOTy; // Get Use, Def, UseAndDef
29 struct ISel : public FunctionPass, InstVisitor<ISel> {
31 MachineFunction *F; // The function we are compiling into
32 MachineBasicBlock *BB; // The current MBB we are compiling
35 std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs
37 ISel(TargetMachine &tm)
38 : TM(tm), F(0), BB(0), CurReg(MRegisterInfo::FirstVirtualRegister) {}
40 /// runOnFunction - Top level implementation of instruction selection for
41 /// the entire function.
43 bool runOnFunction(Function &Fn) {
44 F = &MachineFunction::construct(&Fn, TM);
47 CurReg = MRegisterInfo::FirstVirtualRegister;
49 return false; // We never modify the LLVM itself.
52 /// visitBasicBlock - This method is called when we are visiting a new basic
53 /// 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.
57 void visitBasicBlock(BasicBlock &LLVM_BB) {
58 BB = new MachineBasicBlock(&LLVM_BB);
59 // FIXME: Use the auto-insert form when it's available
60 F->getBasicBlockList().push_back(BB);
63 // Visitation methods for various instructions. These methods simply emit
64 // fixed X86 code for each instruction.
67 // Control flow operators
68 void visitReturnInst(ReturnInst &RI);
69 void visitBranchInst(BranchInst &BI);
70 void visitCallInst(CallInst &I);
72 // Arithmetic operators
73 void visitSimpleBinary(BinaryOperator &B, unsigned OpcodeClass);
74 void visitAdd(BinaryOperator &B) { visitSimpleBinary(B, 0); }
75 void visitSub(BinaryOperator &B) { visitSimpleBinary(B, 1); }
76 void visitMul(BinaryOperator &B);
78 void visitDiv(BinaryOperator &B) { visitDivRem(B); }
79 void visitRem(BinaryOperator &B) { visitDivRem(B); }
80 void visitDivRem(BinaryOperator &B);
83 void visitAnd(BinaryOperator &B) { visitSimpleBinary(B, 2); }
84 void visitOr (BinaryOperator &B) { visitSimpleBinary(B, 3); }
85 void visitXor(BinaryOperator &B) { visitSimpleBinary(B, 4); }
87 // Binary comparison operators
88 void visitSetCCInst(SetCondInst &I, unsigned OpNum);
89 void visitSetEQ(SetCondInst &I) { visitSetCCInst(I, 0); }
90 void visitSetNE(SetCondInst &I) { visitSetCCInst(I, 1); }
91 void visitSetLT(SetCondInst &I) { visitSetCCInst(I, 2); }
92 void visitSetGT(SetCondInst &I) { visitSetCCInst(I, 3); }
93 void visitSetLE(SetCondInst &I) { visitSetCCInst(I, 4); }
94 void visitSetGE(SetCondInst &I) { visitSetCCInst(I, 5); }
96 // Memory Instructions
97 void visitLoadInst(LoadInst &I);
98 void visitStoreInst(StoreInst &I);
101 void visitShiftInst(ShiftInst &I);
102 void visitPHINode(PHINode &I);
103 void visitCastInst(CastInst &I);
105 void visitInstruction(Instruction &I) {
106 std::cerr << "Cannot instruction select: " << I;
110 void promote32 (const unsigned targetReg, Value *v);
112 /// copyConstantToRegister - Output the instructions required to put the
113 /// specified constant into the specified register.
115 void copyConstantToRegister(Constant *C, unsigned Reg);
117 /// getReg - This method turns an LLVM value into a register number. This
118 /// is guaranteed to produce the same register number for a particular value
119 /// every time it is queried.
121 unsigned getReg(Value &V) { return getReg(&V); } // Allow references
122 unsigned getReg(Value *V) {
123 unsigned &Reg = RegMap[V];
128 // Add the mapping of regnumber => reg class to MachineFunction
130 TM.getRegisterInfo()->getRegClassForType(V->getType()));
133 // If this operand is a constant, emit the code to copy the constant into
134 // the register here...
136 if (Constant *C = dyn_cast<Constant>(V)) {
137 copyConstantToRegister(C, Reg);
138 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
139 // Move the address of the global into the register
140 BuildMI(BB, X86::MOVir32, 1, Reg).addReg(GV);
141 } else if (Argument *A = dyn_cast<Argument>(V)) {
142 std::cerr << "ERROR: Arguments not implemented in SimpleInstSel\n";
150 /// TypeClass - Used by the X86 backend to group LLVM types by their basic X86
154 cByte, cShort, cInt, cLong, cFloat, cDouble
157 /// getClass - Turn a primitive type into a "class" number which is based on the
158 /// size of the type, and whether or not it is floating point.
160 static inline TypeClass getClass(const Type *Ty) {
161 switch (Ty->getPrimitiveID()) {
162 case Type::SByteTyID:
163 case Type::UByteTyID: return cByte; // Byte operands are class #0
164 case Type::ShortTyID:
165 case Type::UShortTyID: return cShort; // Short operands are class #1
168 case Type::PointerTyID: return cInt; // Int's and pointers are class #2
171 case Type::ULongTyID: return cLong; // Longs are class #3
172 case Type::FloatTyID: return cFloat; // Float is class #4
173 case Type::DoubleTyID: return cDouble; // Doubles are class #5
175 assert(0 && "Invalid type to getClass!");
176 return cByte; // not reached
181 /// copyConstantToRegister - Output the instructions required to put the
182 /// specified constant into the specified register.
184 void ISel::copyConstantToRegister(Constant *C, unsigned R) {
185 assert (!isa<ConstantExpr>(C) && "Constant expressions not yet handled!\n");
187 if (C->getType()->isIntegral()) {
188 unsigned Class = getClass(C->getType());
189 assert(Class != 3 && "Type not handled yet!");
191 static const unsigned IntegralOpcodeTab[] = {
192 X86::MOVir8, X86::MOVir16, X86::MOVir32
195 if (C->getType()->isSigned()) {
196 ConstantSInt *CSI = cast<ConstantSInt>(C);
197 BuildMI(BB, IntegralOpcodeTab[Class], 1, R).addSImm(CSI->getValue());
199 ConstantUInt *CUI = cast<ConstantUInt>(C);
200 BuildMI(BB, IntegralOpcodeTab[Class], 1, R).addZImm(CUI->getValue());
203 assert(0 && "Type not handled yet!");
208 /// SetCC instructions - Here we just emit boilerplate code to set a byte-sized
209 /// register, then move it to wherever the result should be.
210 /// We handle FP setcc instructions by pushing them, doing a
211 /// compare-and-pop-twice, and then copying the concodes to the main
212 /// processor's concodes (I didn't make this up, it's in the Intel manual)
214 void ISel::visitSetCCInst(SetCondInst &I, unsigned OpNum) {
215 // The arguments are already supposed to be of the same type.
216 const Type *CompTy = I.getOperand(0)->getType();
217 unsigned reg1 = getReg(I.getOperand(0));
218 unsigned reg2 = getReg(I.getOperand(1));
220 unsigned Class = getClass(CompTy);
222 // Emit: cmp <var1>, <var2> (do the comparison). We can
223 // compare 8-bit with 8-bit, 16-bit with 16-bit, 32-bit with
226 BuildMI (BB, X86::CMPrr8, 2).addReg (reg1).addReg (reg2);
229 BuildMI (BB, X86::CMPrr16, 2).addReg (reg1).addReg (reg2);
232 BuildMI (BB, X86::CMPrr32, 2).addReg (reg1).addReg (reg2);
235 // Push the variables on the stack with fldl opcodes.
236 // FIXME: assuming var1, var2 are in memory, if not, spill to
238 case cFloat: // Floats
239 BuildMI (BB, X86::FLDr4, 1).addReg (reg1);
240 BuildMI (BB, X86::FLDr4, 1).addReg (reg2);
242 case cDouble: // Doubles
243 BuildMI (BB, X86::FLDr8, 1).addReg (reg1);
244 BuildMI (BB, X86::FLDr8, 1).addReg (reg2);
251 if (CompTy->isFloatingPoint()) {
252 // (Non-trapping) compare and pop twice.
253 BuildMI (BB, X86::FUCOMPP, 0);
254 // Move fp status word (concodes) to ax.
255 BuildMI (BB, X86::FNSTSWr8, 1, X86::AX);
256 // Load real concodes from ax.
257 BuildMI (BB, X86::SAHF, 1).addReg(X86::AH);
260 // Emit setOp instruction (extract concode; clobbers ax),
261 // using the following mapping:
262 // LLVM -> X86 signed X86 unsigned
264 // seteq -> sete sete
265 // setne -> setne setne
266 // setlt -> setl setb
267 // setgt -> setg seta
268 // setle -> setle setbe
269 // setge -> setge setae
271 static const unsigned OpcodeTab[2][6] = {
272 {X86::SETEr, X86::SETNEr, X86::SETBr, X86::SETAr, X86::SETBEr, X86::SETAEr},
273 {X86::SETEr, X86::SETNEr, X86::SETLr, X86::SETGr, X86::SETLEr, X86::SETGEr},
276 BuildMI(BB, OpcodeTab[CompTy->isSigned()][OpNum], 0, X86::AL);
278 // Put it in the result using a move.
279 BuildMI (BB, X86::MOVrr8, 1, getReg(I)).addReg(X86::AL);
282 /// promote32 - Emit instructions to turn a narrow operand into a 32-bit-wide
283 /// operand, in the specified target register.
285 ISel::promote32 (const unsigned targetReg, Value *v)
287 unsigned vReg = getReg (v);
288 unsigned Class = getClass (v->getType ());
289 bool isUnsigned = v->getType ()->isUnsigned ();
290 assert (((Class == cByte) || (Class == cShort) || (Class == cInt))
291 && "Unpromotable operand class in promote32");
295 // Extend value into target register (8->32)
297 BuildMI (BB, X86::MOVZXr32r8, 1, targetReg).addReg (vReg);
299 BuildMI (BB, X86::MOVSXr32r8, 1, targetReg).addReg (vReg);
302 // Extend value into target register (16->32)
304 BuildMI (BB, X86::MOVZXr32r16, 1, targetReg).addReg (vReg);
306 BuildMI (BB, X86::MOVSXr32r16, 1, targetReg).addReg (vReg);
309 // Move value into target register (32->32)
310 BuildMI (BB, X86::MOVrr32, 1, targetReg).addReg (vReg);
315 /// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such,
316 /// we have the following possibilities:
318 /// ret void: No return value, simply emit a 'ret' instruction
319 /// ret sbyte, ubyte : Extend value into EAX and return
320 /// ret short, ushort: Extend value into EAX and return
321 /// ret int, uint : Move value into EAX and return
322 /// ret pointer : Move value into EAX and return
323 /// ret long, ulong : Move value into EAX/EDX and return
324 /// ret float/double : Top of FP stack
327 ISel::visitReturnInst (ReturnInst &I)
329 if (I.getNumOperands () == 0)
331 // Emit a 'ret' instruction
332 BuildMI (BB, X86::RET, 0);
335 Value *rv = I.getOperand (0);
336 unsigned Class = getClass (rv->getType ());
339 // integral return values: extend or move into EAX and return.
343 promote32 (X86::EAX, rv);
345 // ret float/double: top of FP stack
347 case cFloat: // Floats
348 BuildMI (BB, X86::FLDr4, 1).addReg (getReg (rv));
350 case cDouble: // Doubles
351 BuildMI (BB, X86::FLDr8, 1).addReg (getReg (rv));
354 // ret long: use EAX(least significant 32 bits)/EDX (most
355 // significant 32)...uh, I think so Brain, but how do i call
356 // up the two parts of the value from inside this mouse
359 visitInstruction (I);
361 // Emit a 'ret' instruction
362 BuildMI (BB, X86::RET, 0);
365 /// visitBranchInst - Handle conditional and unconditional branches here. Note
366 /// that since code layout is frozen at this point, that if we are trying to
367 /// jump to a block that is the immediate successor of the current block, we can
368 /// just make a fall-through. (but we don't currently).
371 ISel::visitBranchInst (BranchInst & BI)
373 if (BI.isConditional ())
375 BasicBlock *ifTrue = BI.getSuccessor (0);
376 BasicBlock *ifFalse = BI.getSuccessor (1); // this is really unobvious
378 // simplest thing I can think of: compare condition with zero,
379 // followed by jump-if-equal to ifFalse, and jump-if-nonequal to
381 unsigned int condReg = getReg (BI.getCondition ());
382 BuildMI (BB, X86::CMPri8, 2).addReg (condReg).addZImm (0);
383 BuildMI (BB, X86::JNE, 1).addPCDisp (BI.getSuccessor (0));
384 BuildMI (BB, X86::JE, 1).addPCDisp (BI.getSuccessor (1));
386 else // unconditional branch
388 BuildMI (BB, X86::JMP, 1).addPCDisp (BI.getSuccessor (0));
392 /// visitCallInst - Push args on stack and do a procedure call instruction.
394 ISel::visitCallInst (CallInst & CI)
396 // keep a counter of how many bytes we pushed on the stack
397 unsigned bytesPushed = 0;
399 // Push the arguments on the stack in reverse order, as specified by
401 for (unsigned i = CI.getNumOperands()-1; i >= 1; --i)
403 Value *v = CI.getOperand (i);
404 switch (getClass (v->getType ()))
408 // Promote V to 32 bits wide, and move the result into EAX,
410 promote32 (X86::EAX, v);
411 BuildMI (BB, X86::PUSHr32, 1).addReg (X86::EAX);
416 unsigned Reg = getReg(v);
417 BuildMI (BB, X86::PUSHr32, 1).addReg(Reg);
422 // FIXME: long/ulong/double args not handled.
423 visitInstruction (CI);
427 // Emit a CALL instruction with PC-relative displacement.
428 BuildMI (BB, X86::CALLpcrel32, 1).addPCDisp (CI.getCalledValue ());
430 // Adjust the stack by `bytesPushed' amount if non-zero
432 BuildMI (BB, X86::ADDri32, 2).addReg(X86::ESP).addZImm(bytesPushed);
434 // If there is a return value, scavenge the result from the location the call
437 if (CI.getType() != Type::VoidTy) {
438 switch (getClass(CI.getType())) {
440 BuildMI(BB, X86::MOVrr32, 1, getReg(CI)).addReg(X86::EAX);
444 std::cerr << "Cannot get return value for call of type '"
445 << *CI.getType() << "'\n";
446 visitInstruction(CI);
451 /// visitSimpleBinary - Implement simple binary operators for integral types...
452 /// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or,
455 void ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) {
456 if (B.getType() == Type::BoolTy) // FIXME: Handle bools for logicals
459 unsigned Class = getClass(B.getType());
460 if (Class > 2) // FIXME: Handle longs
463 static const unsigned OpcodeTab[][4] = {
464 // Arithmetic operators
465 { X86::ADDrr8, X86::ADDrr16, X86::ADDrr32, 0 }, // ADD
466 { X86::SUBrr8, X86::SUBrr16, X86::SUBrr32, 0 }, // SUB
469 { X86::ANDrr8, X86::ANDrr16, X86::ANDrr32, 0 }, // AND
470 { X86:: ORrr8, X86:: ORrr16, X86:: ORrr32, 0 }, // OR
471 { X86::XORrr8, X86::XORrr16, X86::XORrr32, 0 }, // XOR
474 unsigned Opcode = OpcodeTab[OperatorClass][Class];
475 unsigned Op0r = getReg(B.getOperand(0));
476 unsigned Op1r = getReg(B.getOperand(1));
477 BuildMI(BB, Opcode, 2, getReg(B)).addReg(Op0r).addReg(Op1r);
480 /// visitMul - Multiplies are not simple binary operators because they must deal
481 /// with the EAX register explicitly.
483 void ISel::visitMul(BinaryOperator &I) {
484 unsigned Class = getClass(I.getType());
485 if (Class > 2) // FIXME: Handle longs
488 static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX };
489 static const unsigned MulOpcode[]={ X86::MULrr8, X86::MULrr16, X86::MULrr32 };
490 static const unsigned MovOpcode[]={ X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 };
492 unsigned Reg = Regs[Class];
493 unsigned Op0Reg = getReg(I.getOperand(0));
494 unsigned Op1Reg = getReg(I.getOperand(1));
496 // Put the first operand into one of the A registers...
497 BuildMI(BB, MovOpcode[Class], 1, Reg).addReg(Op0Reg);
499 // Emit the appropriate multiply instruction...
500 BuildMI(BB, MulOpcode[Class], 1).addReg(Op1Reg);
502 // Put the result into the destination register...
503 BuildMI(BB, MovOpcode[Class], 1, getReg(I)).addReg(Reg);
507 /// visitDivRem - Handle division and remainder instructions... these
508 /// instruction both require the same instructions to be generated, they just
509 /// select the result from a different register. Note that both of these
510 /// instructions work differently for signed and unsigned operands.
512 void ISel::visitDivRem(BinaryOperator &I) {
513 unsigned Class = getClass(I.getType());
514 if (Class > 2) // FIXME: Handle longs
517 static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX };
518 static const unsigned MovOpcode[]={ X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 };
519 static const unsigned ExtOpcode[]={ X86::CBW , X86::CWD , X86::CDQ };
520 static const unsigned ClrOpcode[]={ X86::XORrr8, X86::XORrr16, X86::XORrr32 };
521 static const unsigned ExtRegs[] ={ X86::AH , X86::DX , X86::EDX };
523 static const unsigned DivOpcode[][4] = {
524 { X86::DIVrr8 , X86::DIVrr16 , X86::DIVrr32 , 0 }, // Unsigned division
525 { X86::IDIVrr8, X86::IDIVrr16, X86::IDIVrr32, 0 }, // Signed division
528 bool isSigned = I.getType()->isSigned();
529 unsigned Reg = Regs[Class];
530 unsigned ExtReg = ExtRegs[Class];
531 unsigned Op0Reg = getReg(I.getOperand(0));
532 unsigned Op1Reg = getReg(I.getOperand(1));
534 // Put the first operand into one of the A registers...
535 BuildMI(BB, MovOpcode[Class], 1, Reg).addReg(Op0Reg);
538 // Emit a sign extension instruction...
539 BuildMI(BB, ExtOpcode[Class], 0);
541 // If unsigned, emit a zeroing instruction... (reg = xor reg, reg)
542 BuildMI(BB, ClrOpcode[Class], 2, ExtReg).addReg(ExtReg).addReg(ExtReg);
545 // Emit the appropriate divide or remainder instruction...
546 BuildMI(BB, DivOpcode[isSigned][Class], 1).addReg(Op1Reg);
548 // Figure out which register we want to pick the result out of...
549 unsigned DestReg = (I.getOpcode() == Instruction::Div) ? Reg : ExtReg;
551 // Put the result into the destination register...
552 BuildMI(BB, MovOpcode[Class], 1, getReg(I)).addReg(DestReg);
556 /// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here
557 /// for constant immediate shift values, and for constant immediate
558 /// shift values equal to 1. Even the general case is sort of special,
559 /// because the shift amount has to be in CL, not just any old register.
561 void ISel::visitShiftInst (ShiftInst &I) {
562 unsigned Op0r = getReg (I.getOperand(0));
563 unsigned DestReg = getReg(I);
564 bool isLeftShift = I.getOpcode() == Instruction::Shl;
565 bool isOperandSigned = I.getType()->isUnsigned();
566 unsigned OperandClass = getClass(I.getType());
568 if (OperandClass > 2)
569 visitInstruction(I); // Can't handle longs yet!
571 if (ConstantUInt *CUI = dyn_cast <ConstantUInt> (I.getOperand (1)))
573 // The shift amount is constant, guaranteed to be a ubyte. Get its value.
574 assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?");
575 unsigned char shAmt = CUI->getValue();
577 static const unsigned ConstantOperand[][4] = {
578 { X86::SHRir8, X86::SHRir16, X86::SHRir32, 0 }, // SHR
579 { X86::SARir8, X86::SARir16, X86::SARir32, 0 }, // SAR
580 { X86::SHLir8, X86::SHLir16, X86::SHLir32, 0 }, // SHL
581 { X86::SHLir8, X86::SHLir16, X86::SHLir32, 0 }, // SAL = SHL
584 const unsigned *OpTab = // Figure out the operand table to use
585 ConstantOperand[isLeftShift*2+isOperandSigned];
587 // Emit: <insn> reg, shamt (shift-by-immediate opcode "ir" form.)
588 BuildMI(BB, OpTab[OperandClass], 2, DestReg).addReg(Op0r).addZImm(shAmt);
592 // The shift amount is non-constant.
594 // In fact, you can only shift with a variable shift amount if
595 // that amount is already in the CL register, so we have to put it
599 // Emit: move cl, shiftAmount (put the shift amount in CL.)
600 BuildMI(BB, X86::MOVrr8, 1, X86::CL).addReg(getReg(I.getOperand(1)));
602 // This is a shift right (SHR).
603 static const unsigned NonConstantOperand[][4] = {
604 { X86::SHRrr8, X86::SHRrr16, X86::SHRrr32, 0 }, // SHR
605 { X86::SARrr8, X86::SARrr16, X86::SARrr32, 0 }, // SAR
606 { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32, 0 }, // SHL
607 { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32, 0 }, // SAL = SHL
610 const unsigned *OpTab = // Figure out the operand table to use
611 NonConstantOperand[isLeftShift*2+isOperandSigned];
613 BuildMI(BB, OpTab[OperandClass], 1, DestReg).addReg(Op0r);
618 /// visitLoadInst - Implement LLVM load instructions in terms of the x86 'mov'
621 void ISel::visitLoadInst(LoadInst &I) {
622 unsigned Class = getClass(I.getType());
623 if (Class > 2) // FIXME: Handle longs and others...
626 static const unsigned Opcode[] = { X86::MOVmr8, X86::MOVmr16, X86::MOVmr32 };
628 unsigned AddressReg = getReg(I.getOperand(0));
629 addDirectMem(BuildMI(BB, Opcode[Class], 4, getReg(I)), AddressReg);
633 /// visitStoreInst - Implement LLVM store instructions in terms of the x86 'mov'
636 void ISel::visitStoreInst(StoreInst &I) {
637 unsigned Class = getClass(I.getOperand(0)->getType());
638 if (Class > 2) // FIXME: Handle longs and others...
641 static const unsigned Opcode[] = { X86::MOVrm8, X86::MOVrm16, X86::MOVrm32 };
643 unsigned ValReg = getReg(I.getOperand(0));
644 unsigned AddressReg = getReg(I.getOperand(1));
645 addDirectMem(BuildMI(BB, Opcode[Class], 1+4), AddressReg).addReg(ValReg);
649 /// visitPHINode - Turn an LLVM PHI node into an X86 PHI node...
651 void ISel::visitPHINode(PHINode &PN) {
652 MachineInstr *MI = BuildMI(BB, X86::PHI, PN.getNumOperands(), getReg(PN));
654 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
655 // FIXME: This will put constants after the PHI nodes in the block, which
656 // is invalid. They should be put inline into the PHI node eventually.
658 MI->addRegOperand(getReg(PN.getIncomingValue(i)));
659 MI->addPCDispOperand(PN.getIncomingBlock(i));
663 /// visitCastInst - Here we have various kinds of copying with or without
664 /// sign extension going on.
666 ISel::visitCastInst (CastInst &CI)
668 const Type *targetType = CI.getType ();
669 Value *operand = CI.getOperand (0);
670 unsigned int operandReg = getReg (operand);
671 const Type *sourceType = operand->getType ();
672 unsigned int destReg = getReg (CI);
674 // Currently we handle:
678 // 2) cast {sbyte, ubyte} to {sbyte, ubyte}
679 // cast {short, ushort} to {ushort, short}
680 // cast {int, uint, ptr} to {int, uint, ptr}
682 // 3) cast {sbyte, ubyte} to {ushort, short}
683 // cast {sbyte, ubyte} to {int, uint, ptr}
684 // cast {short, ushort} to {int, uint, ptr}
686 // 4) cast {int, uint, ptr} to {short, ushort}
687 // cast {int, uint, ptr} to {sbyte, ubyte}
688 // cast {short, ushort} to {sbyte, ubyte}
690 // 1) Implement casts to bool by using compare on the operand followed
691 // by set if not zero on the result.
692 if (targetType == Type::BoolTy)
694 BuildMI (BB, X86::CMPri8, 2).addReg (operandReg).addZImm (0);
695 BuildMI (BB, X86::SETNEr, 1, destReg);
698 // 2) Implement casts between values of the same type class (as determined
699 // by getClass) by using a register-to-register move.
700 unsigned int srcClass = getClass (sourceType);
701 unsigned int targClass = getClass (targetType);
702 static const unsigned regRegMove[] = {
703 X86::MOVrr8, X86::MOVrr16, X86::MOVrr32
705 if ((srcClass < 3) && (targClass < 3) && (srcClass == targClass))
707 BuildMI (BB, regRegMove[srcClass], 1, destReg).addReg (operandReg);
710 // 3) Handle cast of SMALLER int to LARGER int using a move with sign
711 // extension or zero extension, depending on whether the source type
713 if ((srcClass < 3) && (targClass < 3) && (srcClass < targClass))
715 static const unsigned ops[] = {
716 X86::MOVSXr16r8, X86::MOVSXr32r8, X86::MOVSXr32r16,
717 X86::MOVZXr16r8, X86::MOVZXr32r8, X86::MOVZXr32r16
719 unsigned srcSigned = sourceType->isSigned ();
720 BuildMI (BB, ops[3 * srcSigned + srcClass + targClass - 1], 1,
721 destReg).addReg (operandReg);
724 // 4) Handle cast of LARGER int to SMALLER int using a move to EAX
725 // followed by a move out of AX or AL.
726 if ((srcClass < 3) && (targClass < 3) && (srcClass > targClass))
728 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX };
729 BuildMI (BB, regRegMove[srcClass], 1,
730 AReg[srcClass]).addReg (operandReg);
731 BuildMI (BB, regRegMove[targClass], 1, destReg).addReg (AReg[srcClass]);
734 // Anything we haven't handled already, we can't (yet) handle at all.
735 visitInstruction (CI);
738 /// createSimpleX86InstructionSelector - This pass converts an LLVM function
739 /// into a machine code representation is a very simple peep-hole fashion. The
740 /// generated code sucks but the implementation is nice and simple.
742 Pass *createSimpleX86InstructionSelector(TargetMachine &TM) {