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