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