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" |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 17 | #include "llvm/DerivedTypes.h" |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 18 | #include "llvm/Constants.h" |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 19 | #include "llvm/Pass.h" |
Chris Lattner | 341a937 | 2002-10-29 17:43:55 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineFunction.h" |
Misha Brukman | d2cc017 | 2002-11-20 00:58:23 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 22 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 23 | #include "llvm/Support/InstVisitor.h" |
Misha Brukman | d2cc017 | 2002-11-20 00:58:23 +0000 | [diff] [blame] | 24 | #include "llvm/Target/MRegisterInfo.h" |
| 25 | #include <map> |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 26 | |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 27 | using namespace MOTy; // Get Use, Def, UseAndDef |
| 28 | |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 29 | |
| 30 | /// BMI - A special BuildMI variant that takes an iterator to insert the |
| 31 | /// instruction at as well as a basic block. |
Brian Gaeke | 71794c0 | 2002-12-13 11:22:48 +0000 | [diff] [blame] | 32 | /// this is the version for when you have a destination register in mind. |
| 33 | inline static MachineInstrBuilder BMI(MachineBasicBlock *MBB, |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 34 | MachineBasicBlock::iterator &I, |
| 35 | MachineOpCode Opcode, |
| 36 | unsigned NumOperands, |
| 37 | unsigned DestReg) { |
Chris Lattner | d7d3872 | 2002-12-13 13:04:04 +0000 | [diff] [blame] | 38 | assert(I >= MBB->begin() && I <= MBB->end() && "Bad iterator!"); |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 39 | MachineInstr *MI = new MachineInstr(Opcode, NumOperands+1, true, true); |
Brian Gaeke | 71794c0 | 2002-12-13 11:22:48 +0000 | [diff] [blame] | 40 | I = ++MBB->insert(I, MI); |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 41 | return MachineInstrBuilder(MI).addReg(DestReg, MOTy::Def); |
| 42 | } |
| 43 | |
Chris Lattner | f08ad9f | 2002-12-13 10:50:40 +0000 | [diff] [blame] | 44 | /// BMI - A special BuildMI variant that takes an iterator to insert the |
| 45 | /// instruction at as well as a basic block. |
Brian Gaeke | 71794c0 | 2002-12-13 11:22:48 +0000 | [diff] [blame] | 46 | inline static MachineInstrBuilder BMI(MachineBasicBlock *MBB, |
Chris Lattner | f08ad9f | 2002-12-13 10:50:40 +0000 | [diff] [blame] | 47 | MachineBasicBlock::iterator &I, |
| 48 | MachineOpCode Opcode, |
| 49 | unsigned NumOperands) { |
Chris Lattner | d7d3872 | 2002-12-13 13:04:04 +0000 | [diff] [blame] | 50 | assert(I > MBB->begin() && I <= MBB->end() && "Bad iterator!"); |
Chris Lattner | f08ad9f | 2002-12-13 10:50:40 +0000 | [diff] [blame] | 51 | MachineInstr *MI = new MachineInstr(Opcode, NumOperands, true, true); |
Brian Gaeke | 71794c0 | 2002-12-13 11:22:48 +0000 | [diff] [blame] | 52 | I = ++MBB->insert(I, MI); |
Chris Lattner | f08ad9f | 2002-12-13 10:50:40 +0000 | [diff] [blame] | 53 | return MachineInstrBuilder(MI); |
| 54 | } |
| 55 | |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 57 | namespace { |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 58 | struct ISel : public FunctionPass, InstVisitor<ISel> { |
| 59 | TargetMachine &TM; |
Chris Lattner | 341a937 | 2002-10-29 17:43:55 +0000 | [diff] [blame] | 60 | MachineFunction *F; // The function we are compiling into |
| 61 | MachineBasicBlock *BB; // The current MBB we are compiling |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 62 | |
| 63 | unsigned CurReg; |
| 64 | std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs |
| 65 | |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 66 | // MBBMap - Mapping between LLVM BB -> Machine BB |
| 67 | std::map<const BasicBlock*, MachineBasicBlock*> MBBMap; |
| 68 | |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 69 | ISel(TargetMachine &tm) |
| 70 | : TM(tm), F(0), BB(0), CurReg(MRegisterInfo::FirstVirtualRegister) {} |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 71 | |
| 72 | /// runOnFunction - Top level implementation of instruction selection for |
| 73 | /// the entire function. |
| 74 | /// |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 75 | bool runOnFunction(Function &Fn) { |
Chris Lattner | 36b3603 | 2002-10-29 23:40:58 +0000 | [diff] [blame] | 76 | F = &MachineFunction::construct(&Fn, TM); |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 77 | |
| 78 | for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) |
| 79 | F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I)); |
| 80 | |
| 81 | // Instruction select everything except PHI nodes |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 82 | visit(Fn); |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 83 | |
| 84 | // Select the PHI nodes |
| 85 | SelectPHINodes(); |
| 86 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 87 | RegMap.clear(); |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 88 | MBBMap.clear(); |
Chris Lattner | 94e8ee2 | 2002-11-21 17:26:58 +0000 | [diff] [blame] | 89 | CurReg = MRegisterInfo::FirstVirtualRegister; |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 90 | F = 0; |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 91 | return false; // We never modify the LLVM itself. |
| 92 | } |
| 93 | |
| 94 | /// visitBasicBlock - This method is called when we are visiting a new basic |
Chris Lattner | 33f53b5 | 2002-10-29 20:48:56 +0000 | [diff] [blame] | 95 | /// block. This simply creates a new MachineBasicBlock to emit code into |
| 96 | /// and adds it to the current MachineFunction. Subsequent visit* for |
| 97 | /// instructions will be invoked for all instructions in the basic block. |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 98 | /// |
| 99 | void visitBasicBlock(BasicBlock &LLVM_BB) { |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 100 | BB = MBBMap[&LLVM_BB]; |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 103 | |
| 104 | /// SelectPHINodes - Insert machine code to generate phis. This is tricky |
| 105 | /// because we have to generate our sources into the source basic blocks, |
| 106 | /// not the current one. |
| 107 | /// |
| 108 | void SelectPHINodes(); |
| 109 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 110 | // Visitation methods for various instructions. These methods simply emit |
| 111 | // fixed X86 code for each instruction. |
| 112 | // |
Brian Gaeke | fa8d571 | 2002-11-22 11:07:01 +0000 | [diff] [blame] | 113 | |
| 114 | // Control flow operators |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 115 | void visitReturnInst(ReturnInst &RI); |
Chris Lattner | 2df035b | 2002-11-02 19:27:56 +0000 | [diff] [blame] | 116 | void visitBranchInst(BranchInst &BI); |
Brian Gaeke | fa8d571 | 2002-11-22 11:07:01 +0000 | [diff] [blame] | 117 | void visitCallInst(CallInst &I); |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 118 | |
| 119 | // Arithmetic operators |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 120 | void visitSimpleBinary(BinaryOperator &B, unsigned OpcodeClass); |
Chris Lattner | 68aad93 | 2002-11-02 20:13:22 +0000 | [diff] [blame] | 121 | void visitAdd(BinaryOperator &B) { visitSimpleBinary(B, 0); } |
| 122 | void visitSub(BinaryOperator &B) { visitSimpleBinary(B, 1); } |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 123 | void doMultiply(unsigned destReg, const Type *resultType, |
Chris Lattner | f08ad9f | 2002-12-13 10:50:40 +0000 | [diff] [blame] | 124 | unsigned op0Reg, unsigned op1Reg, |
| 125 | MachineBasicBlock *MBB, |
| 126 | MachineBasicBlock::iterator &MBBI); |
Chris Lattner | ca9671d | 2002-11-02 20:28:58 +0000 | [diff] [blame] | 127 | void visitMul(BinaryOperator &B); |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 128 | |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 129 | void visitDiv(BinaryOperator &B) { visitDivRem(B); } |
| 130 | void visitRem(BinaryOperator &B) { visitDivRem(B); } |
| 131 | void visitDivRem(BinaryOperator &B); |
| 132 | |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 133 | // Bitwise operators |
Chris Lattner | 68aad93 | 2002-11-02 20:13:22 +0000 | [diff] [blame] | 134 | void visitAnd(BinaryOperator &B) { visitSimpleBinary(B, 2); } |
| 135 | void visitOr (BinaryOperator &B) { visitSimpleBinary(B, 3); } |
| 136 | void visitXor(BinaryOperator &B) { visitSimpleBinary(B, 4); } |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 137 | |
| 138 | // Binary comparison operators |
Chris Lattner | 05093a5 | 2002-11-21 15:52:38 +0000 | [diff] [blame] | 139 | void visitSetCCInst(SetCondInst &I, unsigned OpNum); |
| 140 | void visitSetEQ(SetCondInst &I) { visitSetCCInst(I, 0); } |
| 141 | void visitSetNE(SetCondInst &I) { visitSetCCInst(I, 1); } |
| 142 | void visitSetLT(SetCondInst &I) { visitSetCCInst(I, 2); } |
| 143 | void visitSetGT(SetCondInst &I) { visitSetCCInst(I, 3); } |
| 144 | void visitSetLE(SetCondInst &I) { visitSetCCInst(I, 4); } |
| 145 | void visitSetGE(SetCondInst &I) { visitSetCCInst(I, 5); } |
Chris Lattner | 6fc3c52 | 2002-11-17 21:11:55 +0000 | [diff] [blame] | 146 | |
| 147 | // Memory Instructions |
| 148 | void visitLoadInst(LoadInst &I); |
| 149 | void visitStoreInst(StoreInst &I); |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 150 | void visitGetElementPtrInst(GetElementPtrInst &I); |
| 151 | void visitMallocInst(MallocInst &I); |
Brian Gaeke | e48ec01 | 2002-12-13 06:46:31 +0000 | [diff] [blame] | 152 | void visitFreeInst(FreeInst &I); |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 153 | void visitAllocaInst(AllocaInst &I); |
| 154 | |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 155 | // Other operators |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 156 | void visitShiftInst(ShiftInst &I); |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 157 | void visitPHINode(PHINode &I) {} // PHI nodes handled by second pass |
Brian Gaeke | fa8d571 | 2002-11-22 11:07:01 +0000 | [diff] [blame] | 158 | void visitCastInst(CastInst &I); |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 159 | |
| 160 | void visitInstruction(Instruction &I) { |
| 161 | std::cerr << "Cannot instruction select: " << I; |
| 162 | abort(); |
| 163 | } |
| 164 | |
Brian Gaeke | 95780cc | 2002-12-13 07:56:18 +0000 | [diff] [blame] | 165 | /// promote32 - Make a value 32-bits wide, and put it somewhere. |
| 166 | void promote32 (const unsigned targetReg, Value *v); |
| 167 | |
| 168 | // emitGEPOperation - Common code shared between visitGetElementPtrInst and |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 169 | // constant expression GEP support. |
| 170 | // |
Chris Lattner | f08ad9f | 2002-12-13 10:50:40 +0000 | [diff] [blame] | 171 | void emitGEPOperation(MachineBasicBlock *BB, MachineBasicBlock::iterator&IP, |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 172 | Value *Src, User::op_iterator IdxBegin, |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 173 | User::op_iterator IdxEnd, unsigned TargetReg); |
| 174 | |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 175 | /// copyConstantToRegister - Output the instructions required to put the |
| 176 | /// specified constant into the specified register. |
| 177 | /// |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 178 | void copyConstantToRegister(Constant *C, unsigned Reg, |
| 179 | MachineBasicBlock *MBB, |
Chris Lattner | f08ad9f | 2002-12-13 10:50:40 +0000 | [diff] [blame] | 180 | MachineBasicBlock::iterator &MBBI); |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 181 | |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 182 | /// makeAnotherReg - This method returns the next register number |
| 183 | /// we haven't yet used. |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 184 | unsigned makeAnotherReg(const Type *Ty) { |
| 185 | // Add the mapping of regnumber => reg class to MachineFunction |
| 186 | F->addRegMap(CurReg, TM.getRegisterInfo()->getRegClassForType(Ty)); |
| 187 | return CurReg++; |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 190 | /// getReg - This method turns an LLVM value into a register number. This |
| 191 | /// is guaranteed to produce the same register number for a particular value |
| 192 | /// every time it is queried. |
| 193 | /// |
| 194 | unsigned getReg(Value &V) { return getReg(&V); } // Allow references |
Chris Lattner | f08ad9f | 2002-12-13 10:50:40 +0000 | [diff] [blame] | 195 | unsigned getReg(Value *V) { |
| 196 | // Just append to the end of the current bb. |
| 197 | MachineBasicBlock::iterator It = BB->end(); |
| 198 | return getReg(V, BB, It); |
| 199 | } |
Brian Gaeke | 71794c0 | 2002-12-13 11:22:48 +0000 | [diff] [blame] | 200 | unsigned getReg(Value *V, MachineBasicBlock *MBB, |
Chris Lattner | f08ad9f | 2002-12-13 10:50:40 +0000 | [diff] [blame] | 201 | MachineBasicBlock::iterator &IPt) { |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 202 | unsigned &Reg = RegMap[V]; |
Misha Brukman | d2cc017 | 2002-11-20 00:58:23 +0000 | [diff] [blame] | 203 | if (Reg == 0) { |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 204 | Reg = makeAnotherReg(V->getType()); |
Misha Brukman | d2cc017 | 2002-11-20 00:58:23 +0000 | [diff] [blame] | 205 | RegMap[V] = Reg; |
Misha Brukman | d2cc017 | 2002-11-20 00:58:23 +0000 | [diff] [blame] | 206 | } |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 207 | |
Chris Lattner | 6f8fd25 | 2002-10-27 21:23:43 +0000 | [diff] [blame] | 208 | // If this operand is a constant, emit the code to copy the constant into |
| 209 | // the register here... |
| 210 | // |
Chris Lattner | dbf30f7 | 2002-12-04 06:45:19 +0000 | [diff] [blame] | 211 | if (Constant *C = dyn_cast<Constant>(V)) { |
Brian Gaeke | 992447f | 2002-12-13 11:39:18 +0000 | [diff] [blame] | 212 | copyConstantToRegister(C, Reg, MBB, IPt); |
Chris Lattner | dbf30f7 | 2002-12-04 06:45:19 +0000 | [diff] [blame] | 213 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 214 | // Move the address of the global into the register |
Brian Gaeke | 71794c0 | 2002-12-13 11:22:48 +0000 | [diff] [blame] | 215 | BMI(MBB, IPt, X86::MOVir32, 1, Reg).addReg(GV); |
Chris Lattner | d6c4cfa | 2002-12-04 17:15:34 +0000 | [diff] [blame] | 216 | } else if (Argument *A = dyn_cast<Argument>(V)) { |
Brian Gaeke | 95780cc | 2002-12-13 07:56:18 +0000 | [diff] [blame] | 217 | // Find the position of the argument in the argument list. |
| 218 | const Function *f = F->getFunction (); |
Brian Gaeke | ed6902c | 2002-12-13 09:28:50 +0000 | [diff] [blame] | 219 | // The function's arguments look like this: |
| 220 | // [EBP] -- copy of old EBP |
| 221 | // [EBP + 4] -- return address |
| 222 | // [EBP + 8] -- first argument (leftmost lexically) |
| 223 | // So we want to start with counter = 2. |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 224 | int counter = 2, argPos = -1; |
Brian Gaeke | 95780cc | 2002-12-13 07:56:18 +0000 | [diff] [blame] | 225 | for (Function::const_aiterator ai = f->abegin (), ae = f->aend (); |
| 226 | ai != ae; ++ai) { |
Brian Gaeke | 95780cc | 2002-12-13 07:56:18 +0000 | [diff] [blame] | 227 | if (&(*ai) == A) { |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 228 | argPos = counter; |
Brian Gaeke | ed6902c | 2002-12-13 09:28:50 +0000 | [diff] [blame] | 229 | break; // Only need to find it once. ;-) |
Brian Gaeke | 95780cc | 2002-12-13 07:56:18 +0000 | [diff] [blame] | 230 | } |
Brian Gaeke | ed6902c | 2002-12-13 09:28:50 +0000 | [diff] [blame] | 231 | ++counter; |
Brian Gaeke | 95780cc | 2002-12-13 07:56:18 +0000 | [diff] [blame] | 232 | } |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 233 | assert (argPos != -1 |
Brian Gaeke | 95780cc | 2002-12-13 07:56:18 +0000 | [diff] [blame] | 234 | && "Argument not found in current function's argument list"); |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 235 | // Load it out of the stack frame at EBP + 4*argPos. |
Brian Gaeke | 71794c0 | 2002-12-13 11:22:48 +0000 | [diff] [blame] | 236 | addRegOffset(BMI(MBB, IPt, X86::MOVmr32, 4, Reg), X86::EBP, 4*argPos); |
Chris Lattner | dbf30f7 | 2002-12-04 06:45:19 +0000 | [diff] [blame] | 237 | } |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 238 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 239 | return Reg; |
| 240 | } |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 241 | }; |
| 242 | } |
| 243 | |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame] | 244 | /// TypeClass - Used by the X86 backend to group LLVM types by their basic X86 |
| 245 | /// Representation. |
| 246 | /// |
| 247 | enum TypeClass { |
| 248 | cByte, cShort, cInt, cLong, cFloat, cDouble |
| 249 | }; |
| 250 | |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 251 | /// getClass - Turn a primitive type into a "class" number which is based on the |
| 252 | /// size of the type, and whether or not it is floating point. |
| 253 | /// |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame] | 254 | static inline TypeClass getClass(const Type *Ty) { |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 255 | switch (Ty->getPrimitiveID()) { |
| 256 | case Type::SByteTyID: |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame] | 257 | case Type::UByteTyID: return cByte; // Byte operands are class #0 |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 258 | case Type::ShortTyID: |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame] | 259 | case Type::UShortTyID: return cShort; // Short operands are class #1 |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 260 | case Type::IntTyID: |
| 261 | case Type::UIntTyID: |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame] | 262 | case Type::PointerTyID: return cInt; // Int's and pointers are class #2 |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 263 | |
| 264 | case Type::LongTyID: |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 265 | case Type::ULongTyID: //return cLong; // Longs are class #3 |
| 266 | return cInt; // FIXME: LONGS ARE TREATED AS INTS! |
| 267 | |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame] | 268 | case Type::FloatTyID: return cFloat; // Float is class #4 |
| 269 | case Type::DoubleTyID: return cDouble; // Doubles are class #5 |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 270 | default: |
| 271 | assert(0 && "Invalid type to getClass!"); |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame] | 272 | return cByte; // not reached |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 275 | |
Chris Lattner | 6b993cc | 2002-12-15 08:02:15 +0000 | [diff] [blame^] | 276 | // getClassB - Just like getClass, but treat boolean values as bytes. |
| 277 | static inline TypeClass getClassB(const Type *Ty) { |
| 278 | if (Ty == Type::BoolTy) return cByte; |
| 279 | return getClass(Ty); |
| 280 | } |
| 281 | |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 282 | |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 283 | /// copyConstantToRegister - Output the instructions required to put the |
| 284 | /// specified constant into the specified register. |
| 285 | /// |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 286 | void ISel::copyConstantToRegister(Constant *C, unsigned R, |
Brian Gaeke | 71794c0 | 2002-12-13 11:22:48 +0000 | [diff] [blame] | 287 | MachineBasicBlock *MBB, |
Chris Lattner | f08ad9f | 2002-12-13 10:50:40 +0000 | [diff] [blame] | 288 | MachineBasicBlock::iterator &IP) { |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 289 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 290 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 291 | emitGEPOperation(BB, IP, CE->getOperand(0), |
| 292 | CE->op_begin()+1, CE->op_end(), R); |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 293 | return; |
| 294 | } |
| 295 | |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 296 | std::cerr << "Offending expr: " << C << "\n"; |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 297 | assert (0 && "Constant expressions not yet handled!\n"); |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 298 | } |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 299 | |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 300 | if (C->getType()->isIntegral()) { |
Chris Lattner | 6b993cc | 2002-12-15 08:02:15 +0000 | [diff] [blame^] | 301 | unsigned Class = getClassB(C->getType()); |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 302 | assert(Class != 3 && "Type not handled yet!"); |
| 303 | |
| 304 | static const unsigned IntegralOpcodeTab[] = { |
| 305 | X86::MOVir8, X86::MOVir16, X86::MOVir32 |
| 306 | }; |
| 307 | |
Chris Lattner | 6b993cc | 2002-12-15 08:02:15 +0000 | [diff] [blame^] | 308 | if (C->getType() == Type::BoolTy) { |
| 309 | BMI(MBB, IP, X86::MOVir8, 1, R).addZImm(C == ConstantBool::True); |
| 310 | } else if (C->getType()->isSigned()) { |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 311 | ConstantSInt *CSI = cast<ConstantSInt>(C); |
Brian Gaeke | 71794c0 | 2002-12-13 11:22:48 +0000 | [diff] [blame] | 312 | BMI(MBB, IP, IntegralOpcodeTab[Class], 1, R).addSImm(CSI->getValue()); |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 313 | } else { |
| 314 | ConstantUInt *CUI = cast<ConstantUInt>(C); |
Brian Gaeke | 71794c0 | 2002-12-13 11:22:48 +0000 | [diff] [blame] | 315 | BMI(MBB, IP, IntegralOpcodeTab[Class], 1, R).addZImm(CUI->getValue()); |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 316 | } |
Chris Lattner | f08ad9f | 2002-12-13 10:50:40 +0000 | [diff] [blame] | 317 | } else if (isa<ConstantPointerNull>(C)) { |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 318 | // Copy zero (null pointer) to the register. |
Brian Gaeke | 71794c0 | 2002-12-13 11:22:48 +0000 | [diff] [blame] | 319 | BMI(MBB, IP, X86::MOVir32, 1, R).addZImm(0); |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 320 | } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)) { |
Chris Lattner | f08ad9f | 2002-12-13 10:50:40 +0000 | [diff] [blame] | 321 | unsigned SrcReg = getReg(CPR->getValue(), BB, IP); |
Brian Gaeke | 71794c0 | 2002-12-13 11:22:48 +0000 | [diff] [blame] | 322 | BMI(MBB, IP, X86::MOVrr32, 1, R).addReg(SrcReg); |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 323 | } else { |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 324 | std::cerr << "Offending constant: " << C << "\n"; |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 325 | assert(0 && "Type not handled yet!"); |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 329 | /// SelectPHINodes - Insert machine code to generate phis. This is tricky |
| 330 | /// because we have to generate our sources into the source basic blocks, not |
| 331 | /// the current one. |
| 332 | /// |
| 333 | void ISel::SelectPHINodes() { |
| 334 | const Function &LF = *F->getFunction(); // The LLVM function... |
| 335 | for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) { |
| 336 | const BasicBlock *BB = I; |
| 337 | MachineBasicBlock *MBB = MBBMap[I]; |
| 338 | |
| 339 | // Loop over all of the PHI nodes in the LLVM basic block... |
| 340 | unsigned NumPHIs = 0; |
| 341 | for (BasicBlock::const_iterator I = BB->begin(); |
| 342 | PHINode *PN = (PHINode*)dyn_cast<PHINode>(&*I); ++I) { |
| 343 | // Create a new machine instr PHI node, and insert it. |
| 344 | MachineInstr *MI = BuildMI(X86::PHI, PN->getNumOperands(), getReg(*PN)); |
| 345 | MBB->insert(MBB->begin()+NumPHIs++, MI); // Insert it at the top of the BB |
| 346 | |
| 347 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 348 | MachineBasicBlock *PredMBB = MBBMap[PN->getIncomingBlock(i)]; |
| 349 | |
| 350 | // Get the incoming value into a virtual register. If it is not already |
| 351 | // available in a virtual register, insert the computation code into |
| 352 | // PredMBB |
Chris Lattner | 9205363 | 2002-12-13 11:52:34 +0000 | [diff] [blame] | 353 | // |
| 354 | |
| 355 | MachineBasicBlock::iterator PI = PredMBB->begin(); |
| 356 | while ((*PI)->getOpcode() == X86::PHI) ++PI; |
| 357 | |
Chris Lattner | f08ad9f | 2002-12-13 10:50:40 +0000 | [diff] [blame] | 358 | MI->addRegOperand(getReg(PN->getIncomingValue(i), PredMBB, PI)); |
Chris Lattner | 6b993cc | 2002-12-15 08:02:15 +0000 | [diff] [blame^] | 359 | MI->addMachineBasicBlockOperand(PredMBB); |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 366 | |
Brian Gaeke | 1749d63 | 2002-11-07 17:59:21 +0000 | [diff] [blame] | 367 | /// SetCC instructions - Here we just emit boilerplate code to set a byte-sized |
| 368 | /// register, then move it to wherever the result should be. |
| 369 | /// We handle FP setcc instructions by pushing them, doing a |
| 370 | /// compare-and-pop-twice, and then copying the concodes to the main |
| 371 | /// processor's concodes (I didn't make this up, it's in the Intel manual) |
| 372 | /// |
Chris Lattner | 05093a5 | 2002-11-21 15:52:38 +0000 | [diff] [blame] | 373 | void ISel::visitSetCCInst(SetCondInst &I, unsigned OpNum) { |
Brian Gaeke | 1749d63 | 2002-11-07 17:59:21 +0000 | [diff] [blame] | 374 | // The arguments are already supposed to be of the same type. |
Chris Lattner | 05093a5 | 2002-11-21 15:52:38 +0000 | [diff] [blame] | 375 | const Type *CompTy = I.getOperand(0)->getType(); |
| 376 | unsigned reg1 = getReg(I.getOperand(0)); |
| 377 | unsigned reg2 = getReg(I.getOperand(1)); |
| 378 | |
| 379 | unsigned Class = getClass(CompTy); |
| 380 | switch (Class) { |
| 381 | // Emit: cmp <var1>, <var2> (do the comparison). We can |
| 382 | // compare 8-bit with 8-bit, 16-bit with 16-bit, 32-bit with |
| 383 | // 32-bit. |
| 384 | case cByte: |
| 385 | BuildMI (BB, X86::CMPrr8, 2).addReg (reg1).addReg (reg2); |
| 386 | break; |
| 387 | case cShort: |
| 388 | BuildMI (BB, X86::CMPrr16, 2).addReg (reg1).addReg (reg2); |
| 389 | break; |
| 390 | case cInt: |
| 391 | BuildMI (BB, X86::CMPrr32, 2).addReg (reg1).addReg (reg2); |
| 392 | break; |
| 393 | |
| 394 | // Push the variables on the stack with fldl opcodes. |
| 395 | // FIXME: assuming var1, var2 are in memory, if not, spill to |
| 396 | // stack first |
| 397 | case cFloat: // Floats |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 398 | BuildMI (BB, X86::FLDr32, 1).addReg (reg1); |
| 399 | BuildMI (BB, X86::FLDr32, 1).addReg (reg2); |
Chris Lattner | 05093a5 | 2002-11-21 15:52:38 +0000 | [diff] [blame] | 400 | break; |
| 401 | case cDouble: // Doubles |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 402 | BuildMI (BB, X86::FLDr64, 1).addReg (reg1); |
| 403 | BuildMI (BB, X86::FLDr64, 1).addReg (reg2); |
Chris Lattner | 05093a5 | 2002-11-21 15:52:38 +0000 | [diff] [blame] | 404 | break; |
| 405 | case cLong: |
| 406 | default: |
| 407 | visitInstruction(I); |
| 408 | } |
| 409 | |
| 410 | if (CompTy->isFloatingPoint()) { |
| 411 | // (Non-trapping) compare and pop twice. |
| 412 | BuildMI (BB, X86::FUCOMPP, 0); |
| 413 | // Move fp status word (concodes) to ax. |
| 414 | BuildMI (BB, X86::FNSTSWr8, 1, X86::AX); |
| 415 | // Load real concodes from ax. |
| 416 | BuildMI (BB, X86::SAHF, 1).addReg(X86::AH); |
| 417 | } |
| 418 | |
Brian Gaeke | 1749d63 | 2002-11-07 17:59:21 +0000 | [diff] [blame] | 419 | // Emit setOp instruction (extract concode; clobbers ax), |
| 420 | // using the following mapping: |
| 421 | // LLVM -> X86 signed X86 unsigned |
| 422 | // ----- ----- ----- |
| 423 | // seteq -> sete sete |
| 424 | // setne -> setne setne |
| 425 | // setlt -> setl setb |
| 426 | // setgt -> setg seta |
| 427 | // setle -> setle setbe |
| 428 | // setge -> setge setae |
Chris Lattner | 05093a5 | 2002-11-21 15:52:38 +0000 | [diff] [blame] | 429 | |
| 430 | static const unsigned OpcodeTab[2][6] = { |
Chris Lattner | 4b4e9dd | 2002-11-21 16:19:42 +0000 | [diff] [blame] | 431 | {X86::SETEr, X86::SETNEr, X86::SETBr, X86::SETAr, X86::SETBEr, X86::SETAEr}, |
| 432 | {X86::SETEr, X86::SETNEr, X86::SETLr, X86::SETGr, X86::SETLEr, X86::SETGEr}, |
Chris Lattner | 05093a5 | 2002-11-21 15:52:38 +0000 | [diff] [blame] | 433 | }; |
| 434 | |
| 435 | BuildMI(BB, OpcodeTab[CompTy->isSigned()][OpNum], 0, X86::AL); |
| 436 | |
Brian Gaeke | 1749d63 | 2002-11-07 17:59:21 +0000 | [diff] [blame] | 437 | // Put it in the result using a move. |
Chris Lattner | 05093a5 | 2002-11-21 15:52:38 +0000 | [diff] [blame] | 438 | BuildMI (BB, X86::MOVrr8, 1, getReg(I)).addReg(X86::AL); |
Brian Gaeke | 1749d63 | 2002-11-07 17:59:21 +0000 | [diff] [blame] | 439 | } |
Chris Lattner | 51b49a9 | 2002-11-02 19:45:49 +0000 | [diff] [blame] | 440 | |
Brian Gaeke | c250598 | 2002-11-30 11:57:28 +0000 | [diff] [blame] | 441 | /// promote32 - Emit instructions to turn a narrow operand into a 32-bit-wide |
| 442 | /// operand, in the specified target register. |
| 443 | void |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 444 | ISel::promote32 (unsigned targetReg, Value *v) |
Brian Gaeke | c250598 | 2002-11-30 11:57:28 +0000 | [diff] [blame] | 445 | { |
| 446 | unsigned vReg = getReg (v); |
| 447 | unsigned Class = getClass (v->getType ()); |
| 448 | bool isUnsigned = v->getType ()->isUnsigned (); |
| 449 | assert (((Class == cByte) || (Class == cShort) || (Class == cInt)) |
| 450 | && "Unpromotable operand class in promote32"); |
| 451 | switch (Class) |
| 452 | { |
| 453 | case cByte: |
| 454 | // Extend value into target register (8->32) |
| 455 | if (isUnsigned) |
| 456 | BuildMI (BB, X86::MOVZXr32r8, 1, targetReg).addReg (vReg); |
| 457 | else |
| 458 | BuildMI (BB, X86::MOVSXr32r8, 1, targetReg).addReg (vReg); |
| 459 | break; |
| 460 | case cShort: |
| 461 | // Extend value into target register (16->32) |
| 462 | if (isUnsigned) |
| 463 | BuildMI (BB, X86::MOVZXr32r16, 1, targetReg).addReg (vReg); |
| 464 | else |
| 465 | BuildMI (BB, X86::MOVSXr32r16, 1, targetReg).addReg (vReg); |
| 466 | break; |
| 467 | case cInt: |
| 468 | // Move value into target register (32->32) |
| 469 | BuildMI (BB, X86::MOVrr32, 1, targetReg).addReg (vReg); |
| 470 | break; |
| 471 | } |
| 472 | } |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 473 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 474 | /// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such, |
| 475 | /// we have the following possibilities: |
| 476 | /// |
| 477 | /// ret void: No return value, simply emit a 'ret' instruction |
| 478 | /// ret sbyte, ubyte : Extend value into EAX and return |
| 479 | /// ret short, ushort: Extend value into EAX and return |
| 480 | /// ret int, uint : Move value into EAX and return |
| 481 | /// ret pointer : Move value into EAX and return |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 482 | /// ret long, ulong : Move value into EAX/EDX and return |
| 483 | /// ret float/double : Top of FP stack |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 484 | /// |
Brian Gaeke | c250598 | 2002-11-30 11:57:28 +0000 | [diff] [blame] | 485 | void |
| 486 | ISel::visitReturnInst (ReturnInst &I) |
| 487 | { |
| 488 | if (I.getNumOperands () == 0) |
| 489 | { |
| 490 | // Emit a 'ret' instruction |
| 491 | BuildMI (BB, X86::RET, 0); |
| 492 | return; |
| 493 | } |
| 494 | Value *rv = I.getOperand (0); |
| 495 | unsigned Class = getClass (rv->getType ()); |
| 496 | switch (Class) |
| 497 | { |
| 498 | // integral return values: extend or move into EAX and return. |
| 499 | case cByte: |
| 500 | case cShort: |
| 501 | case cInt: |
| 502 | promote32 (X86::EAX, rv); |
| 503 | break; |
| 504 | // ret float/double: top of FP stack |
| 505 | // FLD <val> |
| 506 | case cFloat: // Floats |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 507 | BuildMI (BB, X86::FLDr32, 1).addReg (getReg (rv)); |
Brian Gaeke | c250598 | 2002-11-30 11:57:28 +0000 | [diff] [blame] | 508 | break; |
| 509 | case cDouble: // Doubles |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 510 | BuildMI (BB, X86::FLDr64, 1).addReg (getReg (rv)); |
Brian Gaeke | c250598 | 2002-11-30 11:57:28 +0000 | [diff] [blame] | 511 | break; |
| 512 | case cLong: |
| 513 | // ret long: use EAX(least significant 32 bits)/EDX (most |
| 514 | // significant 32)...uh, I think so Brain, but how do i call |
| 515 | // up the two parts of the value from inside this mouse |
| 516 | // cage? *zort* |
| 517 | default: |
| 518 | visitInstruction (I); |
| 519 | } |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame] | 520 | // Emit a 'ret' instruction |
Brian Gaeke | c250598 | 2002-11-30 11:57:28 +0000 | [diff] [blame] | 521 | BuildMI (BB, X86::RET, 0); |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Chris Lattner | 51b49a9 | 2002-11-02 19:45:49 +0000 | [diff] [blame] | 524 | /// visitBranchInst - Handle conditional and unconditional branches here. Note |
| 525 | /// that since code layout is frozen at this point, that if we are trying to |
| 526 | /// jump to a block that is the immediate successor of the current block, we can |
| 527 | /// just make a fall-through. (but we don't currently). |
| 528 | /// |
Brian Gaeke | c03a0cb | 2002-11-19 09:08:47 +0000 | [diff] [blame] | 529 | void |
| 530 | ISel::visitBranchInst (BranchInst & BI) |
| 531 | { |
| 532 | if (BI.isConditional ()) |
| 533 | { |
| 534 | BasicBlock *ifTrue = BI.getSuccessor (0); |
| 535 | BasicBlock *ifFalse = BI.getSuccessor (1); // this is really unobvious |
Chris Lattner | 2df035b | 2002-11-02 19:27:56 +0000 | [diff] [blame] | 536 | |
Brian Gaeke | c03a0cb | 2002-11-19 09:08:47 +0000 | [diff] [blame] | 537 | // simplest thing I can think of: compare condition with zero, |
| 538 | // followed by jump-if-equal to ifFalse, and jump-if-nonequal to |
| 539 | // ifTrue |
| 540 | unsigned int condReg = getReg (BI.getCondition ()); |
Chris Lattner | 97ad9e1 | 2002-11-21 01:59:50 +0000 | [diff] [blame] | 541 | BuildMI (BB, X86::CMPri8, 2).addReg (condReg).addZImm (0); |
Brian Gaeke | c03a0cb | 2002-11-19 09:08:47 +0000 | [diff] [blame] | 542 | BuildMI (BB, X86::JNE, 1).addPCDisp (BI.getSuccessor (0)); |
| 543 | BuildMI (BB, X86::JE, 1).addPCDisp (BI.getSuccessor (1)); |
| 544 | } |
| 545 | else // unconditional branch |
| 546 | { |
| 547 | BuildMI (BB, X86::JMP, 1).addPCDisp (BI.getSuccessor (0)); |
| 548 | } |
Chris Lattner | 2df035b | 2002-11-02 19:27:56 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Brian Gaeke | 18a2021 | 2002-11-29 12:01:58 +0000 | [diff] [blame] | 551 | /// visitCallInst - Push args on stack and do a procedure call instruction. |
| 552 | void |
| 553 | ISel::visitCallInst (CallInst & CI) |
| 554 | { |
Misha Brukman | 0d2cf3a | 2002-12-04 19:22:53 +0000 | [diff] [blame] | 555 | // keep a counter of how many bytes we pushed on the stack |
| 556 | unsigned bytesPushed = 0; |
| 557 | |
Brian Gaeke | 18a2021 | 2002-11-29 12:01:58 +0000 | [diff] [blame] | 558 | // Push the arguments on the stack in reverse order, as specified by |
| 559 | // the ABI. |
Chris Lattner | d852c15 | 2002-12-03 20:30:12 +0000 | [diff] [blame] | 560 | for (unsigned i = CI.getNumOperands()-1; i >= 1; --i) |
Brian Gaeke | 18a2021 | 2002-11-29 12:01:58 +0000 | [diff] [blame] | 561 | { |
| 562 | Value *v = CI.getOperand (i); |
Brian Gaeke | 18a2021 | 2002-11-29 12:01:58 +0000 | [diff] [blame] | 563 | switch (getClass (v->getType ())) |
| 564 | { |
Brian Gaeke | c250598 | 2002-11-30 11:57:28 +0000 | [diff] [blame] | 565 | case cByte: |
| 566 | case cShort: |
Brian Gaeke | bb25f2f | 2002-12-03 00:51:09 +0000 | [diff] [blame] | 567 | // Promote V to 32 bits wide, and move the result into EAX, |
| 568 | // then push EAX. |
Brian Gaeke | c250598 | 2002-11-30 11:57:28 +0000 | [diff] [blame] | 569 | promote32 (X86::EAX, v); |
| 570 | BuildMI (BB, X86::PUSHr32, 1).addReg (X86::EAX); |
Misha Brukman | 0d2cf3a | 2002-12-04 19:22:53 +0000 | [diff] [blame] | 571 | bytesPushed += 4; |
Brian Gaeke | c250598 | 2002-11-30 11:57:28 +0000 | [diff] [blame] | 572 | break; |
Brian Gaeke | 18a2021 | 2002-11-29 12:01:58 +0000 | [diff] [blame] | 573 | case cInt: |
Chris Lattner | 33ced56 | 2002-12-04 06:56:56 +0000 | [diff] [blame] | 574 | case cFloat: { |
| 575 | unsigned Reg = getReg(v); |
| 576 | BuildMI (BB, X86::PUSHr32, 1).addReg(Reg); |
Misha Brukman | 0d2cf3a | 2002-12-04 19:22:53 +0000 | [diff] [blame] | 577 | bytesPushed += 4; |
Brian Gaeke | 18a2021 | 2002-11-29 12:01:58 +0000 | [diff] [blame] | 578 | break; |
Chris Lattner | 33ced56 | 2002-12-04 06:56:56 +0000 | [diff] [blame] | 579 | } |
Brian Gaeke | 18a2021 | 2002-11-29 12:01:58 +0000 | [diff] [blame] | 580 | default: |
Brian Gaeke | bb25f2f | 2002-12-03 00:51:09 +0000 | [diff] [blame] | 581 | // FIXME: long/ulong/double args not handled. |
Brian Gaeke | 18a2021 | 2002-11-29 12:01:58 +0000 | [diff] [blame] | 582 | visitInstruction (CI); |
| 583 | break; |
| 584 | } |
| 585 | } |
Chris Lattner | 6e49a4b | 2002-12-13 14:13:27 +0000 | [diff] [blame] | 586 | |
| 587 | if (Function *F = CI.getCalledFunction()) { |
| 588 | // Emit a CALL instruction with PC-relative displacement. |
| 589 | BuildMI(BB, X86::CALLpcrel32, 1).addPCDisp(F); |
| 590 | } else { |
| 591 | unsigned Reg = getReg(CI.getCalledValue()); |
| 592 | BuildMI(BB, X86::CALLr32, 1).addReg(Reg); |
| 593 | } |
Misha Brukman | 0d2cf3a | 2002-12-04 19:22:53 +0000 | [diff] [blame] | 594 | |
| 595 | // Adjust the stack by `bytesPushed' amount if non-zero |
| 596 | if (bytesPushed > 0) |
| 597 | BuildMI (BB, X86::ADDri32, 2).addReg(X86::ESP).addZImm(bytesPushed); |
Chris Lattner | a324364 | 2002-12-04 23:45:28 +0000 | [diff] [blame] | 598 | |
| 599 | // If there is a return value, scavenge the result from the location the call |
| 600 | // leaves it in... |
| 601 | // |
Chris Lattner | 4fa1acc | 2002-12-04 23:50:28 +0000 | [diff] [blame] | 602 | if (CI.getType() != Type::VoidTy) { |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 603 | unsigned resultTypeClass = getClass (CI.getType ()); |
| 604 | switch (resultTypeClass) { |
| 605 | case cByte: |
| 606 | case cShort: |
| 607 | case cInt: { |
| 608 | // Integral results are in %eax, or the appropriate portion |
| 609 | // thereof. |
| 610 | static const unsigned regRegMove[] = { |
| 611 | X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 |
| 612 | }; |
| 613 | static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX }; |
| 614 | BuildMI (BB, regRegMove[resultTypeClass], 1, |
| 615 | getReg (CI)).addReg (AReg[resultTypeClass]); |
Chris Lattner | 4fa1acc | 2002-12-04 23:50:28 +0000 | [diff] [blame] | 616 | break; |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 617 | } |
| 618 | case cFloat: |
| 619 | // Floating-point return values live in %st(0) (i.e., the top of |
| 620 | // the FP stack.) The general way to approach this is to do a |
| 621 | // FSTP to save the top of the FP stack on the real stack, then |
| 622 | // do a MOV to load the top of the real stack into the target |
| 623 | // register. |
| 624 | visitInstruction (CI); // FIXME: add the right args for the calls below |
| 625 | // BuildMI (BB, X86::FSTPm32, 0); |
| 626 | // BuildMI (BB, X86::MOVmr32, 0); |
| 627 | break; |
Chris Lattner | 4fa1acc | 2002-12-04 23:50:28 +0000 | [diff] [blame] | 628 | default: |
| 629 | std::cerr << "Cannot get return value for call of type '" |
| 630 | << *CI.getType() << "'\n"; |
| 631 | visitInstruction(CI); |
| 632 | } |
Chris Lattner | a324364 | 2002-12-04 23:45:28 +0000 | [diff] [blame] | 633 | } |
Brian Gaeke | fa8d571 | 2002-11-22 11:07:01 +0000 | [diff] [blame] | 634 | } |
Chris Lattner | 2df035b | 2002-11-02 19:27:56 +0000 | [diff] [blame] | 635 | |
Chris Lattner | 68aad93 | 2002-11-02 20:13:22 +0000 | [diff] [blame] | 636 | /// visitSimpleBinary - Implement simple binary operators for integral types... |
| 637 | /// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or, |
| 638 | /// 4 for Xor. |
| 639 | /// |
| 640 | void ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) { |
| 641 | if (B.getType() == Type::BoolTy) // FIXME: Handle bools for logicals |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 642 | visitInstruction(B); |
| 643 | |
| 644 | unsigned Class = getClass(B.getType()); |
| 645 | if (Class > 2) // FIXME: Handle longs |
| 646 | visitInstruction(B); |
| 647 | |
| 648 | static const unsigned OpcodeTab[][4] = { |
Chris Lattner | 68aad93 | 2002-11-02 20:13:22 +0000 | [diff] [blame] | 649 | // Arithmetic operators |
| 650 | { X86::ADDrr8, X86::ADDrr16, X86::ADDrr32, 0 }, // ADD |
| 651 | { X86::SUBrr8, X86::SUBrr16, X86::SUBrr32, 0 }, // SUB |
| 652 | |
| 653 | // Bitwise operators |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 654 | { X86::ANDrr8, X86::ANDrr16, X86::ANDrr32, 0 }, // AND |
| 655 | { X86:: ORrr8, X86:: ORrr16, X86:: ORrr32, 0 }, // OR |
| 656 | { X86::XORrr8, X86::XORrr16, X86::XORrr32, 0 }, // XOR |
| 657 | }; |
| 658 | |
| 659 | unsigned Opcode = OpcodeTab[OperatorClass][Class]; |
| 660 | unsigned Op0r = getReg(B.getOperand(0)); |
| 661 | unsigned Op1r = getReg(B.getOperand(1)); |
| 662 | BuildMI(BB, Opcode, 2, getReg(B)).addReg(Op0r).addReg(Op1r); |
| 663 | } |
| 664 | |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 665 | /// doMultiply - Emit appropriate instructions to multiply together |
| 666 | /// the registers op0Reg and op1Reg, and put the result in destReg. |
| 667 | /// The type of the result should be given as resultType. |
| 668 | void |
| 669 | ISel::doMultiply(unsigned destReg, const Type *resultType, |
Chris Lattner | f08ad9f | 2002-12-13 10:50:40 +0000 | [diff] [blame] | 670 | unsigned op0Reg, unsigned op1Reg, |
| 671 | MachineBasicBlock *MBB, MachineBasicBlock::iterator &MBBI) |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 672 | { |
| 673 | unsigned Class = getClass (resultType); |
| 674 | |
| 675 | // FIXME: |
| 676 | assert (Class <= 2 && "Someday, we will learn how to multiply" |
| 677 | "longs and floating-point numbers. This is not that day."); |
| 678 | |
| 679 | static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX }; |
| 680 | static const unsigned MulOpcode[]={ X86::MULrr8, X86::MULrr16, X86::MULrr32 }; |
| 681 | static const unsigned MovOpcode[]={ X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 }; |
| 682 | unsigned Reg = Regs[Class]; |
| 683 | |
| 684 | // Emit a MOV to put the first operand into the appropriately-sized |
| 685 | // subreg of EAX. |
Brian Gaeke | 71794c0 | 2002-12-13 11:22:48 +0000 | [diff] [blame] | 686 | BMI(MBB, MBBI, MovOpcode[Class], 1, Reg).addReg (op0Reg); |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 687 | |
| 688 | // Emit the appropriate multiply instruction. |
Brian Gaeke | 71794c0 | 2002-12-13 11:22:48 +0000 | [diff] [blame] | 689 | BMI(MBB, MBBI, MulOpcode[Class], 1).addReg (op1Reg); |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 690 | |
| 691 | // Emit another MOV to put the result into the destination register. |
Brian Gaeke | 71794c0 | 2002-12-13 11:22:48 +0000 | [diff] [blame] | 692 | BMI(MBB, MBBI, MovOpcode[Class], 1, destReg).addReg (Reg); |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Chris Lattner | ca9671d | 2002-11-02 20:28:58 +0000 | [diff] [blame] | 695 | /// visitMul - Multiplies are not simple binary operators because they must deal |
| 696 | /// with the EAX register explicitly. |
| 697 | /// |
| 698 | void ISel::visitMul(BinaryOperator &I) { |
Chris Lattner | 202a2d0 | 2002-12-13 13:07:42 +0000 | [diff] [blame] | 699 | unsigned DestReg = getReg(I); |
| 700 | unsigned Op0Reg = getReg(I.getOperand(0)); |
| 701 | unsigned Op1Reg = getReg(I.getOperand(1)); |
Chris Lattner | f08ad9f | 2002-12-13 10:50:40 +0000 | [diff] [blame] | 702 | MachineBasicBlock::iterator MBBI = BB->end(); |
Chris Lattner | 202a2d0 | 2002-12-13 13:07:42 +0000 | [diff] [blame] | 703 | doMultiply(DestReg, I.getType(), Op0Reg, Op1Reg, BB, MBBI); |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 704 | } |
Chris Lattner | ca9671d | 2002-11-02 20:28:58 +0000 | [diff] [blame] | 705 | |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 706 | |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 707 | /// visitDivRem - Handle division and remainder instructions... these |
| 708 | /// instruction both require the same instructions to be generated, they just |
| 709 | /// select the result from a different register. Note that both of these |
| 710 | /// instructions work differently for signed and unsigned operands. |
| 711 | /// |
| 712 | void ISel::visitDivRem(BinaryOperator &I) { |
| 713 | unsigned Class = getClass(I.getType()); |
| 714 | if (Class > 2) // FIXME: Handle longs |
| 715 | visitInstruction(I); |
| 716 | |
| 717 | static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX }; |
| 718 | static const unsigned MovOpcode[]={ X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 }; |
Brian Gaeke | 6559bb9 | 2002-11-14 22:32:30 +0000 | [diff] [blame] | 719 | static const unsigned ExtOpcode[]={ X86::CBW , X86::CWD , X86::CDQ }; |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 720 | static const unsigned ClrOpcode[]={ X86::XORrr8, X86::XORrr16, X86::XORrr32 }; |
| 721 | static const unsigned ExtRegs[] ={ X86::AH , X86::DX , X86::EDX }; |
| 722 | |
| 723 | static const unsigned DivOpcode[][4] = { |
| 724 | { X86::DIVrr8 , X86::DIVrr16 , X86::DIVrr32 , 0 }, // Unsigned division |
| 725 | { X86::IDIVrr8, X86::IDIVrr16, X86::IDIVrr32, 0 }, // Signed division |
| 726 | }; |
| 727 | |
| 728 | bool isSigned = I.getType()->isSigned(); |
| 729 | unsigned Reg = Regs[Class]; |
| 730 | unsigned ExtReg = ExtRegs[Class]; |
Chris Lattner | 6fc3c52 | 2002-11-17 21:11:55 +0000 | [diff] [blame] | 731 | unsigned Op0Reg = getReg(I.getOperand(0)); |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 732 | unsigned Op1Reg = getReg(I.getOperand(1)); |
| 733 | |
| 734 | // Put the first operand into one of the A registers... |
| 735 | BuildMI(BB, MovOpcode[Class], 1, Reg).addReg(Op0Reg); |
| 736 | |
| 737 | if (isSigned) { |
| 738 | // Emit a sign extension instruction... |
Chris Lattner | a4978cc | 2002-12-01 23:24:58 +0000 | [diff] [blame] | 739 | BuildMI(BB, ExtOpcode[Class], 0); |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 740 | } else { |
| 741 | // If unsigned, emit a zeroing instruction... (reg = xor reg, reg) |
| 742 | BuildMI(BB, ClrOpcode[Class], 2, ExtReg).addReg(ExtReg).addReg(ExtReg); |
| 743 | } |
| 744 | |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 745 | // Emit the appropriate divide or remainder instruction... |
Chris Lattner | 92845e3 | 2002-11-21 18:54:29 +0000 | [diff] [blame] | 746 | BuildMI(BB, DivOpcode[isSigned][Class], 1).addReg(Op1Reg); |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 747 | |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 748 | // Figure out which register we want to pick the result out of... |
| 749 | unsigned DestReg = (I.getOpcode() == Instruction::Div) ? Reg : ExtReg; |
| 750 | |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 751 | // Put the result into the destination register... |
| 752 | BuildMI(BB, MovOpcode[Class], 1, getReg(I)).addReg(DestReg); |
Chris Lattner | ca9671d | 2002-11-02 20:28:58 +0000 | [diff] [blame] | 753 | } |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 754 | |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 755 | |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 756 | /// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here |
| 757 | /// for constant immediate shift values, and for constant immediate |
| 758 | /// shift values equal to 1. Even the general case is sort of special, |
| 759 | /// because the shift amount has to be in CL, not just any old register. |
| 760 | /// |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 761 | void ISel::visitShiftInst (ShiftInst &I) { |
| 762 | unsigned Op0r = getReg (I.getOperand(0)); |
| 763 | unsigned DestReg = getReg(I); |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 764 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
| 765 | bool isOperandSigned = I.getType()->isUnsigned(); |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 766 | unsigned OperandClass = getClass(I.getType()); |
| 767 | |
| 768 | if (OperandClass > 2) |
| 769 | visitInstruction(I); // Can't handle longs yet! |
Chris Lattner | 796df73 | 2002-11-02 00:44:25 +0000 | [diff] [blame] | 770 | |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 771 | if (ConstantUInt *CUI = dyn_cast <ConstantUInt> (I.getOperand (1))) |
| 772 | { |
Chris Lattner | 796df73 | 2002-11-02 00:44:25 +0000 | [diff] [blame] | 773 | // The shift amount is constant, guaranteed to be a ubyte. Get its value. |
| 774 | assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?"); |
| 775 | unsigned char shAmt = CUI->getValue(); |
| 776 | |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 777 | static const unsigned ConstantOperand[][4] = { |
| 778 | { X86::SHRir8, X86::SHRir16, X86::SHRir32, 0 }, // SHR |
| 779 | { X86::SARir8, X86::SARir16, X86::SARir32, 0 }, // SAR |
| 780 | { X86::SHLir8, X86::SHLir16, X86::SHLir32, 0 }, // SHL |
| 781 | { X86::SHLir8, X86::SHLir16, X86::SHLir32, 0 }, // SAL = SHL |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 782 | }; |
| 783 | |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 784 | const unsigned *OpTab = // Figure out the operand table to use |
| 785 | ConstantOperand[isLeftShift*2+isOperandSigned]; |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 786 | |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 787 | // Emit: <insn> reg, shamt (shift-by-immediate opcode "ir" form.) |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 788 | BuildMI(BB, OpTab[OperandClass], 2, DestReg).addReg(Op0r).addZImm(shAmt); |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 789 | } |
| 790 | else |
| 791 | { |
| 792 | // The shift amount is non-constant. |
| 793 | // |
| 794 | // In fact, you can only shift with a variable shift amount if |
| 795 | // that amount is already in the CL register, so we have to put it |
| 796 | // there first. |
| 797 | // |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 798 | |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 799 | // Emit: move cl, shiftAmount (put the shift amount in CL.) |
Chris Lattner | ca9671d | 2002-11-02 20:28:58 +0000 | [diff] [blame] | 800 | BuildMI(BB, X86::MOVrr8, 1, X86::CL).addReg(getReg(I.getOperand(1))); |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 801 | |
| 802 | // This is a shift right (SHR). |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 803 | static const unsigned NonConstantOperand[][4] = { |
| 804 | { X86::SHRrr8, X86::SHRrr16, X86::SHRrr32, 0 }, // SHR |
| 805 | { X86::SARrr8, X86::SARrr16, X86::SARrr32, 0 }, // SAR |
| 806 | { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32, 0 }, // SHL |
| 807 | { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32, 0 }, // SAL = SHL |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 808 | }; |
| 809 | |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 810 | const unsigned *OpTab = // Figure out the operand table to use |
| 811 | NonConstantOperand[isLeftShift*2+isOperandSigned]; |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 812 | |
Chris Lattner | 3a9a693 | 2002-11-21 22:49:20 +0000 | [diff] [blame] | 813 | BuildMI(BB, OpTab[OperandClass], 1, DestReg).addReg(Op0r); |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 814 | } |
| 815 | } |
| 816 | |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 817 | |
Chris Lattner | 6fc3c52 | 2002-11-17 21:11:55 +0000 | [diff] [blame] | 818 | /// visitLoadInst - Implement LLVM load instructions in terms of the x86 'mov' |
| 819 | /// instruction. |
| 820 | /// |
| 821 | void ISel::visitLoadInst(LoadInst &I) { |
| 822 | unsigned Class = getClass(I.getType()); |
| 823 | if (Class > 2) // FIXME: Handle longs and others... |
| 824 | visitInstruction(I); |
| 825 | |
| 826 | static const unsigned Opcode[] = { X86::MOVmr8, X86::MOVmr16, X86::MOVmr32 }; |
| 827 | |
| 828 | unsigned AddressReg = getReg(I.getOperand(0)); |
| 829 | addDirectMem(BuildMI(BB, Opcode[Class], 4, getReg(I)), AddressReg); |
| 830 | } |
| 831 | |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 832 | |
Chris Lattner | 6fc3c52 | 2002-11-17 21:11:55 +0000 | [diff] [blame] | 833 | /// visitStoreInst - Implement LLVM store instructions in terms of the x86 'mov' |
| 834 | /// instruction. |
| 835 | /// |
| 836 | void ISel::visitStoreInst(StoreInst &I) { |
| 837 | unsigned Class = getClass(I.getOperand(0)->getType()); |
| 838 | if (Class > 2) // FIXME: Handle longs and others... |
| 839 | visitInstruction(I); |
| 840 | |
| 841 | static const unsigned Opcode[] = { X86::MOVrm8, X86::MOVrm16, X86::MOVrm32 }; |
| 842 | |
| 843 | unsigned ValReg = getReg(I.getOperand(0)); |
| 844 | unsigned AddressReg = getReg(I.getOperand(1)); |
| 845 | addDirectMem(BuildMI(BB, Opcode[Class], 1+4), AddressReg).addReg(ValReg); |
| 846 | } |
| 847 | |
| 848 | |
Brian Gaeke | c11232a | 2002-11-26 10:43:30 +0000 | [diff] [blame] | 849 | /// visitCastInst - Here we have various kinds of copying with or without |
| 850 | /// sign extension going on. |
Brian Gaeke | fa8d571 | 2002-11-22 11:07:01 +0000 | [diff] [blame] | 851 | void |
| 852 | ISel::visitCastInst (CastInst &CI) |
| 853 | { |
Chris Lattner | f18a36e | 2002-12-03 18:15:59 +0000 | [diff] [blame] | 854 | const Type *targetType = CI.getType (); |
Brian Gaeke | 07f0261 | 2002-12-03 07:36:03 +0000 | [diff] [blame] | 855 | Value *operand = CI.getOperand (0); |
| 856 | unsigned int operandReg = getReg (operand); |
Chris Lattner | f18a36e | 2002-12-03 18:15:59 +0000 | [diff] [blame] | 857 | const Type *sourceType = operand->getType (); |
Brian Gaeke | 07f0261 | 2002-12-03 07:36:03 +0000 | [diff] [blame] | 858 | unsigned int destReg = getReg (CI); |
Brian Gaeke | d474e9c | 2002-12-06 10:49:33 +0000 | [diff] [blame] | 859 | // |
| 860 | // Currently we handle: |
| 861 | // |
| 862 | // 1) cast * to bool |
| 863 | // |
| 864 | // 2) cast {sbyte, ubyte} to {sbyte, ubyte} |
| 865 | // cast {short, ushort} to {ushort, short} |
| 866 | // cast {int, uint, ptr} to {int, uint, ptr} |
| 867 | // |
| 868 | // 3) cast {sbyte, ubyte} to {ushort, short} |
| 869 | // cast {sbyte, ubyte} to {int, uint, ptr} |
| 870 | // cast {short, ushort} to {int, uint, ptr} |
| 871 | // |
| 872 | // 4) cast {int, uint, ptr} to {short, ushort} |
| 873 | // cast {int, uint, ptr} to {sbyte, ubyte} |
| 874 | // cast {short, ushort} to {sbyte, ubyte} |
Chris Lattner | 7d25589 | 2002-12-13 11:31:59 +0000 | [diff] [blame] | 875 | |
Brian Gaeke | d474e9c | 2002-12-06 10:49:33 +0000 | [diff] [blame] | 876 | // 1) Implement casts to bool by using compare on the operand followed |
| 877 | // by set if not zero on the result. |
| 878 | if (targetType == Type::BoolTy) |
| 879 | { |
| 880 | BuildMI (BB, X86::CMPri8, 2).addReg (operandReg).addZImm (0); |
| 881 | BuildMI (BB, X86::SETNEr, 1, destReg); |
| 882 | return; |
| 883 | } |
Chris Lattner | 7d25589 | 2002-12-13 11:31:59 +0000 | [diff] [blame] | 884 | |
Brian Gaeke | d474e9c | 2002-12-06 10:49:33 +0000 | [diff] [blame] | 885 | // 2) Implement casts between values of the same type class (as determined |
| 886 | // by getClass) by using a register-to-register move. |
Chris Lattner | 6b993cc | 2002-12-15 08:02:15 +0000 | [diff] [blame^] | 887 | unsigned srcClass = getClassB(sourceType); |
Chris Lattner | 7d25589 | 2002-12-13 11:31:59 +0000 | [diff] [blame] | 888 | unsigned targClass = getClass (targetType); |
Brian Gaeke | d474e9c | 2002-12-06 10:49:33 +0000 | [diff] [blame] | 889 | static const unsigned regRegMove[] = { |
| 890 | X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 |
| 891 | }; |
| 892 | if ((srcClass < 3) && (targClass < 3) && (srcClass == targClass)) |
| 893 | { |
| 894 | BuildMI (BB, regRegMove[srcClass], 1, destReg).addReg (operandReg); |
| 895 | return; |
| 896 | } |
| 897 | // 3) Handle cast of SMALLER int to LARGER int using a move with sign |
| 898 | // extension or zero extension, depending on whether the source type |
| 899 | // was signed. |
| 900 | if ((srcClass < 3) && (targClass < 3) && (srcClass < targClass)) |
| 901 | { |
| 902 | static const unsigned ops[] = { |
| 903 | X86::MOVSXr16r8, X86::MOVSXr32r8, X86::MOVSXr32r16, |
| 904 | X86::MOVZXr16r8, X86::MOVZXr32r8, X86::MOVZXr32r16 |
| 905 | }; |
| 906 | unsigned srcSigned = sourceType->isSigned (); |
| 907 | BuildMI (BB, ops[3 * srcSigned + srcClass + targClass - 1], 1, |
| 908 | destReg).addReg (operandReg); |
| 909 | return; |
| 910 | } |
| 911 | // 4) Handle cast of LARGER int to SMALLER int using a move to EAX |
| 912 | // followed by a move out of AX or AL. |
| 913 | if ((srcClass < 3) && (targClass < 3) && (srcClass > targClass)) |
| 914 | { |
| 915 | static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX }; |
| 916 | BuildMI (BB, regRegMove[srcClass], 1, |
| 917 | AReg[srcClass]).addReg (operandReg); |
| 918 | BuildMI (BB, regRegMove[targClass], 1, destReg).addReg (AReg[srcClass]); |
| 919 | return; |
| 920 | } |
| 921 | // Anything we haven't handled already, we can't (yet) handle at all. |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 922 | // |
| 923 | // FP to integral casts can be handled with FISTP to store onto the |
| 924 | // stack while converting to integer, followed by a MOV to load from |
| 925 | // the stack into the result register. Integral to FP casts can be |
| 926 | // handled with MOV to store onto the stack, followed by a FILD to |
| 927 | // load from the stack while converting to FP. For the moment, I |
| 928 | // can't quite get straight in my head how to borrow myself some |
| 929 | // stack space and write on it. Otherwise, this would be trivial. |
Brian Gaeke | fa8d571 | 2002-11-22 11:07:01 +0000 | [diff] [blame] | 930 | visitInstruction (CI); |
| 931 | } |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 932 | |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 933 | /// visitGetElementPtrInst - I don't know, most programs don't have |
| 934 | /// getelementptr instructions, right? That means we can put off |
| 935 | /// implementing this, right? Right. This method emits machine |
| 936 | /// instructions to perform type-safe pointer arithmetic. I am |
| 937 | /// guessing this could be cleaned up somewhat to use fewer temporary |
| 938 | /// registers. |
| 939 | void |
| 940 | ISel::visitGetElementPtrInst (GetElementPtrInst &I) |
| 941 | { |
Chris Lattner | f08ad9f | 2002-12-13 10:50:40 +0000 | [diff] [blame] | 942 | MachineBasicBlock::iterator MI = BB->end(); |
| 943 | emitGEPOperation(BB, MI, I.getOperand(0), |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 944 | I.op_begin()+1, I.op_end(), getReg(I)); |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 945 | } |
| 946 | |
Brian Gaeke | 71794c0 | 2002-12-13 11:22:48 +0000 | [diff] [blame] | 947 | void ISel::emitGEPOperation(MachineBasicBlock *MBB, |
Chris Lattner | f08ad9f | 2002-12-13 10:50:40 +0000 | [diff] [blame] | 948 | MachineBasicBlock::iterator &IP, |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 949 | Value *Src, User::op_iterator IdxBegin, |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 950 | User::op_iterator IdxEnd, unsigned TargetReg) { |
| 951 | const TargetData &TD = TM.getTargetData(); |
| 952 | const Type *Ty = Src->getType(); |
Chris Lattner | f08ad9f | 2002-12-13 10:50:40 +0000 | [diff] [blame] | 953 | unsigned basePtrReg = getReg(Src, BB, IP); |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 954 | |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 955 | // GEPs have zero or more indices; we must perform a struct access |
| 956 | // or array access for each one. |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 957 | for (GetElementPtrInst::op_iterator oi = IdxBegin, |
| 958 | oe = IdxEnd; oi != oe; ++oi) { |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 959 | Value *idx = *oi; |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 960 | unsigned nextBasePtrReg = makeAnotherReg(Type::UIntTy); |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 961 | if (const StructType *StTy = dyn_cast <StructType> (Ty)) { |
| 962 | // It's a struct access. idx is the index into the structure, |
| 963 | // which names the field. This index must have ubyte type. |
| 964 | const ConstantUInt *CUI = cast <ConstantUInt> (idx); |
| 965 | assert (CUI->getType () == Type::UByteTy |
| 966 | && "Funny-looking structure index in GEP"); |
| 967 | // Use the TargetData structure to pick out what the layout of |
| 968 | // the structure is in memory. Since the structure index must |
| 969 | // be constant, we can get its value and use it to find the |
| 970 | // right byte offset from the StructLayout class's list of |
| 971 | // structure member offsets. |
| 972 | unsigned idxValue = CUI->getValue (); |
| 973 | unsigned memberOffset = |
| 974 | TD.getStructLayout (StTy)->MemberOffsets[idxValue]; |
| 975 | // Emit an ADD to add memberOffset to the basePtr. |
Brian Gaeke | 71794c0 | 2002-12-13 11:22:48 +0000 | [diff] [blame] | 976 | BMI(MBB, IP, X86::ADDri32, 2, |
Chris Lattner | f08ad9f | 2002-12-13 10:50:40 +0000 | [diff] [blame] | 977 | nextBasePtrReg).addReg (basePtrReg).addZImm (memberOffset); |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 978 | // The next type is the member of the structure selected by the |
| 979 | // index. |
| 980 | Ty = StTy->getElementTypes ()[idxValue]; |
| 981 | } else if (const SequentialType *SqTy = cast <SequentialType> (Ty)) { |
| 982 | // It's an array or pointer access: [ArraySize x ElementType]. |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 983 | const Type *typeOfSequentialTypeIndex = SqTy->getIndexType (); |
| 984 | // idx is the index into the array. Unlike with structure |
| 985 | // indices, we may not know its actual value at code-generation |
| 986 | // time. |
| 987 | assert (idx->getType () == typeOfSequentialTypeIndex |
| 988 | && "Funny-looking array index in GEP"); |
| 989 | // We want to add basePtrReg to (idxReg * sizeof |
| 990 | // ElementType). First, we must find the size of the pointed-to |
| 991 | // type. (Not coincidentally, the next type is the type of the |
| 992 | // elements in the array.) |
| 993 | Ty = SqTy->getElementType (); |
| 994 | unsigned elementSize = TD.getTypeSize (Ty); |
Brian Gaeke | 71794c0 | 2002-12-13 11:22:48 +0000 | [diff] [blame] | 995 | unsigned elementSizeReg = makeAnotherReg(typeOfSequentialTypeIndex); |
| 996 | copyConstantToRegister(ConstantSInt::get(typeOfSequentialTypeIndex, |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 997 | elementSize), elementSizeReg, |
Chris Lattner | f08ad9f | 2002-12-13 10:50:40 +0000 | [diff] [blame] | 998 | BB, IP); |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 999 | |
Chris Lattner | f08ad9f | 2002-12-13 10:50:40 +0000 | [diff] [blame] | 1000 | unsigned idxReg = getReg(idx, BB, IP); |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 1001 | // Emit a MUL to multiply the register holding the index by |
| 1002 | // elementSize, putting the result in memberOffsetReg. |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 1003 | unsigned memberOffsetReg = makeAnotherReg(Type::UIntTy); |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 1004 | doMultiply (memberOffsetReg, typeOfSequentialTypeIndex, |
Chris Lattner | f08ad9f | 2002-12-13 10:50:40 +0000 | [diff] [blame] | 1005 | elementSizeReg, idxReg, BB, IP); |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 1006 | // Emit an ADD to add memberOffsetReg to the basePtr. |
Brian Gaeke | 71794c0 | 2002-12-13 11:22:48 +0000 | [diff] [blame] | 1007 | BMI(MBB, IP, X86::ADDrr32, 2, |
Chris Lattner | f08ad9f | 2002-12-13 10:50:40 +0000 | [diff] [blame] | 1008 | nextBasePtrReg).addReg (basePtrReg).addReg (memberOffsetReg); |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 1009 | } |
| 1010 | // Now that we are here, further indices refer to subtypes of this |
| 1011 | // one, so we don't need to worry about basePtrReg itself, anymore. |
| 1012 | basePtrReg = nextBasePtrReg; |
| 1013 | } |
| 1014 | // After we have processed all the indices, the result is left in |
| 1015 | // basePtrReg. Move it to the register where we were expected to |
| 1016 | // put the answer. A 32-bit move should do it, because we are in |
| 1017 | // ILP32 land. |
Brian Gaeke | 71794c0 | 2002-12-13 11:22:48 +0000 | [diff] [blame] | 1018 | BMI(MBB, IP, X86::MOVrr32, 1, TargetReg).addReg (basePtrReg); |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | |
| 1022 | /// visitMallocInst - I know that personally, whenever I want to remember |
| 1023 | /// something, I have to clear off some space in my brain. |
| 1024 | void |
| 1025 | ISel::visitMallocInst (MallocInst &I) |
| 1026 | { |
Brian Gaeke | e48ec01 | 2002-12-13 06:46:31 +0000 | [diff] [blame] | 1027 | // We assume that by this point, malloc instructions have been |
| 1028 | // lowered to calls, and dlsym will magically find malloc for us. |
| 1029 | // So we do not want to see malloc instructions here. |
| 1030 | visitInstruction (I); |
| 1031 | } |
| 1032 | |
| 1033 | |
| 1034 | /// visitFreeInst - same story as MallocInst |
| 1035 | void |
| 1036 | ISel::visitFreeInst (FreeInst &I) |
| 1037 | { |
| 1038 | // We assume that by this point, free instructions have been |
| 1039 | // lowered to calls, and dlsym will magically find free for us. |
| 1040 | // So we do not want to see free instructions here. |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 1041 | visitInstruction (I); |
| 1042 | } |
| 1043 | |
| 1044 | |
| 1045 | /// visitAllocaInst - I want some stack space. Come on, man, I said I |
| 1046 | /// want some freakin' stack space. |
| 1047 | void |
| 1048 | ISel::visitAllocaInst (AllocaInst &I) |
| 1049 | { |
Brian Gaeke | e48ec01 | 2002-12-13 06:46:31 +0000 | [diff] [blame] | 1050 | // Find the data size of the alloca inst's getAllocatedType. |
| 1051 | const Type *allocatedType = I.getAllocatedType (); |
| 1052 | const TargetData &TD = TM.DataLayout; |
| 1053 | unsigned allocatedTypeSize = TD.getTypeSize (allocatedType); |
| 1054 | // Keep stack 32-bit aligned. |
| 1055 | unsigned int allocatedTypeWords = allocatedTypeSize / 4; |
| 1056 | if (allocatedTypeSize % 4 != 0) { allocatedTypeWords++; } |
| 1057 | // Subtract size from stack pointer, thereby allocating some space. |
| 1058 | BuildMI (BB, X86::SUBri32, 1, X86::ESP).addZImm (allocatedTypeWords * 4); |
| 1059 | // Put a pointer to the space into the result register, by copying |
| 1060 | // the stack pointer. |
| 1061 | BuildMI (BB, X86::MOVrr32, 1, getReg (I)).addReg (X86::ESP); |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 1065 | /// createSimpleX86InstructionSelector - This pass converts an LLVM function |
| 1066 | /// 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] | 1067 | /// generated code sucks but the implementation is nice and simple. |
| 1068 | /// |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 1069 | Pass *createSimpleX86InstructionSelector(TargetMachine &TM) { |
| 1070 | return new ISel(TM); |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 1071 | } |