Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 1 | //===-- 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 Lattner | 055c965 | 2002-10-29 21:05:24 +0000 | [diff] [blame] | 8 | #include "X86InstrInfo.h" |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 9 | #include "llvm/Function.h" |
| 10 | #include "llvm/iTerminators.h" |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 11 | #include "llvm/iOther.h" |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 12 | #include "llvm/Type.h" |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 13 | #include "llvm/Constants.h" |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 14 | #include "llvm/Pass.h" |
Chris Lattner | 341a937 | 2002-10-29 17:43:55 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/MachineFunction.h" |
| 16 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 17 | #include "llvm/Support/InstVisitor.h" |
| 18 | #include <map> |
| 19 | |
| 20 | namespace { |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 21 | struct ISel : public FunctionPass, InstVisitor<ISel> { |
| 22 | TargetMachine &TM; |
Chris Lattner | 341a937 | 2002-10-29 17:43:55 +0000 | [diff] [blame] | 23 | MachineFunction *F; // The function we are compiling into |
| 24 | MachineBasicBlock *BB; // The current MBB we are compiling |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 25 | |
| 26 | unsigned CurReg; |
| 27 | std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs |
| 28 | |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 29 | ISel(TargetMachine &tm) |
| 30 | : TM(tm), F(0), BB(0), CurReg(MRegisterInfo::FirstVirtualRegister) {} |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 31 | |
| 32 | /// runOnFunction - Top level implementation of instruction selection for |
| 33 | /// the entire function. |
| 34 | /// |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 35 | bool runOnFunction(Function &Fn) { |
Chris Lattner | 36b3603 | 2002-10-29 23:40:58 +0000 | [diff] [blame] | 36 | F = &MachineFunction::construct(&Fn, TM); |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 37 | visit(Fn); |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 38 | RegMap.clear(); |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 39 | F = 0; |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 40 | return false; // We never modify the LLVM itself. |
| 41 | } |
| 42 | |
| 43 | /// visitBasicBlock - This method is called when we are visiting a new basic |
Chris Lattner | 33f53b5 | 2002-10-29 20:48:56 +0000 | [diff] [blame] | 44 | /// block. This simply creates a new MachineBasicBlock to emit code into |
| 45 | /// and adds it to the current MachineFunction. Subsequent visit* for |
| 46 | /// instructions will be invoked for all instructions in the basic block. |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 47 | /// |
| 48 | void visitBasicBlock(BasicBlock &LLVM_BB) { |
Chris Lattner | 42c7786 | 2002-10-30 00:47:40 +0000 | [diff] [blame] | 49 | BB = new MachineBasicBlock(&LLVM_BB); |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 50 | // FIXME: Use the auto-insert form when it's available |
| 51 | F->getBasicBlockList().push_back(BB); |
| 52 | } |
| 53 | |
| 54 | // Visitation methods for various instructions. These methods simply emit |
| 55 | // fixed X86 code for each instruction. |
| 56 | // |
| 57 | void visitReturnInst(ReturnInst &RI); |
| 58 | void visitAdd(BinaryOperator &B); |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 59 | void visitShiftInst(ShiftInst &I); |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 60 | |
| 61 | void visitInstruction(Instruction &I) { |
| 62 | std::cerr << "Cannot instruction select: " << I; |
| 63 | abort(); |
| 64 | } |
| 65 | |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 66 | |
| 67 | /// copyConstantToRegister - Output the instructions required to put the |
| 68 | /// specified constant into the specified register. |
| 69 | /// |
| 70 | void copyConstantToRegister(Constant *C, unsigned Reg); |
| 71 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 72 | /// getReg - This method turns an LLVM value into a register number. This |
| 73 | /// is guaranteed to produce the same register number for a particular value |
| 74 | /// every time it is queried. |
| 75 | /// |
| 76 | unsigned getReg(Value &V) { return getReg(&V); } // Allow references |
| 77 | unsigned getReg(Value *V) { |
| 78 | unsigned &Reg = RegMap[V]; |
| 79 | if (Reg == 0) |
| 80 | Reg = CurReg++; |
| 81 | |
Chris Lattner | 6f8fd25 | 2002-10-27 21:23:43 +0000 | [diff] [blame] | 82 | // If this operand is a constant, emit the code to copy the constant into |
| 83 | // the register here... |
| 84 | // |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 85 | if (Constant *C = dyn_cast<Constant>(V)) |
| 86 | copyConstantToRegister(C, Reg); |
| 87 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 88 | return Reg; |
| 89 | } |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 90 | }; |
| 91 | } |
| 92 | |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 93 | /// getClass - Turn a primitive type into a "class" number which is based on the |
| 94 | /// size of the type, and whether or not it is floating point. |
| 95 | /// |
| 96 | static inline unsigned getClass(const Type *Ty) { |
| 97 | switch (Ty->getPrimitiveID()) { |
| 98 | case Type::SByteTyID: |
| 99 | case Type::UByteTyID: return 0; // Byte operands are class #0 |
| 100 | case Type::ShortTyID: |
| 101 | case Type::UShortTyID: return 1; // Short operands are class #1 |
| 102 | case Type::IntTyID: |
| 103 | case Type::UIntTyID: |
| 104 | case Type::PointerTyID: return 2; // Int's and pointers are class #2 |
| 105 | |
| 106 | case Type::LongTyID: |
| 107 | case Type::ULongTyID: return 3; // Longs are class #3 |
| 108 | case Type::FloatTyID: return 4; // Float is class #4 |
| 109 | case Type::DoubleTyID: return 5; // Doubles are class #5 |
| 110 | default: |
| 111 | assert(0 && "Invalid type to getClass!"); |
| 112 | return 0; // not reached |
| 113 | } |
| 114 | } |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 115 | |
| 116 | /// copyConstantToRegister - Output the instructions required to put the |
| 117 | /// specified constant into the specified register. |
| 118 | /// |
| 119 | void ISel::copyConstantToRegister(Constant *C, unsigned R) { |
| 120 | assert (!isa<ConstantExpr>(C) && "Constant expressions not yet handled!\n"); |
| 121 | |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 122 | if (C->getType()->isIntegral()) { |
| 123 | unsigned Class = getClass(C->getType()); |
| 124 | assert(Class != 3 && "Type not handled yet!"); |
| 125 | |
| 126 | static const unsigned IntegralOpcodeTab[] = { |
| 127 | X86::MOVir8, X86::MOVir16, X86::MOVir32 |
| 128 | }; |
| 129 | |
| 130 | if (C->getType()->isSigned()) { |
| 131 | ConstantSInt *CSI = cast<ConstantSInt>(C); |
| 132 | BuildMI(BB, IntegralOpcodeTab[Class], 1, R).addSImm(CSI->getValue()); |
| 133 | } else { |
| 134 | ConstantUInt *CUI = cast<ConstantUInt>(C); |
| 135 | BuildMI(BB, IntegralOpcodeTab[Class], 1, R).addZImm(CUI->getValue()); |
| 136 | } |
| 137 | } else { |
| 138 | assert(0 && "Type not handled yet!"); |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 143 | /// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such, |
| 144 | /// we have the following possibilities: |
| 145 | /// |
| 146 | /// ret void: No return value, simply emit a 'ret' instruction |
| 147 | /// ret sbyte, ubyte : Extend value into EAX and return |
| 148 | /// ret short, ushort: Extend value into EAX and return |
| 149 | /// ret int, uint : Move value into EAX and return |
| 150 | /// ret pointer : Move value into EAX and return |
| 151 | /// ret long, ulong : Move value into EAX/EDX (?) and return |
| 152 | /// ret float/double : ? Top of FP stack? XMM0? |
| 153 | /// |
| 154 | void ISel::visitReturnInst(ReturnInst &I) { |
| 155 | if (I.getNumOperands() != 0) { // Not 'ret void'? |
| 156 | // Move result into a hard register... then emit a ret |
| 157 | visitInstruction(I); // abort |
| 158 | } |
| 159 | |
| 160 | // Emit a simple 'ret' instruction... appending it to the end of the basic |
| 161 | // block |
Chris Lattner | 341a937 | 2002-10-29 17:43:55 +0000 | [diff] [blame] | 162 | BuildMI(BB, X86::RET, 0); |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 165 | /// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here |
| 166 | /// for constant immediate shift values, and for constant immediate |
| 167 | /// shift values equal to 1. Even the general case is sort of special, |
| 168 | /// because the shift amount has to be in CL, not just any old register. |
| 169 | /// |
| 170 | void |
| 171 | ISel::visitShiftInst (ShiftInst & I) |
| 172 | { |
| 173 | unsigned Op0r = getReg (I.getOperand (0)); |
| 174 | unsigned DestReg = getReg (I); |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 175 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
| 176 | bool isOperandSigned = I.getType()->isUnsigned(); |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 177 | unsigned OperandClass = getClass(I.getType()); |
| 178 | |
| 179 | if (OperandClass > 2) |
| 180 | visitInstruction(I); // Can't handle longs yet! |
Chris Lattner | 796df73 | 2002-11-02 00:44:25 +0000 | [diff] [blame] | 181 | |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 182 | if (ConstantUInt *CUI = dyn_cast <ConstantUInt> (I.getOperand (1))) |
| 183 | { |
Chris Lattner | 796df73 | 2002-11-02 00:44:25 +0000 | [diff] [blame] | 184 | // The shift amount is constant, guaranteed to be a ubyte. Get its value. |
| 185 | assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?"); |
| 186 | unsigned char shAmt = CUI->getValue(); |
| 187 | |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 188 | static const unsigned ConstantOperand[][4] = { |
| 189 | { X86::SHRir8, X86::SHRir16, X86::SHRir32, 0 }, // SHR |
| 190 | { X86::SARir8, X86::SARir16, X86::SARir32, 0 }, // SAR |
| 191 | { X86::SHLir8, X86::SHLir16, X86::SHLir32, 0 }, // SHL |
| 192 | { X86::SHLir8, X86::SHLir16, X86::SHLir32, 0 }, // SAL = SHL |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 193 | }; |
| 194 | |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 195 | const unsigned *OpTab = // Figure out the operand table to use |
| 196 | ConstantOperand[isLeftShift*2+isOperandSigned]; |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 197 | |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 198 | // Emit: <insn> reg, shamt (shift-by-immediate opcode "ir" form.) |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 199 | BuildMI(BB, OpTab[OperandClass], 2, DestReg).addReg(Op0r).addZImm(shAmt); |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 200 | } |
| 201 | else |
| 202 | { |
| 203 | // The shift amount is non-constant. |
| 204 | // |
| 205 | // In fact, you can only shift with a variable shift amount if |
| 206 | // that amount is already in the CL register, so we have to put it |
| 207 | // there first. |
| 208 | // |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 209 | |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 210 | // Emit: move cl, shiftAmount (put the shift amount in CL.) |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 211 | BuildMI (BB, X86::MOVrr8, 2, X86::CL).addReg(getReg(I.getOperand(1))); |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 212 | |
| 213 | // This is a shift right (SHR). |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 214 | static const unsigned NonConstantOperand[][4] = { |
| 215 | { X86::SHRrr8, X86::SHRrr16, X86::SHRrr32, 0 }, // SHR |
| 216 | { X86::SARrr8, X86::SARrr16, X86::SARrr32, 0 }, // SAR |
| 217 | { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32, 0 }, // SHL |
| 218 | { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32, 0 }, // SAL = SHL |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 219 | }; |
| 220 | |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 221 | const unsigned *OpTab = // Figure out the operand table to use |
| 222 | NonConstantOperand[isLeftShift*2+isOperandSigned]; |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 223 | |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 224 | BuildMI(BB, OpTab[OperandClass], 2, DestReg).addReg(Op0r).addReg(X86::CL); |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 228 | |
| 229 | /// 'add' instruction - Simply turn this into an x86 reg,reg add instruction. |
| 230 | void ISel::visitAdd(BinaryOperator &B) { |
| 231 | unsigned Op0r = getReg(B.getOperand(0)), Op1r = getReg(B.getOperand(1)); |
| 232 | unsigned DestReg = getReg(B); |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 233 | unsigned Class = getClass(B.getType()); |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 234 | |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 235 | static const unsigned Opcodes[] = { X86::ADDrr8, X86::ADDrr16, X86::ADDrr32 }; |
| 236 | |
| 237 | if (Class >= sizeof(Opcodes)/sizeof(Opcodes[0])) |
| 238 | visitInstruction(B); // Not handled class yet... |
| 239 | |
| 240 | BuildMI(BB, Opcodes[Class], 2, DestReg).addReg(Op0r).addReg(Op1r); |
| 241 | |
| 242 | // For Longs: Here we have a pair of operands each occupying a pair of |
| 243 | // registers. We need to do an ADDrr32 of the least-significant pair |
| 244 | // immediately followed by an ADCrr32 (Add with Carry) of the most-significant |
| 245 | // pair. I don't know how we are representing these multi-register arguments. |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 248 | |
| 249 | |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 250 | /// createSimpleX86InstructionSelector - This pass converts an LLVM function |
| 251 | /// into a machine code representation is a very simple peep-hole fashion. The |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 252 | /// generated code sucks but the implementation is nice and simple. |
| 253 | /// |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 254 | Pass *createSimpleX86InstructionSelector(TargetMachine &TM) { |
| 255 | return new ISel(TM); |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 256 | } |