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 | 6fc3c52 | 2002-11-17 21:11:55 +0000 | [diff] [blame] | 9 | #include "X86InstrBuilder.h" |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 10 | #include "llvm/Function.h" |
| 11 | #include "llvm/iTerminators.h" |
Brian Gaeke | 1749d63 | 2002-11-07 17:59:21 +0000 | [diff] [blame] | 12 | #include "llvm/iOperators.h" |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 13 | #include "llvm/iOther.h" |
Chris Lattner | 51b49a9 | 2002-11-02 19:45:49 +0000 | [diff] [blame] | 14 | #include "llvm/iPHINode.h" |
Chris Lattner | 6fc3c52 | 2002-11-17 21:11:55 +0000 | [diff] [blame] | 15 | #include "llvm/iMemory.h" |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 16 | #include "llvm/Type.h" |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 17 | #include "llvm/Constants.h" |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 18 | #include "llvm/Pass.h" |
Chris Lattner | 341a937 | 2002-10-29 17:43:55 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineFunction.h" |
Misha Brukman | d2cc017 | 2002-11-20 00:58:23 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 21 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 22 | #include "llvm/Support/InstVisitor.h" |
Misha Brukman | d2cc017 | 2002-11-20 00:58:23 +0000 | [diff] [blame] | 23 | #include "llvm/Target/MRegisterInfo.h" |
| 24 | #include <map> |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 25 | |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 26 | using namespace MOTy; // Get Use, Def, UseAndDef |
| 27 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 28 | namespace { |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 29 | struct ISel : public FunctionPass, InstVisitor<ISel> { |
| 30 | TargetMachine &TM; |
Chris Lattner | 341a937 | 2002-10-29 17:43:55 +0000 | [diff] [blame] | 31 | MachineFunction *F; // The function we are compiling into |
| 32 | MachineBasicBlock *BB; // The current MBB we are compiling |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 33 | |
| 34 | unsigned CurReg; |
| 35 | std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs |
| 36 | |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 37 | ISel(TargetMachine &tm) |
| 38 | : TM(tm), F(0), BB(0), CurReg(MRegisterInfo::FirstVirtualRegister) {} |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 39 | |
| 40 | /// runOnFunction - Top level implementation of instruction selection for |
| 41 | /// the entire function. |
| 42 | /// |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 43 | bool runOnFunction(Function &Fn) { |
Chris Lattner | 36b3603 | 2002-10-29 23:40:58 +0000 | [diff] [blame] | 44 | F = &MachineFunction::construct(&Fn, TM); |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 45 | visit(Fn); |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 46 | RegMap.clear(); |
Chris Lattner | 94e8ee2 | 2002-11-21 17:26:58 +0000 | [diff] [blame] | 47 | CurReg = MRegisterInfo::FirstVirtualRegister; |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 48 | F = 0; |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 49 | return false; // We never modify the LLVM itself. |
| 50 | } |
| 51 | |
| 52 | /// visitBasicBlock - This method is called when we are visiting a new basic |
Chris Lattner | 33f53b5 | 2002-10-29 20:48:56 +0000 | [diff] [blame] | 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. |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 56 | /// |
| 57 | void visitBasicBlock(BasicBlock &LLVM_BB) { |
Chris Lattner | 42c7786 | 2002-10-30 00:47:40 +0000 | [diff] [blame] | 58 | BB = new MachineBasicBlock(&LLVM_BB); |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 59 | // FIXME: Use the auto-insert form when it's available |
| 60 | F->getBasicBlockList().push_back(BB); |
| 61 | } |
| 62 | |
| 63 | // Visitation methods for various instructions. These methods simply emit |
| 64 | // fixed X86 code for each instruction. |
| 65 | // |
Brian Gaeke | fa8d571 | 2002-11-22 11:07:01 +0000 | [diff] [blame] | 66 | |
| 67 | // Control flow operators |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 68 | void visitReturnInst(ReturnInst &RI); |
Chris Lattner | 2df035b | 2002-11-02 19:27:56 +0000 | [diff] [blame] | 69 | void visitBranchInst(BranchInst &BI); |
Brian Gaeke | fa8d571 | 2002-11-22 11:07:01 +0000 | [diff] [blame] | 70 | void visitCallInst(CallInst &I); |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 71 | |
| 72 | // Arithmetic operators |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 73 | void visitSimpleBinary(BinaryOperator &B, unsigned OpcodeClass); |
Chris Lattner | 68aad93 | 2002-11-02 20:13:22 +0000 | [diff] [blame] | 74 | void visitAdd(BinaryOperator &B) { visitSimpleBinary(B, 0); } |
| 75 | void visitSub(BinaryOperator &B) { visitSimpleBinary(B, 1); } |
Chris Lattner | ca9671d | 2002-11-02 20:28:58 +0000 | [diff] [blame] | 76 | void visitMul(BinaryOperator &B); |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 77 | |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 78 | void visitDiv(BinaryOperator &B) { visitDivRem(B); } |
| 79 | void visitRem(BinaryOperator &B) { visitDivRem(B); } |
| 80 | void visitDivRem(BinaryOperator &B); |
| 81 | |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 82 | // Bitwise operators |
Chris Lattner | 68aad93 | 2002-11-02 20:13:22 +0000 | [diff] [blame] | 83 | void visitAnd(BinaryOperator &B) { visitSimpleBinary(B, 2); } |
| 84 | void visitOr (BinaryOperator &B) { visitSimpleBinary(B, 3); } |
| 85 | void visitXor(BinaryOperator &B) { visitSimpleBinary(B, 4); } |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 86 | |
| 87 | // Binary comparison operators |
Chris Lattner | 05093a5 | 2002-11-21 15:52:38 +0000 | [diff] [blame] | 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); } |
Chris Lattner | 6fc3c52 | 2002-11-17 21:11:55 +0000 | [diff] [blame] | 95 | |
| 96 | // Memory Instructions |
| 97 | void visitLoadInst(LoadInst &I); |
| 98 | void visitStoreInst(StoreInst &I); |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 99 | |
| 100 | // Other operators |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 101 | void visitShiftInst(ShiftInst &I); |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 102 | void visitPHINode(PHINode &I); |
Brian Gaeke | fa8d571 | 2002-11-22 11:07:01 +0000 | [diff] [blame] | 103 | void visitCastInst(CastInst &I); |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 104 | |
| 105 | void visitInstruction(Instruction &I) { |
| 106 | std::cerr << "Cannot instruction select: " << I; |
| 107 | abort(); |
| 108 | } |
| 109 | |
Brian Gaeke | c250598 | 2002-11-30 11:57:28 +0000 | [diff] [blame] | 110 | void promote32 (const unsigned targetReg, Value *v); |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 111 | |
| 112 | /// copyConstantToRegister - Output the instructions required to put the |
| 113 | /// specified constant into the specified register. |
| 114 | /// |
| 115 | void copyConstantToRegister(Constant *C, unsigned Reg); |
| 116 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 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. |
| 120 | /// |
| 121 | unsigned getReg(Value &V) { return getReg(&V); } // Allow references |
| 122 | unsigned getReg(Value *V) { |
| 123 | unsigned &Reg = RegMap[V]; |
Misha Brukman | d2cc017 | 2002-11-20 00:58:23 +0000 | [diff] [blame] | 124 | if (Reg == 0) { |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 125 | Reg = CurReg++; |
Misha Brukman | d2cc017 | 2002-11-20 00:58:23 +0000 | [diff] [blame] | 126 | RegMap[V] = Reg; |
| 127 | |
| 128 | // Add the mapping of regnumber => reg class to MachineFunction |
| 129 | F->addRegMap(Reg, |
| 130 | TM.getRegisterInfo()->getRegClassForType(V->getType())); |
| 131 | } |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 132 | |
Chris Lattner | 6f8fd25 | 2002-10-27 21:23:43 +0000 | [diff] [blame] | 133 | // If this operand is a constant, emit the code to copy the constant into |
| 134 | // the register here... |
| 135 | // |
Chris Lattner | dbf30f7 | 2002-12-04 06:45:19 +0000 | [diff] [blame] | 136 | if (Constant *C = dyn_cast<Constant>(V)) { |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 137 | copyConstantToRegister(C, Reg); |
Chris Lattner | dbf30f7 | 2002-12-04 06:45:19 +0000 | [diff] [blame] | 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); |
Chris Lattner | d6c4cfa | 2002-12-04 17:15:34 +0000 | [diff] [blame] | 141 | } else if (Argument *A = dyn_cast<Argument>(V)) { |
| 142 | std::cerr << "ERROR: Arguments not implemented in SimpleInstSel\n"; |
Chris Lattner | dbf30f7 | 2002-12-04 06:45:19 +0000 | [diff] [blame] | 143 | } |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 144 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 145 | return Reg; |
| 146 | } |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 147 | }; |
| 148 | } |
| 149 | |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame] | 150 | /// TypeClass - Used by the X86 backend to group LLVM types by their basic X86 |
| 151 | /// Representation. |
| 152 | /// |
| 153 | enum TypeClass { |
| 154 | cByte, cShort, cInt, cLong, cFloat, cDouble |
| 155 | }; |
| 156 | |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 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. |
| 159 | /// |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame] | 160 | static inline TypeClass getClass(const Type *Ty) { |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 161 | switch (Ty->getPrimitiveID()) { |
| 162 | case Type::SByteTyID: |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame] | 163 | case Type::UByteTyID: return cByte; // Byte operands are class #0 |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 164 | case Type::ShortTyID: |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame] | 165 | case Type::UShortTyID: return cShort; // Short operands are class #1 |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 166 | case Type::IntTyID: |
| 167 | case Type::UIntTyID: |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame] | 168 | case Type::PointerTyID: return cInt; // Int's and pointers are class #2 |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 169 | |
| 170 | case Type::LongTyID: |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame] | 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 |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 174 | default: |
| 175 | assert(0 && "Invalid type to getClass!"); |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame] | 176 | return cByte; // not reached |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 177 | } |
| 178 | } |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 179 | |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 180 | |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 181 | /// copyConstantToRegister - Output the instructions required to put the |
| 182 | /// specified constant into the specified register. |
| 183 | /// |
| 184 | void ISel::copyConstantToRegister(Constant *C, unsigned R) { |
| 185 | assert (!isa<ConstantExpr>(C) && "Constant expressions not yet handled!\n"); |
| 186 | |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 187 | if (C->getType()->isIntegral()) { |
| 188 | unsigned Class = getClass(C->getType()); |
| 189 | assert(Class != 3 && "Type not handled yet!"); |
| 190 | |
| 191 | static const unsigned IntegralOpcodeTab[] = { |
| 192 | X86::MOVir8, X86::MOVir16, X86::MOVir32 |
| 193 | }; |
| 194 | |
| 195 | if (C->getType()->isSigned()) { |
| 196 | ConstantSInt *CSI = cast<ConstantSInt>(C); |
| 197 | BuildMI(BB, IntegralOpcodeTab[Class], 1, R).addSImm(CSI->getValue()); |
| 198 | } else { |
| 199 | ConstantUInt *CUI = cast<ConstantUInt>(C); |
| 200 | BuildMI(BB, IntegralOpcodeTab[Class], 1, R).addZImm(CUI->getValue()); |
| 201 | } |
| 202 | } else { |
| 203 | assert(0 && "Type not handled yet!"); |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 207 | |
Brian Gaeke | 1749d63 | 2002-11-07 17:59:21 +0000 | [diff] [blame] | 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) |
| 213 | /// |
Chris Lattner | 05093a5 | 2002-11-21 15:52:38 +0000 | [diff] [blame] | 214 | void ISel::visitSetCCInst(SetCondInst &I, unsigned OpNum) { |
Brian Gaeke | 1749d63 | 2002-11-07 17:59:21 +0000 | [diff] [blame] | 215 | // The arguments are already supposed to be of the same type. |
Chris Lattner | 05093a5 | 2002-11-21 15:52:38 +0000 | [diff] [blame] | 216 | const Type *CompTy = I.getOperand(0)->getType(); |
| 217 | unsigned reg1 = getReg(I.getOperand(0)); |
| 218 | unsigned reg2 = getReg(I.getOperand(1)); |
| 219 | |
| 220 | unsigned Class = getClass(CompTy); |
| 221 | switch (Class) { |
| 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 |
| 224 | // 32-bit. |
| 225 | case cByte: |
| 226 | BuildMI (BB, X86::CMPrr8, 2).addReg (reg1).addReg (reg2); |
| 227 | break; |
| 228 | case cShort: |
| 229 | BuildMI (BB, X86::CMPrr16, 2).addReg (reg1).addReg (reg2); |
| 230 | break; |
| 231 | case cInt: |
| 232 | BuildMI (BB, X86::CMPrr32, 2).addReg (reg1).addReg (reg2); |
| 233 | break; |
| 234 | |
| 235 | // Push the variables on the stack with fldl opcodes. |
| 236 | // FIXME: assuming var1, var2 are in memory, if not, spill to |
| 237 | // stack first |
| 238 | case cFloat: // Floats |
Chris Lattner | 3a9a693 | 2002-11-21 22:49:20 +0000 | [diff] [blame] | 239 | BuildMI (BB, X86::FLDr4, 1).addReg (reg1); |
| 240 | BuildMI (BB, X86::FLDr4, 1).addReg (reg2); |
Chris Lattner | 05093a5 | 2002-11-21 15:52:38 +0000 | [diff] [blame] | 241 | break; |
| 242 | case cDouble: // Doubles |
Chris Lattner | 3a9a693 | 2002-11-21 22:49:20 +0000 | [diff] [blame] | 243 | BuildMI (BB, X86::FLDr8, 1).addReg (reg1); |
| 244 | BuildMI (BB, X86::FLDr8, 1).addReg (reg2); |
Chris Lattner | 05093a5 | 2002-11-21 15:52:38 +0000 | [diff] [blame] | 245 | break; |
| 246 | case cLong: |
| 247 | default: |
| 248 | visitInstruction(I); |
| 249 | } |
| 250 | |
| 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); |
| 258 | } |
| 259 | |
Brian Gaeke | 1749d63 | 2002-11-07 17:59:21 +0000 | [diff] [blame] | 260 | // Emit setOp instruction (extract concode; clobbers ax), |
| 261 | // using the following mapping: |
| 262 | // LLVM -> X86 signed X86 unsigned |
| 263 | // ----- ----- ----- |
| 264 | // seteq -> sete sete |
| 265 | // setne -> setne setne |
| 266 | // setlt -> setl setb |
| 267 | // setgt -> setg seta |
| 268 | // setle -> setle setbe |
| 269 | // setge -> setge setae |
Chris Lattner | 05093a5 | 2002-11-21 15:52:38 +0000 | [diff] [blame] | 270 | |
| 271 | static const unsigned OpcodeTab[2][6] = { |
Chris Lattner | 4b4e9dd | 2002-11-21 16:19:42 +0000 | [diff] [blame] | 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}, |
Chris Lattner | 05093a5 | 2002-11-21 15:52:38 +0000 | [diff] [blame] | 274 | }; |
| 275 | |
| 276 | BuildMI(BB, OpcodeTab[CompTy->isSigned()][OpNum], 0, X86::AL); |
| 277 | |
Brian Gaeke | 1749d63 | 2002-11-07 17:59:21 +0000 | [diff] [blame] | 278 | // Put it in the result using a move. |
Chris Lattner | 05093a5 | 2002-11-21 15:52:38 +0000 | [diff] [blame] | 279 | BuildMI (BB, X86::MOVrr8, 1, getReg(I)).addReg(X86::AL); |
Brian Gaeke | 1749d63 | 2002-11-07 17:59:21 +0000 | [diff] [blame] | 280 | } |
Chris Lattner | 51b49a9 | 2002-11-02 19:45:49 +0000 | [diff] [blame] | 281 | |
Brian Gaeke | c250598 | 2002-11-30 11:57:28 +0000 | [diff] [blame] | 282 | /// promote32 - Emit instructions to turn a narrow operand into a 32-bit-wide |
| 283 | /// operand, in the specified target register. |
| 284 | void |
| 285 | ISel::promote32 (const unsigned targetReg, Value *v) |
| 286 | { |
| 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"); |
| 292 | switch (Class) |
| 293 | { |
| 294 | case cByte: |
| 295 | // Extend value into target register (8->32) |
| 296 | if (isUnsigned) |
| 297 | BuildMI (BB, X86::MOVZXr32r8, 1, targetReg).addReg (vReg); |
| 298 | else |
| 299 | BuildMI (BB, X86::MOVSXr32r8, 1, targetReg).addReg (vReg); |
| 300 | break; |
| 301 | case cShort: |
| 302 | // Extend value into target register (16->32) |
| 303 | if (isUnsigned) |
| 304 | BuildMI (BB, X86::MOVZXr32r16, 1, targetReg).addReg (vReg); |
| 305 | else |
| 306 | BuildMI (BB, X86::MOVSXr32r16, 1, targetReg).addReg (vReg); |
| 307 | break; |
| 308 | case cInt: |
| 309 | // Move value into target register (32->32) |
| 310 | BuildMI (BB, X86::MOVrr32, 1, targetReg).addReg (vReg); |
| 311 | break; |
| 312 | } |
| 313 | } |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 314 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 315 | /// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such, |
| 316 | /// we have the following possibilities: |
| 317 | /// |
| 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 |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 323 | /// ret long, ulong : Move value into EAX/EDX and return |
| 324 | /// ret float/double : Top of FP stack |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 325 | /// |
Brian Gaeke | c250598 | 2002-11-30 11:57:28 +0000 | [diff] [blame] | 326 | void |
| 327 | ISel::visitReturnInst (ReturnInst &I) |
| 328 | { |
| 329 | if (I.getNumOperands () == 0) |
| 330 | { |
| 331 | // Emit a 'ret' instruction |
| 332 | BuildMI (BB, X86::RET, 0); |
| 333 | return; |
| 334 | } |
| 335 | Value *rv = I.getOperand (0); |
| 336 | unsigned Class = getClass (rv->getType ()); |
| 337 | switch (Class) |
| 338 | { |
| 339 | // integral return values: extend or move into EAX and return. |
| 340 | case cByte: |
| 341 | case cShort: |
| 342 | case cInt: |
| 343 | promote32 (X86::EAX, rv); |
| 344 | break; |
| 345 | // ret float/double: top of FP stack |
| 346 | // FLD <val> |
| 347 | case cFloat: // Floats |
| 348 | BuildMI (BB, X86::FLDr4, 1).addReg (getReg (rv)); |
| 349 | break; |
| 350 | case cDouble: // Doubles |
| 351 | BuildMI (BB, X86::FLDr8, 1).addReg (getReg (rv)); |
| 352 | break; |
| 353 | case cLong: |
| 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 |
| 357 | // cage? *zort* |
| 358 | default: |
| 359 | visitInstruction (I); |
| 360 | } |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame] | 361 | // Emit a 'ret' instruction |
Brian Gaeke | c250598 | 2002-11-30 11:57:28 +0000 | [diff] [blame] | 362 | BuildMI (BB, X86::RET, 0); |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Chris Lattner | 51b49a9 | 2002-11-02 19:45:49 +0000 | [diff] [blame] | 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). |
| 369 | /// |
Brian Gaeke | c03a0cb | 2002-11-19 09:08:47 +0000 | [diff] [blame] | 370 | void |
| 371 | ISel::visitBranchInst (BranchInst & BI) |
| 372 | { |
| 373 | if (BI.isConditional ()) |
| 374 | { |
| 375 | BasicBlock *ifTrue = BI.getSuccessor (0); |
| 376 | BasicBlock *ifFalse = BI.getSuccessor (1); // this is really unobvious |
Chris Lattner | 2df035b | 2002-11-02 19:27:56 +0000 | [diff] [blame] | 377 | |
Brian Gaeke | c03a0cb | 2002-11-19 09:08:47 +0000 | [diff] [blame] | 378 | // simplest thing I can think of: compare condition with zero, |
| 379 | // followed by jump-if-equal to ifFalse, and jump-if-nonequal to |
| 380 | // ifTrue |
| 381 | unsigned int condReg = getReg (BI.getCondition ()); |
Chris Lattner | 97ad9e1 | 2002-11-21 01:59:50 +0000 | [diff] [blame] | 382 | BuildMI (BB, X86::CMPri8, 2).addReg (condReg).addZImm (0); |
Brian Gaeke | c03a0cb | 2002-11-19 09:08:47 +0000 | [diff] [blame] | 383 | BuildMI (BB, X86::JNE, 1).addPCDisp (BI.getSuccessor (0)); |
| 384 | BuildMI (BB, X86::JE, 1).addPCDisp (BI.getSuccessor (1)); |
| 385 | } |
| 386 | else // unconditional branch |
| 387 | { |
| 388 | BuildMI (BB, X86::JMP, 1).addPCDisp (BI.getSuccessor (0)); |
| 389 | } |
Chris Lattner | 2df035b | 2002-11-02 19:27:56 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Brian Gaeke | 18a2021 | 2002-11-29 12:01:58 +0000 | [diff] [blame] | 392 | /// visitCallInst - Push args on stack and do a procedure call instruction. |
| 393 | void |
| 394 | ISel::visitCallInst (CallInst & CI) |
| 395 | { |
Misha Brukman | 0d2cf3a | 2002-12-04 19:22:53 +0000 | [diff] [blame^] | 396 | // keep a counter of how many bytes we pushed on the stack |
| 397 | unsigned bytesPushed = 0; |
| 398 | |
Brian Gaeke | 18a2021 | 2002-11-29 12:01:58 +0000 | [diff] [blame] | 399 | // Push the arguments on the stack in reverse order, as specified by |
| 400 | // the ABI. |
Chris Lattner | d852c15 | 2002-12-03 20:30:12 +0000 | [diff] [blame] | 401 | for (unsigned i = CI.getNumOperands()-1; i >= 1; --i) |
Brian Gaeke | 18a2021 | 2002-11-29 12:01:58 +0000 | [diff] [blame] | 402 | { |
| 403 | Value *v = CI.getOperand (i); |
Brian Gaeke | 18a2021 | 2002-11-29 12:01:58 +0000 | [diff] [blame] | 404 | switch (getClass (v->getType ())) |
| 405 | { |
Brian Gaeke | c250598 | 2002-11-30 11:57:28 +0000 | [diff] [blame] | 406 | case cByte: |
| 407 | case cShort: |
Brian Gaeke | bb25f2f | 2002-12-03 00:51:09 +0000 | [diff] [blame] | 408 | // Promote V to 32 bits wide, and move the result into EAX, |
| 409 | // then push EAX. |
Brian Gaeke | c250598 | 2002-11-30 11:57:28 +0000 | [diff] [blame] | 410 | promote32 (X86::EAX, v); |
| 411 | BuildMI (BB, X86::PUSHr32, 1).addReg (X86::EAX); |
Misha Brukman | 0d2cf3a | 2002-12-04 19:22:53 +0000 | [diff] [blame^] | 412 | bytesPushed += 4; |
Brian Gaeke | c250598 | 2002-11-30 11:57:28 +0000 | [diff] [blame] | 413 | break; |
Brian Gaeke | 18a2021 | 2002-11-29 12:01:58 +0000 | [diff] [blame] | 414 | case cInt: |
Chris Lattner | 33ced56 | 2002-12-04 06:56:56 +0000 | [diff] [blame] | 415 | case cFloat: { |
| 416 | unsigned Reg = getReg(v); |
| 417 | BuildMI (BB, X86::PUSHr32, 1).addReg(Reg); |
Misha Brukman | 0d2cf3a | 2002-12-04 19:22:53 +0000 | [diff] [blame^] | 418 | bytesPushed += 4; |
Brian Gaeke | 18a2021 | 2002-11-29 12:01:58 +0000 | [diff] [blame] | 419 | break; |
Chris Lattner | 33ced56 | 2002-12-04 06:56:56 +0000 | [diff] [blame] | 420 | } |
Brian Gaeke | 18a2021 | 2002-11-29 12:01:58 +0000 | [diff] [blame] | 421 | default: |
Brian Gaeke | bb25f2f | 2002-12-03 00:51:09 +0000 | [diff] [blame] | 422 | // FIXME: long/ulong/double args not handled. |
Brian Gaeke | 18a2021 | 2002-11-29 12:01:58 +0000 | [diff] [blame] | 423 | visitInstruction (CI); |
| 424 | break; |
| 425 | } |
| 426 | } |
| 427 | // Emit a CALL instruction with PC-relative displacement. |
| 428 | BuildMI (BB, X86::CALLpcrel32, 1).addPCDisp (CI.getCalledValue ()); |
Misha Brukman | 0d2cf3a | 2002-12-04 19:22:53 +0000 | [diff] [blame^] | 429 | |
| 430 | // Adjust the stack by `bytesPushed' amount if non-zero |
| 431 | if (bytesPushed > 0) |
| 432 | BuildMI (BB, X86::ADDri32, 2).addReg(X86::ESP).addZImm(bytesPushed); |
Brian Gaeke | fa8d571 | 2002-11-22 11:07:01 +0000 | [diff] [blame] | 433 | } |
Chris Lattner | 2df035b | 2002-11-02 19:27:56 +0000 | [diff] [blame] | 434 | |
Chris Lattner | 68aad93 | 2002-11-02 20:13:22 +0000 | [diff] [blame] | 435 | /// visitSimpleBinary - Implement simple binary operators for integral types... |
| 436 | /// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or, |
| 437 | /// 4 for Xor. |
| 438 | /// |
| 439 | void ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) { |
| 440 | if (B.getType() == Type::BoolTy) // FIXME: Handle bools for logicals |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 441 | visitInstruction(B); |
| 442 | |
| 443 | unsigned Class = getClass(B.getType()); |
| 444 | if (Class > 2) // FIXME: Handle longs |
| 445 | visitInstruction(B); |
| 446 | |
| 447 | static const unsigned OpcodeTab[][4] = { |
Chris Lattner | 68aad93 | 2002-11-02 20:13:22 +0000 | [diff] [blame] | 448 | // Arithmetic operators |
| 449 | { X86::ADDrr8, X86::ADDrr16, X86::ADDrr32, 0 }, // ADD |
| 450 | { X86::SUBrr8, X86::SUBrr16, X86::SUBrr32, 0 }, // SUB |
| 451 | |
| 452 | // Bitwise operators |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 453 | { X86::ANDrr8, X86::ANDrr16, X86::ANDrr32, 0 }, // AND |
| 454 | { X86:: ORrr8, X86:: ORrr16, X86:: ORrr32, 0 }, // OR |
| 455 | { X86::XORrr8, X86::XORrr16, X86::XORrr32, 0 }, // XOR |
| 456 | }; |
| 457 | |
| 458 | unsigned Opcode = OpcodeTab[OperatorClass][Class]; |
| 459 | unsigned Op0r = getReg(B.getOperand(0)); |
| 460 | unsigned Op1r = getReg(B.getOperand(1)); |
| 461 | BuildMI(BB, Opcode, 2, getReg(B)).addReg(Op0r).addReg(Op1r); |
| 462 | } |
| 463 | |
Chris Lattner | ca9671d | 2002-11-02 20:28:58 +0000 | [diff] [blame] | 464 | /// visitMul - Multiplies are not simple binary operators because they must deal |
| 465 | /// with the EAX register explicitly. |
| 466 | /// |
| 467 | void ISel::visitMul(BinaryOperator &I) { |
| 468 | unsigned Class = getClass(I.getType()); |
| 469 | if (Class > 2) // FIXME: Handle longs |
| 470 | visitInstruction(I); |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 471 | |
Chris Lattner | ca9671d | 2002-11-02 20:28:58 +0000 | [diff] [blame] | 472 | static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX }; |
| 473 | static const unsigned MulOpcode[]={ X86::MULrr8, X86::MULrr16, X86::MULrr32 }; |
| 474 | static const unsigned MovOpcode[]={ X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 }; |
| 475 | |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 476 | unsigned Reg = Regs[Class]; |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 477 | unsigned Op0Reg = getReg(I.getOperand(0)); |
| 478 | unsigned Op1Reg = getReg(I.getOperand(1)); |
Chris Lattner | ca9671d | 2002-11-02 20:28:58 +0000 | [diff] [blame] | 479 | |
| 480 | // Put the first operand into one of the A registers... |
| 481 | BuildMI(BB, MovOpcode[Class], 1, Reg).addReg(Op0Reg); |
| 482 | |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 483 | // Emit the appropriate multiply instruction... |
Chris Lattner | 92845e3 | 2002-11-21 18:54:29 +0000 | [diff] [blame] | 484 | BuildMI(BB, MulOpcode[Class], 1).addReg(Op1Reg); |
Chris Lattner | ca9671d | 2002-11-02 20:28:58 +0000 | [diff] [blame] | 485 | |
| 486 | // Put the result into the destination register... |
| 487 | BuildMI(BB, MovOpcode[Class], 1, getReg(I)).addReg(Reg); |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 488 | } |
Chris Lattner | ca9671d | 2002-11-02 20:28:58 +0000 | [diff] [blame] | 489 | |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 490 | |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 491 | /// visitDivRem - Handle division and remainder instructions... these |
| 492 | /// instruction both require the same instructions to be generated, they just |
| 493 | /// select the result from a different register. Note that both of these |
| 494 | /// instructions work differently for signed and unsigned operands. |
| 495 | /// |
| 496 | void ISel::visitDivRem(BinaryOperator &I) { |
| 497 | unsigned Class = getClass(I.getType()); |
| 498 | if (Class > 2) // FIXME: Handle longs |
| 499 | visitInstruction(I); |
| 500 | |
| 501 | static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX }; |
| 502 | static const unsigned MovOpcode[]={ X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 }; |
Brian Gaeke | 6559bb9 | 2002-11-14 22:32:30 +0000 | [diff] [blame] | 503 | static const unsigned ExtOpcode[]={ X86::CBW , X86::CWD , X86::CDQ }; |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 504 | static const unsigned ClrOpcode[]={ X86::XORrr8, X86::XORrr16, X86::XORrr32 }; |
| 505 | static const unsigned ExtRegs[] ={ X86::AH , X86::DX , X86::EDX }; |
| 506 | |
| 507 | static const unsigned DivOpcode[][4] = { |
| 508 | { X86::DIVrr8 , X86::DIVrr16 , X86::DIVrr32 , 0 }, // Unsigned division |
| 509 | { X86::IDIVrr8, X86::IDIVrr16, X86::IDIVrr32, 0 }, // Signed division |
| 510 | }; |
| 511 | |
| 512 | bool isSigned = I.getType()->isSigned(); |
| 513 | unsigned Reg = Regs[Class]; |
| 514 | unsigned ExtReg = ExtRegs[Class]; |
Chris Lattner | 6fc3c52 | 2002-11-17 21:11:55 +0000 | [diff] [blame] | 515 | unsigned Op0Reg = getReg(I.getOperand(0)); |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 516 | unsigned Op1Reg = getReg(I.getOperand(1)); |
| 517 | |
| 518 | // Put the first operand into one of the A registers... |
| 519 | BuildMI(BB, MovOpcode[Class], 1, Reg).addReg(Op0Reg); |
| 520 | |
| 521 | if (isSigned) { |
| 522 | // Emit a sign extension instruction... |
Chris Lattner | a4978cc | 2002-12-01 23:24:58 +0000 | [diff] [blame] | 523 | BuildMI(BB, ExtOpcode[Class], 0); |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 524 | } else { |
| 525 | // If unsigned, emit a zeroing instruction... (reg = xor reg, reg) |
| 526 | BuildMI(BB, ClrOpcode[Class], 2, ExtReg).addReg(ExtReg).addReg(ExtReg); |
| 527 | } |
| 528 | |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 529 | // Emit the appropriate divide or remainder instruction... |
Chris Lattner | 92845e3 | 2002-11-21 18:54:29 +0000 | [diff] [blame] | 530 | BuildMI(BB, DivOpcode[isSigned][Class], 1).addReg(Op1Reg); |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 531 | |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 532 | // Figure out which register we want to pick the result out of... |
| 533 | unsigned DestReg = (I.getOpcode() == Instruction::Div) ? Reg : ExtReg; |
| 534 | |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 535 | // Put the result into the destination register... |
| 536 | BuildMI(BB, MovOpcode[Class], 1, getReg(I)).addReg(DestReg); |
Chris Lattner | ca9671d | 2002-11-02 20:28:58 +0000 | [diff] [blame] | 537 | } |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 538 | |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 539 | |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 540 | /// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here |
| 541 | /// for constant immediate shift values, and for constant immediate |
| 542 | /// shift values equal to 1. Even the general case is sort of special, |
| 543 | /// because the shift amount has to be in CL, not just any old register. |
| 544 | /// |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 545 | void ISel::visitShiftInst (ShiftInst &I) { |
| 546 | unsigned Op0r = getReg (I.getOperand(0)); |
| 547 | unsigned DestReg = getReg(I); |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 548 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
| 549 | bool isOperandSigned = I.getType()->isUnsigned(); |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 550 | unsigned OperandClass = getClass(I.getType()); |
| 551 | |
| 552 | if (OperandClass > 2) |
| 553 | visitInstruction(I); // Can't handle longs yet! |
Chris Lattner | 796df73 | 2002-11-02 00:44:25 +0000 | [diff] [blame] | 554 | |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 555 | if (ConstantUInt *CUI = dyn_cast <ConstantUInt> (I.getOperand (1))) |
| 556 | { |
Chris Lattner | 796df73 | 2002-11-02 00:44:25 +0000 | [diff] [blame] | 557 | // The shift amount is constant, guaranteed to be a ubyte. Get its value. |
| 558 | assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?"); |
| 559 | unsigned char shAmt = CUI->getValue(); |
| 560 | |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 561 | static const unsigned ConstantOperand[][4] = { |
| 562 | { X86::SHRir8, X86::SHRir16, X86::SHRir32, 0 }, // SHR |
| 563 | { X86::SARir8, X86::SARir16, X86::SARir32, 0 }, // SAR |
| 564 | { X86::SHLir8, X86::SHLir16, X86::SHLir32, 0 }, // SHL |
| 565 | { X86::SHLir8, X86::SHLir16, X86::SHLir32, 0 }, // SAL = SHL |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 566 | }; |
| 567 | |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 568 | const unsigned *OpTab = // Figure out the operand table to use |
| 569 | ConstantOperand[isLeftShift*2+isOperandSigned]; |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 570 | |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 571 | // Emit: <insn> reg, shamt (shift-by-immediate opcode "ir" form.) |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 572 | BuildMI(BB, OpTab[OperandClass], 2, DestReg).addReg(Op0r).addZImm(shAmt); |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 573 | } |
| 574 | else |
| 575 | { |
| 576 | // The shift amount is non-constant. |
| 577 | // |
| 578 | // In fact, you can only shift with a variable shift amount if |
| 579 | // that amount is already in the CL register, so we have to put it |
| 580 | // there first. |
| 581 | // |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 582 | |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 583 | // Emit: move cl, shiftAmount (put the shift amount in CL.) |
Chris Lattner | ca9671d | 2002-11-02 20:28:58 +0000 | [diff] [blame] | 584 | BuildMI(BB, X86::MOVrr8, 1, X86::CL).addReg(getReg(I.getOperand(1))); |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 585 | |
| 586 | // This is a shift right (SHR). |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 587 | static const unsigned NonConstantOperand[][4] = { |
| 588 | { X86::SHRrr8, X86::SHRrr16, X86::SHRrr32, 0 }, // SHR |
| 589 | { X86::SARrr8, X86::SARrr16, X86::SARrr32, 0 }, // SAR |
| 590 | { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32, 0 }, // SHL |
| 591 | { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32, 0 }, // SAL = SHL |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 592 | }; |
| 593 | |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 594 | const unsigned *OpTab = // Figure out the operand table to use |
| 595 | NonConstantOperand[isLeftShift*2+isOperandSigned]; |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 596 | |
Chris Lattner | 3a9a693 | 2002-11-21 22:49:20 +0000 | [diff] [blame] | 597 | BuildMI(BB, OpTab[OperandClass], 1, DestReg).addReg(Op0r); |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 598 | } |
| 599 | } |
| 600 | |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 601 | |
Chris Lattner | 6fc3c52 | 2002-11-17 21:11:55 +0000 | [diff] [blame] | 602 | /// visitLoadInst - Implement LLVM load instructions in terms of the x86 'mov' |
| 603 | /// instruction. |
| 604 | /// |
| 605 | void ISel::visitLoadInst(LoadInst &I) { |
| 606 | unsigned Class = getClass(I.getType()); |
| 607 | if (Class > 2) // FIXME: Handle longs and others... |
| 608 | visitInstruction(I); |
| 609 | |
| 610 | static const unsigned Opcode[] = { X86::MOVmr8, X86::MOVmr16, X86::MOVmr32 }; |
| 611 | |
| 612 | unsigned AddressReg = getReg(I.getOperand(0)); |
| 613 | addDirectMem(BuildMI(BB, Opcode[Class], 4, getReg(I)), AddressReg); |
| 614 | } |
| 615 | |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 616 | |
Chris Lattner | 6fc3c52 | 2002-11-17 21:11:55 +0000 | [diff] [blame] | 617 | /// visitStoreInst - Implement LLVM store instructions in terms of the x86 'mov' |
| 618 | /// instruction. |
| 619 | /// |
| 620 | void ISel::visitStoreInst(StoreInst &I) { |
| 621 | unsigned Class = getClass(I.getOperand(0)->getType()); |
| 622 | if (Class > 2) // FIXME: Handle longs and others... |
| 623 | visitInstruction(I); |
| 624 | |
| 625 | static const unsigned Opcode[] = { X86::MOVrm8, X86::MOVrm16, X86::MOVrm32 }; |
| 626 | |
| 627 | unsigned ValReg = getReg(I.getOperand(0)); |
| 628 | unsigned AddressReg = getReg(I.getOperand(1)); |
| 629 | addDirectMem(BuildMI(BB, Opcode[Class], 1+4), AddressReg).addReg(ValReg); |
| 630 | } |
| 631 | |
| 632 | |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 633 | /// visitPHINode - Turn an LLVM PHI node into an X86 PHI node... |
| 634 | /// |
| 635 | void ISel::visitPHINode(PHINode &PN) { |
| 636 | MachineInstr *MI = BuildMI(BB, X86::PHI, PN.getNumOperands(), getReg(PN)); |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 637 | |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 638 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 639 | // FIXME: This will put constants after the PHI nodes in the block, which |
| 640 | // is invalid. They should be put inline into the PHI node eventually. |
| 641 | // |
| 642 | MI->addRegOperand(getReg(PN.getIncomingValue(i))); |
| 643 | MI->addPCDispOperand(PN.getIncomingBlock(i)); |
| 644 | } |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Brian Gaeke | c11232a | 2002-11-26 10:43:30 +0000 | [diff] [blame] | 647 | /// visitCastInst - Here we have various kinds of copying with or without |
| 648 | /// sign extension going on. |
Brian Gaeke | fa8d571 | 2002-11-22 11:07:01 +0000 | [diff] [blame] | 649 | void |
| 650 | ISel::visitCastInst (CastInst &CI) |
| 651 | { |
Brian Gaeke | c11232a | 2002-11-26 10:43:30 +0000 | [diff] [blame] | 652 | //> cast larger int to smaller int --> copy least significant byte/word w/ mov? |
| 653 | // |
| 654 | //I'm not really sure what to do with this. We could insert a pseudo-op |
| 655 | //that says take the low X bits of a Y bit register, but for now we can just |
| 656 | //force the value into, say, EAX, then rip out AL or AX. The advantage of |
| 657 | //the former is that the register allocator could use any register it wants, |
| 658 | //but for now this obviously doesn't matter. :) |
| 659 | |
Chris Lattner | f18a36e | 2002-12-03 18:15:59 +0000 | [diff] [blame] | 660 | const Type *targetType = CI.getType (); |
Brian Gaeke | 07f0261 | 2002-12-03 07:36:03 +0000 | [diff] [blame] | 661 | Value *operand = CI.getOperand (0); |
| 662 | unsigned int operandReg = getReg (operand); |
Chris Lattner | f18a36e | 2002-12-03 18:15:59 +0000 | [diff] [blame] | 663 | const Type *sourceType = operand->getType (); |
Brian Gaeke | 07f0261 | 2002-12-03 07:36:03 +0000 | [diff] [blame] | 664 | unsigned int destReg = getReg (CI); |
| 665 | |
| 666 | // cast to bool: |
| 667 | if (targetType == Type::BoolTy) { |
| 668 | // Emit Compare |
| 669 | BuildMI (BB, X86::CMPri8, 2).addReg (operandReg).addZImm (0); |
| 670 | // Emit Set-if-not-zero |
| 671 | BuildMI (BB, X86::SETNEr, 1, destReg); |
| 672 | return; |
| 673 | } |
Brian Gaeke | c11232a | 2002-11-26 10:43:30 +0000 | [diff] [blame] | 674 | |
| 675 | // if size of target type == size of source type |
| 676 | // Emit Mov reg(target) <- reg(source) |
| 677 | |
| 678 | // if size of target type > size of source type |
| 679 | // if both types are integer types |
| 680 | // if source type is signed |
| 681 | // sbyte to short, ushort: Emit movsx 8->16 |
| 682 | // sbyte to int, uint: Emit movsx 8->32 |
| 683 | // short to int, uint: Emit movsx 16->32 |
| 684 | // else if source type is unsigned |
| 685 | // ubyte to short, ushort: Emit movzx 8->16 |
| 686 | // ubyte to int, uint: Emit movzx 8->32 |
| 687 | // ushort to int, uint: Emit movzx 16->32 |
| 688 | // if both types are fp types |
| 689 | // float to double: Emit fstp, fld (???) |
| 690 | |
Brian Gaeke | fa8d571 | 2002-11-22 11:07:01 +0000 | [diff] [blame] | 691 | visitInstruction (CI); |
| 692 | } |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 693 | |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 694 | /// createSimpleX86InstructionSelector - This pass converts an LLVM function |
| 695 | /// 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] | 696 | /// generated code sucks but the implementation is nice and simple. |
| 697 | /// |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 698 | Pass *createSimpleX86InstructionSelector(TargetMachine &TM) { |
| 699 | return new ISel(TM); |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 700 | } |