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