Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 1 | //===-- InstSelectSimple.cpp - A simple instruction selector for x86 ------===// |
| 2 | // |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 3 | // This file defines a simple peephole instruction selector for the x86 target |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 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" |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/SSARegMap.h" |
Chris Lattner | aa09b75 | 2002-12-28 21:08:28 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineConstantPool.h" |
Misha Brukman | d2cc017 | 2002-11-20 00:58:23 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 26 | #include "llvm/Support/InstVisitor.h" |
Misha Brukman | d2cc017 | 2002-11-20 00:58:23 +0000 | [diff] [blame] | 27 | #include "llvm/Target/MRegisterInfo.h" |
| 28 | #include <map> |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 30 | /// BMI - A special BuildMI variant that takes an iterator to insert the |
Chris Lattner | 8bdd129 | 2003-04-25 21:58:54 +0000 | [diff] [blame] | 31 | /// instruction at as well as a basic block. This is the version for when you |
| 32 | /// have a destination register in mind. |
Brian Gaeke | 71794c0 | 2002-12-13 11:22:48 +0000 | [diff] [blame] | 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); |
Chris Lattner | e8f0d92 | 2002-12-24 00:03:11 +0000 | [diff] [blame] | 40 | I = MBB->insert(I, MI)+1; |
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 | 8bdd129 | 2003-04-25 21:58:54 +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); |
Chris Lattner | e8f0d92 | 2002-12-24 00:03:11 +0000 | [diff] [blame] | 52 | I = MBB->insert(I, MI)+1; |
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 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 63 | std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs |
| 64 | |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 65 | // MBBMap - Mapping between LLVM BB -> Machine BB |
| 66 | std::map<const BasicBlock*, MachineBasicBlock*> MBBMap; |
| 67 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 68 | ISel(TargetMachine &tm) : TM(tm), F(0), BB(0) {} |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 69 | |
| 70 | /// runOnFunction - Top level implementation of instruction selection for |
| 71 | /// the entire function. |
| 72 | /// |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 73 | bool runOnFunction(Function &Fn) { |
Chris Lattner | 36b3603 | 2002-10-29 23:40:58 +0000 | [diff] [blame] | 74 | F = &MachineFunction::construct(&Fn, TM); |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 75 | |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 76 | // Create all of the machine basic blocks for the function... |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 77 | for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) |
| 78 | F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I)); |
| 79 | |
Chris Lattner | 14aa7fe | 2002-12-16 22:54:46 +0000 | [diff] [blame] | 80 | BB = &F->front(); |
Chris Lattner | dbd7372 | 2003-05-06 21:32:22 +0000 | [diff] [blame] | 81 | |
Chris Lattner | dbd7372 | 2003-05-06 21:32:22 +0000 | [diff] [blame] | 82 | // Copy incoming arguments off of the stack... |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 83 | LoadArgumentsToVirtualRegs(Fn); |
Chris Lattner | 14aa7fe | 2002-12-16 22:54:46 +0000 | [diff] [blame] | 84 | |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 85 | // Instruction select everything except PHI nodes |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 86 | visit(Fn); |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 87 | |
| 88 | // Select the PHI nodes |
| 89 | SelectPHINodes(); |
| 90 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 91 | RegMap.clear(); |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 92 | MBBMap.clear(); |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 93 | F = 0; |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 94 | return false; // We never modify the LLVM itself. |
| 95 | } |
| 96 | |
Chris Lattner | f0eb7be | 2002-12-15 21:13:40 +0000 | [diff] [blame] | 97 | virtual const char *getPassName() const { |
| 98 | return "X86 Simple Instruction Selection"; |
| 99 | } |
| 100 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 101 | /// visitBasicBlock - This method is called when we are visiting a new basic |
Chris Lattner | 33f53b5 | 2002-10-29 20:48:56 +0000 | [diff] [blame] | 102 | /// block. This simply creates a new MachineBasicBlock to emit code into |
| 103 | /// and adds it to the current MachineFunction. Subsequent visit* for |
| 104 | /// instructions will be invoked for all instructions in the basic block. |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 105 | /// |
| 106 | void visitBasicBlock(BasicBlock &LLVM_BB) { |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 107 | BB = MBBMap[&LLVM_BB]; |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 110 | /// LoadArgumentsToVirtualRegs - Load all of the arguments to this function |
| 111 | /// from the stack into virtual registers. |
| 112 | /// |
| 113 | void LoadArgumentsToVirtualRegs(Function &F); |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 114 | |
| 115 | /// SelectPHINodes - Insert machine code to generate phis. This is tricky |
| 116 | /// because we have to generate our sources into the source basic blocks, |
| 117 | /// not the current one. |
| 118 | /// |
| 119 | void SelectPHINodes(); |
| 120 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 121 | // Visitation methods for various instructions. These methods simply emit |
| 122 | // fixed X86 code for each instruction. |
| 123 | // |
Brian Gaeke | fa8d571 | 2002-11-22 11:07:01 +0000 | [diff] [blame] | 124 | |
| 125 | // Control flow operators |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 126 | void visitReturnInst(ReturnInst &RI); |
Chris Lattner | 2df035b | 2002-11-02 19:27:56 +0000 | [diff] [blame] | 127 | void visitBranchInst(BranchInst &BI); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 128 | |
| 129 | struct ValueRecord { |
| 130 | unsigned Reg; |
| 131 | const Type *Ty; |
| 132 | ValueRecord(unsigned R, const Type *T) : Reg(R), Ty(T) {} |
| 133 | }; |
| 134 | void doCall(const ValueRecord &Ret, MachineInstr *CallMI, |
| 135 | const std::vector<ValueRecord> &Args); |
Brian Gaeke | fa8d571 | 2002-11-22 11:07:01 +0000 | [diff] [blame] | 136 | void visitCallInst(CallInst &I); |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 137 | |
| 138 | // Arithmetic operators |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 139 | void visitSimpleBinary(BinaryOperator &B, unsigned OpcodeClass); |
Chris Lattner | 68aad93 | 2002-11-02 20:13:22 +0000 | [diff] [blame] | 140 | void visitAdd(BinaryOperator &B) { visitSimpleBinary(B, 0); } |
| 141 | void visitSub(BinaryOperator &B) { visitSimpleBinary(B, 1); } |
Chris Lattner | 8a307e8 | 2002-12-16 19:32:50 +0000 | [diff] [blame] | 142 | void doMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator &MBBI, |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 143 | unsigned DestReg, const Type *DestTy, |
| 144 | unsigned Op0Reg, unsigned Op1Reg); |
Chris Lattner | ca9671d | 2002-11-02 20:28:58 +0000 | [diff] [blame] | 145 | void visitMul(BinaryOperator &B); |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 146 | |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 147 | void visitDiv(BinaryOperator &B) { visitDivRem(B); } |
| 148 | void visitRem(BinaryOperator &B) { visitDivRem(B); } |
| 149 | void visitDivRem(BinaryOperator &B); |
| 150 | |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 151 | // Bitwise operators |
Chris Lattner | 68aad93 | 2002-11-02 20:13:22 +0000 | [diff] [blame] | 152 | void visitAnd(BinaryOperator &B) { visitSimpleBinary(B, 2); } |
| 153 | void visitOr (BinaryOperator &B) { visitSimpleBinary(B, 3); } |
| 154 | void visitXor(BinaryOperator &B) { visitSimpleBinary(B, 4); } |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 155 | |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 156 | // Comparison operators... |
| 157 | void visitSetCondInst(SetCondInst &I); |
| 158 | bool EmitComparisonGetSignedness(unsigned OpNum, Value *Op0, Value *Op1); |
Chris Lattner | 6fc3c52 | 2002-11-17 21:11:55 +0000 | [diff] [blame] | 159 | |
| 160 | // Memory Instructions |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 161 | MachineInstr *doFPLoad(MachineBasicBlock *MBB, |
| 162 | MachineBasicBlock::iterator &MBBI, |
| 163 | const Type *Ty, unsigned DestReg); |
Chris Lattner | 6fc3c52 | 2002-11-17 21:11:55 +0000 | [diff] [blame] | 164 | void visitLoadInst(LoadInst &I); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 165 | void doFPStore(const Type *Ty, unsigned DestAddrReg, unsigned SrcReg); |
Chris Lattner | 6fc3c52 | 2002-11-17 21:11:55 +0000 | [diff] [blame] | 166 | void visitStoreInst(StoreInst &I); |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 167 | void visitGetElementPtrInst(GetElementPtrInst &I); |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 168 | void visitAllocaInst(AllocaInst &I); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 169 | void visitMallocInst(MallocInst &I); |
| 170 | void visitFreeInst(FreeInst &I); |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 171 | |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 172 | // Other operators |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 173 | void visitShiftInst(ShiftInst &I); |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 174 | void visitPHINode(PHINode &I) {} // PHI nodes handled by second pass |
Brian Gaeke | fa8d571 | 2002-11-22 11:07:01 +0000 | [diff] [blame] | 175 | void visitCastInst(CastInst &I); |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 176 | |
| 177 | void visitInstruction(Instruction &I) { |
| 178 | std::cerr << "Cannot instruction select: " << I; |
| 179 | abort(); |
| 180 | } |
| 181 | |
Brian Gaeke | 95780cc | 2002-12-13 07:56:18 +0000 | [diff] [blame] | 182 | /// promote32 - Make a value 32-bits wide, and put it somewhere. |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 183 | /// |
| 184 | void promote32(unsigned targetReg, const ValueRecord &VR); |
| 185 | |
| 186 | /// EmitByteSwap - Byteswap SrcReg into DestReg. |
| 187 | /// |
| 188 | void EmitByteSwap(unsigned DestReg, unsigned SrcReg, unsigned Class); |
Brian Gaeke | 95780cc | 2002-12-13 07:56:18 +0000 | [diff] [blame] | 189 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 190 | /// emitGEPOperation - Common code shared between visitGetElementPtrInst and |
| 191 | /// constant expression GEP support. |
| 192 | /// |
Chris Lattner | f08ad9f | 2002-12-13 10:50:40 +0000 | [diff] [blame] | 193 | void emitGEPOperation(MachineBasicBlock *BB, MachineBasicBlock::iterator&IP, |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 194 | Value *Src, User::op_iterator IdxBegin, |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 195 | User::op_iterator IdxEnd, unsigned TargetReg); |
| 196 | |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 197 | /// emitCastOperation - Common code shared between visitCastInst and |
| 198 | /// constant expression cast support. |
| 199 | void emitCastOperation(MachineBasicBlock *BB,MachineBasicBlock::iterator&IP, |
| 200 | Value *Src, const Type *DestTy, unsigned TargetReg); |
| 201 | |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 202 | /// copyConstantToRegister - Output the instructions required to put the |
| 203 | /// specified constant into the specified register. |
| 204 | /// |
Chris Lattner | 8a307e8 | 2002-12-16 19:32:50 +0000 | [diff] [blame] | 205 | void copyConstantToRegister(MachineBasicBlock *MBB, |
| 206 | MachineBasicBlock::iterator &MBBI, |
| 207 | Constant *C, unsigned Reg); |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 208 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 209 | /// makeAnotherReg - This method returns the next register number we haven't |
| 210 | /// yet used. |
| 211 | /// |
| 212 | /// Long values are handled somewhat specially. They are always allocated |
| 213 | /// as pairs of 32 bit integer values. The register number returned is the |
| 214 | /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits |
| 215 | /// of the long value. |
| 216 | /// |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 217 | unsigned makeAnotherReg(const Type *Ty) { |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 218 | if (Ty == Type::LongTy || Ty == Type::ULongTy) { |
| 219 | const TargetRegisterClass *RC = |
| 220 | TM.getRegisterInfo()->getRegClassForType(Type::IntTy); |
| 221 | // Create the lower part |
| 222 | F->getSSARegMap()->createVirtualRegister(RC); |
| 223 | // Create the upper part. |
| 224 | return F->getSSARegMap()->createVirtualRegister(RC)-1; |
| 225 | } |
| 226 | |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 227 | // Add the mapping of regnumber => reg class to MachineFunction |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 228 | const TargetRegisterClass *RC = |
| 229 | TM.getRegisterInfo()->getRegClassForType(Ty); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 230 | return F->getSSARegMap()->createVirtualRegister(RC); |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 233 | /// getReg - This method turns an LLVM value into a register number. This |
| 234 | /// is guaranteed to produce the same register number for a particular value |
| 235 | /// every time it is queried. |
| 236 | /// |
| 237 | unsigned getReg(Value &V) { return getReg(&V); } // Allow references |
Chris Lattner | f08ad9f | 2002-12-13 10:50:40 +0000 | [diff] [blame] | 238 | unsigned getReg(Value *V) { |
| 239 | // Just append to the end of the current bb. |
| 240 | MachineBasicBlock::iterator It = BB->end(); |
| 241 | return getReg(V, BB, It); |
| 242 | } |
Brian Gaeke | 71794c0 | 2002-12-13 11:22:48 +0000 | [diff] [blame] | 243 | unsigned getReg(Value *V, MachineBasicBlock *MBB, |
Chris Lattner | f08ad9f | 2002-12-13 10:50:40 +0000 | [diff] [blame] | 244 | MachineBasicBlock::iterator &IPt) { |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 245 | unsigned &Reg = RegMap[V]; |
Misha Brukman | d2cc017 | 2002-11-20 00:58:23 +0000 | [diff] [blame] | 246 | if (Reg == 0) { |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 247 | Reg = makeAnotherReg(V->getType()); |
Misha Brukman | d2cc017 | 2002-11-20 00:58:23 +0000 | [diff] [blame] | 248 | RegMap[V] = Reg; |
Misha Brukman | d2cc017 | 2002-11-20 00:58:23 +0000 | [diff] [blame] | 249 | } |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 250 | |
Chris Lattner | 6f8fd25 | 2002-10-27 21:23:43 +0000 | [diff] [blame] | 251 | // If this operand is a constant, emit the code to copy the constant into |
| 252 | // the register here... |
| 253 | // |
Chris Lattner | dbf30f7 | 2002-12-04 06:45:19 +0000 | [diff] [blame] | 254 | if (Constant *C = dyn_cast<Constant>(V)) { |
Chris Lattner | 8a307e8 | 2002-12-16 19:32:50 +0000 | [diff] [blame] | 255 | copyConstantToRegister(MBB, IPt, C, Reg); |
Chris Lattner | 14aa7fe | 2002-12-16 22:54:46 +0000 | [diff] [blame] | 256 | RegMap.erase(V); // Assign a new name to this constant if ref'd again |
Chris Lattner | dbf30f7 | 2002-12-04 06:45:19 +0000 | [diff] [blame] | 257 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 258 | // Move the address of the global into the register |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 259 | BMI(MBB, IPt, X86::MOVir32, 1, Reg).addGlobalAddress(GV); |
Chris Lattner | 14aa7fe | 2002-12-16 22:54:46 +0000 | [diff] [blame] | 260 | RegMap.erase(V); // Assign a new name to this address if ref'd again |
Chris Lattner | dbf30f7 | 2002-12-04 06:45:19 +0000 | [diff] [blame] | 261 | } |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 262 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 263 | return Reg; |
| 264 | } |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 265 | }; |
| 266 | } |
| 267 | |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame] | 268 | /// TypeClass - Used by the X86 backend to group LLVM types by their basic X86 |
| 269 | /// Representation. |
| 270 | /// |
| 271 | enum TypeClass { |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 272 | cByte, cShort, cInt, cFP, cLong |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame] | 273 | }; |
| 274 | |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 275 | /// getClass - Turn a primitive type into a "class" number which is based on the |
| 276 | /// size of the type, and whether or not it is floating point. |
| 277 | /// |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame] | 278 | static inline TypeClass getClass(const Type *Ty) { |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 279 | switch (Ty->getPrimitiveID()) { |
| 280 | case Type::SByteTyID: |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame] | 281 | case Type::UByteTyID: return cByte; // Byte operands are class #0 |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 282 | case Type::ShortTyID: |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame] | 283 | case Type::UShortTyID: return cShort; // Short operands are class #1 |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 284 | case Type::IntTyID: |
| 285 | case Type::UIntTyID: |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame] | 286 | case Type::PointerTyID: return cInt; // Int's and pointers are class #2 |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 287 | |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 288 | case Type::FloatTyID: |
| 289 | case Type::DoubleTyID: return cFP; // Floating Point is #3 |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 290 | |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 291 | case Type::LongTyID: |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 292 | case Type::ULongTyID: return cLong; // Longs are class #4 |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 293 | default: |
| 294 | assert(0 && "Invalid type to getClass!"); |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame] | 295 | return cByte; // not reached |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 296 | } |
| 297 | } |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 298 | |
Chris Lattner | 6b993cc | 2002-12-15 08:02:15 +0000 | [diff] [blame] | 299 | // getClassB - Just like getClass, but treat boolean values as bytes. |
| 300 | static inline TypeClass getClassB(const Type *Ty) { |
| 301 | if (Ty == Type::BoolTy) return cByte; |
| 302 | return getClass(Ty); |
| 303 | } |
| 304 | |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 305 | |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 306 | /// copyConstantToRegister - Output the instructions required to put the |
| 307 | /// specified constant into the specified register. |
| 308 | /// |
Chris Lattner | 8a307e8 | 2002-12-16 19:32:50 +0000 | [diff] [blame] | 309 | void ISel::copyConstantToRegister(MachineBasicBlock *MBB, |
| 310 | MachineBasicBlock::iterator &IP, |
| 311 | Constant *C, unsigned R) { |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 312 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 313 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
Brian Gaeke | 68b1edc | 2002-12-16 04:23:29 +0000 | [diff] [blame] | 314 | emitGEPOperation(MBB, IP, CE->getOperand(0), |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 315 | CE->op_begin()+1, CE->op_end(), R); |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 316 | return; |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 317 | } else if (CE->getOpcode() == Instruction::Cast) { |
| 318 | emitCastOperation(MBB, IP, CE->getOperand(0), CE->getType(), R); |
Chris Lattner | 4b12cde | 2003-04-21 21:33:44 +0000 | [diff] [blame] | 319 | return; |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 322 | std::cerr << "Offending expr: " << C << "\n"; |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 323 | assert(0 && "Constant expressions not yet handled!\n"); |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 324 | } |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 325 | |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 326 | if (C->getType()->isIntegral()) { |
Chris Lattner | 6b993cc | 2002-12-15 08:02:15 +0000 | [diff] [blame] | 327 | unsigned Class = getClassB(C->getType()); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 328 | |
| 329 | if (Class == cLong) { |
| 330 | // Copy the value into the register pair. |
| 331 | uint64_t Val; |
| 332 | if (C->getType()->isSigned()) |
| 333 | Val = cast<ConstantSInt>(C)->getValue(); |
| 334 | else |
| 335 | Val = cast<ConstantUInt>(C)->getValue(); |
| 336 | |
| 337 | BMI(MBB, IP, X86::MOVir32, 1, R).addZImm(Val & 0xFFFFFFFF); |
| 338 | BMI(MBB, IP, X86::MOVir32, 1, R+1).addZImm(Val >> 32); |
| 339 | return; |
| 340 | } |
| 341 | |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 342 | assert(Class <= cInt && "Type not handled yet!"); |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 343 | |
| 344 | static const unsigned IntegralOpcodeTab[] = { |
| 345 | X86::MOVir8, X86::MOVir16, X86::MOVir32 |
| 346 | }; |
| 347 | |
Chris Lattner | 6b993cc | 2002-12-15 08:02:15 +0000 | [diff] [blame] | 348 | if (C->getType() == Type::BoolTy) { |
| 349 | BMI(MBB, IP, X86::MOVir8, 1, R).addZImm(C == ConstantBool::True); |
| 350 | } else if (C->getType()->isSigned()) { |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 351 | ConstantSInt *CSI = cast<ConstantSInt>(C); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 352 | BMI(MBB, IP, IntegralOpcodeTab[Class], 1, R).addZImm(CSI->getValue()); |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 353 | } else { |
| 354 | ConstantUInt *CUI = cast<ConstantUInt>(C); |
Brian Gaeke | 71794c0 | 2002-12-13 11:22:48 +0000 | [diff] [blame] | 355 | BMI(MBB, IP, IntegralOpcodeTab[Class], 1, R).addZImm(CUI->getValue()); |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 356 | } |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 357 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
| 358 | double Value = CFP->getValue(); |
| 359 | if (Value == +0.0) |
| 360 | BMI(MBB, IP, X86::FLD0, 0, R); |
| 361 | else if (Value == +1.0) |
| 362 | BMI(MBB, IP, X86::FLD1, 0, R); |
| 363 | else { |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 364 | // Otherwise we need to spill the constant to memory... |
| 365 | MachineConstantPool *CP = F->getConstantPool(); |
| 366 | unsigned CPI = CP->getConstantPoolIndex(CFP); |
| 367 | addConstantPoolReference(doFPLoad(MBB, IP, CFP->getType(), R), CPI); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Chris Lattner | f08ad9f | 2002-12-13 10:50:40 +0000 | [diff] [blame] | 370 | } else if (isa<ConstantPointerNull>(C)) { |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 371 | // Copy zero (null pointer) to the register. |
Brian Gaeke | 71794c0 | 2002-12-13 11:22:48 +0000 | [diff] [blame] | 372 | BMI(MBB, IP, X86::MOVir32, 1, R).addZImm(0); |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 373 | } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)) { |
Brian Gaeke | 68b1edc | 2002-12-16 04:23:29 +0000 | [diff] [blame] | 374 | unsigned SrcReg = getReg(CPR->getValue(), MBB, IP); |
Brian Gaeke | 71794c0 | 2002-12-13 11:22:48 +0000 | [diff] [blame] | 375 | BMI(MBB, IP, X86::MOVrr32, 1, R).addReg(SrcReg); |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 376 | } else { |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 377 | std::cerr << "Offending constant: " << C << "\n"; |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 378 | assert(0 && "Type not handled yet!"); |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 382 | /// LoadArgumentsToVirtualRegs - Load all of the arguments to this function from |
| 383 | /// the stack into virtual registers. |
| 384 | /// |
| 385 | void ISel::LoadArgumentsToVirtualRegs(Function &Fn) { |
| 386 | // Emit instructions to load the arguments... On entry to a function on the |
| 387 | // X86, the stack frame looks like this: |
| 388 | // |
| 389 | // [ESP] -- return address |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 390 | // [ESP + 4] -- first argument (leftmost lexically) |
| 391 | // [ESP + 8] -- second argument, if first argument is four bytes in size |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 392 | // ... |
| 393 | // |
Chris Lattner | f158da2 | 2003-01-16 02:20:12 +0000 | [diff] [blame] | 394 | unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot |
Chris Lattner | aa09b75 | 2002-12-28 21:08:28 +0000 | [diff] [blame] | 395 | MachineFrameInfo *MFI = F->getFrameInfo(); |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 396 | |
| 397 | for (Function::aiterator I = Fn.abegin(), E = Fn.aend(); I != E; ++I) { |
| 398 | unsigned Reg = getReg(*I); |
| 399 | |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 400 | int FI; // Frame object index |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 401 | switch (getClassB(I->getType())) { |
| 402 | case cByte: |
Chris Lattner | aa09b75 | 2002-12-28 21:08:28 +0000 | [diff] [blame] | 403 | FI = MFI->CreateFixedObject(1, ArgOffset); |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 404 | addFrameReference(BuildMI(BB, X86::MOVmr8, 4, Reg), FI); |
| 405 | break; |
| 406 | case cShort: |
Chris Lattner | aa09b75 | 2002-12-28 21:08:28 +0000 | [diff] [blame] | 407 | FI = MFI->CreateFixedObject(2, ArgOffset); |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 408 | addFrameReference(BuildMI(BB, X86::MOVmr16, 4, Reg), FI); |
| 409 | break; |
| 410 | case cInt: |
Chris Lattner | aa09b75 | 2002-12-28 21:08:28 +0000 | [diff] [blame] | 411 | FI = MFI->CreateFixedObject(4, ArgOffset); |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 412 | addFrameReference(BuildMI(BB, X86::MOVmr32, 4, Reg), FI); |
| 413 | break; |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 414 | case cLong: |
| 415 | FI = MFI->CreateFixedObject(8, ArgOffset); |
| 416 | addFrameReference(BuildMI(BB, X86::MOVmr32, 4, Reg), FI); |
| 417 | addFrameReference(BuildMI(BB, X86::MOVmr32, 4, Reg+1), FI, 4); |
| 418 | ArgOffset += 4; // longs require 4 additional bytes |
| 419 | break; |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 420 | case cFP: |
| 421 | unsigned Opcode; |
| 422 | if (I->getType() == Type::FloatTy) { |
| 423 | Opcode = X86::FLDr32; |
Chris Lattner | aa09b75 | 2002-12-28 21:08:28 +0000 | [diff] [blame] | 424 | FI = MFI->CreateFixedObject(4, ArgOffset); |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 425 | } else { |
| 426 | Opcode = X86::FLDr64; |
Chris Lattner | aa09b75 | 2002-12-28 21:08:28 +0000 | [diff] [blame] | 427 | FI = MFI->CreateFixedObject(8, ArgOffset); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 428 | ArgOffset += 4; // doubles require 4 additional bytes |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 429 | } |
| 430 | addFrameReference(BuildMI(BB, Opcode, 4, Reg), FI); |
| 431 | break; |
| 432 | default: |
| 433 | assert(0 && "Unhandled argument type!"); |
| 434 | } |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 435 | ArgOffset += 4; // Each argument takes at least 4 bytes on the stack... |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | |
| 439 | |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 440 | /// SelectPHINodes - Insert machine code to generate phis. This is tricky |
| 441 | /// because we have to generate our sources into the source basic blocks, not |
| 442 | /// the current one. |
| 443 | /// |
| 444 | void ISel::SelectPHINodes() { |
Chris Lattner | 3501fea | 2003-01-14 22:00:31 +0000 | [diff] [blame] | 445 | const TargetInstrInfo &TII = TM.getInstrInfo(); |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 446 | const Function &LF = *F->getFunction(); // The LLVM function... |
| 447 | for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) { |
| 448 | const BasicBlock *BB = I; |
| 449 | MachineBasicBlock *MBB = MBBMap[I]; |
| 450 | |
| 451 | // Loop over all of the PHI nodes in the LLVM basic block... |
| 452 | unsigned NumPHIs = 0; |
| 453 | for (BasicBlock::const_iterator I = BB->begin(); |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 454 | PHINode *PN = (PHINode*)dyn_cast<PHINode>(I); ++I) { |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 455 | |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 456 | // Create a new machine instr PHI node, and insert it. |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 457 | unsigned PHIReg = getReg(*PN); |
| 458 | MachineInstr *PhiMI = BuildMI(X86::PHI, PN->getNumOperands(), PHIReg); |
| 459 | MBB->insert(MBB->begin()+NumPHIs++, PhiMI); |
| 460 | |
| 461 | MachineInstr *LongPhiMI = 0; |
| 462 | if (PN->getType() == Type::LongTy || PN->getType() == Type::ULongTy) { |
| 463 | LongPhiMI = BuildMI(X86::PHI, PN->getNumOperands(), PHIReg+1); |
| 464 | MBB->insert(MBB->begin()+NumPHIs++, LongPhiMI); |
| 465 | } |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 466 | |
| 467 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 468 | MachineBasicBlock *PredMBB = MBBMap[PN->getIncomingBlock(i)]; |
| 469 | |
| 470 | // Get the incoming value into a virtual register. If it is not already |
| 471 | // available in a virtual register, insert the computation code into |
| 472 | // PredMBB |
Chris Lattner | 9205363 | 2002-12-13 11:52:34 +0000 | [diff] [blame] | 473 | // |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 474 | MachineBasicBlock::iterator PI = PredMBB->end(); |
| 475 | while (PI != PredMBB->begin() && |
Chris Lattner | 3501fea | 2003-01-14 22:00:31 +0000 | [diff] [blame] | 476 | TII.isTerminatorInstr((*(PI-1))->getOpcode())) |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 477 | --PI; |
| 478 | unsigned ValReg = getReg(PN->getIncomingValue(i), PredMBB, PI); |
| 479 | PhiMI->addRegOperand(ValReg); |
| 480 | PhiMI->addMachineBasicBlockOperand(PredMBB); |
| 481 | if (LongPhiMI) { |
| 482 | LongPhiMI->addRegOperand(ValReg+1); |
| 483 | LongPhiMI->addMachineBasicBlockOperand(PredMBB); |
| 484 | } |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 490 | // canFoldSetCCIntoBranch - Return the setcc instruction if we can fold it into |
| 491 | // the conditional branch instruction which is the only user of the cc |
| 492 | // instruction. This is the case if the conditional branch is the only user of |
| 493 | // the setcc, and if the setcc is in the same basic block as the conditional |
| 494 | // branch. We also don't handle long arguments below, so we reject them here as |
| 495 | // well. |
| 496 | // |
| 497 | static SetCondInst *canFoldSetCCIntoBranch(Value *V) { |
| 498 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(V)) |
| 499 | if (SCI->use_size() == 1 && isa<BranchInst>(SCI->use_back()) && |
| 500 | SCI->getParent() == cast<BranchInst>(SCI->use_back())->getParent()) { |
| 501 | const Type *Ty = SCI->getOperand(0)->getType(); |
| 502 | if (Ty != Type::LongTy && Ty != Type::ULongTy) |
| 503 | return SCI; |
| 504 | } |
| 505 | return 0; |
| 506 | } |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 507 | |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 508 | // Return a fixed numbering for setcc instructions which does not depend on the |
| 509 | // order of the opcodes. |
| 510 | // |
| 511 | static unsigned getSetCCNumber(unsigned Opcode) { |
| 512 | switch(Opcode) { |
| 513 | default: assert(0 && "Unknown setcc instruction!"); |
| 514 | case Instruction::SetEQ: return 0; |
| 515 | case Instruction::SetNE: return 1; |
| 516 | case Instruction::SetLT: return 2; |
Chris Lattner | 55f6fab | 2003-01-16 18:07:23 +0000 | [diff] [blame] | 517 | case Instruction::SetGE: return 3; |
| 518 | case Instruction::SetGT: return 4; |
| 519 | case Instruction::SetLE: return 5; |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 520 | } |
| 521 | } |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 522 | |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 523 | // LLVM -> X86 signed X86 unsigned |
| 524 | // ----- ---------- ------------ |
| 525 | // seteq -> sete sete |
| 526 | // setne -> setne setne |
| 527 | // setlt -> setl setb |
Chris Lattner | 55f6fab | 2003-01-16 18:07:23 +0000 | [diff] [blame] | 528 | // setge -> setge setae |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 529 | // setgt -> setg seta |
| 530 | // setle -> setle setbe |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 531 | static const unsigned SetCCOpcodeTab[2][6] = { |
Chris Lattner | 55f6fab | 2003-01-16 18:07:23 +0000 | [diff] [blame] | 532 | {X86::SETEr, X86::SETNEr, X86::SETBr, X86::SETAEr, X86::SETAr, X86::SETBEr}, |
| 533 | {X86::SETEr, X86::SETNEr, X86::SETLr, X86::SETGEr, X86::SETGr, X86::SETLEr}, |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 534 | }; |
| 535 | |
| 536 | bool ISel::EmitComparisonGetSignedness(unsigned OpNum, Value *Op0, Value *Op1) { |
| 537 | |
Brian Gaeke | 1749d63 | 2002-11-07 17:59:21 +0000 | [diff] [blame] | 538 | // The arguments are already supposed to be of the same type. |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 539 | const Type *CompTy = Op0->getType(); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 540 | bool isSigned = CompTy->isSigned(); |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 541 | unsigned reg1 = getReg(Op0); |
| 542 | unsigned reg2 = getReg(Op1); |
Chris Lattner | 05093a5 | 2002-11-21 15:52:38 +0000 | [diff] [blame] | 543 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 544 | unsigned Class = getClassB(CompTy); |
| 545 | switch (Class) { |
| 546 | default: assert(0 && "Unknown type class!"); |
| 547 | // Emit: cmp <var1>, <var2> (do the comparison). We can |
| 548 | // compare 8-bit with 8-bit, 16-bit with 16-bit, 32-bit with |
| 549 | // 32-bit. |
| 550 | case cByte: |
| 551 | BuildMI(BB, X86::CMPrr8, 2).addReg(reg1).addReg(reg2); |
| 552 | break; |
| 553 | case cShort: |
| 554 | BuildMI(BB, X86::CMPrr16, 2).addReg(reg1).addReg(reg2); |
| 555 | break; |
| 556 | case cInt: |
| 557 | BuildMI(BB, X86::CMPrr32, 2).addReg(reg1).addReg(reg2); |
| 558 | break; |
| 559 | case cFP: |
| 560 | BuildMI(BB, X86::FpUCOM, 2).addReg(reg1).addReg(reg2); |
| 561 | BuildMI(BB, X86::FNSTSWr8, 0); |
| 562 | BuildMI(BB, X86::SAHF, 1); |
| 563 | isSigned = false; // Compare with unsigned operators |
| 564 | break; |
| 565 | |
| 566 | case cLong: |
| 567 | if (OpNum < 2) { // seteq, setne |
| 568 | unsigned LoTmp = makeAnotherReg(Type::IntTy); |
| 569 | unsigned HiTmp = makeAnotherReg(Type::IntTy); |
| 570 | unsigned FinalTmp = makeAnotherReg(Type::IntTy); |
| 571 | BuildMI(BB, X86::XORrr32, 2, LoTmp).addReg(reg1).addReg(reg2); |
| 572 | BuildMI(BB, X86::XORrr32, 2, HiTmp).addReg(reg1+1).addReg(reg2+1); |
| 573 | BuildMI(BB, X86::ORrr32, 2, FinalTmp).addReg(LoTmp).addReg(HiTmp); |
| 574 | break; // Allow the sete or setne to be generated from flags set by OR |
| 575 | } else { |
| 576 | // Emit a sequence of code which compares the high and low parts once |
| 577 | // each, then uses a conditional move to handle the overflow case. For |
| 578 | // example, a setlt for long would generate code like this: |
| 579 | // |
| 580 | // AL = lo(op1) < lo(op2) // Signedness depends on operands |
| 581 | // BL = hi(op1) < hi(op2) // Always unsigned comparison |
| 582 | // dest = hi(op1) == hi(op2) ? AL : BL; |
| 583 | // |
| 584 | |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 585 | // FIXME: This would be much better if we had hierarchical register |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 586 | // classes! Until then, hardcode registers so that we can deal with their |
| 587 | // aliases (because we don't have conditional byte moves). |
| 588 | // |
| 589 | BuildMI(BB, X86::CMPrr32, 2).addReg(reg1).addReg(reg2); |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 590 | BuildMI(BB, SetCCOpcodeTab[0][OpNum], 0, X86::AL); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 591 | BuildMI(BB, X86::CMPrr32, 2).addReg(reg1+1).addReg(reg2+1); |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 592 | BuildMI(BB, SetCCOpcodeTab[isSigned][OpNum], 0, X86::BL); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 593 | BuildMI(BB, X86::CMOVErr16, 2, X86::BX).addReg(X86::BX).addReg(X86::AX); |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 594 | // NOTE: visitSetCondInst knows that the value is dumped into the BL |
| 595 | // register at this point for long values... |
| 596 | return isSigned; |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 597 | } |
| 598 | } |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 599 | return isSigned; |
| 600 | } |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 601 | |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 602 | |
| 603 | /// SetCC instructions - Here we just emit boilerplate code to set a byte-sized |
| 604 | /// register, then move it to wherever the result should be. |
| 605 | /// |
| 606 | void ISel::visitSetCondInst(SetCondInst &I) { |
| 607 | if (canFoldSetCCIntoBranch(&I)) return; // Fold this into a branch... |
| 608 | |
| 609 | unsigned OpNum = getSetCCNumber(I.getOpcode()); |
| 610 | unsigned DestReg = getReg(I); |
| 611 | bool isSigned = EmitComparisonGetSignedness(OpNum, I.getOperand(0), |
| 612 | I.getOperand(1)); |
| 613 | |
| 614 | if (getClassB(I.getOperand(0)->getType()) != cLong || OpNum < 2) { |
| 615 | // Handle normal comparisons with a setcc instruction... |
| 616 | BuildMI(BB, SetCCOpcodeTab[isSigned][OpNum], 0, DestReg); |
| 617 | } else { |
| 618 | // Handle long comparisons by copying the value which is already in BL into |
| 619 | // the register we want... |
| 620 | BuildMI(BB, X86::MOVrr8, 1, DestReg).addReg(X86::BL); |
| 621 | } |
Brian Gaeke | 1749d63 | 2002-11-07 17:59:21 +0000 | [diff] [blame] | 622 | } |
Chris Lattner | 51b49a9 | 2002-11-02 19:45:49 +0000 | [diff] [blame] | 623 | |
Brian Gaeke | c250598 | 2002-11-30 11:57:28 +0000 | [diff] [blame] | 624 | /// promote32 - Emit instructions to turn a narrow operand into a 32-bit-wide |
| 625 | /// operand, in the specified target register. |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 626 | void ISel::promote32(unsigned targetReg, const ValueRecord &VR) { |
| 627 | bool isUnsigned = VR.Ty->isUnsigned(); |
| 628 | switch (getClassB(VR.Ty)) { |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 629 | case cByte: |
| 630 | // Extend value into target register (8->32) |
| 631 | if (isUnsigned) |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 632 | BuildMI(BB, X86::MOVZXr32r8, 1, targetReg).addReg(VR.Reg); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 633 | else |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 634 | BuildMI(BB, X86::MOVSXr32r8, 1, targetReg).addReg(VR.Reg); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 635 | break; |
| 636 | case cShort: |
| 637 | // Extend value into target register (16->32) |
| 638 | if (isUnsigned) |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 639 | BuildMI(BB, X86::MOVZXr32r16, 1, targetReg).addReg(VR.Reg); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 640 | else |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 641 | BuildMI(BB, X86::MOVSXr32r16, 1, targetReg).addReg(VR.Reg); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 642 | break; |
| 643 | case cInt: |
| 644 | // Move value into target register (32->32) |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 645 | BuildMI(BB, X86::MOVrr32, 1, targetReg).addReg(VR.Reg); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 646 | break; |
| 647 | default: |
| 648 | assert(0 && "Unpromotable operand class in promote32"); |
| 649 | } |
Brian Gaeke | c250598 | 2002-11-30 11:57:28 +0000 | [diff] [blame] | 650 | } |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 651 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 652 | /// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such, |
| 653 | /// we have the following possibilities: |
| 654 | /// |
| 655 | /// ret void: No return value, simply emit a 'ret' instruction |
| 656 | /// ret sbyte, ubyte : Extend value into EAX and return |
| 657 | /// ret short, ushort: Extend value into EAX and return |
| 658 | /// ret int, uint : Move value into EAX and return |
| 659 | /// ret pointer : Move value into EAX and return |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 660 | /// ret long, ulong : Move value into EAX/EDX and return |
| 661 | /// ret float/double : Top of FP stack |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 662 | /// |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 663 | void ISel::visitReturnInst(ReturnInst &I) { |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 664 | if (I.getNumOperands() == 0) { |
| 665 | BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction |
| 666 | return; |
| 667 | } |
| 668 | |
| 669 | Value *RetVal = I.getOperand(0); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 670 | unsigned RetReg = getReg(RetVal); |
| 671 | switch (getClassB(RetVal->getType())) { |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 672 | case cByte: // integral return values: extend or move into EAX and return |
| 673 | case cShort: |
| 674 | case cInt: |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 675 | promote32(X86::EAX, ValueRecord(RetReg, RetVal->getType())); |
Chris Lattner | dbd7372 | 2003-05-06 21:32:22 +0000 | [diff] [blame] | 676 | // Declare that EAX is live on exit |
Chris Lattner | c248903 | 2003-05-07 19:21:28 +0000 | [diff] [blame^] | 677 | BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 678 | break; |
| 679 | case cFP: // Floats & Doubles: Return in ST(0) |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 680 | BuildMI(BB, X86::FpSETRESULT, 1).addReg(RetReg); |
Chris Lattner | dbd7372 | 2003-05-06 21:32:22 +0000 | [diff] [blame] | 681 | // Declare that top-of-stack is live on exit |
Chris Lattner | c248903 | 2003-05-07 19:21:28 +0000 | [diff] [blame^] | 682 | BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 683 | break; |
| 684 | case cLong: |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 685 | BuildMI(BB, X86::MOVrr32, 1, X86::EAX).addReg(RetReg); |
| 686 | BuildMI(BB, X86::MOVrr32, 1, X86::EDX).addReg(RetReg+1); |
Chris Lattner | dbd7372 | 2003-05-06 21:32:22 +0000 | [diff] [blame] | 687 | // Declare that EAX & EDX are live on exit |
Chris Lattner | c248903 | 2003-05-07 19:21:28 +0000 | [diff] [blame^] | 688 | BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX).addReg(X86::ESP); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 689 | break; |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 690 | default: |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 691 | visitInstruction(I); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 692 | } |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame] | 693 | // Emit a 'ret' instruction |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 694 | BuildMI(BB, X86::RET, 0); |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 695 | } |
| 696 | |
Chris Lattner | 55f6fab | 2003-01-16 18:07:23 +0000 | [diff] [blame] | 697 | // getBlockAfter - Return the basic block which occurs lexically after the |
| 698 | // specified one. |
| 699 | static inline BasicBlock *getBlockAfter(BasicBlock *BB) { |
| 700 | Function::iterator I = BB; ++I; // Get iterator to next block |
| 701 | return I != BB->getParent()->end() ? &*I : 0; |
| 702 | } |
| 703 | |
Chris Lattner | 51b49a9 | 2002-11-02 19:45:49 +0000 | [diff] [blame] | 704 | /// visitBranchInst - Handle conditional and unconditional branches here. Note |
| 705 | /// that since code layout is frozen at this point, that if we are trying to |
| 706 | /// jump to a block that is the immediate successor of the current block, we can |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 707 | /// just make a fall-through (but we don't currently). |
Chris Lattner | 51b49a9 | 2002-11-02 19:45:49 +0000 | [diff] [blame] | 708 | /// |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 709 | void ISel::visitBranchInst(BranchInst &BI) { |
Chris Lattner | 55f6fab | 2003-01-16 18:07:23 +0000 | [diff] [blame] | 710 | BasicBlock *NextBB = getBlockAfter(BI.getParent()); // BB after current one |
| 711 | |
| 712 | if (!BI.isConditional()) { // Unconditional branch? |
| 713 | if (BI.getSuccessor(0) != NextBB) |
| 714 | BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(0)); |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 715 | return; |
| 716 | } |
| 717 | |
| 718 | // See if we can fold the setcc into the branch itself... |
| 719 | SetCondInst *SCI = canFoldSetCCIntoBranch(BI.getCondition()); |
| 720 | if (SCI == 0) { |
| 721 | // Nope, cannot fold setcc into this branch. Emit a branch on a condition |
| 722 | // computed some other way... |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 723 | unsigned condReg = getReg(BI.getCondition()); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 724 | BuildMI(BB, X86::CMPri8, 2).addReg(condReg).addZImm(0); |
Chris Lattner | 55f6fab | 2003-01-16 18:07:23 +0000 | [diff] [blame] | 725 | if (BI.getSuccessor(1) == NextBB) { |
| 726 | if (BI.getSuccessor(0) != NextBB) |
| 727 | BuildMI(BB, X86::JNE, 1).addPCDisp(BI.getSuccessor(0)); |
| 728 | } else { |
| 729 | BuildMI(BB, X86::JE, 1).addPCDisp(BI.getSuccessor(1)); |
| 730 | |
| 731 | if (BI.getSuccessor(0) != NextBB) |
| 732 | BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(0)); |
| 733 | } |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 734 | return; |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 735 | } |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 736 | |
| 737 | unsigned OpNum = getSetCCNumber(SCI->getOpcode()); |
| 738 | bool isSigned = EmitComparisonGetSignedness(OpNum, SCI->getOperand(0), |
| 739 | SCI->getOperand(1)); |
| 740 | |
| 741 | // LLVM -> X86 signed X86 unsigned |
| 742 | // ----- ---------- ------------ |
| 743 | // seteq -> je je |
| 744 | // setne -> jne jne |
| 745 | // setlt -> jl jb |
Chris Lattner | 55f6fab | 2003-01-16 18:07:23 +0000 | [diff] [blame] | 746 | // setge -> jge jae |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 747 | // setgt -> jg ja |
| 748 | // setle -> jle jbe |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 749 | static const unsigned OpcodeTab[2][6] = { |
Chris Lattner | 55f6fab | 2003-01-16 18:07:23 +0000 | [diff] [blame] | 750 | { X86::JE, X86::JNE, X86::JB, X86::JAE, X86::JA, X86::JBE }, |
| 751 | { X86::JE, X86::JNE, X86::JL, X86::JGE, X86::JG, X86::JLE }, |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 752 | }; |
| 753 | |
Chris Lattner | 55f6fab | 2003-01-16 18:07:23 +0000 | [diff] [blame] | 754 | if (BI.getSuccessor(0) != NextBB) { |
| 755 | BuildMI(BB, OpcodeTab[isSigned][OpNum], 1).addPCDisp(BI.getSuccessor(0)); |
| 756 | if (BI.getSuccessor(1) != NextBB) |
| 757 | BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(1)); |
| 758 | } else { |
| 759 | // Change to the inverse condition... |
| 760 | if (BI.getSuccessor(1) != NextBB) { |
| 761 | OpNum ^= 1; |
| 762 | BuildMI(BB, OpcodeTab[isSigned][OpNum], 1).addPCDisp(BI.getSuccessor(1)); |
| 763 | } |
| 764 | } |
Chris Lattner | 2df035b | 2002-11-02 19:27:56 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 767 | |
| 768 | /// doCall - This emits an abstract call instruction, setting up the arguments |
| 769 | /// and the return value as appropriate. For the actual function call itself, |
| 770 | /// it inserts the specified CallMI instruction into the stream. |
| 771 | /// |
| 772 | void ISel::doCall(const ValueRecord &Ret, MachineInstr *CallMI, |
| 773 | const std::vector<ValueRecord> &Args) { |
| 774 | |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 775 | // Count how many bytes are to be pushed on the stack... |
| 776 | unsigned NumBytes = 0; |
Misha Brukman | 0d2cf3a | 2002-12-04 19:22:53 +0000 | [diff] [blame] | 777 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 778 | if (!Args.empty()) { |
| 779 | for (unsigned i = 0, e = Args.size(); i != e; ++i) |
| 780 | switch (getClassB(Args[i].Ty)) { |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 781 | case cByte: case cShort: case cInt: |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 782 | NumBytes += 4; break; |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 783 | case cLong: |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 784 | NumBytes += 8; break; |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 785 | case cFP: |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 786 | NumBytes += Args[i].Ty == Type::FloatTy ? 4 : 8; |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 787 | break; |
| 788 | default: assert(0 && "Unknown class!"); |
| 789 | } |
| 790 | |
| 791 | // Adjust the stack pointer for the new arguments... |
| 792 | BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addZImm(NumBytes); |
| 793 | |
| 794 | // Arguments go on the stack in reverse order, as specified by the ABI. |
| 795 | unsigned ArgOffset = 0; |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 796 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
| 797 | unsigned ArgReg = Args[i].Reg; |
| 798 | switch (getClassB(Args[i].Ty)) { |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 799 | case cByte: |
| 800 | case cShort: { |
| 801 | // Promote arg to 32 bits wide into a temporary register... |
| 802 | unsigned R = makeAnotherReg(Type::UIntTy); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 803 | promote32(R, Args[i]); |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 804 | addRegOffset(BuildMI(BB, X86::MOVrm32, 5), |
| 805 | X86::ESP, ArgOffset).addReg(R); |
| 806 | break; |
| 807 | } |
| 808 | case cInt: |
| 809 | addRegOffset(BuildMI(BB, X86::MOVrm32, 5), |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 810 | X86::ESP, ArgOffset).addReg(ArgReg); |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 811 | break; |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 812 | case cLong: |
| 813 | addRegOffset(BuildMI(BB, X86::MOVrm32, 5), |
| 814 | X86::ESP, ArgOffset).addReg(ArgReg); |
| 815 | addRegOffset(BuildMI(BB, X86::MOVrm32, 5), |
| 816 | X86::ESP, ArgOffset+4).addReg(ArgReg+1); |
| 817 | ArgOffset += 4; // 8 byte entry, not 4. |
| 818 | break; |
| 819 | |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 820 | case cFP: |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 821 | if (Args[i].Ty == Type::FloatTy) { |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 822 | addRegOffset(BuildMI(BB, X86::FSTr32, 5), |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 823 | X86::ESP, ArgOffset).addReg(ArgReg); |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 824 | } else { |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 825 | assert(Args[i].Ty == Type::DoubleTy && "Unknown FP type!"); |
| 826 | addRegOffset(BuildMI(BB, X86::FSTr64, 5), |
| 827 | X86::ESP, ArgOffset).addReg(ArgReg); |
| 828 | ArgOffset += 4; // 8 byte entry, not 4. |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 829 | } |
| 830 | break; |
| 831 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 832 | default: assert(0 && "Unknown class!"); |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 833 | } |
| 834 | ArgOffset += 4; |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 835 | } |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 836 | } else { |
| 837 | BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addZImm(0); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 838 | } |
Chris Lattner | 6e49a4b | 2002-12-13 14:13:27 +0000 | [diff] [blame] | 839 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 840 | BB->push_back(CallMI); |
Misha Brukman | 0d2cf3a | 2002-12-04 19:22:53 +0000 | [diff] [blame] | 841 | |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 842 | BuildMI(BB, X86::ADJCALLSTACKUP, 1).addZImm(NumBytes); |
Chris Lattner | a324364 | 2002-12-04 23:45:28 +0000 | [diff] [blame] | 843 | |
| 844 | // If there is a return value, scavenge the result from the location the call |
| 845 | // leaves it in... |
| 846 | // |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 847 | if (Ret.Ty != Type::VoidTy) { |
| 848 | unsigned DestClass = getClassB(Ret.Ty); |
| 849 | switch (DestClass) { |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 850 | case cByte: |
| 851 | case cShort: |
| 852 | case cInt: { |
| 853 | // Integral results are in %eax, or the appropriate portion |
| 854 | // thereof. |
| 855 | static const unsigned regRegMove[] = { |
| 856 | X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 |
| 857 | }; |
| 858 | static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX }; |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 859 | BuildMI(BB, regRegMove[DestClass], 1, Ret.Reg).addReg(AReg[DestClass]); |
Chris Lattner | 4fa1acc | 2002-12-04 23:50:28 +0000 | [diff] [blame] | 860 | break; |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 861 | } |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 862 | case cFP: // Floating-point return values live in %ST(0) |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 863 | BuildMI(BB, X86::FpGETRESULT, 1, Ret.Reg); |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 864 | break; |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 865 | case cLong: // Long values are left in EDX:EAX |
| 866 | BuildMI(BB, X86::MOVrr32, 1, Ret.Reg).addReg(X86::EAX); |
| 867 | BuildMI(BB, X86::MOVrr32, 1, Ret.Reg+1).addReg(X86::EDX); |
| 868 | break; |
| 869 | default: assert(0 && "Unknown class!"); |
Chris Lattner | 4fa1acc | 2002-12-04 23:50:28 +0000 | [diff] [blame] | 870 | } |
Chris Lattner | a324364 | 2002-12-04 23:45:28 +0000 | [diff] [blame] | 871 | } |
Brian Gaeke | fa8d571 | 2002-11-22 11:07:01 +0000 | [diff] [blame] | 872 | } |
Chris Lattner | 2df035b | 2002-11-02 19:27:56 +0000 | [diff] [blame] | 873 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 874 | |
| 875 | /// visitCallInst - Push args on stack and do a procedure call instruction. |
| 876 | void ISel::visitCallInst(CallInst &CI) { |
| 877 | MachineInstr *TheCall; |
| 878 | if (Function *F = CI.getCalledFunction()) { |
| 879 | // Emit a CALL instruction with PC-relative displacement. |
| 880 | TheCall = BuildMI(X86::CALLpcrel32, 1).addGlobalAddress(F, true); |
| 881 | } else { // Emit an indirect call... |
| 882 | unsigned Reg = getReg(CI.getCalledValue()); |
| 883 | TheCall = BuildMI(X86::CALLr32, 1).addReg(Reg); |
| 884 | } |
| 885 | |
| 886 | std::vector<ValueRecord> Args; |
| 887 | for (unsigned i = 1, e = CI.getNumOperands(); i != e; ++i) |
| 888 | Args.push_back(ValueRecord(getReg(CI.getOperand(i)), |
| 889 | CI.getOperand(i)->getType())); |
| 890 | |
| 891 | unsigned DestReg = CI.getType() != Type::VoidTy ? getReg(CI) : 0; |
| 892 | doCall(ValueRecord(DestReg, CI.getType()), TheCall, Args); |
| 893 | } |
| 894 | |
| 895 | |
Chris Lattner | 68aad93 | 2002-11-02 20:13:22 +0000 | [diff] [blame] | 896 | /// visitSimpleBinary - Implement simple binary operators for integral types... |
| 897 | /// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or, |
| 898 | /// 4 for Xor. |
| 899 | /// |
| 900 | void ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) { |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 901 | unsigned Class = getClassB(B.getType()); |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 902 | |
| 903 | static const unsigned OpcodeTab[][4] = { |
Chris Lattner | 68aad93 | 2002-11-02 20:13:22 +0000 | [diff] [blame] | 904 | // Arithmetic operators |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 905 | { X86::ADDrr8, X86::ADDrr16, X86::ADDrr32, X86::FpADD }, // ADD |
| 906 | { X86::SUBrr8, X86::SUBrr16, X86::SUBrr32, X86::FpSUB }, // SUB |
Chris Lattner | 68aad93 | 2002-11-02 20:13:22 +0000 | [diff] [blame] | 907 | |
| 908 | // Bitwise operators |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 909 | { X86::ANDrr8, X86::ANDrr16, X86::ANDrr32, 0 }, // AND |
| 910 | { X86:: ORrr8, X86:: ORrr16, X86:: ORrr32, 0 }, // OR |
| 911 | { X86::XORrr8, X86::XORrr16, X86::XORrr32, 0 }, // XOR |
| 912 | }; |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 913 | |
| 914 | bool isLong = false; |
| 915 | if (Class == cLong) { |
| 916 | isLong = true; |
| 917 | Class = cInt; // Bottom 32 bits are handled just like ints |
| 918 | } |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 919 | |
| 920 | unsigned Opcode = OpcodeTab[OperatorClass][Class]; |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 921 | assert(Opcode && "Floating point arguments to logical inst?"); |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 922 | unsigned Op0r = getReg(B.getOperand(0)); |
| 923 | unsigned Op1r = getReg(B.getOperand(1)); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 924 | unsigned DestReg = getReg(B); |
| 925 | BuildMI(BB, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r); |
| 926 | |
| 927 | if (isLong) { // Handle the upper 32 bits of long values... |
| 928 | static const unsigned TopTab[] = { |
| 929 | X86::ADCrr32, X86::SBBrr32, X86::ANDrr32, X86::ORrr32, X86::XORrr32 |
| 930 | }; |
| 931 | BuildMI(BB, TopTab[OperatorClass], 2, |
| 932 | DestReg+1).addReg(Op0r+1).addReg(Op1r+1); |
| 933 | } |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 934 | } |
| 935 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 936 | /// doMultiply - Emit appropriate instructions to multiply together the |
| 937 | /// registers op0Reg and op1Reg, and put the result in DestReg. The type of the |
| 938 | /// result should be given as DestTy. |
| 939 | /// |
| 940 | /// FIXME: doMultiply should use one of the two address IMUL instructions! |
| 941 | /// |
Chris Lattner | 8a307e8 | 2002-12-16 19:32:50 +0000 | [diff] [blame] | 942 | void ISel::doMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator &MBBI, |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 943 | unsigned DestReg, const Type *DestTy, |
Chris Lattner | 8a307e8 | 2002-12-16 19:32:50 +0000 | [diff] [blame] | 944 | unsigned op0Reg, unsigned op1Reg) { |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 945 | unsigned Class = getClass(DestTy); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 946 | switch (Class) { |
| 947 | case cFP: // Floating point multiply |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 948 | BMI(BB, MBBI, X86::FpMUL, 2, DestReg).addReg(op0Reg).addReg(op1Reg); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 949 | return; |
| 950 | default: |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 951 | case cLong: assert(0 && "doMultiply cannot operate on LONG values!"); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 952 | case cByte: |
| 953 | case cShort: |
| 954 | case cInt: // Small integerals, handled below... |
| 955 | break; |
| 956 | } |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 957 | |
| 958 | static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX }; |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 959 | static const unsigned MulOpcode[]={ X86::MULr8 , X86::MULr16 , X86::MULr32 }; |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 960 | static const unsigned MovOpcode[]={ X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 }; |
| 961 | unsigned Reg = Regs[Class]; |
| 962 | |
| 963 | // Emit a MOV to put the first operand into the appropriately-sized |
| 964 | // subreg of EAX. |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 965 | BMI(MBB, MBBI, MovOpcode[Class], 1, Reg).addReg(op0Reg); |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 966 | |
| 967 | // Emit the appropriate multiply instruction. |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 968 | BMI(MBB, MBBI, MulOpcode[Class], 1).addReg(op1Reg); |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 969 | |
| 970 | // Emit another MOV to put the result into the destination register. |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 971 | BMI(MBB, MBBI, MovOpcode[Class], 1, DestReg).addReg(Reg); |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 972 | } |
| 973 | |
Chris Lattner | ca9671d | 2002-11-02 20:28:58 +0000 | [diff] [blame] | 974 | /// visitMul - Multiplies are not simple binary operators because they must deal |
| 975 | /// with the EAX register explicitly. |
| 976 | /// |
| 977 | void ISel::visitMul(BinaryOperator &I) { |
Chris Lattner | 202a2d0 | 2002-12-13 13:07:42 +0000 | [diff] [blame] | 978 | unsigned Op0Reg = getReg(I.getOperand(0)); |
| 979 | unsigned Op1Reg = getReg(I.getOperand(1)); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 980 | unsigned DestReg = getReg(I); |
| 981 | |
| 982 | // Simple scalar multiply? |
| 983 | if (I.getType() != Type::LongTy && I.getType() != Type::ULongTy) { |
| 984 | MachineBasicBlock::iterator MBBI = BB->end(); |
| 985 | doMultiply(BB, MBBI, DestReg, I.getType(), Op0Reg, Op1Reg); |
| 986 | } else { |
| 987 | // Long value. We have to do things the hard way... |
| 988 | // Multiply the two low parts... capturing carry into EDX |
| 989 | BuildMI(BB, X86::MOVrr32, 1, X86::EAX).addReg(Op0Reg); |
| 990 | BuildMI(BB, X86::MULr32, 1).addReg(Op1Reg); // AL*BL |
| 991 | |
| 992 | unsigned OverflowReg = makeAnotherReg(Type::UIntTy); |
| 993 | BuildMI(BB, X86::MOVrr32, 1, DestReg).addReg(X86::EAX); // AL*BL |
| 994 | BuildMI(BB, X86::MOVrr32, 1, OverflowReg).addReg(X86::EDX); // AL*BL >> 32 |
| 995 | |
| 996 | MachineBasicBlock::iterator MBBI = BB->end(); |
| 997 | unsigned AHBLReg = makeAnotherReg(Type::UIntTy); |
| 998 | doMultiply(BB, MBBI, AHBLReg, Type::UIntTy, Op0Reg+1, Op1Reg); // AH*BL |
| 999 | |
| 1000 | unsigned AHBLplusOverflowReg = makeAnotherReg(Type::UIntTy); |
| 1001 | BuildMI(BB, X86::ADDrr32, 2, // AH*BL+(AL*BL >> 32) |
| 1002 | AHBLplusOverflowReg).addReg(AHBLReg).addReg(OverflowReg); |
| 1003 | |
| 1004 | MBBI = BB->end(); |
| 1005 | unsigned ALBHReg = makeAnotherReg(Type::UIntTy); |
| 1006 | doMultiply(BB, MBBI, ALBHReg, Type::UIntTy, Op0Reg, Op1Reg+1); // AL*BH |
| 1007 | |
| 1008 | BuildMI(BB, X86::ADDrr32, 2, // AL*BH + AH*BL + (AL*BL >> 32) |
| 1009 | DestReg+1).addReg(AHBLplusOverflowReg).addReg(ALBHReg); |
| 1010 | } |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 1011 | } |
Chris Lattner | ca9671d | 2002-11-02 20:28:58 +0000 | [diff] [blame] | 1012 | |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 1013 | |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 1014 | /// visitDivRem - Handle division and remainder instructions... these |
| 1015 | /// instruction both require the same instructions to be generated, they just |
| 1016 | /// select the result from a different register. Note that both of these |
| 1017 | /// instructions work differently for signed and unsigned operands. |
| 1018 | /// |
| 1019 | void ISel::visitDivRem(BinaryOperator &I) { |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1020 | unsigned Class = getClass(I.getType()); |
| 1021 | unsigned Op0Reg = getReg(I.getOperand(0)); |
| 1022 | unsigned Op1Reg = getReg(I.getOperand(1)); |
| 1023 | unsigned ResultReg = getReg(I); |
| 1024 | |
| 1025 | switch (Class) { |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1026 | case cFP: // Floating point divide |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1027 | if (I.getOpcode() == Instruction::Div) |
| 1028 | BuildMI(BB, X86::FpDIV, 2, ResultReg).addReg(Op0Reg).addReg(Op1Reg); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1029 | else { // Floating point remainder... |
| 1030 | MachineInstr *TheCall = |
| 1031 | BuildMI(X86::CALLpcrel32, 1).addExternalSymbol("fmod", true); |
| 1032 | std::vector<ValueRecord> Args; |
| 1033 | Args.push_back(ValueRecord(Op0Reg, Type::DoubleTy)); |
| 1034 | Args.push_back(ValueRecord(Op1Reg, Type::DoubleTy)); |
| 1035 | doCall(ValueRecord(ResultReg, Type::DoubleTy), TheCall, Args); |
| 1036 | } |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1037 | return; |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1038 | case cLong: { |
| 1039 | static const char *FnName[] = |
| 1040 | { "__moddi3", "__divdi3", "__umoddi3", "__udivdi3" }; |
| 1041 | |
| 1042 | unsigned NameIdx = I.getType()->isUnsigned()*2; |
| 1043 | NameIdx += I.getOpcode() == Instruction::Div; |
| 1044 | MachineInstr *TheCall = |
| 1045 | BuildMI(X86::CALLpcrel32, 1).addExternalSymbol(FnName[NameIdx], true); |
| 1046 | |
| 1047 | std::vector<ValueRecord> Args; |
| 1048 | Args.push_back(ValueRecord(Op0Reg, Type::LongTy)); |
| 1049 | Args.push_back(ValueRecord(Op1Reg, Type::LongTy)); |
| 1050 | doCall(ValueRecord(ResultReg, Type::LongTy), TheCall, Args); |
| 1051 | return; |
| 1052 | } |
| 1053 | case cByte: case cShort: case cInt: |
| 1054 | break; // Small integerals, handled below... |
| 1055 | default: assert(0 && "Unknown class!"); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1056 | } |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 1057 | |
| 1058 | static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX }; |
| 1059 | static const unsigned MovOpcode[]={ X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 }; |
Brian Gaeke | 6559bb9 | 2002-11-14 22:32:30 +0000 | [diff] [blame] | 1060 | static const unsigned ExtOpcode[]={ X86::CBW , X86::CWD , X86::CDQ }; |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 1061 | static const unsigned ClrOpcode[]={ X86::XORrr8, X86::XORrr16, X86::XORrr32 }; |
| 1062 | static const unsigned ExtRegs[] ={ X86::AH , X86::DX , X86::EDX }; |
| 1063 | |
| 1064 | static const unsigned DivOpcode[][4] = { |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1065 | { X86::DIVr8 , X86::DIVr16 , X86::DIVr32 , 0 }, // Unsigned division |
| 1066 | { X86::IDIVr8, X86::IDIVr16, X86::IDIVr32, 0 }, // Signed division |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 1067 | }; |
| 1068 | |
| 1069 | bool isSigned = I.getType()->isSigned(); |
| 1070 | unsigned Reg = Regs[Class]; |
| 1071 | unsigned ExtReg = ExtRegs[Class]; |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 1072 | |
| 1073 | // Put the first operand into one of the A registers... |
| 1074 | BuildMI(BB, MovOpcode[Class], 1, Reg).addReg(Op0Reg); |
| 1075 | |
| 1076 | if (isSigned) { |
| 1077 | // Emit a sign extension instruction... |
Chris Lattner | a4978cc | 2002-12-01 23:24:58 +0000 | [diff] [blame] | 1078 | BuildMI(BB, ExtOpcode[Class], 0); |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 1079 | } else { |
| 1080 | // If unsigned, emit a zeroing instruction... (reg = xor reg, reg) |
| 1081 | BuildMI(BB, ClrOpcode[Class], 2, ExtReg).addReg(ExtReg).addReg(ExtReg); |
| 1082 | } |
| 1083 | |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 1084 | // Emit the appropriate divide or remainder instruction... |
Chris Lattner | 92845e3 | 2002-11-21 18:54:29 +0000 | [diff] [blame] | 1085 | BuildMI(BB, DivOpcode[isSigned][Class], 1).addReg(Op1Reg); |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 1086 | |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 1087 | // Figure out which register we want to pick the result out of... |
| 1088 | unsigned DestReg = (I.getOpcode() == Instruction::Div) ? Reg : ExtReg; |
| 1089 | |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 1090 | // Put the result into the destination register... |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1091 | BuildMI(BB, MovOpcode[Class], 1, ResultReg).addReg(DestReg); |
Chris Lattner | ca9671d | 2002-11-02 20:28:58 +0000 | [diff] [blame] | 1092 | } |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 1093 | |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 1094 | |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 1095 | /// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here |
| 1096 | /// for constant immediate shift values, and for constant immediate |
| 1097 | /// shift values equal to 1. Even the general case is sort of special, |
| 1098 | /// because the shift amount has to be in CL, not just any old register. |
| 1099 | /// |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1100 | void ISel::visitShiftInst(ShiftInst &I) { |
| 1101 | unsigned SrcReg = getReg(I.getOperand(0)); |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 1102 | unsigned DestReg = getReg(I); |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 1103 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1104 | bool isSigned = I.getType()->isSigned(); |
| 1105 | unsigned Class = getClass(I.getType()); |
| 1106 | |
| 1107 | static const unsigned ConstantOperand[][4] = { |
| 1108 | { X86::SHRir8, X86::SHRir16, X86::SHRir32, X86::SHRDir32 }, // SHR |
| 1109 | { X86::SARir8, X86::SARir16, X86::SARir32, X86::SHRDir32 }, // SAR |
| 1110 | { X86::SHLir8, X86::SHLir16, X86::SHLir32, X86::SHLDir32 }, // SHL |
| 1111 | { X86::SHLir8, X86::SHLir16, X86::SHLir32, X86::SHLDir32 }, // SAL = SHL |
| 1112 | }; |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 1113 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1114 | static const unsigned NonConstantOperand[][4] = { |
| 1115 | { X86::SHRrr8, X86::SHRrr16, X86::SHRrr32 }, // SHR |
| 1116 | { X86::SARrr8, X86::SARrr16, X86::SARrr32 }, // SAR |
| 1117 | { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32 }, // SHL |
| 1118 | { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32 }, // SAL = SHL |
| 1119 | }; |
Chris Lattner | 796df73 | 2002-11-02 00:44:25 +0000 | [diff] [blame] | 1120 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1121 | // Longs, as usual, are handled specially... |
| 1122 | if (Class == cLong) { |
| 1123 | // If we have a constant shift, we can generate much more efficient code |
| 1124 | // than otherwise... |
| 1125 | // |
| 1126 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(I.getOperand(1))) { |
| 1127 | unsigned Amount = CUI->getValue(); |
| 1128 | if (Amount < 32) { |
| 1129 | const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned]; |
| 1130 | if (isLeftShift) { |
| 1131 | BuildMI(BB, Opc[3], 3, |
| 1132 | DestReg+1).addReg(SrcReg+1).addReg(SrcReg).addZImm(Amount); |
| 1133 | BuildMI(BB, Opc[2], 2, DestReg).addReg(SrcReg).addZImm(Amount); |
| 1134 | } else { |
| 1135 | BuildMI(BB, Opc[3], 3, |
| 1136 | DestReg).addReg(SrcReg ).addReg(SrcReg+1).addZImm(Amount); |
| 1137 | BuildMI(BB, Opc[2], 2, DestReg+1).addReg(SrcReg+1).addZImm(Amount); |
| 1138 | } |
| 1139 | } else { // Shifting more than 32 bits |
| 1140 | Amount -= 32; |
| 1141 | if (isLeftShift) { |
| 1142 | BuildMI(BB, X86::SHLir32, 2,DestReg+1).addReg(SrcReg).addZImm(Amount); |
| 1143 | BuildMI(BB, X86::MOVir32, 1,DestReg ).addZImm(0); |
| 1144 | } else { |
| 1145 | unsigned Opcode = isSigned ? X86::SARir32 : X86::SHRir32; |
| 1146 | BuildMI(BB, Opcode, 2, DestReg).addReg(SrcReg+1).addZImm(Amount); |
| 1147 | BuildMI(BB, X86::MOVir32, 1, DestReg+1).addZImm(0); |
| 1148 | } |
| 1149 | } |
| 1150 | } else { |
| 1151 | visitInstruction(I); // FIXME: Implement long shift by non-constant |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 1152 | } |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1153 | return; |
| 1154 | } |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 1155 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1156 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(I.getOperand(1))) { |
| 1157 | // The shift amount is constant, guaranteed to be a ubyte. Get its value. |
| 1158 | assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?"); |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 1159 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1160 | const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned]; |
| 1161 | BuildMI(BB, Opc[Class], 2, DestReg).addReg(SrcReg).addZImm(CUI->getValue()); |
| 1162 | } else { // The shift amount is non-constant. |
| 1163 | BuildMI(BB, X86::MOVrr8, 1, X86::CL).addReg(getReg(I.getOperand(1))); |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 1164 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1165 | const unsigned *Opc = NonConstantOperand[isLeftShift*2+isSigned]; |
| 1166 | BuildMI(BB, Opc[Class], 1, DestReg).addReg(SrcReg); |
| 1167 | } |
| 1168 | } |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 1169 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1170 | |
| 1171 | /// doFPLoad - This method is used to load an FP value from memory using the |
| 1172 | /// current endianness. NOTE: This method returns a partially constructed load |
| 1173 | /// instruction which needs to have the memory source filled in still. |
| 1174 | /// |
| 1175 | MachineInstr *ISel::doFPLoad(MachineBasicBlock *MBB, |
| 1176 | MachineBasicBlock::iterator &MBBI, |
| 1177 | const Type *Ty, unsigned DestReg) { |
| 1178 | assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!"); |
| 1179 | unsigned LoadOpcode = Ty == Type::FloatTy ? X86::FLDr32 : X86::FLDr64; |
| 1180 | |
| 1181 | if (TM.getTargetData().isLittleEndian()) // fast path... |
| 1182 | return BMI(MBB, MBBI, LoadOpcode, 4, DestReg); |
| 1183 | |
| 1184 | // If we are big-endian, start by creating an LEA instruction to represent the |
| 1185 | // address of the memory location to load from... |
| 1186 | // |
| 1187 | unsigned SrcAddrReg = makeAnotherReg(Type::UIntTy); |
| 1188 | MachineInstr *Result = BMI(MBB, MBBI, X86::LEAr32, 5, SrcAddrReg); |
| 1189 | |
| 1190 | // Allocate a temporary stack slot to transform the value into... |
| 1191 | int FrameIdx = F->getFrameInfo()->CreateStackObject(Ty, TM.getTargetData()); |
| 1192 | |
| 1193 | // Perform the bswaps 32 bits at a time... |
| 1194 | unsigned TmpReg1 = makeAnotherReg(Type::UIntTy); |
| 1195 | unsigned TmpReg2 = makeAnotherReg(Type::UIntTy); |
| 1196 | addDirectMem(BMI(MBB, MBBI, X86::MOVmr32, 4, TmpReg1), SrcAddrReg); |
| 1197 | BMI(MBB, MBBI, X86::BSWAPr32, 1, TmpReg2).addReg(TmpReg1); |
| 1198 | unsigned Offset = (Ty == Type::DoubleTy) << 2; |
| 1199 | addFrameReference(BMI(MBB, MBBI, X86::MOVrm32, 5), |
| 1200 | FrameIdx, Offset).addReg(TmpReg2); |
| 1201 | |
| 1202 | if (Ty == Type::DoubleTy) { // Swap the other 32 bits of a double value... |
| 1203 | TmpReg1 = makeAnotherReg(Type::UIntTy); |
| 1204 | TmpReg2 = makeAnotherReg(Type::UIntTy); |
| 1205 | |
| 1206 | addRegOffset(BMI(MBB, MBBI, X86::MOVmr32, 4, TmpReg1), SrcAddrReg, 4); |
| 1207 | BMI(MBB, MBBI, X86::BSWAPr32, 1, TmpReg2).addReg(TmpReg1); |
| 1208 | unsigned Offset = (Ty == Type::DoubleTy) << 2; |
| 1209 | addFrameReference(BMI(MBB, MBBI, X86::MOVrm32,5), FrameIdx).addReg(TmpReg2); |
| 1210 | } |
| 1211 | |
| 1212 | // Now we can reload the final byteswapped result into the final destination. |
| 1213 | addFrameReference(BMI(MBB, MBBI, LoadOpcode, 4, DestReg), FrameIdx); |
| 1214 | return Result; |
| 1215 | } |
| 1216 | |
| 1217 | /// EmitByteSwap - Byteswap SrcReg into DestReg. |
| 1218 | /// |
| 1219 | void ISel::EmitByteSwap(unsigned DestReg, unsigned SrcReg, unsigned Class) { |
| 1220 | // Emit the byte swap instruction... |
| 1221 | switch (Class) { |
| 1222 | case cByte: |
Misha Brukman | baf0607 | 2003-04-22 17:54:23 +0000 | [diff] [blame] | 1223 | // No byteswap necessary for 8 bit value... |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1224 | BuildMI(BB, X86::MOVrr8, 1, DestReg).addReg(SrcReg); |
| 1225 | break; |
| 1226 | case cInt: |
| 1227 | // Use the 32 bit bswap instruction to do a 32 bit swap... |
| 1228 | BuildMI(BB, X86::BSWAPr32, 1, DestReg).addReg(SrcReg); |
| 1229 | break; |
| 1230 | |
| 1231 | case cShort: |
| 1232 | // For 16 bit we have to use an xchg instruction, because there is no |
Misha Brukman | baf0607 | 2003-04-22 17:54:23 +0000 | [diff] [blame] | 1233 | // 16-bit bswap. XCHG is necessarily not in SSA form, so we force things |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1234 | // into AX to do the xchg. |
| 1235 | // |
| 1236 | BuildMI(BB, X86::MOVrr16, 1, X86::AX).addReg(SrcReg); |
| 1237 | BuildMI(BB, X86::XCHGrr8, 2).addReg(X86::AL, MOTy::UseAndDef) |
| 1238 | .addReg(X86::AH, MOTy::UseAndDef); |
| 1239 | BuildMI(BB, X86::MOVrr16, 1, DestReg).addReg(X86::AX); |
| 1240 | break; |
| 1241 | default: assert(0 && "Cannot byteswap this class!"); |
| 1242 | } |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 1245 | |
Chris Lattner | 6fc3c52 | 2002-11-17 21:11:55 +0000 | [diff] [blame] | 1246 | /// visitLoadInst - Implement LLVM load instructions in terms of the x86 'mov' |
Chris Lattner | e8f0d92 | 2002-12-24 00:03:11 +0000 | [diff] [blame] | 1247 | /// instruction. The load and store instructions are the only place where we |
| 1248 | /// need to worry about the memory layout of the target machine. |
Chris Lattner | 6fc3c52 | 2002-11-17 21:11:55 +0000 | [diff] [blame] | 1249 | /// |
| 1250 | void ISel::visitLoadInst(LoadInst &I) { |
Chris Lattner | e8f0d92 | 2002-12-24 00:03:11 +0000 | [diff] [blame] | 1251 | bool isLittleEndian = TM.getTargetData().isLittleEndian(); |
| 1252 | bool hasLongPointers = TM.getTargetData().getPointerSize() == 8; |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1253 | unsigned SrcAddrReg = getReg(I.getOperand(0)); |
| 1254 | unsigned DestReg = getReg(I); |
Chris Lattner | e8f0d92 | 2002-12-24 00:03:11 +0000 | [diff] [blame] | 1255 | |
Chris Lattner | 6fc3c52 | 2002-11-17 21:11:55 +0000 | [diff] [blame] | 1256 | unsigned Class = getClass(I.getType()); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1257 | switch (Class) { |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1258 | case cFP: { |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1259 | MachineBasicBlock::iterator MBBI = BB->end(); |
| 1260 | addDirectMem(doFPLoad(BB, MBBI, I.getType(), DestReg), SrcAddrReg); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1261 | return; |
| 1262 | } |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1263 | case cLong: case cInt: case cShort: case cByte: |
| 1264 | break; // Integers of various sizes handled below |
| 1265 | default: assert(0 && "Unknown memory class!"); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1266 | } |
Chris Lattner | 6fc3c52 | 2002-11-17 21:11:55 +0000 | [diff] [blame] | 1267 | |
Chris Lattner | e8f0d92 | 2002-12-24 00:03:11 +0000 | [diff] [blame] | 1268 | // We need to adjust the input pointer if we are emulating a big-endian |
| 1269 | // long-pointer target. On these systems, the pointer that we are interested |
| 1270 | // in is in the upper part of the eight byte memory image of the pointer. It |
| 1271 | // also happens to be byte-swapped, but this will be handled later. |
| 1272 | // |
| 1273 | if (!isLittleEndian && hasLongPointers && isa<PointerType>(I.getType())) { |
| 1274 | unsigned R = makeAnotherReg(Type::UIntTy); |
| 1275 | BuildMI(BB, X86::ADDri32, 2, R).addReg(SrcAddrReg).addZImm(4); |
| 1276 | SrcAddrReg = R; |
| 1277 | } |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1278 | |
Chris Lattner | e8f0d92 | 2002-12-24 00:03:11 +0000 | [diff] [blame] | 1279 | unsigned IReg = DestReg; |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1280 | if (!isLittleEndian) // If big endian we need an intermediate stage |
| 1281 | DestReg = makeAnotherReg(Class != cLong ? I.getType() : Type::UIntTy); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1282 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1283 | static const unsigned Opcode[] = { |
| 1284 | X86::MOVmr8, X86::MOVmr16, X86::MOVmr32, 0, X86::MOVmr32 |
| 1285 | }; |
Chris Lattner | e8f0d92 | 2002-12-24 00:03:11 +0000 | [diff] [blame] | 1286 | addDirectMem(BuildMI(BB, Opcode[Class], 4, DestReg), SrcAddrReg); |
| 1287 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1288 | // Handle long values now... |
| 1289 | if (Class == cLong) { |
| 1290 | if (isLittleEndian) { |
| 1291 | addRegOffset(BuildMI(BB, X86::MOVmr32, 4, DestReg+1), SrcAddrReg, 4); |
| 1292 | } else { |
| 1293 | EmitByteSwap(IReg+1, DestReg, cInt); |
| 1294 | unsigned TempReg = makeAnotherReg(Type::IntTy); |
| 1295 | addRegOffset(BuildMI(BB, X86::MOVmr32, 4, TempReg), SrcAddrReg, 4); |
| 1296 | EmitByteSwap(IReg, TempReg, cInt); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1297 | } |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1298 | return; |
| 1299 | } |
| 1300 | |
| 1301 | if (!isLittleEndian) |
| 1302 | EmitByteSwap(IReg, DestReg, Class); |
| 1303 | } |
| 1304 | |
| 1305 | |
| 1306 | /// doFPStore - This method is used to store an FP value to memory using the |
| 1307 | /// current endianness. |
| 1308 | /// |
| 1309 | void ISel::doFPStore(const Type *Ty, unsigned DestAddrReg, unsigned SrcReg) { |
| 1310 | assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!"); |
| 1311 | unsigned StoreOpcode = Ty == Type::FloatTy ? X86::FSTr32 : X86::FSTr64; |
| 1312 | |
| 1313 | if (TM.getTargetData().isLittleEndian()) { // fast path... |
| 1314 | addDirectMem(BuildMI(BB, StoreOpcode,5), DestAddrReg).addReg(SrcReg); |
| 1315 | return; |
| 1316 | } |
| 1317 | |
| 1318 | // Allocate a temporary stack slot to transform the value into... |
| 1319 | int FrameIdx = F->getFrameInfo()->CreateStackObject(Ty, TM.getTargetData()); |
| 1320 | unsigned SrcAddrReg = makeAnotherReg(Type::UIntTy); |
| 1321 | addFrameReference(BuildMI(BB, X86::LEAr32, 5, SrcAddrReg), FrameIdx); |
| 1322 | |
| 1323 | // Store the value into a temporary stack slot... |
| 1324 | addDirectMem(BuildMI(BB, StoreOpcode, 5), SrcAddrReg).addReg(SrcReg); |
| 1325 | |
| 1326 | // Perform the bswaps 32 bits at a time... |
| 1327 | unsigned TmpReg1 = makeAnotherReg(Type::UIntTy); |
| 1328 | unsigned TmpReg2 = makeAnotherReg(Type::UIntTy); |
| 1329 | addDirectMem(BuildMI(BB, X86::MOVmr32, 4, TmpReg1), SrcAddrReg); |
| 1330 | BuildMI(BB, X86::BSWAPr32, 1, TmpReg2).addReg(TmpReg1); |
| 1331 | unsigned Offset = (Ty == Type::DoubleTy) << 2; |
| 1332 | addRegOffset(BuildMI(BB, X86::MOVrm32, 5), |
| 1333 | DestAddrReg, Offset).addReg(TmpReg2); |
| 1334 | |
| 1335 | if (Ty == Type::DoubleTy) { // Swap the other 32 bits of a double value... |
| 1336 | TmpReg1 = makeAnotherReg(Type::UIntTy); |
| 1337 | TmpReg2 = makeAnotherReg(Type::UIntTy); |
| 1338 | |
| 1339 | addRegOffset(BuildMI(BB, X86::MOVmr32, 4, TmpReg1), SrcAddrReg, 4); |
| 1340 | BuildMI(BB, X86::BSWAPr32, 1, TmpReg2).addReg(TmpReg1); |
| 1341 | unsigned Offset = (Ty == Type::DoubleTy) << 2; |
| 1342 | addDirectMem(BuildMI(BB, X86::MOVrm32, 5), DestAddrReg).addReg(TmpReg2); |
Chris Lattner | e8f0d92 | 2002-12-24 00:03:11 +0000 | [diff] [blame] | 1343 | } |
Chris Lattner | 6fc3c52 | 2002-11-17 21:11:55 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 1346 | |
Chris Lattner | 6fc3c52 | 2002-11-17 21:11:55 +0000 | [diff] [blame] | 1347 | /// visitStoreInst - Implement LLVM store instructions in terms of the x86 'mov' |
| 1348 | /// instruction. |
| 1349 | /// |
| 1350 | void ISel::visitStoreInst(StoreInst &I) { |
Chris Lattner | e8f0d92 | 2002-12-24 00:03:11 +0000 | [diff] [blame] | 1351 | bool isLittleEndian = TM.getTargetData().isLittleEndian(); |
| 1352 | bool hasLongPointers = TM.getTargetData().getPointerSize() == 8; |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1353 | unsigned ValReg = getReg(I.getOperand(0)); |
| 1354 | unsigned AddressReg = getReg(I.getOperand(1)); |
Chris Lattner | e8f0d92 | 2002-12-24 00:03:11 +0000 | [diff] [blame] | 1355 | |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1356 | unsigned Class = getClass(I.getOperand(0)->getType()); |
| 1357 | switch (Class) { |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1358 | case cLong: |
| 1359 | if (isLittleEndian) { |
| 1360 | addDirectMem(BuildMI(BB, X86::MOVrm32, 1+4), AddressReg).addReg(ValReg); |
| 1361 | addRegOffset(BuildMI(BB, X86::MOVrm32, 1+4), |
| 1362 | AddressReg, 4).addReg(ValReg+1); |
| 1363 | } else { |
| 1364 | unsigned T1 = makeAnotherReg(Type::IntTy); |
| 1365 | unsigned T2 = makeAnotherReg(Type::IntTy); |
| 1366 | EmitByteSwap(T1, ValReg , cInt); |
| 1367 | EmitByteSwap(T2, ValReg+1, cInt); |
| 1368 | addDirectMem(BuildMI(BB, X86::MOVrm32, 1+4), AddressReg).addReg(T2); |
| 1369 | addRegOffset(BuildMI(BB, X86::MOVrm32, 1+4), AddressReg, 4).addReg(T1); |
| 1370 | } |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1371 | return; |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1372 | case cFP: |
| 1373 | doFPStore(I.getOperand(0)->getType(), AddressReg, ValReg); |
| 1374 | return; |
| 1375 | case cInt: case cShort: case cByte: |
| 1376 | break; // Integers of various sizes handled below |
| 1377 | default: assert(0 && "Unknown memory class!"); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | if (!isLittleEndian && hasLongPointers && |
| 1381 | isa<PointerType>(I.getOperand(0)->getType())) { |
Chris Lattner | e8f0d92 | 2002-12-24 00:03:11 +0000 | [diff] [blame] | 1382 | unsigned R = makeAnotherReg(Type::UIntTy); |
| 1383 | BuildMI(BB, X86::ADDri32, 2, R).addReg(AddressReg).addZImm(4); |
| 1384 | AddressReg = R; |
| 1385 | } |
| 1386 | |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1387 | if (!isLittleEndian && Class != cByte) { |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1388 | unsigned R = makeAnotherReg(I.getOperand(0)->getType()); |
| 1389 | EmitByteSwap(R, ValReg, Class); |
| 1390 | ValReg = R; |
Chris Lattner | e8f0d92 | 2002-12-24 00:03:11 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1393 | static const unsigned Opcode[] = { X86::MOVrm8, X86::MOVrm16, X86::MOVrm32 }; |
Chris Lattner | 6fc3c52 | 2002-11-17 21:11:55 +0000 | [diff] [blame] | 1394 | addDirectMem(BuildMI(BB, Opcode[Class], 1+4), AddressReg).addReg(ValReg); |
| 1395 | } |
| 1396 | |
| 1397 | |
Brian Gaeke | c11232a | 2002-11-26 10:43:30 +0000 | [diff] [blame] | 1398 | /// visitCastInst - Here we have various kinds of copying with or without |
| 1399 | /// sign extension going on. |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1400 | void ISel::visitCastInst(CastInst &CI) { |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 1401 | unsigned DestReg = getReg(CI); |
| 1402 | MachineBasicBlock::iterator MI = BB->end(); |
| 1403 | emitCastOperation(BB, MI, CI.getOperand(0), CI.getType(), DestReg); |
| 1404 | } |
| 1405 | |
| 1406 | /// emitCastOperation - Common code shared between visitCastInst and |
| 1407 | /// constant expression cast support. |
| 1408 | void ISel::emitCastOperation(MachineBasicBlock *BB, |
| 1409 | MachineBasicBlock::iterator &IP, |
| 1410 | Value *Src, const Type *DestTy, |
| 1411 | unsigned DestReg) { |
Chris Lattner | 3907d11 | 2003-04-23 17:57:48 +0000 | [diff] [blame] | 1412 | unsigned SrcReg = getReg(Src, BB, IP); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1413 | const Type *SrcTy = Src->getType(); |
| 1414 | unsigned SrcClass = getClassB(SrcTy); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1415 | unsigned DestClass = getClassB(DestTy); |
Chris Lattner | 7d25589 | 2002-12-13 11:31:59 +0000 | [diff] [blame] | 1416 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1417 | // Implement casts to bool by using compare on the operand followed by set if |
| 1418 | // not zero on the result. |
| 1419 | if (DestTy == Type::BoolTy) { |
| 1420 | if (SrcClass == cFP || SrcClass == cLong) |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 1421 | abort(); // FIXME: implement cast (long & FP) to bool |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1422 | |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 1423 | BMI(BB, IP, X86::CMPri8, 2).addReg(SrcReg).addZImm(0); |
| 1424 | BMI(BB, IP, X86::SETNEr, 1, DestReg); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1425 | return; |
| 1426 | } |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1427 | |
| 1428 | static const unsigned RegRegMove[] = { |
| 1429 | X86::MOVrr8, X86::MOVrr16, X86::MOVrr32, X86::FpMOV, X86::MOVrr32 |
| 1430 | }; |
| 1431 | |
| 1432 | // Implement casts between values of the same type class (as determined by |
| 1433 | // getClass) by using a register-to-register move. |
| 1434 | if (SrcClass == DestClass) { |
| 1435 | if (SrcClass <= cInt || (SrcClass == cFP && SrcTy == DestTy)) { |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 1436 | BMI(BB, IP, RegRegMove[SrcClass], 1, DestReg).addReg(SrcReg); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1437 | } else if (SrcClass == cFP) { |
| 1438 | if (SrcTy == Type::FloatTy) { // double -> float |
| 1439 | assert(DestTy == Type::DoubleTy && "Unknown cFP member!"); |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 1440 | BMI(BB, IP, X86::FpMOV, 1, DestReg).addReg(SrcReg); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1441 | } else { // float -> double |
| 1442 | assert(SrcTy == Type::DoubleTy && DestTy == Type::FloatTy && |
| 1443 | "Unknown cFP member!"); |
| 1444 | // Truncate from double to float by storing to memory as short, then |
| 1445 | // reading it back. |
| 1446 | unsigned FltAlign = TM.getTargetData().getFloatAlignment(); |
| 1447 | int FrameIdx = F->getFrameInfo()->CreateStackObject(4, FltAlign); |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 1448 | addFrameReference(BMI(BB, IP, X86::FSTr32, 5), FrameIdx).addReg(SrcReg); |
| 1449 | addFrameReference(BMI(BB, IP, X86::FLDr32, 5, DestReg), FrameIdx); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1450 | } |
| 1451 | } else if (SrcClass == cLong) { |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 1452 | BMI(BB, IP, X86::MOVrr32, 1, DestReg).addReg(SrcReg); |
| 1453 | BMI(BB, IP, X86::MOVrr32, 1, DestReg+1).addReg(SrcReg+1); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1454 | } else { |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 1455 | abort(); |
Brian Gaeke | d474e9c | 2002-12-06 10:49:33 +0000 | [diff] [blame] | 1456 | } |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1457 | return; |
| 1458 | } |
| 1459 | |
| 1460 | // Handle cast of SMALLER int to LARGER int using a move with sign extension |
| 1461 | // or zero extension, depending on whether the source type was signed. |
| 1462 | if (SrcClass <= cInt && (DestClass <= cInt || DestClass == cLong) && |
| 1463 | SrcClass < DestClass) { |
| 1464 | bool isLong = DestClass == cLong; |
| 1465 | if (isLong) DestClass = cInt; |
| 1466 | |
| 1467 | static const unsigned Opc[][4] = { |
| 1468 | { X86::MOVSXr16r8, X86::MOVSXr32r8, X86::MOVSXr32r16, X86::MOVrr32 }, // s |
| 1469 | { X86::MOVZXr16r8, X86::MOVZXr32r8, X86::MOVZXr32r16, X86::MOVrr32 } // u |
| 1470 | }; |
| 1471 | |
| 1472 | bool isUnsigned = SrcTy->isUnsigned(); |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 1473 | BMI(BB, IP, Opc[isUnsigned][SrcClass + DestClass - 1], 1, |
| 1474 | DestReg).addReg(SrcReg); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1475 | |
| 1476 | if (isLong) { // Handle upper 32 bits as appropriate... |
| 1477 | if (isUnsigned) // Zero out top bits... |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 1478 | BMI(BB, IP, X86::MOVir32, 1, DestReg+1).addZImm(0); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1479 | else // Sign extend bottom half... |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 1480 | BMI(BB, IP, X86::SARir32, 2, DestReg+1).addReg(DestReg).addZImm(31); |
Brian Gaeke | d474e9c | 2002-12-06 10:49:33 +0000 | [diff] [blame] | 1481 | } |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1482 | return; |
| 1483 | } |
| 1484 | |
| 1485 | // Special case long -> int ... |
| 1486 | if (SrcClass == cLong && DestClass == cInt) { |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 1487 | BMI(BB, IP, X86::MOVrr32, 1, DestReg).addReg(SrcReg); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1488 | return; |
| 1489 | } |
| 1490 | |
| 1491 | // Handle cast of LARGER int to SMALLER int using a move to EAX followed by a |
| 1492 | // move out of AX or AL. |
| 1493 | if ((SrcClass <= cInt || SrcClass == cLong) && DestClass <= cInt |
| 1494 | && SrcClass > DestClass) { |
| 1495 | static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX, 0, X86::EAX }; |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 1496 | BMI(BB, IP, RegRegMove[SrcClass], 1, AReg[SrcClass]).addReg(SrcReg); |
| 1497 | BMI(BB, IP, RegRegMove[DestClass], 1, DestReg).addReg(AReg[DestClass]); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1498 | return; |
| 1499 | } |
| 1500 | |
| 1501 | // Handle casts from integer to floating point now... |
| 1502 | if (DestClass == cFP) { |
| 1503 | // unsigned int -> load as 64 bit int. |
| 1504 | // unsigned long long -> more complex |
| 1505 | if (SrcTy->isUnsigned() && SrcTy != Type::UByteTy) |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 1506 | abort(); // don't handle unsigned src yet! |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1507 | |
| 1508 | // We don't have the facilities for directly loading byte sized data from |
| 1509 | // memory. Promote it to 16 bits. |
| 1510 | if (SrcClass == cByte) { |
| 1511 | unsigned TmpReg = makeAnotherReg(Type::ShortTy); |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 1512 | BMI(BB, IP, SrcTy->isSigned() ? X86::MOVSXr16r8 : X86::MOVZXr16r8, |
| 1513 | 1, TmpReg).addReg(SrcReg); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1514 | SrcTy = Type::ShortTy; // Pretend the short is our input now! |
| 1515 | SrcClass = cShort; |
| 1516 | SrcReg = TmpReg; |
| 1517 | } |
| 1518 | |
| 1519 | // Spill the integer to memory and reload it from there... |
| 1520 | int FrameIdx = |
| 1521 | F->getFrameInfo()->CreateStackObject(SrcTy, TM.getTargetData()); |
| 1522 | |
| 1523 | if (SrcClass == cLong) { |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 1524 | if (SrcTy == Type::ULongTy) abort(); // FIXME: Handle ulong -> FP |
| 1525 | addFrameReference(BMI(BB, IP, X86::MOVrm32, 5), FrameIdx).addReg(SrcReg); |
| 1526 | addFrameReference(BMI(BB, IP, X86::MOVrm32, 5), |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1527 | FrameIdx, 4).addReg(SrcReg+1); |
| 1528 | } else { |
| 1529 | static const unsigned Op1[] = { X86::MOVrm8, X86::MOVrm16, X86::MOVrm32 }; |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 1530 | addFrameReference(BMI(BB, IP, Op1[SrcClass], 5), FrameIdx).addReg(SrcReg); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | static const unsigned Op2[] = |
| 1534 | { 0, X86::FILDr16, X86::FILDr32, 0, X86::FILDr64 }; |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 1535 | addFrameReference(BMI(BB, IP, Op2[SrcClass], 5, DestReg), FrameIdx); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1536 | return; |
| 1537 | } |
| 1538 | |
| 1539 | // Handle casts from floating point to integer now... |
| 1540 | if (SrcClass == cFP) { |
| 1541 | // Change the floating point control register to use "round towards zero" |
| 1542 | // mode when truncating to an integer value. |
| 1543 | // |
| 1544 | int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2); |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 1545 | addFrameReference(BMI(BB, IP, X86::FNSTCWm16, 4), CWFrameIdx); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1546 | |
| 1547 | // Load the old value of the high byte of the control word... |
| 1548 | unsigned HighPartOfCW = makeAnotherReg(Type::UByteTy); |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 1549 | addFrameReference(BMI(BB, IP, X86::MOVmr8, 4, HighPartOfCW), CWFrameIdx, 1); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1550 | |
| 1551 | // Set the high part to be round to zero... |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 1552 | addFrameReference(BMI(BB, IP, X86::MOVim8, 5), CWFrameIdx, 1).addZImm(12); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1553 | |
| 1554 | // Reload the modified control word now... |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 1555 | addFrameReference(BMI(BB, IP, X86::FLDCWm16, 4), CWFrameIdx); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1556 | |
| 1557 | // Restore the memory image of control word to original value |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 1558 | addFrameReference(BMI(BB, IP, X86::MOVrm8, 5), |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1559 | CWFrameIdx, 1).addReg(HighPartOfCW); |
| 1560 | |
| 1561 | // We don't have the facilities for directly storing byte sized data to |
| 1562 | // memory. Promote it to 16 bits. We also must promote unsigned values to |
| 1563 | // larger classes because we only have signed FP stores. |
| 1564 | unsigned StoreClass = DestClass; |
| 1565 | const Type *StoreTy = DestTy; |
| 1566 | if (StoreClass == cByte || DestTy->isUnsigned()) |
| 1567 | switch (StoreClass) { |
| 1568 | case cByte: StoreTy = Type::ShortTy; StoreClass = cShort; break; |
| 1569 | case cShort: StoreTy = Type::IntTy; StoreClass = cInt; break; |
| 1570 | case cInt: StoreTy = Type::LongTy; StoreClass = cLong; break; |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 1571 | case cLong: abort(); // FIXME: unsigned long long -> more complex |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1572 | default: assert(0 && "Unknown store class!"); |
| 1573 | } |
| 1574 | |
| 1575 | // Spill the integer to memory and reload it from there... |
| 1576 | int FrameIdx = |
| 1577 | F->getFrameInfo()->CreateStackObject(StoreTy, TM.getTargetData()); |
| 1578 | |
| 1579 | static const unsigned Op1[] = |
| 1580 | { 0, X86::FISTr16, X86::FISTr32, 0, X86::FISTPr64 }; |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 1581 | addFrameReference(BMI(BB, IP, Op1[StoreClass], 5), FrameIdx).addReg(SrcReg); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1582 | |
| 1583 | if (DestClass == cLong) { |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 1584 | addFrameReference(BMI(BB, IP, X86::MOVmr32, 4, DestReg), FrameIdx); |
| 1585 | addFrameReference(BMI(BB, IP, X86::MOVmr32, 4, DestReg+1), FrameIdx, 4); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1586 | } else { |
| 1587 | static const unsigned Op2[] = { X86::MOVmr8, X86::MOVmr16, X86::MOVmr32 }; |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 1588 | addFrameReference(BMI(BB, IP, Op2[DestClass], 4, DestReg), FrameIdx); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | // Reload the original control word now... |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 1592 | addFrameReference(BMI(BB, IP, X86::FLDCWm16, 4), CWFrameIdx); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1593 | return; |
| 1594 | } |
| 1595 | |
Brian Gaeke | d474e9c | 2002-12-06 10:49:33 +0000 | [diff] [blame] | 1596 | // Anything we haven't handled already, we can't (yet) handle at all. |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 1597 | abort(); |
Brian Gaeke | fa8d571 | 2002-11-22 11:07:01 +0000 | [diff] [blame] | 1598 | } |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 1599 | |
Chris Lattner | 8a307e8 | 2002-12-16 19:32:50 +0000 | [diff] [blame] | 1600 | // ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It |
| 1601 | // returns zero when the input is not exactly a power of two. |
| 1602 | static unsigned ExactLog2(unsigned Val) { |
| 1603 | if (Val == 0) return 0; |
| 1604 | unsigned Count = 0; |
| 1605 | while (Val != 1) { |
| 1606 | if (Val & 1) return 0; |
| 1607 | Val >>= 1; |
| 1608 | ++Count; |
| 1609 | } |
| 1610 | return Count+1; |
| 1611 | } |
| 1612 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1613 | void ISel::visitGetElementPtrInst(GetElementPtrInst &I) { |
| 1614 | unsigned outputReg = getReg(I); |
Chris Lattner | f08ad9f | 2002-12-13 10:50:40 +0000 | [diff] [blame] | 1615 | MachineBasicBlock::iterator MI = BB->end(); |
| 1616 | emitGEPOperation(BB, MI, I.getOperand(0), |
Brian Gaeke | 68b1edc | 2002-12-16 04:23:29 +0000 | [diff] [blame] | 1617 | I.op_begin()+1, I.op_end(), outputReg); |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
Brian Gaeke | 71794c0 | 2002-12-13 11:22:48 +0000 | [diff] [blame] | 1620 | void ISel::emitGEPOperation(MachineBasicBlock *MBB, |
Chris Lattner | f08ad9f | 2002-12-13 10:50:40 +0000 | [diff] [blame] | 1621 | MachineBasicBlock::iterator &IP, |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 1622 | Value *Src, User::op_iterator IdxBegin, |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 1623 | User::op_iterator IdxEnd, unsigned TargetReg) { |
| 1624 | const TargetData &TD = TM.getTargetData(); |
| 1625 | const Type *Ty = Src->getType(); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1626 | unsigned BaseReg = getReg(Src, MBB, IP); |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 1627 | |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 1628 | // GEPs have zero or more indices; we must perform a struct access |
| 1629 | // or array access for each one. |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 1630 | for (GetElementPtrInst::op_iterator oi = IdxBegin, |
| 1631 | oe = IdxEnd; oi != oe; ++oi) { |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 1632 | Value *idx = *oi; |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1633 | unsigned NextReg = BaseReg; |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 1634 | if (const StructType *StTy = dyn_cast<StructType>(Ty)) { |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 1635 | // It's a struct access. idx is the index into the structure, |
| 1636 | // which names the field. This index must have ubyte type. |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 1637 | const ConstantUInt *CUI = cast<ConstantUInt>(idx); |
| 1638 | assert(CUI->getType() == Type::UByteTy |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 1639 | && "Funny-looking structure index in GEP"); |
| 1640 | // Use the TargetData structure to pick out what the layout of |
| 1641 | // the structure is in memory. Since the structure index must |
| 1642 | // be constant, we can get its value and use it to find the |
| 1643 | // right byte offset from the StructLayout class's list of |
| 1644 | // structure member offsets. |
Chris Lattner | e8f0d92 | 2002-12-24 00:03:11 +0000 | [diff] [blame] | 1645 | unsigned idxValue = CUI->getValue(); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1646 | unsigned FieldOff = TD.getStructLayout(StTy)->MemberOffsets[idxValue]; |
| 1647 | if (FieldOff) { |
| 1648 | NextReg = makeAnotherReg(Type::UIntTy); |
| 1649 | // Emit an ADD to add FieldOff to the basePtr. |
| 1650 | BMI(MBB, IP, X86::ADDri32, 2,NextReg).addReg(BaseReg).addZImm(FieldOff); |
| 1651 | } |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 1652 | // The next type is the member of the structure selected by the |
| 1653 | // index. |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 1654 | Ty = StTy->getElementTypes()[idxValue]; |
| 1655 | } else if (const SequentialType *SqTy = cast<SequentialType>(Ty)) { |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 1656 | // It's an array or pointer access: [ArraySize x ElementType]. |
Chris Lattner | 8a307e8 | 2002-12-16 19:32:50 +0000 | [diff] [blame] | 1657 | |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 1658 | // idx is the index into the array. Unlike with structure |
| 1659 | // indices, we may not know its actual value at code-generation |
| 1660 | // time. |
Chris Lattner | 8a307e8 | 2002-12-16 19:32:50 +0000 | [diff] [blame] | 1661 | assert(idx->getType() == Type::LongTy && "Bad GEP array index!"); |
| 1662 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1663 | // We want to add BaseReg to(idxReg * sizeof ElementType). First, we |
Chris Lattner | 8a307e8 | 2002-12-16 19:32:50 +0000 | [diff] [blame] | 1664 | // must find the size of the pointed-to type (Not coincidentally, the next |
| 1665 | // type is the type of the elements in the array). |
| 1666 | Ty = SqTy->getElementType(); |
| 1667 | unsigned elementSize = TD.getTypeSize(Ty); |
| 1668 | |
| 1669 | // If idxReg is a constant, we don't need to perform the multiply! |
| 1670 | if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(idx)) { |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1671 | if (!CSI->isNullValue()) { |
Chris Lattner | 8a307e8 | 2002-12-16 19:32:50 +0000 | [diff] [blame] | 1672 | unsigned Offset = elementSize*CSI->getValue(); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1673 | NextReg = makeAnotherReg(Type::UIntTy); |
| 1674 | BMI(MBB, IP, X86::ADDri32, 2,NextReg).addReg(BaseReg).addZImm(Offset); |
Chris Lattner | 8a307e8 | 2002-12-16 19:32:50 +0000 | [diff] [blame] | 1675 | } |
| 1676 | } else if (elementSize == 1) { |
| 1677 | // If the element size is 1, we don't have to multiply, just add |
| 1678 | unsigned idxReg = getReg(idx, MBB, IP); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1679 | NextReg = makeAnotherReg(Type::UIntTy); |
| 1680 | BMI(MBB, IP, X86::ADDrr32, 2, NextReg).addReg(BaseReg).addReg(idxReg); |
Chris Lattner | 8a307e8 | 2002-12-16 19:32:50 +0000 | [diff] [blame] | 1681 | } else { |
| 1682 | unsigned idxReg = getReg(idx, MBB, IP); |
| 1683 | unsigned OffsetReg = makeAnotherReg(Type::UIntTy); |
| 1684 | if (unsigned Shift = ExactLog2(elementSize)) { |
| 1685 | // If the element size is exactly a power of 2, use a shift to get it. |
Chris Lattner | 8a307e8 | 2002-12-16 19:32:50 +0000 | [diff] [blame] | 1686 | BMI(MBB, IP, X86::SHLir32, 2, |
| 1687 | OffsetReg).addReg(idxReg).addZImm(Shift-1); |
| 1688 | } else { |
| 1689 | // Most general case, emit a multiply... |
| 1690 | unsigned elementSizeReg = makeAnotherReg(Type::LongTy); |
| 1691 | BMI(MBB, IP, X86::MOVir32, 1, elementSizeReg).addZImm(elementSize); |
| 1692 | |
| 1693 | // Emit a MUL to multiply the register holding the index by |
| 1694 | // elementSize, putting the result in OffsetReg. |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1695 | doMultiply(MBB, IP, OffsetReg, Type::IntTy, idxReg, elementSizeReg); |
Chris Lattner | 8a307e8 | 2002-12-16 19:32:50 +0000 | [diff] [blame] | 1696 | } |
| 1697 | // Emit an ADD to add OffsetReg to the basePtr. |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1698 | NextReg = makeAnotherReg(Type::UIntTy); |
| 1699 | BMI(MBB, IP, X86::ADDrr32, 2,NextReg).addReg(BaseReg).addReg(OffsetReg); |
Chris Lattner | 8a307e8 | 2002-12-16 19:32:50 +0000 | [diff] [blame] | 1700 | } |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 1701 | } |
| 1702 | // Now that we are here, further indices refer to subtypes of this |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1703 | // one, so we don't need to worry about BaseReg itself, anymore. |
| 1704 | BaseReg = NextReg; |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 1705 | } |
| 1706 | // After we have processed all the indices, the result is left in |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1707 | // BaseReg. Move it to the register where we were expected to |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 1708 | // put the answer. A 32-bit move should do it, because we are in |
| 1709 | // ILP32 land. |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1710 | BMI(MBB, IP, X86::MOVrr32, 1, TargetReg).addReg(BaseReg); |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
| 1713 | |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 1714 | /// visitAllocaInst - If this is a fixed size alloca, allocate space from the |
| 1715 | /// frame manager, otherwise do it the hard way. |
| 1716 | /// |
| 1717 | void ISel::visitAllocaInst(AllocaInst &I) { |
Brian Gaeke | e48ec01 | 2002-12-13 06:46:31 +0000 | [diff] [blame] | 1718 | // Find the data size of the alloca inst's getAllocatedType. |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 1719 | const Type *Ty = I.getAllocatedType(); |
| 1720 | unsigned TySize = TM.getTargetData().getTypeSize(Ty); |
| 1721 | |
| 1722 | // If this is a fixed size alloca in the entry block for the function, |
| 1723 | // statically stack allocate the space. |
| 1724 | // |
| 1725 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(I.getArraySize())) { |
| 1726 | if (I.getParent() == I.getParent()->getParent()->begin()) { |
| 1727 | TySize *= CUI->getValue(); // Get total allocated size... |
| 1728 | unsigned Alignment = TM.getTargetData().getTypeAlignment(Ty); |
| 1729 | |
| 1730 | // Create a new stack object using the frame manager... |
| 1731 | int FrameIdx = F->getFrameInfo()->CreateStackObject(TySize, Alignment); |
| 1732 | addFrameReference(BuildMI(BB, X86::LEAr32, 5, getReg(I)), FrameIdx); |
| 1733 | return; |
| 1734 | } |
| 1735 | } |
| 1736 | |
| 1737 | // Create a register to hold the temporary result of multiplying the type size |
| 1738 | // constant by the variable amount. |
| 1739 | unsigned TotalSizeReg = makeAnotherReg(Type::UIntTy); |
| 1740 | unsigned SrcReg1 = getReg(I.getArraySize()); |
| 1741 | unsigned SizeReg = makeAnotherReg(Type::UIntTy); |
| 1742 | BuildMI(BB, X86::MOVir32, 1, SizeReg).addZImm(TySize); |
| 1743 | |
| 1744 | // TotalSizeReg = mul <numelements>, <TypeSize> |
| 1745 | MachineBasicBlock::iterator MBBI = BB->end(); |
| 1746 | doMultiply(BB, MBBI, TotalSizeReg, Type::UIntTy, SrcReg1, SizeReg); |
| 1747 | |
| 1748 | // AddedSize = add <TotalSizeReg>, 15 |
| 1749 | unsigned AddedSizeReg = makeAnotherReg(Type::UIntTy); |
| 1750 | BuildMI(BB, X86::ADDri32, 2, AddedSizeReg).addReg(TotalSizeReg).addZImm(15); |
| 1751 | |
| 1752 | // AlignedSize = and <AddedSize>, ~15 |
| 1753 | unsigned AlignedSize = makeAnotherReg(Type::UIntTy); |
| 1754 | BuildMI(BB, X86::ANDri32, 2, AlignedSize).addReg(AddedSizeReg).addZImm(~15); |
| 1755 | |
Brian Gaeke | e48ec01 | 2002-12-13 06:46:31 +0000 | [diff] [blame] | 1756 | // Subtract size from stack pointer, thereby allocating some space. |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1757 | BuildMI(BB, X86::SUBrr32, 2, X86::ESP).addReg(X86::ESP).addReg(AlignedSize); |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 1758 | |
Brian Gaeke | e48ec01 | 2002-12-13 06:46:31 +0000 | [diff] [blame] | 1759 | // Put a pointer to the space into the result register, by copying |
| 1760 | // the stack pointer. |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 1761 | BuildMI(BB, X86::MOVrr32, 1, getReg(I)).addReg(X86::ESP); |
| 1762 | |
Misha Brukman | 48196b3 | 2003-05-03 02:18:17 +0000 | [diff] [blame] | 1763 | // Inform the Frame Information that we have just allocated a variable-sized |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 1764 | // object. |
| 1765 | F->getFrameInfo()->CreateVariableSizedObject(); |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 1766 | } |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1767 | |
| 1768 | /// visitMallocInst - Malloc instructions are code generated into direct calls |
| 1769 | /// to the library malloc. |
| 1770 | /// |
| 1771 | void ISel::visitMallocInst(MallocInst &I) { |
| 1772 | unsigned AllocSize = TM.getTargetData().getTypeSize(I.getAllocatedType()); |
| 1773 | unsigned Arg; |
| 1774 | |
| 1775 | if (ConstantUInt *C = dyn_cast<ConstantUInt>(I.getOperand(0))) { |
| 1776 | Arg = getReg(ConstantUInt::get(Type::UIntTy, C->getValue() * AllocSize)); |
| 1777 | } else { |
| 1778 | Arg = makeAnotherReg(Type::UIntTy); |
| 1779 | unsigned Op0Reg = getReg(ConstantUInt::get(Type::UIntTy, AllocSize)); |
| 1780 | unsigned Op1Reg = getReg(I.getOperand(0)); |
| 1781 | MachineBasicBlock::iterator MBBI = BB->end(); |
| 1782 | doMultiply(BB, MBBI, Arg, Type::UIntTy, Op0Reg, Op1Reg); |
| 1783 | |
| 1784 | |
| 1785 | } |
| 1786 | |
| 1787 | std::vector<ValueRecord> Args; |
| 1788 | Args.push_back(ValueRecord(Arg, Type::UIntTy)); |
| 1789 | MachineInstr *TheCall = BuildMI(X86::CALLpcrel32, |
| 1790 | 1).addExternalSymbol("malloc", true); |
| 1791 | doCall(ValueRecord(getReg(I), I.getType()), TheCall, Args); |
| 1792 | } |
| 1793 | |
| 1794 | |
| 1795 | /// visitFreeInst - Free instructions are code gen'd to call the free libc |
| 1796 | /// function. |
| 1797 | /// |
| 1798 | void ISel::visitFreeInst(FreeInst &I) { |
| 1799 | std::vector<ValueRecord> Args; |
| 1800 | Args.push_back(ValueRecord(getReg(I.getOperand(0)), |
| 1801 | I.getOperand(0)->getType())); |
| 1802 | MachineInstr *TheCall = BuildMI(X86::CALLpcrel32, |
| 1803 | 1).addExternalSymbol("free", true); |
| 1804 | doCall(ValueRecord(0, Type::VoidTy), TheCall, Args); |
| 1805 | } |
| 1806 | |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 1807 | |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 1808 | /// createSimpleX86InstructionSelector - This pass converts an LLVM function |
| 1809 | /// 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] | 1810 | /// generated code sucks but the implementation is nice and simple. |
| 1811 | /// |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 1812 | Pass *createSimpleX86InstructionSelector(TargetMachine &TM) { |
| 1813 | return new ISel(TM); |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 1814 | } |