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 | 1749d63 | 2002-11-07 17:59:21 +0000 | [diff] [blame] | 11 | #include "llvm/iOperators.h" |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 12 | #include "llvm/iOther.h" |
Chris Lattner | 51b49a9 | 2002-11-02 19:45:49 +0000 | [diff] [blame] | 13 | #include "llvm/iPHINode.h" |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 14 | #include "llvm/Type.h" |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 15 | #include "llvm/Constants.h" |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 16 | #include "llvm/Pass.h" |
Chris Lattner | 341a937 | 2002-10-29 17:43:55 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineFunction.h" |
| 18 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 19 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 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); |
Brian Gaeke | 1749d63 | 2002-11-07 17:59:21 +0000 | [diff] [blame] | 80 | void visitSetCondInst(SetCondInst &I); |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 81 | void visitPHINode(PHINode &I); |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 82 | |
| 83 | void visitInstruction(Instruction &I) { |
| 84 | std::cerr << "Cannot instruction select: " << I; |
| 85 | abort(); |
| 86 | } |
| 87 | |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 88 | |
| 89 | /// copyConstantToRegister - Output the instructions required to put the |
| 90 | /// specified constant into the specified register. |
| 91 | /// |
| 92 | void copyConstantToRegister(Constant *C, unsigned Reg); |
| 93 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 94 | /// getReg - This method turns an LLVM value into a register number. This |
| 95 | /// is guaranteed to produce the same register number for a particular value |
| 96 | /// every time it is queried. |
| 97 | /// |
| 98 | unsigned getReg(Value &V) { return getReg(&V); } // Allow references |
| 99 | unsigned getReg(Value *V) { |
| 100 | unsigned &Reg = RegMap[V]; |
| 101 | if (Reg == 0) |
| 102 | Reg = CurReg++; |
| 103 | |
Chris Lattner | 6f8fd25 | 2002-10-27 21:23:43 +0000 | [diff] [blame] | 104 | // If this operand is a constant, emit the code to copy the constant into |
| 105 | // the register here... |
| 106 | // |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 107 | if (Constant *C = dyn_cast<Constant>(V)) |
| 108 | copyConstantToRegister(C, Reg); |
| 109 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 110 | return Reg; |
| 111 | } |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 112 | }; |
| 113 | } |
| 114 | |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame^] | 115 | /// TypeClass - Used by the X86 backend to group LLVM types by their basic X86 |
| 116 | /// Representation. |
| 117 | /// |
| 118 | enum TypeClass { |
| 119 | cByte, cShort, cInt, cLong, cFloat, cDouble |
| 120 | }; |
| 121 | |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 122 | /// getClass - Turn a primitive type into a "class" number which is based on the |
| 123 | /// size of the type, and whether or not it is floating point. |
| 124 | /// |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame^] | 125 | static inline TypeClass getClass(const Type *Ty) { |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 126 | switch (Ty->getPrimitiveID()) { |
| 127 | case Type::SByteTyID: |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame^] | 128 | case Type::UByteTyID: return cByte; // Byte operands are class #0 |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 129 | case Type::ShortTyID: |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame^] | 130 | case Type::UShortTyID: return cShort; // Short operands are class #1 |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 131 | case Type::IntTyID: |
| 132 | case Type::UIntTyID: |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame^] | 133 | case Type::PointerTyID: return cInt; // Int's and pointers are class #2 |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 134 | |
| 135 | case Type::LongTyID: |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame^] | 136 | case Type::ULongTyID: return cLong; // Longs are class #3 |
| 137 | case Type::FloatTyID: return cFloat; // Float is class #4 |
| 138 | case Type::DoubleTyID: return cDouble; // Doubles are class #5 |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 139 | default: |
| 140 | assert(0 && "Invalid type to getClass!"); |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame^] | 141 | return cByte; // not reached |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 144 | |
| 145 | /// copyConstantToRegister - Output the instructions required to put the |
| 146 | /// specified constant into the specified register. |
| 147 | /// |
| 148 | void ISel::copyConstantToRegister(Constant *C, unsigned R) { |
| 149 | assert (!isa<ConstantExpr>(C) && "Constant expressions not yet handled!\n"); |
| 150 | |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 151 | if (C->getType()->isIntegral()) { |
| 152 | unsigned Class = getClass(C->getType()); |
| 153 | assert(Class != 3 && "Type not handled yet!"); |
| 154 | |
| 155 | static const unsigned IntegralOpcodeTab[] = { |
| 156 | X86::MOVir8, X86::MOVir16, X86::MOVir32 |
| 157 | }; |
| 158 | |
| 159 | if (C->getType()->isSigned()) { |
| 160 | ConstantSInt *CSI = cast<ConstantSInt>(C); |
| 161 | BuildMI(BB, IntegralOpcodeTab[Class], 1, R).addSImm(CSI->getValue()); |
| 162 | } else { |
| 163 | ConstantUInt *CUI = cast<ConstantUInt>(C); |
| 164 | BuildMI(BB, IntegralOpcodeTab[Class], 1, R).addZImm(CUI->getValue()); |
| 165 | } |
| 166 | } else { |
| 167 | assert(0 && "Type not handled yet!"); |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
Brian Gaeke | 1749d63 | 2002-11-07 17:59:21 +0000 | [diff] [blame] | 171 | /// SetCC instructions - Here we just emit boilerplate code to set a byte-sized |
| 172 | /// register, then move it to wherever the result should be. |
| 173 | /// We handle FP setcc instructions by pushing them, doing a |
| 174 | /// compare-and-pop-twice, and then copying the concodes to the main |
| 175 | /// processor's concodes (I didn't make this up, it's in the Intel manual) |
| 176 | /// |
| 177 | void |
| 178 | ISel::visitSetCondInst (SetCondInst & I) |
| 179 | { |
| 180 | // The arguments are already supposed to be of the same type. |
| 181 | Value *var1 = I.getOperand (0); |
| 182 | Value *var2 = I.getOperand (1); |
| 183 | unsigned reg1 = getReg (var1); |
| 184 | unsigned reg2 = getReg (var2); |
| 185 | unsigned resultReg = getReg (I); |
| 186 | unsigned comparisonWidth = var1->getType ()->getPrimitiveSize (); |
| 187 | unsigned unsignedComparison = var1->getType ()->isUnsigned (); |
| 188 | unsigned resultWidth = I.getType ()->getPrimitiveSize (); |
| 189 | bool fpComparison = var1->getType ()->isFloatingPoint (); |
| 190 | if (fpComparison) |
| 191 | { |
| 192 | // Push the variables on the stack with fldl opcodes. |
| 193 | // FIXME: assuming var1, var2 are in memory, if not, spill to |
| 194 | // stack first |
| 195 | switch (comparisonWidth) |
| 196 | { |
| 197 | case 4: |
| 198 | BuildMI (BB, X86::FLDr4, 1, X86::NoReg).addReg (reg1); |
| 199 | break; |
| 200 | case 8: |
| 201 | BuildMI (BB, X86::FLDr8, 1, X86::NoReg).addReg (reg1); |
| 202 | break; |
| 203 | default: |
| 204 | visitInstruction (I); |
| 205 | break; |
| 206 | } |
| 207 | switch (comparisonWidth) |
| 208 | { |
| 209 | case 4: |
| 210 | BuildMI (BB, X86::FLDr4, 1, X86::NoReg).addReg (reg2); |
| 211 | break; |
| 212 | case 8: |
| 213 | BuildMI (BB, X86::FLDr8, 1, X86::NoReg).addReg (reg2); |
| 214 | break; |
| 215 | default: |
| 216 | visitInstruction (I); |
| 217 | break; |
| 218 | } |
| 219 | // (Non-trapping) compare and pop twice. |
Brian Gaeke | 1749d63 | 2002-11-07 17:59:21 +0000 | [diff] [blame] | 220 | BuildMI (BB, X86::FUCOMPP, 0); |
| 221 | // Move fp status word (concodes) to ax. |
| 222 | BuildMI (BB, X86::FNSTSWr8, 1, X86::AX); |
| 223 | // Load real concodes from ax. |
Brian Gaeke | 6559bb9 | 2002-11-14 22:32:30 +0000 | [diff] [blame] | 224 | BuildMI (BB, X86::SAHF, 1, X86::EFLAGS).addReg(X86::AH); |
Brian Gaeke | 1749d63 | 2002-11-07 17:59:21 +0000 | [diff] [blame] | 225 | } |
| 226 | else |
| 227 | { // integer comparison |
| 228 | // Emit: cmp <var1>, <var2> (do the comparison). We can |
| 229 | // compare 8-bit with 8-bit, 16-bit with 16-bit, 32-bit with |
| 230 | // 32-bit. |
Brian Gaeke | 1749d63 | 2002-11-07 17:59:21 +0000 | [diff] [blame] | 231 | switch (comparisonWidth) |
| 232 | { |
| 233 | case 1: |
| 234 | BuildMI (BB, X86::CMPrr8, 2, |
Brian Gaeke | 6559bb9 | 2002-11-14 22:32:30 +0000 | [diff] [blame] | 235 | X86::EFLAGS).addReg (reg1).addReg (reg2); |
Brian Gaeke | 1749d63 | 2002-11-07 17:59:21 +0000 | [diff] [blame] | 236 | break; |
| 237 | case 2: |
| 238 | BuildMI (BB, X86::CMPrr16, 2, |
Brian Gaeke | 6559bb9 | 2002-11-14 22:32:30 +0000 | [diff] [blame] | 239 | X86::EFLAGS).addReg (reg1).addReg (reg2); |
Brian Gaeke | 1749d63 | 2002-11-07 17:59:21 +0000 | [diff] [blame] | 240 | break; |
| 241 | case 4: |
| 242 | BuildMI (BB, X86::CMPrr32, 2, |
Brian Gaeke | 6559bb9 | 2002-11-14 22:32:30 +0000 | [diff] [blame] | 243 | X86::EFLAGS).addReg (reg1).addReg (reg2); |
Brian Gaeke | 1749d63 | 2002-11-07 17:59:21 +0000 | [diff] [blame] | 244 | break; |
| 245 | case 8: |
| 246 | default: |
| 247 | visitInstruction (I); |
| 248 | break; |
| 249 | } |
| 250 | } |
| 251 | // Emit setOp instruction (extract concode; clobbers ax), |
| 252 | // using the following mapping: |
| 253 | // LLVM -> X86 signed X86 unsigned |
| 254 | // ----- ----- ----- |
| 255 | // seteq -> sete sete |
| 256 | // setne -> setne setne |
| 257 | // setlt -> setl setb |
| 258 | // setgt -> setg seta |
| 259 | // setle -> setle setbe |
| 260 | // setge -> setge setae |
| 261 | switch (I.getOpcode ()) |
| 262 | { |
| 263 | case Instruction::SetEQ: |
| 264 | BuildMI (BB, X86::SETE, 0, X86::AL); |
| 265 | break; |
| 266 | case Instruction::SetGE: |
| 267 | if (unsignedComparison) |
| 268 | BuildMI (BB, X86::SETAE, 0, X86::AL); |
| 269 | else |
| 270 | BuildMI (BB, X86::SETGE, 0, X86::AL); |
| 271 | break; |
| 272 | case Instruction::SetGT: |
| 273 | if (unsignedComparison) |
| 274 | BuildMI (BB, X86::SETA, 0, X86::AL); |
| 275 | else |
| 276 | BuildMI (BB, X86::SETG, 0, X86::AL); |
| 277 | break; |
| 278 | case Instruction::SetLE: |
| 279 | if (unsignedComparison) |
| 280 | BuildMI (BB, X86::SETBE, 0, X86::AL); |
| 281 | else |
| 282 | BuildMI (BB, X86::SETLE, 0, X86::AL); |
| 283 | break; |
| 284 | case Instruction::SetLT: |
| 285 | if (unsignedComparison) |
| 286 | BuildMI (BB, X86::SETB, 0, X86::AL); |
| 287 | else |
| 288 | BuildMI (BB, X86::SETL, 0, X86::AL); |
| 289 | break; |
| 290 | case Instruction::SetNE: |
| 291 | BuildMI (BB, X86::SETNE, 0, X86::AL); |
| 292 | break; |
| 293 | default: |
| 294 | visitInstruction (I); |
| 295 | break; |
| 296 | } |
| 297 | // Put it in the result using a move. |
| 298 | switch (resultWidth) |
| 299 | { |
| 300 | case 1: |
| 301 | BuildMI (BB, X86::MOVrr8, 1, resultReg).addReg (X86::AL); |
| 302 | break; |
Brian Gaeke | 1749d63 | 2002-11-07 17:59:21 +0000 | [diff] [blame] | 303 | case 2: |
Brian Gaeke | 6559bb9 | 2002-11-14 22:32:30 +0000 | [diff] [blame] | 304 | BuildMI (BB, X86::MOVZXr16r8, 1, resultReg).addReg (X86::AL); |
Brian Gaeke | 1749d63 | 2002-11-07 17:59:21 +0000 | [diff] [blame] | 305 | break; |
| 306 | case 4: |
Brian Gaeke | 6559bb9 | 2002-11-14 22:32:30 +0000 | [diff] [blame] | 307 | BuildMI (BB, X86::MOVZXr32r8, 1, resultReg).addReg (X86::AL); |
Brian Gaeke | 1749d63 | 2002-11-07 17:59:21 +0000 | [diff] [blame] | 308 | break; |
| 309 | case 8: |
| 310 | default: |
| 311 | visitInstruction (I); |
| 312 | break; |
| 313 | } |
| 314 | } |
Chris Lattner | 51b49a9 | 2002-11-02 19:45:49 +0000 | [diff] [blame] | 315 | |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 316 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 317 | /// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such, |
| 318 | /// we have the following possibilities: |
| 319 | /// |
| 320 | /// ret void: No return value, simply emit a 'ret' instruction |
| 321 | /// ret sbyte, ubyte : Extend value into EAX and return |
| 322 | /// ret short, ushort: Extend value into EAX and return |
| 323 | /// ret int, uint : Move value into EAX and return |
| 324 | /// ret pointer : Move value into EAX and return |
| 325 | /// ret long, ulong : Move value into EAX/EDX (?) and return |
| 326 | /// ret float/double : ? Top of FP stack? XMM0? |
| 327 | /// |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame^] | 328 | void ISel::visitReturnInst (ReturnInst & I) { |
| 329 | if (I.getNumOperands() == 0) { |
| 330 | // Emit a 'ret' instruction |
| 331 | BuildMI(BB, X86::RET, 0); |
| 332 | return; |
| 333 | } |
| 334 | |
| 335 | unsigned val = getReg(I.getOperand(0)); |
| 336 | unsigned Class = getClass(I.getType()); |
| 337 | bool isUnsigned = I.getOperand(0)->getType()->isUnsigned(); |
| 338 | switch (Class) { |
| 339 | case cByte: |
| 340 | // ret sbyte, ubyte: Extend value into EAX and return |
| 341 | if (isUnsigned) { |
| 342 | BuildMI (BB, X86::MOVZXr32r8, 1, X86::EAX).addReg (val); |
| 343 | } else { |
| 344 | BuildMI (BB, X86::MOVSXr32r8, 1, X86::EAX).addReg (val); |
Brian Gaeke | 20abb6b | 2002-11-11 19:37:09 +0000 | [diff] [blame] | 345 | } |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame^] | 346 | break; |
| 347 | case cShort: |
| 348 | // ret short, ushort: Extend value into EAX and return |
| 349 | if (unsignedReturnValue) { |
| 350 | BuildMI (BB, X86::MOVZXr32r16, 1, X86::EAX).addReg (val); |
| 351 | } else { |
| 352 | BuildMI (BB, X86::MOVSXr32r16, 1, X86::EAX).addReg (val); |
| 353 | } |
| 354 | break; |
| 355 | case cInt: |
| 356 | // ret int, uint, ptr: Move value into EAX and return |
| 357 | // MOV EAX, <val> |
| 358 | BuildMI(BB, X86::MOVrr32, 1, X86::EAX).addReg(val); |
| 359 | break; |
| 360 | |
| 361 | // ret float/double: top of FP stack |
| 362 | // FLD <val> |
| 363 | case cFloat: // Floats |
| 364 | BuildMI(BB, X86::FLDr4, 1).addReg(val); |
| 365 | break; |
| 366 | case cDouble: // Doubles |
| 367 | BuildMI(BB, X86::FLDr8, 1).addReg(val); |
| 368 | break; |
| 369 | case cLong: |
| 370 | // ret long: use EAX(least significant 32 bits)/EDX (most |
| 371 | // significant 32)...uh, I think so Brain, but how do i call |
| 372 | // up the two parts of the value from inside this mouse |
| 373 | // cage? *zort* |
| 374 | default: |
| 375 | visitInstruction(I); |
| 376 | } |
| 377 | |
| 378 | // Emit a 'ret' instruction |
| 379 | BuildMI(BB, X86::RET, 0); |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Chris Lattner | 51b49a9 | 2002-11-02 19:45:49 +0000 | [diff] [blame] | 382 | /// visitBranchInst - Handle conditional and unconditional branches here. Note |
| 383 | /// that since code layout is frozen at this point, that if we are trying to |
| 384 | /// jump to a block that is the immediate successor of the current block, we can |
| 385 | /// just make a fall-through. (but we don't currently). |
| 386 | /// |
Chris Lattner | 2df035b | 2002-11-02 19:27:56 +0000 | [diff] [blame] | 387 | void ISel::visitBranchInst(BranchInst &BI) { |
| 388 | if (BI.isConditional()) // Only handles unconditional branches so far... |
| 389 | visitInstruction(BI); |
| 390 | |
| 391 | BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(0)); |
| 392 | } |
| 393 | |
| 394 | |
Chris Lattner | 68aad93 | 2002-11-02 20:13:22 +0000 | [diff] [blame] | 395 | /// visitSimpleBinary - Implement simple binary operators for integral types... |
| 396 | /// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or, |
| 397 | /// 4 for Xor. |
| 398 | /// |
| 399 | void ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) { |
| 400 | if (B.getType() == Type::BoolTy) // FIXME: Handle bools for logicals |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 401 | visitInstruction(B); |
| 402 | |
| 403 | unsigned Class = getClass(B.getType()); |
| 404 | if (Class > 2) // FIXME: Handle longs |
| 405 | visitInstruction(B); |
| 406 | |
| 407 | static const unsigned OpcodeTab[][4] = { |
Chris Lattner | 68aad93 | 2002-11-02 20:13:22 +0000 | [diff] [blame] | 408 | // Arithmetic operators |
| 409 | { X86::ADDrr8, X86::ADDrr16, X86::ADDrr32, 0 }, // ADD |
| 410 | { X86::SUBrr8, X86::SUBrr16, X86::SUBrr32, 0 }, // SUB |
| 411 | |
| 412 | // Bitwise operators |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 413 | { X86::ANDrr8, X86::ANDrr16, X86::ANDrr32, 0 }, // AND |
| 414 | { X86:: ORrr8, X86:: ORrr16, X86:: ORrr32, 0 }, // OR |
| 415 | { X86::XORrr8, X86::XORrr16, X86::XORrr32, 0 }, // XOR |
| 416 | }; |
| 417 | |
| 418 | unsigned Opcode = OpcodeTab[OperatorClass][Class]; |
| 419 | unsigned Op0r = getReg(B.getOperand(0)); |
| 420 | unsigned Op1r = getReg(B.getOperand(1)); |
| 421 | BuildMI(BB, Opcode, 2, getReg(B)).addReg(Op0r).addReg(Op1r); |
| 422 | } |
| 423 | |
Chris Lattner | ca9671d | 2002-11-02 20:28:58 +0000 | [diff] [blame] | 424 | /// visitMul - Multiplies are not simple binary operators because they must deal |
| 425 | /// with the EAX register explicitly. |
| 426 | /// |
| 427 | void ISel::visitMul(BinaryOperator &I) { |
| 428 | unsigned Class = getClass(I.getType()); |
| 429 | if (Class > 2) // FIXME: Handle longs |
| 430 | visitInstruction(I); |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 431 | |
Chris Lattner | ca9671d | 2002-11-02 20:28:58 +0000 | [diff] [blame] | 432 | static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX }; |
| 433 | static const unsigned MulOpcode[]={ X86::MULrr8, X86::MULrr16, X86::MULrr32 }; |
| 434 | static const unsigned MovOpcode[]={ X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 }; |
| 435 | |
| 436 | unsigned Reg = Regs[Class]; |
| 437 | unsigned Op0Reg = getReg(I.getOperand(1)); |
| 438 | unsigned Op1Reg = getReg(I.getOperand(1)); |
| 439 | |
| 440 | // Put the first operand into one of the A registers... |
| 441 | BuildMI(BB, MovOpcode[Class], 1, Reg).addReg(Op0Reg); |
| 442 | |
| 443 | // Emit the appropriate multiple instruction... |
| 444 | // FIXME: We need to mark that this modified AH, DX, or EDX also!! |
| 445 | BuildMI(BB, MulOpcode[Class], 2, Reg).addReg(Reg).addReg(Op1Reg); |
| 446 | |
| 447 | // Put the result into the destination register... |
| 448 | BuildMI(BB, MovOpcode[Class], 1, getReg(I)).addReg(Reg); |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 449 | } |
Chris Lattner | ca9671d | 2002-11-02 20:28:58 +0000 | [diff] [blame] | 450 | |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 451 | /// visitDivRem - Handle division and remainder instructions... these |
| 452 | /// instruction both require the same instructions to be generated, they just |
| 453 | /// select the result from a different register. Note that both of these |
| 454 | /// instructions work differently for signed and unsigned operands. |
| 455 | /// |
| 456 | void ISel::visitDivRem(BinaryOperator &I) { |
| 457 | unsigned Class = getClass(I.getType()); |
| 458 | if (Class > 2) // FIXME: Handle longs |
| 459 | visitInstruction(I); |
| 460 | |
| 461 | static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX }; |
| 462 | static const unsigned MovOpcode[]={ X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 }; |
Brian Gaeke | 6559bb9 | 2002-11-14 22:32:30 +0000 | [diff] [blame] | 463 | static const unsigned ExtOpcode[]={ X86::CBW , X86::CWD , X86::CDQ }; |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 464 | static const unsigned ClrOpcode[]={ X86::XORrr8, X86::XORrr16, X86::XORrr32 }; |
| 465 | static const unsigned ExtRegs[] ={ X86::AH , X86::DX , X86::EDX }; |
| 466 | |
| 467 | static const unsigned DivOpcode[][4] = { |
| 468 | { X86::DIVrr8 , X86::DIVrr16 , X86::DIVrr32 , 0 }, // Unsigned division |
| 469 | { X86::IDIVrr8, X86::IDIVrr16, X86::IDIVrr32, 0 }, // Signed division |
| 470 | }; |
| 471 | |
| 472 | bool isSigned = I.getType()->isSigned(); |
| 473 | unsigned Reg = Regs[Class]; |
| 474 | unsigned ExtReg = ExtRegs[Class]; |
| 475 | unsigned Op0Reg = getReg(I.getOperand(1)); |
| 476 | unsigned Op1Reg = getReg(I.getOperand(1)); |
| 477 | |
| 478 | // Put the first operand into one of the A registers... |
| 479 | BuildMI(BB, MovOpcode[Class], 1, Reg).addReg(Op0Reg); |
| 480 | |
| 481 | if (isSigned) { |
| 482 | // Emit a sign extension instruction... |
| 483 | BuildMI(BB, ExtOpcode[Class], 1, ExtReg).addReg(Reg); |
| 484 | } else { |
| 485 | // If unsigned, emit a zeroing instruction... (reg = xor reg, reg) |
| 486 | BuildMI(BB, ClrOpcode[Class], 2, ExtReg).addReg(ExtReg).addReg(ExtReg); |
| 487 | } |
| 488 | |
| 489 | // Figure out which register we want to pick the result out of... |
| 490 | unsigned DestReg = (I.getOpcode() == Instruction::Div) ? Reg : ExtReg; |
| 491 | |
Brian Gaeke | 6559bb9 | 2002-11-14 22:32:30 +0000 | [diff] [blame] | 492 | // Emit the appropriate divide or remainder instruction... |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 493 | // FIXME: We need to mark that this modified AH, DX, or EDX also!! |
| 494 | BuildMI(BB,DivOpcode[isSigned][Class], 2, DestReg).addReg(Reg).addReg(Op1Reg); |
| 495 | |
| 496 | // Put the result into the destination register... |
| 497 | BuildMI(BB, MovOpcode[Class], 1, getReg(I)).addReg(DestReg); |
Chris Lattner | ca9671d | 2002-11-02 20:28:58 +0000 | [diff] [blame] | 498 | } |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 499 | |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 500 | /// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here |
| 501 | /// for constant immediate shift values, and for constant immediate |
| 502 | /// shift values equal to 1. Even the general case is sort of special, |
| 503 | /// because the shift amount has to be in CL, not just any old register. |
| 504 | /// |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 505 | void ISel::visitShiftInst (ShiftInst &I) { |
| 506 | unsigned Op0r = getReg (I.getOperand(0)); |
| 507 | unsigned DestReg = getReg(I); |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 508 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
| 509 | bool isOperandSigned = I.getType()->isUnsigned(); |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 510 | unsigned OperandClass = getClass(I.getType()); |
| 511 | |
| 512 | if (OperandClass > 2) |
| 513 | visitInstruction(I); // Can't handle longs yet! |
Chris Lattner | 796df73 | 2002-11-02 00:44:25 +0000 | [diff] [blame] | 514 | |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 515 | if (ConstantUInt *CUI = dyn_cast <ConstantUInt> (I.getOperand (1))) |
| 516 | { |
Chris Lattner | 796df73 | 2002-11-02 00:44:25 +0000 | [diff] [blame] | 517 | // The shift amount is constant, guaranteed to be a ubyte. Get its value. |
| 518 | assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?"); |
| 519 | unsigned char shAmt = CUI->getValue(); |
| 520 | |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 521 | static const unsigned ConstantOperand[][4] = { |
| 522 | { X86::SHRir8, X86::SHRir16, X86::SHRir32, 0 }, // SHR |
| 523 | { X86::SARir8, X86::SARir16, X86::SARir32, 0 }, // SAR |
| 524 | { X86::SHLir8, X86::SHLir16, X86::SHLir32, 0 }, // SHL |
| 525 | { X86::SHLir8, X86::SHLir16, X86::SHLir32, 0 }, // SAL = SHL |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 526 | }; |
| 527 | |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 528 | const unsigned *OpTab = // Figure out the operand table to use |
| 529 | ConstantOperand[isLeftShift*2+isOperandSigned]; |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 530 | |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 531 | // Emit: <insn> reg, shamt (shift-by-immediate opcode "ir" form.) |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 532 | BuildMI(BB, OpTab[OperandClass], 2, DestReg).addReg(Op0r).addZImm(shAmt); |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 533 | } |
| 534 | else |
| 535 | { |
| 536 | // The shift amount is non-constant. |
| 537 | // |
| 538 | // In fact, you can only shift with a variable shift amount if |
| 539 | // that amount is already in the CL register, so we have to put it |
| 540 | // there first. |
| 541 | // |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 542 | |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 543 | // Emit: move cl, shiftAmount (put the shift amount in CL.) |
Chris Lattner | ca9671d | 2002-11-02 20:28:58 +0000 | [diff] [blame] | 544 | BuildMI(BB, X86::MOVrr8, 1, X86::CL).addReg(getReg(I.getOperand(1))); |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 545 | |
| 546 | // This is a shift right (SHR). |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 547 | static const unsigned NonConstantOperand[][4] = { |
| 548 | { X86::SHRrr8, X86::SHRrr16, X86::SHRrr32, 0 }, // SHR |
| 549 | { X86::SARrr8, X86::SARrr16, X86::SARrr32, 0 }, // SAR |
| 550 | { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32, 0 }, // SHL |
| 551 | { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32, 0 }, // SAL = SHL |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 552 | }; |
| 553 | |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 554 | const unsigned *OpTab = // Figure out the operand table to use |
| 555 | NonConstantOperand[isLeftShift*2+isOperandSigned]; |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 556 | |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 557 | BuildMI(BB, OpTab[OperandClass], 2, DestReg).addReg(Op0r).addReg(X86::CL); |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 561 | /// visitPHINode - Turn an LLVM PHI node into an X86 PHI node... |
| 562 | /// |
| 563 | void ISel::visitPHINode(PHINode &PN) { |
| 564 | MachineInstr *MI = BuildMI(BB, X86::PHI, PN.getNumOperands(), getReg(PN)); |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 565 | |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 566 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 567 | // FIXME: This will put constants after the PHI nodes in the block, which |
| 568 | // is invalid. They should be put inline into the PHI node eventually. |
| 569 | // |
| 570 | MI->addRegOperand(getReg(PN.getIncomingValue(i))); |
| 571 | MI->addPCDispOperand(PN.getIncomingBlock(i)); |
| 572 | } |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 575 | |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 576 | /// createSimpleX86InstructionSelector - This pass converts an LLVM function |
| 577 | /// 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] | 578 | /// generated code sucks but the implementation is nice and simple. |
| 579 | /// |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 580 | Pass *createSimpleX86InstructionSelector(TargetMachine &TM) { |
| 581 | return new ISel(TM); |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 582 | } |