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