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 | 51b49a9 | 2002-11-02 19:45:49 +0000 | [diff] [blame] | 12 | #include "llvm/iPHINode.h" |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 13 | #include "llvm/Type.h" |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 14 | #include "llvm/Constants.h" |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 15 | #include "llvm/Pass.h" |
Chris Lattner | 341a937 | 2002-10-29 17:43:55 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineFunction.h" |
| 17 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 18 | #include "llvm/Support/InstVisitor.h" |
| 19 | #include <map> |
| 20 | |
| 21 | namespace { |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 22 | struct ISel : public FunctionPass, InstVisitor<ISel> { |
| 23 | TargetMachine &TM; |
Chris Lattner | 341a937 | 2002-10-29 17:43:55 +0000 | [diff] [blame] | 24 | MachineFunction *F; // The function we are compiling into |
| 25 | MachineBasicBlock *BB; // The current MBB we are compiling |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 26 | |
| 27 | unsigned CurReg; |
| 28 | std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs |
| 29 | |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 30 | ISel(TargetMachine &tm) |
| 31 | : TM(tm), F(0), BB(0), CurReg(MRegisterInfo::FirstVirtualRegister) {} |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 32 | |
| 33 | /// runOnFunction - Top level implementation of instruction selection for |
| 34 | /// the entire function. |
| 35 | /// |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 36 | bool runOnFunction(Function &Fn) { |
Chris Lattner | 36b3603 | 2002-10-29 23:40:58 +0000 | [diff] [blame] | 37 | F = &MachineFunction::construct(&Fn, TM); |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 38 | visit(Fn); |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 39 | RegMap.clear(); |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 40 | F = 0; |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 41 | return false; // We never modify the LLVM itself. |
| 42 | } |
| 43 | |
| 44 | /// visitBasicBlock - This method is called when we are visiting a new basic |
Chris Lattner | 33f53b5 | 2002-10-29 20:48:56 +0000 | [diff] [blame] | 45 | /// block. This simply creates a new MachineBasicBlock to emit code into |
| 46 | /// and adds it to the current MachineFunction. Subsequent visit* for |
| 47 | /// instructions will be invoked for all instructions in the basic block. |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 48 | /// |
| 49 | void visitBasicBlock(BasicBlock &LLVM_BB) { |
Chris Lattner | 42c7786 | 2002-10-30 00:47:40 +0000 | [diff] [blame] | 50 | BB = new MachineBasicBlock(&LLVM_BB); |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 51 | // FIXME: Use the auto-insert form when it's available |
| 52 | F->getBasicBlockList().push_back(BB); |
| 53 | } |
| 54 | |
| 55 | // Visitation methods for various instructions. These methods simply emit |
| 56 | // fixed X86 code for each instruction. |
| 57 | // |
| 58 | void visitReturnInst(ReturnInst &RI); |
Chris Lattner | 2df035b | 2002-11-02 19:27:56 +0000 | [diff] [blame] | 59 | void visitBranchInst(BranchInst &BI); |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 60 | |
| 61 | // Arithmetic operators |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame^] | 62 | void visitSimpleBinary(BinaryOperator &B, unsigned OpcodeClass); |
Chris Lattner | 68aad93 | 2002-11-02 20:13:22 +0000 | [diff] [blame] | 63 | void visitAdd(BinaryOperator &B) { visitSimpleBinary(B, 0); } |
| 64 | void visitSub(BinaryOperator &B) { visitSimpleBinary(B, 1); } |
Chris Lattner | ca9671d | 2002-11-02 20:28:58 +0000 | [diff] [blame] | 65 | void visitMul(BinaryOperator &B); |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 66 | |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame^] | 67 | void visitDiv(BinaryOperator &B) { visitDivRem(B); } |
| 68 | void visitRem(BinaryOperator &B) { visitDivRem(B); } |
| 69 | void visitDivRem(BinaryOperator &B); |
| 70 | |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 71 | // Bitwise operators |
Chris Lattner | 68aad93 | 2002-11-02 20:13:22 +0000 | [diff] [blame] | 72 | void visitAnd(BinaryOperator &B) { visitSimpleBinary(B, 2); } |
| 73 | void visitOr (BinaryOperator &B) { visitSimpleBinary(B, 3); } |
| 74 | void visitXor(BinaryOperator &B) { visitSimpleBinary(B, 4); } |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 75 | |
| 76 | // Binary comparison operators |
| 77 | |
| 78 | // Other operators |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 79 | void visitShiftInst(ShiftInst &I); |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 80 | void visitPHINode(PHINode &I); |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 81 | |
| 82 | void visitInstruction(Instruction &I) { |
| 83 | std::cerr << "Cannot instruction select: " << I; |
| 84 | abort(); |
| 85 | } |
| 86 | |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 87 | |
| 88 | /// copyConstantToRegister - Output the instructions required to put the |
| 89 | /// specified constant into the specified register. |
| 90 | /// |
| 91 | void copyConstantToRegister(Constant *C, unsigned Reg); |
| 92 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 93 | /// getReg - This method turns an LLVM value into a register number. This |
| 94 | /// is guaranteed to produce the same register number for a particular value |
| 95 | /// every time it is queried. |
| 96 | /// |
| 97 | unsigned getReg(Value &V) { return getReg(&V); } // Allow references |
| 98 | unsigned getReg(Value *V) { |
| 99 | unsigned &Reg = RegMap[V]; |
| 100 | if (Reg == 0) |
| 101 | Reg = CurReg++; |
| 102 | |
Chris Lattner | 6f8fd25 | 2002-10-27 21:23:43 +0000 | [diff] [blame] | 103 | // If this operand is a constant, emit the code to copy the constant into |
| 104 | // the register here... |
| 105 | // |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 106 | if (Constant *C = dyn_cast<Constant>(V)) |
| 107 | copyConstantToRegister(C, Reg); |
| 108 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 109 | return Reg; |
| 110 | } |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 111 | }; |
| 112 | } |
| 113 | |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 114 | /// getClass - Turn a primitive type into a "class" number which is based on the |
| 115 | /// size of the type, and whether or not it is floating point. |
| 116 | /// |
| 117 | static inline unsigned getClass(const Type *Ty) { |
| 118 | switch (Ty->getPrimitiveID()) { |
| 119 | case Type::SByteTyID: |
| 120 | case Type::UByteTyID: return 0; // Byte operands are class #0 |
| 121 | case Type::ShortTyID: |
| 122 | case Type::UShortTyID: return 1; // Short operands are class #1 |
| 123 | case Type::IntTyID: |
| 124 | case Type::UIntTyID: |
| 125 | case Type::PointerTyID: return 2; // Int's and pointers are class #2 |
| 126 | |
| 127 | case Type::LongTyID: |
| 128 | case Type::ULongTyID: return 3; // Longs are class #3 |
| 129 | case Type::FloatTyID: return 4; // Float is class #4 |
| 130 | case Type::DoubleTyID: return 5; // Doubles are class #5 |
| 131 | default: |
| 132 | assert(0 && "Invalid type to getClass!"); |
| 133 | return 0; // not reached |
| 134 | } |
| 135 | } |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 136 | |
| 137 | /// copyConstantToRegister - Output the instructions required to put the |
| 138 | /// specified constant into the specified register. |
| 139 | /// |
| 140 | void ISel::copyConstantToRegister(Constant *C, unsigned R) { |
| 141 | assert (!isa<ConstantExpr>(C) && "Constant expressions not yet handled!\n"); |
| 142 | |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 143 | if (C->getType()->isIntegral()) { |
| 144 | unsigned Class = getClass(C->getType()); |
| 145 | assert(Class != 3 && "Type not handled yet!"); |
| 146 | |
| 147 | static const unsigned IntegralOpcodeTab[] = { |
| 148 | X86::MOVir8, X86::MOVir16, X86::MOVir32 |
| 149 | }; |
| 150 | |
| 151 | if (C->getType()->isSigned()) { |
| 152 | ConstantSInt *CSI = cast<ConstantSInt>(C); |
| 153 | BuildMI(BB, IntegralOpcodeTab[Class], 1, R).addSImm(CSI->getValue()); |
| 154 | } else { |
| 155 | ConstantUInt *CUI = cast<ConstantUInt>(C); |
| 156 | BuildMI(BB, IntegralOpcodeTab[Class], 1, R).addZImm(CUI->getValue()); |
| 157 | } |
| 158 | } else { |
| 159 | assert(0 && "Type not handled yet!"); |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
Chris Lattner | 51b49a9 | 2002-11-02 19:45:49 +0000 | [diff] [blame] | 163 | |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 164 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 165 | /// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such, |
| 166 | /// we have the following possibilities: |
| 167 | /// |
| 168 | /// ret void: No return value, simply emit a 'ret' instruction |
| 169 | /// ret sbyte, ubyte : Extend value into EAX and return |
| 170 | /// ret short, ushort: Extend value into EAX and return |
| 171 | /// ret int, uint : Move value into EAX and return |
| 172 | /// ret pointer : Move value into EAX and return |
| 173 | /// ret long, ulong : Move value into EAX/EDX (?) and return |
| 174 | /// ret float/double : ? Top of FP stack? XMM0? |
| 175 | /// |
| 176 | void ISel::visitReturnInst(ReturnInst &I) { |
| 177 | if (I.getNumOperands() != 0) { // Not 'ret void'? |
| 178 | // Move result into a hard register... then emit a ret |
| 179 | visitInstruction(I); // abort |
| 180 | } |
| 181 | |
| 182 | // Emit a simple 'ret' instruction... appending it to the end of the basic |
| 183 | // block |
Chris Lattner | 341a937 | 2002-10-29 17:43:55 +0000 | [diff] [blame] | 184 | BuildMI(BB, X86::RET, 0); |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Chris Lattner | 51b49a9 | 2002-11-02 19:45:49 +0000 | [diff] [blame] | 187 | /// visitBranchInst - Handle conditional and unconditional branches here. Note |
| 188 | /// that since code layout is frozen at this point, that if we are trying to |
| 189 | /// jump to a block that is the immediate successor of the current block, we can |
| 190 | /// just make a fall-through. (but we don't currently). |
| 191 | /// |
Chris Lattner | 2df035b | 2002-11-02 19:27:56 +0000 | [diff] [blame] | 192 | void ISel::visitBranchInst(BranchInst &BI) { |
| 193 | if (BI.isConditional()) // Only handles unconditional branches so far... |
| 194 | visitInstruction(BI); |
| 195 | |
| 196 | BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(0)); |
| 197 | } |
| 198 | |
| 199 | |
Chris Lattner | 68aad93 | 2002-11-02 20:13:22 +0000 | [diff] [blame] | 200 | /// visitSimpleBinary - Implement simple binary operators for integral types... |
| 201 | /// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or, |
| 202 | /// 4 for Xor. |
| 203 | /// |
| 204 | void ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) { |
| 205 | if (B.getType() == Type::BoolTy) // FIXME: Handle bools for logicals |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 206 | visitInstruction(B); |
| 207 | |
| 208 | unsigned Class = getClass(B.getType()); |
| 209 | if (Class > 2) // FIXME: Handle longs |
| 210 | visitInstruction(B); |
| 211 | |
| 212 | static const unsigned OpcodeTab[][4] = { |
Chris Lattner | 68aad93 | 2002-11-02 20:13:22 +0000 | [diff] [blame] | 213 | // Arithmetic operators |
| 214 | { X86::ADDrr8, X86::ADDrr16, X86::ADDrr32, 0 }, // ADD |
| 215 | { X86::SUBrr8, X86::SUBrr16, X86::SUBrr32, 0 }, // SUB |
| 216 | |
| 217 | // Bitwise operators |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 218 | { X86::ANDrr8, X86::ANDrr16, X86::ANDrr32, 0 }, // AND |
| 219 | { X86:: ORrr8, X86:: ORrr16, X86:: ORrr32, 0 }, // OR |
| 220 | { X86::XORrr8, X86::XORrr16, X86::XORrr32, 0 }, // XOR |
| 221 | }; |
| 222 | |
| 223 | unsigned Opcode = OpcodeTab[OperatorClass][Class]; |
| 224 | unsigned Op0r = getReg(B.getOperand(0)); |
| 225 | unsigned Op1r = getReg(B.getOperand(1)); |
| 226 | BuildMI(BB, Opcode, 2, getReg(B)).addReg(Op0r).addReg(Op1r); |
| 227 | } |
| 228 | |
Chris Lattner | ca9671d | 2002-11-02 20:28:58 +0000 | [diff] [blame] | 229 | /// visitMul - Multiplies are not simple binary operators because they must deal |
| 230 | /// with the EAX register explicitly. |
| 231 | /// |
| 232 | void ISel::visitMul(BinaryOperator &I) { |
| 233 | unsigned Class = getClass(I.getType()); |
| 234 | if (Class > 2) // FIXME: Handle longs |
| 235 | visitInstruction(I); |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 236 | |
Chris Lattner | ca9671d | 2002-11-02 20:28:58 +0000 | [diff] [blame] | 237 | static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX }; |
| 238 | static const unsigned MulOpcode[]={ X86::MULrr8, X86::MULrr16, X86::MULrr32 }; |
| 239 | static const unsigned MovOpcode[]={ X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 }; |
| 240 | |
| 241 | unsigned Reg = Regs[Class]; |
| 242 | unsigned Op0Reg = getReg(I.getOperand(1)); |
| 243 | unsigned Op1Reg = getReg(I.getOperand(1)); |
| 244 | |
| 245 | // Put the first operand into one of the A registers... |
| 246 | BuildMI(BB, MovOpcode[Class], 1, Reg).addReg(Op0Reg); |
| 247 | |
| 248 | // Emit the appropriate multiple instruction... |
| 249 | // FIXME: We need to mark that this modified AH, DX, or EDX also!! |
| 250 | BuildMI(BB, MulOpcode[Class], 2, Reg).addReg(Reg).addReg(Op1Reg); |
| 251 | |
| 252 | // Put the result into the destination register... |
| 253 | BuildMI(BB, MovOpcode[Class], 1, getReg(I)).addReg(Reg); |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame^] | 254 | } |
Chris Lattner | ca9671d | 2002-11-02 20:28:58 +0000 | [diff] [blame] | 255 | |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame^] | 256 | /// visitDivRem - Handle division and remainder instructions... these |
| 257 | /// instruction both require the same instructions to be generated, they just |
| 258 | /// select the result from a different register. Note that both of these |
| 259 | /// instructions work differently for signed and unsigned operands. |
| 260 | /// |
| 261 | void ISel::visitDivRem(BinaryOperator &I) { |
| 262 | unsigned Class = getClass(I.getType()); |
| 263 | if (Class > 2) // FIXME: Handle longs |
| 264 | visitInstruction(I); |
| 265 | |
| 266 | static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX }; |
| 267 | static const unsigned MovOpcode[]={ X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 }; |
| 268 | static const unsigned ExtOpcode[]={ X86::CBW , X86::CWD , X86::CWQ }; |
| 269 | static const unsigned ClrOpcode[]={ X86::XORrr8, X86::XORrr16, X86::XORrr32 }; |
| 270 | static const unsigned ExtRegs[] ={ X86::AH , X86::DX , X86::EDX }; |
| 271 | |
| 272 | static const unsigned DivOpcode[][4] = { |
| 273 | { X86::DIVrr8 , X86::DIVrr16 , X86::DIVrr32 , 0 }, // Unsigned division |
| 274 | { X86::IDIVrr8, X86::IDIVrr16, X86::IDIVrr32, 0 }, // Signed division |
| 275 | }; |
| 276 | |
| 277 | bool isSigned = I.getType()->isSigned(); |
| 278 | unsigned Reg = Regs[Class]; |
| 279 | unsigned ExtReg = ExtRegs[Class]; |
| 280 | unsigned Op0Reg = getReg(I.getOperand(1)); |
| 281 | unsigned Op1Reg = getReg(I.getOperand(1)); |
| 282 | |
| 283 | // Put the first operand into one of the A registers... |
| 284 | BuildMI(BB, MovOpcode[Class], 1, Reg).addReg(Op0Reg); |
| 285 | |
| 286 | if (isSigned) { |
| 287 | // Emit a sign extension instruction... |
| 288 | BuildMI(BB, ExtOpcode[Class], 1, ExtReg).addReg(Reg); |
| 289 | } else { |
| 290 | // If unsigned, emit a zeroing instruction... (reg = xor reg, reg) |
| 291 | BuildMI(BB, ClrOpcode[Class], 2, ExtReg).addReg(ExtReg).addReg(ExtReg); |
| 292 | } |
| 293 | |
| 294 | // Figure out which register we want to pick the result out of... |
| 295 | unsigned DestReg = (I.getOpcode() == Instruction::Div) ? Reg : ExtReg; |
| 296 | |
| 297 | // Emit the appropriate multiple instruction... |
| 298 | // FIXME: We need to mark that this modified AH, DX, or EDX also!! |
| 299 | BuildMI(BB,DivOpcode[isSigned][Class], 2, DestReg).addReg(Reg).addReg(Op1Reg); |
| 300 | |
| 301 | // Put the result into the destination register... |
| 302 | BuildMI(BB, MovOpcode[Class], 1, getReg(I)).addReg(DestReg); |
Chris Lattner | ca9671d | 2002-11-02 20:28:58 +0000 | [diff] [blame] | 303 | } |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 304 | |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 305 | /// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here |
| 306 | /// for constant immediate shift values, and for constant immediate |
| 307 | /// shift values equal to 1. Even the general case is sort of special, |
| 308 | /// because the shift amount has to be in CL, not just any old register. |
| 309 | /// |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame^] | 310 | void ISel::visitShiftInst (ShiftInst &I) { |
| 311 | unsigned Op0r = getReg (I.getOperand(0)); |
| 312 | unsigned DestReg = getReg(I); |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 313 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
| 314 | bool isOperandSigned = I.getType()->isUnsigned(); |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 315 | unsigned OperandClass = getClass(I.getType()); |
| 316 | |
| 317 | if (OperandClass > 2) |
| 318 | visitInstruction(I); // Can't handle longs yet! |
Chris Lattner | 796df73 | 2002-11-02 00:44:25 +0000 | [diff] [blame] | 319 | |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 320 | if (ConstantUInt *CUI = dyn_cast <ConstantUInt> (I.getOperand (1))) |
| 321 | { |
Chris Lattner | 796df73 | 2002-11-02 00:44:25 +0000 | [diff] [blame] | 322 | // The shift amount is constant, guaranteed to be a ubyte. Get its value. |
| 323 | assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?"); |
| 324 | unsigned char shAmt = CUI->getValue(); |
| 325 | |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 326 | static const unsigned ConstantOperand[][4] = { |
| 327 | { X86::SHRir8, X86::SHRir16, X86::SHRir32, 0 }, // SHR |
| 328 | { X86::SARir8, X86::SARir16, X86::SARir32, 0 }, // SAR |
| 329 | { X86::SHLir8, X86::SHLir16, X86::SHLir32, 0 }, // SHL |
| 330 | { X86::SHLir8, X86::SHLir16, X86::SHLir32, 0 }, // SAL = SHL |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 331 | }; |
| 332 | |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 333 | const unsigned *OpTab = // Figure out the operand table to use |
| 334 | ConstantOperand[isLeftShift*2+isOperandSigned]; |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 335 | |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 336 | // Emit: <insn> reg, shamt (shift-by-immediate opcode "ir" form.) |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 337 | BuildMI(BB, OpTab[OperandClass], 2, DestReg).addReg(Op0r).addZImm(shAmt); |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 338 | } |
| 339 | else |
| 340 | { |
| 341 | // The shift amount is non-constant. |
| 342 | // |
| 343 | // In fact, you can only shift with a variable shift amount if |
| 344 | // that amount is already in the CL register, so we have to put it |
| 345 | // there first. |
| 346 | // |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 347 | |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 348 | // Emit: move cl, shiftAmount (put the shift amount in CL.) |
Chris Lattner | ca9671d | 2002-11-02 20:28:58 +0000 | [diff] [blame] | 349 | BuildMI(BB, X86::MOVrr8, 1, X86::CL).addReg(getReg(I.getOperand(1))); |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 350 | |
| 351 | // This is a shift right (SHR). |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 352 | static const unsigned NonConstantOperand[][4] = { |
| 353 | { X86::SHRrr8, X86::SHRrr16, X86::SHRrr32, 0 }, // SHR |
| 354 | { X86::SARrr8, X86::SARrr16, X86::SARrr32, 0 }, // SAR |
| 355 | { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32, 0 }, // SHL |
| 356 | { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32, 0 }, // SAL = SHL |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 357 | }; |
| 358 | |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 359 | const unsigned *OpTab = // Figure out the operand table to use |
| 360 | NonConstantOperand[isLeftShift*2+isOperandSigned]; |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 361 | |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 362 | BuildMI(BB, OpTab[OperandClass], 2, DestReg).addReg(Op0r).addReg(X86::CL); |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 366 | /// visitPHINode - Turn an LLVM PHI node into an X86 PHI node... |
| 367 | /// |
| 368 | void ISel::visitPHINode(PHINode &PN) { |
| 369 | MachineInstr *MI = BuildMI(BB, X86::PHI, PN.getNumOperands(), getReg(PN)); |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 370 | |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 371 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 372 | // FIXME: This will put constants after the PHI nodes in the block, which |
| 373 | // is invalid. They should be put inline into the PHI node eventually. |
| 374 | // |
| 375 | MI->addRegOperand(getReg(PN.getIncomingValue(i))); |
| 376 | MI->addPCDispOperand(PN.getIncomingBlock(i)); |
| 377 | } |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 380 | |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 381 | /// createSimpleX86InstructionSelector - This pass converts an LLVM function |
| 382 | /// 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] | 383 | /// generated code sucks but the implementation is nice and simple. |
| 384 | /// |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 385 | Pass *createSimpleX86InstructionSelector(TargetMachine &TM) { |
| 386 | return new ISel(TM); |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 387 | } |