Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 1 | //===-- InstSelectSimple.cpp - A simple instruction selector for x86 ------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 10 | // This file defines a simple peephole instruction selector for the x86 target |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "X86.h" |
Chris Lattner | 6fc3c52 | 2002-11-17 21:11:55 +0000 | [diff] [blame] | 15 | #include "X86InstrBuilder.h" |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 16 | #include "X86InstrInfo.h" |
| 17 | #include "llvm/Constants.h" |
| 18 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 19 | #include "llvm/Function.h" |
Chris Lattner | 67580ed | 2003-05-13 20:21:19 +0000 | [diff] [blame] | 20 | #include "llvm/Instructions.h" |
Chris Lattner | 4482715 | 2003-12-28 09:47:19 +0000 | [diff] [blame] | 21 | #include "llvm/IntrinsicLowering.h" |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 22 | #include "llvm/Pass.h" |
| 23 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 24 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Chris Lattner | 341a937 | 2002-10-29 17:43:55 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineFunction.h" |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/SSARegMap.h" |
Misha Brukman | d2cc017 | 2002-11-20 00:58:23 +0000 | [diff] [blame] | 27 | #include "llvm/Target/MRegisterInfo.h" |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 3f1e8e7 | 2004-02-22 07:04:00 +0000 | [diff] [blame] | 29 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | 67580ed | 2003-05-13 20:21:19 +0000 | [diff] [blame] | 30 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | cf93cdd | 2004-01-30 22:13:44 +0000 | [diff] [blame] | 31 | #include "llvm/Support/CFG.h" |
Chris Lattner | 986618e | 2004-02-22 19:47:26 +0000 | [diff] [blame] | 32 | #include "Support/Statistic.h" |
Chris Lattner | 4482715 | 2003-12-28 09:47:19 +0000 | [diff] [blame] | 33 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 986618e | 2004-02-22 19:47:26 +0000 | [diff] [blame] | 35 | namespace { |
| 36 | Statistic<> |
| 37 | NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added"); |
| 38 | } |
Chris Lattner | cf93cdd | 2004-01-30 22:13:44 +0000 | [diff] [blame] | 39 | |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 40 | /// BMI - A special BuildMI variant that takes an iterator to insert the |
Chris Lattner | 8bdd129 | 2003-04-25 21:58:54 +0000 | [diff] [blame] | 41 | /// instruction at as well as a basic block. This is the version for when you |
| 42 | /// have a destination register in mind. |
Brian Gaeke | 71794c0 | 2002-12-13 11:22:48 +0000 | [diff] [blame] | 43 | inline static MachineInstrBuilder BMI(MachineBasicBlock *MBB, |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 44 | MachineBasicBlock::iterator I, |
Chris Lattner | 8cc72d2 | 2003-06-03 15:41:58 +0000 | [diff] [blame] | 45 | int Opcode, unsigned NumOperands, |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 46 | unsigned DestReg) { |
| 47 | MachineInstr *MI = new MachineInstr(Opcode, NumOperands+1, true, true); |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 48 | MBB->insert(I, MI); |
Alkis Evlogimenos | 890f923 | 2004-02-22 19:23:26 +0000 | [diff] [blame] | 49 | return MachineInstrBuilder(MI).addReg(DestReg, MachineOperand::Def); |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Chris Lattner | f08ad9f | 2002-12-13 10:50:40 +0000 | [diff] [blame] | 52 | /// BMI - A special BuildMI variant that takes an iterator to insert the |
| 53 | /// instruction at as well as a basic block. |
Brian Gaeke | 71794c0 | 2002-12-13 11:22:48 +0000 | [diff] [blame] | 54 | inline static MachineInstrBuilder BMI(MachineBasicBlock *MBB, |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 55 | MachineBasicBlock::iterator I, |
Chris Lattner | 8cc72d2 | 2003-06-03 15:41:58 +0000 | [diff] [blame] | 56 | int Opcode, unsigned NumOperands) { |
Chris Lattner | f08ad9f | 2002-12-13 10:50:40 +0000 | [diff] [blame] | 57 | MachineInstr *MI = new MachineInstr(Opcode, NumOperands, true, true); |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 58 | MBB->insert(I, MI); |
Chris Lattner | f08ad9f | 2002-12-13 10:50:40 +0000 | [diff] [blame] | 59 | return MachineInstrBuilder(MI); |
| 60 | } |
| 61 | |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 63 | namespace { |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 64 | struct ISel : public FunctionPass, InstVisitor<ISel> { |
| 65 | TargetMachine &TM; |
Chris Lattner | eca195e | 2003-05-08 19:44:13 +0000 | [diff] [blame] | 66 | MachineFunction *F; // The function we are compiling into |
| 67 | MachineBasicBlock *BB; // The current MBB we are compiling |
| 68 | int VarArgsFrameIndex; // FrameIndex for start of varargs area |
Chris Lattner | 0e5b79c | 2004-02-15 01:04:03 +0000 | [diff] [blame] | 69 | int ReturnAddressIndex; // FrameIndex for the return address |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 71 | std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs |
| 72 | |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 73 | // MBBMap - Mapping between LLVM BB -> Machine BB |
| 74 | std::map<const BasicBlock*, MachineBasicBlock*> MBBMap; |
| 75 | |
Chris Lattner | f70e0c2 | 2003-12-28 21:23:38 +0000 | [diff] [blame] | 76 | ISel(TargetMachine &tm) : TM(tm), F(0), BB(0) {} |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 77 | |
| 78 | /// runOnFunction - Top level implementation of instruction selection for |
| 79 | /// the entire function. |
| 80 | /// |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 81 | bool runOnFunction(Function &Fn) { |
Chris Lattner | 4482715 | 2003-12-28 09:47:19 +0000 | [diff] [blame] | 82 | // First pass over the function, lower any unknown intrinsic functions |
| 83 | // with the IntrinsicLowering class. |
| 84 | LowerUnknownIntrinsicFunctionCalls(Fn); |
| 85 | |
Chris Lattner | 36b3603 | 2002-10-29 23:40:58 +0000 | [diff] [blame] | 86 | F = &MachineFunction::construct(&Fn, TM); |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 87 | |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 88 | // Create all of the machine basic blocks for the function... |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 89 | for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) |
| 90 | F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I)); |
| 91 | |
Chris Lattner | 14aa7fe | 2002-12-16 22:54:46 +0000 | [diff] [blame] | 92 | BB = &F->front(); |
Chris Lattner | dbd7372 | 2003-05-06 21:32:22 +0000 | [diff] [blame] | 93 | |
Chris Lattner | 0e5b79c | 2004-02-15 01:04:03 +0000 | [diff] [blame] | 94 | // Set up a frame object for the return address. This is used by the |
| 95 | // llvm.returnaddress & llvm.frameaddress intrinisics. |
| 96 | ReturnAddressIndex = F->getFrameInfo()->CreateFixedObject(4, -4); |
| 97 | |
Chris Lattner | dbd7372 | 2003-05-06 21:32:22 +0000 | [diff] [blame] | 98 | // Copy incoming arguments off of the stack... |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 99 | LoadArgumentsToVirtualRegs(Fn); |
Chris Lattner | 14aa7fe | 2002-12-16 22:54:46 +0000 | [diff] [blame] | 100 | |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 101 | // Instruction select everything except PHI nodes |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 102 | visit(Fn); |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 103 | |
| 104 | // Select the PHI nodes |
| 105 | SelectPHINodes(); |
| 106 | |
Chris Lattner | 986618e | 2004-02-22 19:47:26 +0000 | [diff] [blame] | 107 | // Insert the FP_REG_KILL instructions into blocks that need them. |
| 108 | InsertFPRegKills(); |
| 109 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 110 | RegMap.clear(); |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 111 | MBBMap.clear(); |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 112 | F = 0; |
Chris Lattner | 2a865b0 | 2003-07-26 23:05:37 +0000 | [diff] [blame] | 113 | // We always build a machine code representation for the function |
| 114 | return true; |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Chris Lattner | f0eb7be | 2002-12-15 21:13:40 +0000 | [diff] [blame] | 117 | virtual const char *getPassName() const { |
| 118 | return "X86 Simple Instruction Selection"; |
| 119 | } |
| 120 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 121 | /// visitBasicBlock - This method is called when we are visiting a new basic |
Chris Lattner | 33f53b5 | 2002-10-29 20:48:56 +0000 | [diff] [blame] | 122 | /// block. This simply creates a new MachineBasicBlock to emit code into |
| 123 | /// and adds it to the current MachineFunction. Subsequent visit* for |
| 124 | /// instructions will be invoked for all instructions in the basic block. |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 125 | /// |
| 126 | void visitBasicBlock(BasicBlock &LLVM_BB) { |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 127 | BB = MBBMap[&LLVM_BB]; |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Chris Lattner | 4482715 | 2003-12-28 09:47:19 +0000 | [diff] [blame] | 130 | /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the |
| 131 | /// function, lowering any calls to unknown intrinsic functions into the |
| 132 | /// equivalent LLVM code. |
| 133 | void LowerUnknownIntrinsicFunctionCalls(Function &F); |
| 134 | |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 135 | /// LoadArgumentsToVirtualRegs - Load all of the arguments to this function |
| 136 | /// from the stack into virtual registers. |
| 137 | /// |
| 138 | void LoadArgumentsToVirtualRegs(Function &F); |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 139 | |
| 140 | /// SelectPHINodes - Insert machine code to generate phis. This is tricky |
| 141 | /// because we have to generate our sources into the source basic blocks, |
| 142 | /// not the current one. |
| 143 | /// |
| 144 | void SelectPHINodes(); |
| 145 | |
Chris Lattner | 986618e | 2004-02-22 19:47:26 +0000 | [diff] [blame] | 146 | /// InsertFPRegKills - Insert FP_REG_KILL instructions into basic blocks |
| 147 | /// that need them. This only occurs due to the floating point stackifier |
| 148 | /// not being aggressive enough to handle arbitrary global stackification. |
| 149 | /// |
| 150 | void InsertFPRegKills(); |
| 151 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 152 | // Visitation methods for various instructions. These methods simply emit |
| 153 | // fixed X86 code for each instruction. |
| 154 | // |
Brian Gaeke | fa8d571 | 2002-11-22 11:07:01 +0000 | [diff] [blame] | 155 | |
| 156 | // Control flow operators |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 157 | void visitReturnInst(ReturnInst &RI); |
Chris Lattner | 2df035b | 2002-11-02 19:27:56 +0000 | [diff] [blame] | 158 | void visitBranchInst(BranchInst &BI); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 159 | |
| 160 | struct ValueRecord { |
Chris Lattner | 5e2cb8b | 2003-08-04 02:12:48 +0000 | [diff] [blame] | 161 | Value *Val; |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 162 | unsigned Reg; |
| 163 | const Type *Ty; |
Chris Lattner | 5e2cb8b | 2003-08-04 02:12:48 +0000 | [diff] [blame] | 164 | ValueRecord(unsigned R, const Type *T) : Val(0), Reg(R), Ty(T) {} |
| 165 | ValueRecord(Value *V) : Val(V), Reg(0), Ty(V->getType()) {} |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 166 | }; |
| 167 | void doCall(const ValueRecord &Ret, MachineInstr *CallMI, |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 168 | const std::vector<ValueRecord> &Args); |
Brian Gaeke | fa8d571 | 2002-11-22 11:07:01 +0000 | [diff] [blame] | 169 | void visitCallInst(CallInst &I); |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 170 | void visitIntrinsicCall(Intrinsic::ID ID, CallInst &I); |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 171 | |
| 172 | // Arithmetic operators |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 173 | void visitSimpleBinary(BinaryOperator &B, unsigned OpcodeClass); |
Chris Lattner | 68aad93 | 2002-11-02 20:13:22 +0000 | [diff] [blame] | 174 | void visitAdd(BinaryOperator &B) { visitSimpleBinary(B, 0); } |
| 175 | void visitSub(BinaryOperator &B) { visitSimpleBinary(B, 1); } |
Chris Lattner | baa58a5 | 2004-02-23 03:10:10 +0000 | [diff] [blame] | 176 | void doMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI, |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 177 | unsigned DestReg, const Type *DestTy, |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 178 | unsigned Op0Reg, unsigned Op1Reg); |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 179 | void doMultiplyConst(MachineBasicBlock *MBB, |
Chris Lattner | baa58a5 | 2004-02-23 03:10:10 +0000 | [diff] [blame] | 180 | MachineBasicBlock::iterator MBBI, |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 181 | unsigned DestReg, const Type *DestTy, |
| 182 | unsigned Op0Reg, unsigned Op1Val); |
Chris Lattner | ca9671d | 2002-11-02 20:28:58 +0000 | [diff] [blame] | 183 | void visitMul(BinaryOperator &B); |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 184 | |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 185 | void visitDiv(BinaryOperator &B) { visitDivRem(B); } |
| 186 | void visitRem(BinaryOperator &B) { visitDivRem(B); } |
| 187 | void visitDivRem(BinaryOperator &B); |
| 188 | |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 189 | // Bitwise operators |
Chris Lattner | 68aad93 | 2002-11-02 20:13:22 +0000 | [diff] [blame] | 190 | void visitAnd(BinaryOperator &B) { visitSimpleBinary(B, 2); } |
| 191 | void visitOr (BinaryOperator &B) { visitSimpleBinary(B, 3); } |
| 192 | void visitXor(BinaryOperator &B) { visitSimpleBinary(B, 4); } |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 193 | |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 194 | // Comparison operators... |
| 195 | void visitSetCondInst(SetCondInst &I); |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 196 | unsigned EmitComparison(unsigned OpNum, Value *Op0, Value *Op1, |
| 197 | MachineBasicBlock *MBB, |
Chris Lattner | baa58a5 | 2004-02-23 03:10:10 +0000 | [diff] [blame] | 198 | MachineBasicBlock::iterator MBBI); |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 199 | |
Chris Lattner | 6fc3c52 | 2002-11-17 21:11:55 +0000 | [diff] [blame] | 200 | // Memory Instructions |
| 201 | void visitLoadInst(LoadInst &I); |
| 202 | void visitStoreInst(StoreInst &I); |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 203 | void visitGetElementPtrInst(GetElementPtrInst &I); |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 204 | void visitAllocaInst(AllocaInst &I); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 205 | void visitMallocInst(MallocInst &I); |
| 206 | void visitFreeInst(FreeInst &I); |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 207 | |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 208 | // Other operators |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 209 | void visitShiftInst(ShiftInst &I); |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 210 | void visitPHINode(PHINode &I) {} // PHI nodes handled by second pass |
Brian Gaeke | fa8d571 | 2002-11-22 11:07:01 +0000 | [diff] [blame] | 211 | void visitCastInst(CastInst &I); |
Chris Lattner | 7381506 | 2003-10-18 05:56:40 +0000 | [diff] [blame] | 212 | void visitVANextInst(VANextInst &I); |
| 213 | void visitVAArgInst(VAArgInst &I); |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 214 | |
| 215 | void visitInstruction(Instruction &I) { |
| 216 | std::cerr << "Cannot instruction select: " << I; |
| 217 | abort(); |
| 218 | } |
| 219 | |
Brian Gaeke | 95780cc | 2002-12-13 07:56:18 +0000 | [diff] [blame] | 220 | /// promote32 - Make a value 32-bits wide, and put it somewhere. |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 221 | /// |
| 222 | void promote32(unsigned targetReg, const ValueRecord &VR); |
| 223 | |
Chris Lattner | b6bac51 | 2004-02-25 06:13:04 +0000 | [diff] [blame] | 224 | // getGEPIndex - This is used to fold GEP instructions into X86 addressing |
| 225 | // expressions. |
| 226 | void getGEPIndex(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP, |
| 227 | std::vector<Value*> &GEPOps, |
| 228 | std::vector<const Type*> &GEPTypes, unsigned &BaseReg, |
| 229 | unsigned &Scale, unsigned &IndexReg, unsigned &Disp); |
| 230 | |
| 231 | /// isGEPFoldable - Return true if the specified GEP can be completely |
| 232 | /// folded into the addressing mode of a load/store or lea instruction. |
| 233 | bool isGEPFoldable(MachineBasicBlock *MBB, |
| 234 | Value *Src, User::op_iterator IdxBegin, |
| 235 | User::op_iterator IdxEnd, unsigned &BaseReg, |
| 236 | unsigned &Scale, unsigned &IndexReg, unsigned &Disp); |
| 237 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 238 | /// emitGEPOperation - Common code shared between visitGetElementPtrInst and |
| 239 | /// constant expression GEP support. |
| 240 | /// |
Chris Lattner | 827832c | 2004-02-22 17:05:38 +0000 | [diff] [blame] | 241 | void emitGEPOperation(MachineBasicBlock *BB, MachineBasicBlock::iterator IP, |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 242 | Value *Src, User::op_iterator IdxBegin, |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 243 | User::op_iterator IdxEnd, unsigned TargetReg); |
| 244 | |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 245 | /// emitCastOperation - Common code shared between visitCastInst and |
| 246 | /// constant expression cast support. |
Chris Lattner | baa58a5 | 2004-02-23 03:10:10 +0000 | [diff] [blame] | 247 | void emitCastOperation(MachineBasicBlock *BB,MachineBasicBlock::iterator IP, |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 248 | Value *Src, const Type *DestTy, unsigned TargetReg); |
| 249 | |
Chris Lattner | b515f6d | 2003-05-08 20:49:25 +0000 | [diff] [blame] | 250 | /// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary |
| 251 | /// and constant expression support. |
| 252 | void emitSimpleBinaryOperation(MachineBasicBlock *BB, |
Chris Lattner | baa58a5 | 2004-02-23 03:10:10 +0000 | [diff] [blame] | 253 | MachineBasicBlock::iterator IP, |
Chris Lattner | b515f6d | 2003-05-08 20:49:25 +0000 | [diff] [blame] | 254 | Value *Op0, Value *Op1, |
| 255 | unsigned OperatorClass, unsigned TargetReg); |
| 256 | |
Chris Lattner | cadff44 | 2003-10-23 17:21:43 +0000 | [diff] [blame] | 257 | void emitDivRemOperation(MachineBasicBlock *BB, |
Chris Lattner | baa58a5 | 2004-02-23 03:10:10 +0000 | [diff] [blame] | 258 | MachineBasicBlock::iterator IP, |
Chris Lattner | cadff44 | 2003-10-23 17:21:43 +0000 | [diff] [blame] | 259 | unsigned Op0Reg, unsigned Op1Reg, bool isDiv, |
| 260 | const Type *Ty, unsigned TargetReg); |
| 261 | |
Chris Lattner | 58c41fe | 2003-08-24 19:19:47 +0000 | [diff] [blame] | 262 | /// emitSetCCOperation - Common code shared between visitSetCondInst and |
| 263 | /// constant expression support. |
| 264 | void emitSetCCOperation(MachineBasicBlock *BB, |
Chris Lattner | baa58a5 | 2004-02-23 03:10:10 +0000 | [diff] [blame] | 265 | MachineBasicBlock::iterator IP, |
Chris Lattner | 58c41fe | 2003-08-24 19:19:47 +0000 | [diff] [blame] | 266 | Value *Op0, Value *Op1, unsigned Opcode, |
| 267 | unsigned TargetReg); |
Brian Gaeke | 2dd3e1b | 2003-11-22 05:18:35 +0000 | [diff] [blame] | 268 | |
| 269 | /// emitShiftOperation - Common code shared between visitShiftInst and |
| 270 | /// constant expression support. |
Brian Gaeke | dfcc9cf | 2003-11-22 06:49:41 +0000 | [diff] [blame] | 271 | void emitShiftOperation(MachineBasicBlock *MBB, |
Chris Lattner | baa58a5 | 2004-02-23 03:10:10 +0000 | [diff] [blame] | 272 | MachineBasicBlock::iterator IP, |
Brian Gaeke | dfcc9cf | 2003-11-22 06:49:41 +0000 | [diff] [blame] | 273 | Value *Op, Value *ShiftAmount, bool isLeftShift, |
| 274 | const Type *ResultTy, unsigned DestReg); |
| 275 | |
Chris Lattner | 58c41fe | 2003-08-24 19:19:47 +0000 | [diff] [blame] | 276 | |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 277 | /// copyConstantToRegister - Output the instructions required to put the |
| 278 | /// specified constant into the specified register. |
| 279 | /// |
Chris Lattner | 8a307e8 | 2002-12-16 19:32:50 +0000 | [diff] [blame] | 280 | void copyConstantToRegister(MachineBasicBlock *MBB, |
Chris Lattner | baa58a5 | 2004-02-23 03:10:10 +0000 | [diff] [blame] | 281 | MachineBasicBlock::iterator MBBI, |
Chris Lattner | 8a307e8 | 2002-12-16 19:32:50 +0000 | [diff] [blame] | 282 | Constant *C, unsigned Reg); |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 283 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 284 | /// makeAnotherReg - This method returns the next register number we haven't |
| 285 | /// yet used. |
| 286 | /// |
| 287 | /// Long values are handled somewhat specially. They are always allocated |
| 288 | /// as pairs of 32 bit integer values. The register number returned is the |
| 289 | /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits |
| 290 | /// of the long value. |
| 291 | /// |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 292 | unsigned makeAnotherReg(const Type *Ty) { |
Chris Lattner | 7db1fa9 | 2003-07-30 05:33:48 +0000 | [diff] [blame] | 293 | assert(dynamic_cast<const X86RegisterInfo*>(TM.getRegisterInfo()) && |
| 294 | "Current target doesn't have X86 reg info??"); |
| 295 | const X86RegisterInfo *MRI = |
| 296 | static_cast<const X86RegisterInfo*>(TM.getRegisterInfo()); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 297 | if (Ty == Type::LongTy || Ty == Type::ULongTy) { |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 298 | const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy); |
| 299 | // Create the lower part |
| 300 | F->getSSARegMap()->createVirtualRegister(RC); |
| 301 | // Create the upper part. |
| 302 | return F->getSSARegMap()->createVirtualRegister(RC)-1; |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 305 | // Add the mapping of regnumber => reg class to MachineFunction |
Chris Lattner | 7db1fa9 | 2003-07-30 05:33:48 +0000 | [diff] [blame] | 306 | const TargetRegisterClass *RC = MRI->getRegClassForType(Ty); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 307 | return F->getSSARegMap()->createVirtualRegister(RC); |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 310 | /// getReg - This method turns an LLVM value into a register number. This |
| 311 | /// is guaranteed to produce the same register number for a particular value |
| 312 | /// every time it is queried. |
| 313 | /// |
| 314 | unsigned getReg(Value &V) { return getReg(&V); } // Allow references |
Chris Lattner | f08ad9f | 2002-12-13 10:50:40 +0000 | [diff] [blame] | 315 | unsigned getReg(Value *V) { |
| 316 | // Just append to the end of the current bb. |
| 317 | MachineBasicBlock::iterator It = BB->end(); |
| 318 | return getReg(V, BB, It); |
| 319 | } |
Brian Gaeke | 71794c0 | 2002-12-13 11:22:48 +0000 | [diff] [blame] | 320 | unsigned getReg(Value *V, MachineBasicBlock *MBB, |
Chris Lattner | baa58a5 | 2004-02-23 03:10:10 +0000 | [diff] [blame] | 321 | MachineBasicBlock::iterator IPt) { |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 322 | unsigned &Reg = RegMap[V]; |
Misha Brukman | d2cc017 | 2002-11-20 00:58:23 +0000 | [diff] [blame] | 323 | if (Reg == 0) { |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 324 | Reg = makeAnotherReg(V->getType()); |
Misha Brukman | d2cc017 | 2002-11-20 00:58:23 +0000 | [diff] [blame] | 325 | RegMap[V] = Reg; |
Misha Brukman | d2cc017 | 2002-11-20 00:58:23 +0000 | [diff] [blame] | 326 | } |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 327 | |
Chris Lattner | 6f8fd25 | 2002-10-27 21:23:43 +0000 | [diff] [blame] | 328 | // If this operand is a constant, emit the code to copy the constant into |
| 329 | // the register here... |
| 330 | // |
Chris Lattner | dbf30f7 | 2002-12-04 06:45:19 +0000 | [diff] [blame] | 331 | if (Constant *C = dyn_cast<Constant>(V)) { |
Chris Lattner | 8a307e8 | 2002-12-16 19:32:50 +0000 | [diff] [blame] | 332 | copyConstantToRegister(MBB, IPt, C, Reg); |
Chris Lattner | 14aa7fe | 2002-12-16 22:54:46 +0000 | [diff] [blame] | 333 | 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] | 334 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 335 | // Move the address of the global into the register |
Chris Lattner | 6e173a0 | 2004-02-17 06:16:44 +0000 | [diff] [blame] | 336 | BMI(MBB, IPt, X86::MOVri32, 1, Reg).addGlobalAddress(GV); |
Chris Lattner | 14aa7fe | 2002-12-16 22:54:46 +0000 | [diff] [blame] | 337 | 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] | 338 | } |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 339 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 340 | return Reg; |
| 341 | } |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 342 | }; |
| 343 | } |
| 344 | |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame] | 345 | /// TypeClass - Used by the X86 backend to group LLVM types by their basic X86 |
| 346 | /// Representation. |
| 347 | /// |
| 348 | enum TypeClass { |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 349 | cByte, cShort, cInt, cFP, cLong |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame] | 350 | }; |
| 351 | |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 352 | /// getClass - Turn a primitive type into a "class" number which is based on the |
| 353 | /// size of the type, and whether or not it is floating point. |
| 354 | /// |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame] | 355 | static inline TypeClass getClass(const Type *Ty) { |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 356 | switch (Ty->getPrimitiveID()) { |
| 357 | case Type::SByteTyID: |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame] | 358 | case Type::UByteTyID: return cByte; // Byte operands are class #0 |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 359 | case Type::ShortTyID: |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame] | 360 | case Type::UShortTyID: return cShort; // Short operands are class #1 |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 361 | case Type::IntTyID: |
| 362 | case Type::UIntTyID: |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame] | 363 | case Type::PointerTyID: return cInt; // Int's and pointers are class #2 |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 364 | |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 365 | case Type::FloatTyID: |
| 366 | case Type::DoubleTyID: return cFP; // Floating Point is #3 |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 367 | |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 368 | case Type::LongTyID: |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 369 | case Type::ULongTyID: return cLong; // Longs are class #4 |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 370 | default: |
| 371 | assert(0 && "Invalid type to getClass!"); |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame] | 372 | return cByte; // not reached |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 373 | } |
| 374 | } |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 375 | |
Chris Lattner | 6b993cc | 2002-12-15 08:02:15 +0000 | [diff] [blame] | 376 | // getClassB - Just like getClass, but treat boolean values as bytes. |
| 377 | static inline TypeClass getClassB(const Type *Ty) { |
| 378 | if (Ty == Type::BoolTy) return cByte; |
| 379 | return getClass(Ty); |
| 380 | } |
| 381 | |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 382 | |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 383 | /// copyConstantToRegister - Output the instructions required to put the |
| 384 | /// specified constant into the specified register. |
| 385 | /// |
Chris Lattner | 8a307e8 | 2002-12-16 19:32:50 +0000 | [diff] [blame] | 386 | void ISel::copyConstantToRegister(MachineBasicBlock *MBB, |
Chris Lattner | baa58a5 | 2004-02-23 03:10:10 +0000 | [diff] [blame] | 387 | MachineBasicBlock::iterator IP, |
Chris Lattner | 8a307e8 | 2002-12-16 19:32:50 +0000 | [diff] [blame] | 388 | Constant *C, unsigned R) { |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 389 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
Chris Lattner | b515f6d | 2003-05-08 20:49:25 +0000 | [diff] [blame] | 390 | unsigned Class = 0; |
| 391 | switch (CE->getOpcode()) { |
| 392 | case Instruction::GetElementPtr: |
Brian Gaeke | 68b1edc | 2002-12-16 04:23:29 +0000 | [diff] [blame] | 393 | emitGEPOperation(MBB, IP, CE->getOperand(0), |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 394 | CE->op_begin()+1, CE->op_end(), R); |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 395 | return; |
Chris Lattner | b515f6d | 2003-05-08 20:49:25 +0000 | [diff] [blame] | 396 | case Instruction::Cast: |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 397 | emitCastOperation(MBB, IP, CE->getOperand(0), CE->getType(), R); |
Chris Lattner | 4b12cde | 2003-04-21 21:33:44 +0000 | [diff] [blame] | 398 | return; |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 399 | |
Chris Lattner | b515f6d | 2003-05-08 20:49:25 +0000 | [diff] [blame] | 400 | case Instruction::Xor: ++Class; // FALL THROUGH |
| 401 | case Instruction::Or: ++Class; // FALL THROUGH |
| 402 | case Instruction::And: ++Class; // FALL THROUGH |
| 403 | case Instruction::Sub: ++Class; // FALL THROUGH |
| 404 | case Instruction::Add: |
| 405 | emitSimpleBinaryOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1), |
| 406 | Class, R); |
| 407 | return; |
| 408 | |
Chris Lattner | cadff44 | 2003-10-23 17:21:43 +0000 | [diff] [blame] | 409 | case Instruction::Mul: { |
| 410 | unsigned Op0Reg = getReg(CE->getOperand(0), MBB, IP); |
| 411 | unsigned Op1Reg = getReg(CE->getOperand(1), MBB, IP); |
| 412 | doMultiply(MBB, IP, R, CE->getType(), Op0Reg, Op1Reg); |
| 413 | return; |
| 414 | } |
| 415 | case Instruction::Div: |
| 416 | case Instruction::Rem: { |
| 417 | unsigned Op0Reg = getReg(CE->getOperand(0), MBB, IP); |
| 418 | unsigned Op1Reg = getReg(CE->getOperand(1), MBB, IP); |
| 419 | emitDivRemOperation(MBB, IP, Op0Reg, Op1Reg, |
| 420 | CE->getOpcode() == Instruction::Div, |
| 421 | CE->getType(), R); |
| 422 | return; |
| 423 | } |
| 424 | |
Chris Lattner | 58c41fe | 2003-08-24 19:19:47 +0000 | [diff] [blame] | 425 | case Instruction::SetNE: |
| 426 | case Instruction::SetEQ: |
| 427 | case Instruction::SetLT: |
| 428 | case Instruction::SetGT: |
| 429 | case Instruction::SetLE: |
| 430 | case Instruction::SetGE: |
| 431 | emitSetCCOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1), |
| 432 | CE->getOpcode(), R); |
| 433 | return; |
| 434 | |
Brian Gaeke | 2dd3e1b | 2003-11-22 05:18:35 +0000 | [diff] [blame] | 435 | case Instruction::Shl: |
| 436 | case Instruction::Shr: |
| 437 | emitShiftOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1), |
Brian Gaeke | dfcc9cf | 2003-11-22 06:49:41 +0000 | [diff] [blame] | 438 | CE->getOpcode() == Instruction::Shl, CE->getType(), R); |
| 439 | return; |
Brian Gaeke | 2dd3e1b | 2003-11-22 05:18:35 +0000 | [diff] [blame] | 440 | |
Chris Lattner | b515f6d | 2003-05-08 20:49:25 +0000 | [diff] [blame] | 441 | default: |
| 442 | std::cerr << "Offending expr: " << C << "\n"; |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 443 | assert(0 && "Constant expression not yet handled!\n"); |
Chris Lattner | b515f6d | 2003-05-08 20:49:25 +0000 | [diff] [blame] | 444 | } |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 445 | } |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 446 | |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 447 | if (C->getType()->isIntegral()) { |
Chris Lattner | 6b993cc | 2002-12-15 08:02:15 +0000 | [diff] [blame] | 448 | unsigned Class = getClassB(C->getType()); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 449 | |
| 450 | if (Class == cLong) { |
| 451 | // Copy the value into the register pair. |
Chris Lattner | c07736a | 2003-07-23 15:22:26 +0000 | [diff] [blame] | 452 | uint64_t Val = cast<ConstantInt>(C)->getRawValue(); |
Chris Lattner | 6e173a0 | 2004-02-17 06:16:44 +0000 | [diff] [blame] | 453 | BMI(MBB, IP, X86::MOVri32, 1, R).addZImm(Val & 0xFFFFFFFF); |
| 454 | BMI(MBB, IP, X86::MOVri32, 1, R+1).addZImm(Val >> 32); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 455 | return; |
| 456 | } |
| 457 | |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 458 | assert(Class <= cInt && "Type not handled yet!"); |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 459 | |
| 460 | static const unsigned IntegralOpcodeTab[] = { |
Chris Lattner | 6e173a0 | 2004-02-17 06:16:44 +0000 | [diff] [blame] | 461 | X86::MOVri8, X86::MOVri16, X86::MOVri32 |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 462 | }; |
| 463 | |
Chris Lattner | 6b993cc | 2002-12-15 08:02:15 +0000 | [diff] [blame] | 464 | if (C->getType() == Type::BoolTy) { |
Chris Lattner | 6e173a0 | 2004-02-17 06:16:44 +0000 | [diff] [blame] | 465 | BMI(MBB, IP, X86::MOVri8, 1, R).addZImm(C == ConstantBool::True); |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 466 | } else { |
Chris Lattner | c07736a | 2003-07-23 15:22:26 +0000 | [diff] [blame] | 467 | ConstantInt *CI = cast<ConstantInt>(C); |
| 468 | BMI(MBB, IP, IntegralOpcodeTab[Class], 1, R).addZImm(CI->getRawValue()); |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 469 | } |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 470 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
Chris Lattner | af70362 | 2004-02-02 18:56:30 +0000 | [diff] [blame] | 471 | if (CFP->isExactlyValue(+0.0)) |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 472 | BMI(MBB, IP, X86::FLD0, 0, R); |
Chris Lattner | af70362 | 2004-02-02 18:56:30 +0000 | [diff] [blame] | 473 | else if (CFP->isExactlyValue(+1.0)) |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 474 | BMI(MBB, IP, X86::FLD1, 0, R); |
| 475 | else { |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 476 | // Otherwise we need to spill the constant to memory... |
| 477 | MachineConstantPool *CP = F->getConstantPool(); |
| 478 | unsigned CPI = CP->getConstantPoolIndex(CFP); |
Chris Lattner | 6c09db2 | 2003-10-20 04:11:23 +0000 | [diff] [blame] | 479 | const Type *Ty = CFP->getType(); |
| 480 | |
| 481 | assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!"); |
Alkis Evlogimenos | 8e475b8 | 2004-02-28 23:42:35 +0000 | [diff] [blame] | 482 | unsigned LoadOpcode = Ty == Type::FloatTy ? X86::FLDm32 : X86::FLDm64; |
Chris Lattner | 6c09db2 | 2003-10-20 04:11:23 +0000 | [diff] [blame] | 483 | addConstantPoolReference(BMI(MBB, IP, LoadOpcode, 4, R), CPI); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Chris Lattner | f08ad9f | 2002-12-13 10:50:40 +0000 | [diff] [blame] | 486 | } else if (isa<ConstantPointerNull>(C)) { |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 487 | // Copy zero (null pointer) to the register. |
Chris Lattner | 6e173a0 | 2004-02-17 06:16:44 +0000 | [diff] [blame] | 488 | BMI(MBB, IP, X86::MOVri32, 1, R).addZImm(0); |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 489 | } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)) { |
Chris Lattner | 7ca0409 | 2004-02-22 17:35:42 +0000 | [diff] [blame] | 490 | BMI(MBB, IP, X86::MOVri32, 1, R).addGlobalAddress(CPR->getValue()); |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 491 | } else { |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 492 | std::cerr << "Offending constant: " << C << "\n"; |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 493 | assert(0 && "Type not handled yet!"); |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 497 | /// LoadArgumentsToVirtualRegs - Load all of the arguments to this function from |
| 498 | /// the stack into virtual registers. |
| 499 | /// |
| 500 | void ISel::LoadArgumentsToVirtualRegs(Function &Fn) { |
| 501 | // Emit instructions to load the arguments... On entry to a function on the |
| 502 | // X86, the stack frame looks like this: |
| 503 | // |
| 504 | // [ESP] -- return address |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 505 | // [ESP + 4] -- first argument (leftmost lexically) |
| 506 | // [ESP + 8] -- second argument, if first argument is four bytes in size |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 507 | // ... |
| 508 | // |
Chris Lattner | f158da2 | 2003-01-16 02:20:12 +0000 | [diff] [blame] | 509 | unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot |
Chris Lattner | aa09b75 | 2002-12-28 21:08:28 +0000 | [diff] [blame] | 510 | MachineFrameInfo *MFI = F->getFrameInfo(); |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 511 | |
| 512 | for (Function::aiterator I = Fn.abegin(), E = Fn.aend(); I != E; ++I) { |
| 513 | unsigned Reg = getReg(*I); |
| 514 | |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 515 | int FI; // Frame object index |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 516 | switch (getClassB(I->getType())) { |
| 517 | case cByte: |
Chris Lattner | aa09b75 | 2002-12-28 21:08:28 +0000 | [diff] [blame] | 518 | FI = MFI->CreateFixedObject(1, ArgOffset); |
Chris Lattner | e87331d | 2004-02-17 06:28:19 +0000 | [diff] [blame] | 519 | addFrameReference(BuildMI(BB, X86::MOVrm8, 4, Reg), FI); |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 520 | break; |
| 521 | case cShort: |
Chris Lattner | aa09b75 | 2002-12-28 21:08:28 +0000 | [diff] [blame] | 522 | FI = MFI->CreateFixedObject(2, ArgOffset); |
Chris Lattner | e87331d | 2004-02-17 06:28:19 +0000 | [diff] [blame] | 523 | addFrameReference(BuildMI(BB, X86::MOVrm16, 4, Reg), FI); |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 524 | break; |
| 525 | case cInt: |
Chris Lattner | aa09b75 | 2002-12-28 21:08:28 +0000 | [diff] [blame] | 526 | FI = MFI->CreateFixedObject(4, ArgOffset); |
Chris Lattner | e87331d | 2004-02-17 06:28:19 +0000 | [diff] [blame] | 527 | addFrameReference(BuildMI(BB, X86::MOVrm32, 4, Reg), FI); |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 528 | break; |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 529 | case cLong: |
| 530 | FI = MFI->CreateFixedObject(8, ArgOffset); |
Chris Lattner | e87331d | 2004-02-17 06:28:19 +0000 | [diff] [blame] | 531 | addFrameReference(BuildMI(BB, X86::MOVrm32, 4, Reg), FI); |
| 532 | addFrameReference(BuildMI(BB, X86::MOVrm32, 4, Reg+1), FI, 4); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 533 | ArgOffset += 4; // longs require 4 additional bytes |
| 534 | break; |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 535 | case cFP: |
| 536 | unsigned Opcode; |
| 537 | if (I->getType() == Type::FloatTy) { |
Alkis Evlogimenos | 8e475b8 | 2004-02-28 23:42:35 +0000 | [diff] [blame] | 538 | Opcode = X86::FLDm32; |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 539 | FI = MFI->CreateFixedObject(4, ArgOffset); |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 540 | } else { |
Alkis Evlogimenos | 8e475b8 | 2004-02-28 23:42:35 +0000 | [diff] [blame] | 541 | Opcode = X86::FLDm64; |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 542 | FI = MFI->CreateFixedObject(8, ArgOffset); |
| 543 | ArgOffset += 4; // doubles require 4 additional bytes |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 544 | } |
| 545 | addFrameReference(BuildMI(BB, Opcode, 4, Reg), FI); |
| 546 | break; |
| 547 | default: |
| 548 | assert(0 && "Unhandled argument type!"); |
| 549 | } |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 550 | ArgOffset += 4; // Each argument takes at least 4 bytes on the stack... |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 551 | } |
Chris Lattner | eca195e | 2003-05-08 19:44:13 +0000 | [diff] [blame] | 552 | |
| 553 | // If the function takes variable number of arguments, add a frame offset for |
| 554 | // the start of the first vararg value... this is used to expand |
| 555 | // llvm.va_start. |
| 556 | if (Fn.getFunctionType()->isVarArg()) |
| 557 | VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset); |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 561 | /// SelectPHINodes - Insert machine code to generate phis. This is tricky |
| 562 | /// because we have to generate our sources into the source basic blocks, not |
| 563 | /// the current one. |
| 564 | /// |
| 565 | void ISel::SelectPHINodes() { |
Chris Lattner | 3501fea | 2003-01-14 22:00:31 +0000 | [diff] [blame] | 566 | const TargetInstrInfo &TII = TM.getInstrInfo(); |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 567 | const Function &LF = *F->getFunction(); // The LLVM function... |
| 568 | for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) { |
| 569 | const BasicBlock *BB = I; |
Chris Lattner | 168aa90 | 2004-02-29 07:10:16 +0000 | [diff] [blame^] | 570 | MachineBasicBlock &MBB = *MBBMap[I]; |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 571 | |
| 572 | // Loop over all of the PHI nodes in the LLVM basic block... |
Chris Lattner | 168aa90 | 2004-02-29 07:10:16 +0000 | [diff] [blame^] | 573 | MachineBasicBlock::iterator PHIInsertPoint = MBB.begin(); |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 574 | for (BasicBlock::const_iterator I = BB->begin(); |
Chris Lattner | a81fc68 | 2003-10-19 00:26:11 +0000 | [diff] [blame] | 575 | PHINode *PN = const_cast<PHINode*>(dyn_cast<PHINode>(I)); ++I) { |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 576 | |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 577 | // Create a new machine instr PHI node, and insert it. |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 578 | unsigned PHIReg = getReg(*PN); |
Chris Lattner | 168aa90 | 2004-02-29 07:10:16 +0000 | [diff] [blame^] | 579 | MachineInstr *PhiMI = BuildMI(MBB, PHIInsertPoint, |
| 580 | X86::PHI, PN->getNumOperands(), PHIReg); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 581 | |
| 582 | MachineInstr *LongPhiMI = 0; |
Chris Lattner | 168aa90 | 2004-02-29 07:10:16 +0000 | [diff] [blame^] | 583 | if (PN->getType() == Type::LongTy || PN->getType() == Type::ULongTy) |
| 584 | LongPhiMI = BuildMI(MBB, PHIInsertPoint, |
| 585 | X86::PHI, PN->getNumOperands(), PHIReg+1); |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 586 | |
Chris Lattner | a6e73f1 | 2003-05-12 14:22:21 +0000 | [diff] [blame] | 587 | // PHIValues - Map of blocks to incoming virtual registers. We use this |
| 588 | // so that we only initialize one incoming value for a particular block, |
| 589 | // even if the block has multiple entries in the PHI node. |
| 590 | // |
| 591 | std::map<MachineBasicBlock*, unsigned> PHIValues; |
| 592 | |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 593 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 594 | MachineBasicBlock *PredMBB = MBBMap[PN->getIncomingBlock(i)]; |
Chris Lattner | a6e73f1 | 2003-05-12 14:22:21 +0000 | [diff] [blame] | 595 | unsigned ValReg; |
| 596 | std::map<MachineBasicBlock*, unsigned>::iterator EntryIt = |
| 597 | PHIValues.lower_bound(PredMBB); |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 598 | |
Chris Lattner | a6e73f1 | 2003-05-12 14:22:21 +0000 | [diff] [blame] | 599 | if (EntryIt != PHIValues.end() && EntryIt->first == PredMBB) { |
| 600 | // We already inserted an initialization of the register for this |
| 601 | // predecessor. Recycle it. |
| 602 | ValReg = EntryIt->second; |
| 603 | |
| 604 | } else { |
Chris Lattner | a81fc68 | 2003-10-19 00:26:11 +0000 | [diff] [blame] | 605 | // Get the incoming value into a virtual register. |
Chris Lattner | a6e73f1 | 2003-05-12 14:22:21 +0000 | [diff] [blame] | 606 | // |
Chris Lattner | a81fc68 | 2003-10-19 00:26:11 +0000 | [diff] [blame] | 607 | Value *Val = PN->getIncomingValue(i); |
| 608 | |
| 609 | // If this is a constant or GlobalValue, we may have to insert code |
| 610 | // into the basic block to compute it into a virtual register. |
| 611 | if (isa<Constant>(Val) || isa<GlobalValue>(Val)) { |
| 612 | // Because we don't want to clobber any values which might be in |
| 613 | // physical registers with the computation of this constant (which |
| 614 | // might be arbitrarily complex if it is a constant expression), |
| 615 | // just insert the computation at the top of the basic block. |
| 616 | MachineBasicBlock::iterator PI = PredMBB->begin(); |
| 617 | |
| 618 | // Skip over any PHI nodes though! |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 619 | while (PI != PredMBB->end() && PI->getOpcode() == X86::PHI) |
Chris Lattner | a81fc68 | 2003-10-19 00:26:11 +0000 | [diff] [blame] | 620 | ++PI; |
| 621 | |
| 622 | ValReg = getReg(Val, PredMBB, PI); |
| 623 | } else { |
| 624 | ValReg = getReg(Val); |
| 625 | } |
Chris Lattner | a6e73f1 | 2003-05-12 14:22:21 +0000 | [diff] [blame] | 626 | |
| 627 | // Remember that we inserted a value for this PHI for this predecessor |
| 628 | PHIValues.insert(EntryIt, std::make_pair(PredMBB, ValReg)); |
| 629 | } |
| 630 | |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 631 | PhiMI->addRegOperand(ValReg); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 632 | PhiMI->addMachineBasicBlockOperand(PredMBB); |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 633 | if (LongPhiMI) { |
| 634 | LongPhiMI->addRegOperand(ValReg+1); |
| 635 | LongPhiMI->addMachineBasicBlockOperand(PredMBB); |
| 636 | } |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 637 | } |
Chris Lattner | 168aa90 | 2004-02-29 07:10:16 +0000 | [diff] [blame^] | 638 | |
| 639 | // Now that we emitted all of the incoming values for the PHI node, make |
| 640 | // sure to reposition the InsertPoint after the PHI that we just added. |
| 641 | // This is needed because we might have inserted a constant into this |
| 642 | // block, right after the PHI's which is before the old insert point! |
| 643 | PHIInsertPoint = LongPhiMI ? LongPhiMI : PhiMI; |
| 644 | ++PHIInsertPoint; |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 645 | } |
| 646 | } |
| 647 | } |
| 648 | |
Chris Lattner | 986618e | 2004-02-22 19:47:26 +0000 | [diff] [blame] | 649 | /// RequiresFPRegKill - The floating point stackifier pass cannot insert |
| 650 | /// compensation code on critical edges. As such, it requires that we kill all |
| 651 | /// FP registers on the exit from any blocks that either ARE critical edges, or |
| 652 | /// branch to a block that has incoming critical edges. |
| 653 | /// |
| 654 | /// Note that this kill instruction will eventually be eliminated when |
| 655 | /// restrictions in the stackifier are relaxed. |
| 656 | /// |
| 657 | static bool RequiresFPRegKill(const BasicBlock *BB) { |
| 658 | #if 0 |
| 659 | for (succ_const_iterator SI = succ_begin(BB), E = succ_end(BB); SI!=E; ++SI) { |
| 660 | const BasicBlock *Succ = *SI; |
| 661 | pred_const_iterator PI = pred_begin(Succ), PE = pred_end(Succ); |
| 662 | ++PI; // Block have at least one predecessory |
| 663 | if (PI != PE) { // If it has exactly one, this isn't crit edge |
| 664 | // If this block has more than one predecessor, check all of the |
| 665 | // predecessors to see if they have multiple successors. If so, then the |
| 666 | // block we are analyzing needs an FPRegKill. |
| 667 | for (PI = pred_begin(Succ); PI != PE; ++PI) { |
| 668 | const BasicBlock *Pred = *PI; |
| 669 | succ_const_iterator SI2 = succ_begin(Pred); |
| 670 | ++SI2; // There must be at least one successor of this block. |
| 671 | if (SI2 != succ_end(Pred)) |
| 672 | return true; // Yes, we must insert the kill on this edge. |
| 673 | } |
| 674 | } |
| 675 | } |
| 676 | // If we got this far, there is no need to insert the kill instruction. |
| 677 | return false; |
| 678 | #else |
| 679 | return true; |
| 680 | #endif |
| 681 | } |
| 682 | |
| 683 | // InsertFPRegKills - Insert FP_REG_KILL instructions into basic blocks that |
| 684 | // need them. This only occurs due to the floating point stackifier not being |
| 685 | // aggressive enough to handle arbitrary global stackification. |
| 686 | // |
| 687 | // Currently we insert an FP_REG_KILL instruction into each block that uses or |
| 688 | // defines a floating point virtual register. |
| 689 | // |
| 690 | // When the global register allocators (like linear scan) finally update live |
| 691 | // variable analysis, we can keep floating point values in registers across |
| 692 | // portions of the CFG that do not involve critical edges. This will be a big |
| 693 | // win, but we are waiting on the global allocators before we can do this. |
| 694 | // |
| 695 | // With a bit of work, the floating point stackifier pass can be enhanced to |
| 696 | // break critical edges as needed (to make a place to put compensation code), |
| 697 | // but this will require some infrastructure improvements as well. |
| 698 | // |
| 699 | void ISel::InsertFPRegKills() { |
| 700 | SSARegMap &RegMap = *F->getSSARegMap(); |
Chris Lattner | 986618e | 2004-02-22 19:47:26 +0000 | [diff] [blame] | 701 | |
| 702 | for (MachineFunction::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { |
Chris Lattner | 986618e | 2004-02-22 19:47:26 +0000 | [diff] [blame] | 703 | for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I!=E; ++I) |
Alkis Evlogimenos | 71e353e | 2004-02-26 22:00:20 +0000 | [diff] [blame] | 704 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 705 | MachineOperand& MO = I->getOperand(i); |
| 706 | if (MO.isRegister() && MO.getReg()) { |
| 707 | unsigned Reg = MO.getReg(); |
Chris Lattner | 986618e | 2004-02-22 19:47:26 +0000 | [diff] [blame] | 708 | if (MRegisterInfo::isVirtualRegister(Reg)) |
Chris Lattner | 65cf42d | 2004-02-23 07:29:45 +0000 | [diff] [blame] | 709 | if (RegMap.getRegClass(Reg)->getSize() == 10) |
| 710 | goto UsesFPReg; |
Chris Lattner | 986618e | 2004-02-22 19:47:26 +0000 | [diff] [blame] | 711 | } |
Alkis Evlogimenos | 71e353e | 2004-02-26 22:00:20 +0000 | [diff] [blame] | 712 | } |
Chris Lattner | 65cf42d | 2004-02-23 07:29:45 +0000 | [diff] [blame] | 713 | // If we haven't found an FP register use or def in this basic block, check |
| 714 | // to see if any of our successors has an FP PHI node, which will cause a |
| 715 | // copy to be inserted into this block. |
Chris Lattner | fbc39d5 | 2004-02-23 07:42:19 +0000 | [diff] [blame] | 716 | for (succ_const_iterator SI = succ_begin(BB->getBasicBlock()), |
| 717 | E = succ_end(BB->getBasicBlock()); SI != E; ++SI) { |
| 718 | MachineBasicBlock *SBB = MBBMap[*SI]; |
| 719 | for (MachineBasicBlock::iterator I = SBB->begin(); |
| 720 | I != SBB->end() && I->getOpcode() == X86::PHI; ++I) { |
| 721 | if (RegMap.getRegClass(I->getOperand(0).getReg())->getSize() == 10) |
| 722 | goto UsesFPReg; |
Chris Lattner | 986618e | 2004-02-22 19:47:26 +0000 | [diff] [blame] | 723 | } |
Chris Lattner | fbc39d5 | 2004-02-23 07:42:19 +0000 | [diff] [blame] | 724 | } |
Chris Lattner | 65cf42d | 2004-02-23 07:29:45 +0000 | [diff] [blame] | 725 | continue; |
| 726 | UsesFPReg: |
| 727 | // Okay, this block uses an FP register. If the block has successors (ie, |
| 728 | // it's not an unwind/return), insert the FP_REG_KILL instruction. |
| 729 | if (BB->getBasicBlock()->getTerminator()->getNumSuccessors() && |
| 730 | RequiresFPRegKill(BB->getBasicBlock())) { |
Alkis Evlogimenos | 743d0a1 | 2004-02-23 18:14:48 +0000 | [diff] [blame] | 731 | BMI(BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0); |
Chris Lattner | 65cf42d | 2004-02-23 07:29:45 +0000 | [diff] [blame] | 732 | ++NumFPKill; |
Chris Lattner | 986618e | 2004-02-22 19:47:26 +0000 | [diff] [blame] | 733 | } |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 738 | // canFoldSetCCIntoBranch - Return the setcc instruction if we can fold it into |
| 739 | // the conditional branch instruction which is the only user of the cc |
| 740 | // instruction. This is the case if the conditional branch is the only user of |
| 741 | // the setcc, and if the setcc is in the same basic block as the conditional |
| 742 | // branch. We also don't handle long arguments below, so we reject them here as |
| 743 | // well. |
| 744 | // |
| 745 | static SetCondInst *canFoldSetCCIntoBranch(Value *V) { |
| 746 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(V)) |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 747 | if (SCI->hasOneUse() && isa<BranchInst>(SCI->use_back()) && |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 748 | SCI->getParent() == cast<BranchInst>(SCI->use_back())->getParent()) { |
| 749 | const Type *Ty = SCI->getOperand(0)->getType(); |
| 750 | if (Ty != Type::LongTy && Ty != Type::ULongTy) |
| 751 | return SCI; |
| 752 | } |
| 753 | return 0; |
| 754 | } |
Chris Lattner | 333b2fa | 2002-12-13 10:09:43 +0000 | [diff] [blame] | 755 | |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 756 | // Return a fixed numbering for setcc instructions which does not depend on the |
| 757 | // order of the opcodes. |
| 758 | // |
| 759 | static unsigned getSetCCNumber(unsigned Opcode) { |
| 760 | switch(Opcode) { |
| 761 | default: assert(0 && "Unknown setcc instruction!"); |
| 762 | case Instruction::SetEQ: return 0; |
| 763 | case Instruction::SetNE: return 1; |
| 764 | case Instruction::SetLT: return 2; |
Chris Lattner | 55f6fab | 2003-01-16 18:07:23 +0000 | [diff] [blame] | 765 | case Instruction::SetGE: return 3; |
| 766 | case Instruction::SetGT: return 4; |
| 767 | case Instruction::SetLE: return 5; |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 768 | } |
| 769 | } |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 770 | |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 771 | // LLVM -> X86 signed X86 unsigned |
| 772 | // ----- ---------- ------------ |
| 773 | // seteq -> sete sete |
| 774 | // setne -> setne setne |
| 775 | // setlt -> setl setb |
Chris Lattner | 55f6fab | 2003-01-16 18:07:23 +0000 | [diff] [blame] | 776 | // setge -> setge setae |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 777 | // setgt -> setg seta |
| 778 | // setle -> setle setbe |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 779 | // ---- |
| 780 | // sets // Used by comparison with 0 optimization |
| 781 | // setns |
| 782 | static const unsigned SetCCOpcodeTab[2][8] = { |
| 783 | { X86::SETEr, X86::SETNEr, X86::SETBr, X86::SETAEr, X86::SETAr, X86::SETBEr, |
| 784 | 0, 0 }, |
| 785 | { X86::SETEr, X86::SETNEr, X86::SETLr, X86::SETGEr, X86::SETGr, X86::SETLEr, |
| 786 | X86::SETSr, X86::SETNSr }, |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 787 | }; |
| 788 | |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 789 | // EmitComparison - This function emits a comparison of the two operands, |
| 790 | // returning the extended setcc code to use. |
| 791 | unsigned ISel::EmitComparison(unsigned OpNum, Value *Op0, Value *Op1, |
| 792 | MachineBasicBlock *MBB, |
Chris Lattner | baa58a5 | 2004-02-23 03:10:10 +0000 | [diff] [blame] | 793 | MachineBasicBlock::iterator IP) { |
Brian Gaeke | 1749d63 | 2002-11-07 17:59:21 +0000 | [diff] [blame] | 794 | // The arguments are already supposed to be of the same type. |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 795 | const Type *CompTy = Op0->getType(); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 796 | unsigned Class = getClassB(CompTy); |
Chris Lattner | 58c41fe | 2003-08-24 19:19:47 +0000 | [diff] [blame] | 797 | unsigned Op0r = getReg(Op0, MBB, IP); |
Chris Lattner | 333864d | 2003-06-05 19:30:30 +0000 | [diff] [blame] | 798 | |
| 799 | // Special case handling of: cmp R, i |
| 800 | if (Class == cByte || Class == cShort || Class == cInt) |
| 801 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | c07736a | 2003-07-23 15:22:26 +0000 | [diff] [blame] | 802 | uint64_t Op1v = cast<ConstantInt>(CI)->getRawValue(); |
| 803 | |
Chris Lattner | 333864d | 2003-06-05 19:30:30 +0000 | [diff] [blame] | 804 | // Mask off any upper bits of the constant, if there are any... |
| 805 | Op1v &= (1ULL << (8 << Class)) - 1; |
| 806 | |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 807 | // If this is a comparison against zero, emit more efficient code. We |
| 808 | // can't handle unsigned comparisons against zero unless they are == or |
| 809 | // !=. These should have been strength reduced already anyway. |
| 810 | if (Op1v == 0 && (CompTy->isSigned() || OpNum < 2)) { |
| 811 | static const unsigned TESTTab[] = { |
| 812 | X86::TESTrr8, X86::TESTrr16, X86::TESTrr32 |
| 813 | }; |
| 814 | BMI(MBB, IP, TESTTab[Class], 2).addReg(Op0r).addReg(Op0r); |
| 815 | |
| 816 | if (OpNum == 2) return 6; // Map jl -> js |
| 817 | if (OpNum == 3) return 7; // Map jg -> jns |
| 818 | return OpNum; |
Chris Lattner | 333864d | 2003-06-05 19:30:30 +0000 | [diff] [blame] | 819 | } |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 820 | |
| 821 | static const unsigned CMPTab[] = { |
| 822 | X86::CMPri8, X86::CMPri16, X86::CMPri32 |
| 823 | }; |
| 824 | |
| 825 | BMI(MBB, IP, CMPTab[Class], 2).addReg(Op0r).addZImm(Op1v); |
| 826 | return OpNum; |
Chris Lattner | 333864d | 2003-06-05 19:30:30 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Chris Lattner | 9f08a92 | 2004-02-03 18:54:04 +0000 | [diff] [blame] | 829 | // Special case handling of comparison against +/- 0.0 |
| 830 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(Op1)) |
| 831 | if (CFP->isExactlyValue(+0.0) || CFP->isExactlyValue(-0.0)) { |
| 832 | BMI(MBB, IP, X86::FTST, 1).addReg(Op0r); |
| 833 | BMI(MBB, IP, X86::FNSTSWr8, 0); |
| 834 | BMI(MBB, IP, X86::SAHF, 1); |
| 835 | return OpNum; |
| 836 | } |
| 837 | |
Chris Lattner | 58c41fe | 2003-08-24 19:19:47 +0000 | [diff] [blame] | 838 | unsigned Op1r = getReg(Op1, MBB, IP); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 839 | switch (Class) { |
| 840 | default: assert(0 && "Unknown type class!"); |
| 841 | // Emit: cmp <var1>, <var2> (do the comparison). We can |
| 842 | // compare 8-bit with 8-bit, 16-bit with 16-bit, 32-bit with |
| 843 | // 32-bit. |
| 844 | case cByte: |
Chris Lattner | 58c41fe | 2003-08-24 19:19:47 +0000 | [diff] [blame] | 845 | BMI(MBB, IP, X86::CMPrr8, 2).addReg(Op0r).addReg(Op1r); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 846 | break; |
| 847 | case cShort: |
Chris Lattner | 58c41fe | 2003-08-24 19:19:47 +0000 | [diff] [blame] | 848 | BMI(MBB, IP, X86::CMPrr16, 2).addReg(Op0r).addReg(Op1r); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 849 | break; |
| 850 | case cInt: |
Chris Lattner | 58c41fe | 2003-08-24 19:19:47 +0000 | [diff] [blame] | 851 | BMI(MBB, IP, X86::CMPrr32, 2).addReg(Op0r).addReg(Op1r); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 852 | break; |
| 853 | case cFP: |
Chris Lattner | 58c41fe | 2003-08-24 19:19:47 +0000 | [diff] [blame] | 854 | BMI(MBB, IP, X86::FpUCOM, 2).addReg(Op0r).addReg(Op1r); |
| 855 | BMI(MBB, IP, X86::FNSTSWr8, 0); |
| 856 | BMI(MBB, IP, X86::SAHF, 1); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 857 | break; |
| 858 | |
| 859 | case cLong: |
| 860 | if (OpNum < 2) { // seteq, setne |
| 861 | unsigned LoTmp = makeAnotherReg(Type::IntTy); |
| 862 | unsigned HiTmp = makeAnotherReg(Type::IntTy); |
| 863 | unsigned FinalTmp = makeAnotherReg(Type::IntTy); |
Chris Lattner | 58c41fe | 2003-08-24 19:19:47 +0000 | [diff] [blame] | 864 | BMI(MBB, IP, X86::XORrr32, 2, LoTmp).addReg(Op0r).addReg(Op1r); |
| 865 | BMI(MBB, IP, X86::XORrr32, 2, HiTmp).addReg(Op0r+1).addReg(Op1r+1); |
| 866 | BMI(MBB, IP, X86::ORrr32, 2, FinalTmp).addReg(LoTmp).addReg(HiTmp); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 867 | break; // Allow the sete or setne to be generated from flags set by OR |
| 868 | } else { |
| 869 | // Emit a sequence of code which compares the high and low parts once |
| 870 | // each, then uses a conditional move to handle the overflow case. For |
| 871 | // example, a setlt for long would generate code like this: |
| 872 | // |
| 873 | // AL = lo(op1) < lo(op2) // Signedness depends on operands |
| 874 | // BL = hi(op1) < hi(op2) // Always unsigned comparison |
| 875 | // dest = hi(op1) == hi(op2) ? AL : BL; |
| 876 | // |
| 877 | |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 878 | // FIXME: This would be much better if we had hierarchical register |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 879 | // classes! Until then, hardcode registers so that we can deal with their |
| 880 | // aliases (because we don't have conditional byte moves). |
| 881 | // |
Chris Lattner | 58c41fe | 2003-08-24 19:19:47 +0000 | [diff] [blame] | 882 | BMI(MBB, IP, X86::CMPrr32, 2).addReg(Op0r).addReg(Op1r); |
| 883 | BMI(MBB, IP, SetCCOpcodeTab[0][OpNum], 0, X86::AL); |
| 884 | BMI(MBB, IP, X86::CMPrr32, 2).addReg(Op0r+1).addReg(Op1r+1); |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 885 | BMI(MBB, IP, SetCCOpcodeTab[CompTy->isSigned()][OpNum], 0, X86::BL); |
Chris Lattner | 58c41fe | 2003-08-24 19:19:47 +0000 | [diff] [blame] | 886 | BMI(MBB, IP, X86::IMPLICIT_DEF, 0, X86::BH); |
| 887 | BMI(MBB, IP, X86::IMPLICIT_DEF, 0, X86::AH); |
| 888 | BMI(MBB, IP, X86::CMOVErr16, 2, X86::BX).addReg(X86::BX).addReg(X86::AX); |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 889 | // NOTE: visitSetCondInst knows that the value is dumped into the BL |
| 890 | // register at this point for long values... |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 891 | return OpNum; |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 892 | } |
| 893 | } |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 894 | return OpNum; |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 895 | } |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 896 | |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 897 | |
| 898 | /// SetCC instructions - Here we just emit boilerplate code to set a byte-sized |
| 899 | /// register, then move it to wherever the result should be. |
| 900 | /// |
| 901 | void ISel::visitSetCondInst(SetCondInst &I) { |
| 902 | if (canFoldSetCCIntoBranch(&I)) return; // Fold this into a branch... |
| 903 | |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 904 | unsigned DestReg = getReg(I); |
Chris Lattner | 58c41fe | 2003-08-24 19:19:47 +0000 | [diff] [blame] | 905 | MachineBasicBlock::iterator MII = BB->end(); |
| 906 | emitSetCCOperation(BB, MII, I.getOperand(0), I.getOperand(1), I.getOpcode(), |
| 907 | DestReg); |
| 908 | } |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 909 | |
Chris Lattner | 58c41fe | 2003-08-24 19:19:47 +0000 | [diff] [blame] | 910 | /// emitSetCCOperation - Common code shared between visitSetCondInst and |
| 911 | /// constant expression support. |
| 912 | void ISel::emitSetCCOperation(MachineBasicBlock *MBB, |
Chris Lattner | baa58a5 | 2004-02-23 03:10:10 +0000 | [diff] [blame] | 913 | MachineBasicBlock::iterator IP, |
Chris Lattner | 58c41fe | 2003-08-24 19:19:47 +0000 | [diff] [blame] | 914 | Value *Op0, Value *Op1, unsigned Opcode, |
| 915 | unsigned TargetReg) { |
| 916 | unsigned OpNum = getSetCCNumber(Opcode); |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 917 | OpNum = EmitComparison(OpNum, Op0, Op1, MBB, IP); |
Chris Lattner | 58c41fe | 2003-08-24 19:19:47 +0000 | [diff] [blame] | 918 | |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 919 | const Type *CompTy = Op0->getType(); |
| 920 | unsigned CompClass = getClassB(CompTy); |
| 921 | bool isSigned = CompTy->isSigned() && CompClass != cFP; |
| 922 | |
| 923 | if (CompClass != cLong || OpNum < 2) { |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 924 | // Handle normal comparisons with a setcc instruction... |
Chris Lattner | 58c41fe | 2003-08-24 19:19:47 +0000 | [diff] [blame] | 925 | BMI(MBB, IP, SetCCOpcodeTab[isSigned][OpNum], 0, TargetReg); |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 926 | } else { |
| 927 | // Handle long comparisons by copying the value which is already in BL into |
| 928 | // the register we want... |
Chris Lattner | 58c41fe | 2003-08-24 19:19:47 +0000 | [diff] [blame] | 929 | BMI(MBB, IP, X86::MOVrr8, 1, TargetReg).addReg(X86::BL); |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 930 | } |
Brian Gaeke | 1749d63 | 2002-11-07 17:59:21 +0000 | [diff] [blame] | 931 | } |
Chris Lattner | 51b49a9 | 2002-11-02 19:45:49 +0000 | [diff] [blame] | 932 | |
Chris Lattner | 58c41fe | 2003-08-24 19:19:47 +0000 | [diff] [blame] | 933 | |
| 934 | |
| 935 | |
Brian Gaeke | c250598 | 2002-11-30 11:57:28 +0000 | [diff] [blame] | 936 | /// promote32 - Emit instructions to turn a narrow operand into a 32-bit-wide |
| 937 | /// operand, in the specified target register. |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 938 | void ISel::promote32(unsigned targetReg, const ValueRecord &VR) { |
| 939 | bool isUnsigned = VR.Ty->isUnsigned(); |
Chris Lattner | 5e2cb8b | 2003-08-04 02:12:48 +0000 | [diff] [blame] | 940 | |
| 941 | // Make sure we have the register number for this value... |
| 942 | unsigned Reg = VR.Val ? getReg(VR.Val) : VR.Reg; |
| 943 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 944 | switch (getClassB(VR.Ty)) { |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 945 | case cByte: |
| 946 | // Extend value into target register (8->32) |
| 947 | if (isUnsigned) |
Chris Lattner | 5e2cb8b | 2003-08-04 02:12:48 +0000 | [diff] [blame] | 948 | BuildMI(BB, X86::MOVZXr32r8, 1, targetReg).addReg(Reg); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 949 | else |
Chris Lattner | 5e2cb8b | 2003-08-04 02:12:48 +0000 | [diff] [blame] | 950 | BuildMI(BB, X86::MOVSXr32r8, 1, targetReg).addReg(Reg); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 951 | break; |
| 952 | case cShort: |
| 953 | // Extend value into target register (16->32) |
| 954 | if (isUnsigned) |
Chris Lattner | 5e2cb8b | 2003-08-04 02:12:48 +0000 | [diff] [blame] | 955 | BuildMI(BB, X86::MOVZXr32r16, 1, targetReg).addReg(Reg); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 956 | else |
Chris Lattner | 5e2cb8b | 2003-08-04 02:12:48 +0000 | [diff] [blame] | 957 | BuildMI(BB, X86::MOVSXr32r16, 1, targetReg).addReg(Reg); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 958 | break; |
| 959 | case cInt: |
| 960 | // Move value into target register (32->32) |
Chris Lattner | 5e2cb8b | 2003-08-04 02:12:48 +0000 | [diff] [blame] | 961 | BuildMI(BB, X86::MOVrr32, 1, targetReg).addReg(Reg); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 962 | break; |
| 963 | default: |
| 964 | assert(0 && "Unpromotable operand class in promote32"); |
| 965 | } |
Brian Gaeke | c250598 | 2002-11-30 11:57:28 +0000 | [diff] [blame] | 966 | } |
Chris Lattner | c5291f5 | 2002-10-27 21:16:59 +0000 | [diff] [blame] | 967 | |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 968 | /// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such, |
| 969 | /// we have the following possibilities: |
| 970 | /// |
| 971 | /// ret void: No return value, simply emit a 'ret' instruction |
| 972 | /// ret sbyte, ubyte : Extend value into EAX and return |
| 973 | /// ret short, ushort: Extend value into EAX and return |
| 974 | /// ret int, uint : Move value into EAX and return |
| 975 | /// ret pointer : Move value into EAX and return |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 976 | /// ret long, ulong : Move value into EAX/EDX and return |
| 977 | /// ret float/double : Top of FP stack |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 978 | /// |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 979 | void ISel::visitReturnInst(ReturnInst &I) { |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 980 | if (I.getNumOperands() == 0) { |
| 981 | BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction |
| 982 | return; |
| 983 | } |
| 984 | |
| 985 | Value *RetVal = I.getOperand(0); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 986 | unsigned RetReg = getReg(RetVal); |
| 987 | switch (getClassB(RetVal->getType())) { |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 988 | case cByte: // integral return values: extend or move into EAX and return |
| 989 | case cShort: |
| 990 | case cInt: |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 991 | promote32(X86::EAX, ValueRecord(RetReg, RetVal->getType())); |
Chris Lattner | dbd7372 | 2003-05-06 21:32:22 +0000 | [diff] [blame] | 992 | // Declare that EAX is live on exit |
Chris Lattner | c248903 | 2003-05-07 19:21:28 +0000 | [diff] [blame] | 993 | BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 994 | break; |
| 995 | case cFP: // Floats & Doubles: Return in ST(0) |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 996 | BuildMI(BB, X86::FpSETRESULT, 1).addReg(RetReg); |
Chris Lattner | dbd7372 | 2003-05-06 21:32:22 +0000 | [diff] [blame] | 997 | // Declare that top-of-stack is live on exit |
Chris Lattner | c248903 | 2003-05-07 19:21:28 +0000 | [diff] [blame] | 998 | BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 999 | break; |
| 1000 | case cLong: |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1001 | BuildMI(BB, X86::MOVrr32, 1, X86::EAX).addReg(RetReg); |
| 1002 | BuildMI(BB, X86::MOVrr32, 1, X86::EDX).addReg(RetReg+1); |
Chris Lattner | dbd7372 | 2003-05-06 21:32:22 +0000 | [diff] [blame] | 1003 | // Declare that EAX & EDX are live on exit |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 1004 | BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX) |
| 1005 | .addReg(X86::ESP); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1006 | break; |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1007 | default: |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1008 | visitInstruction(I); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1009 | } |
Chris Lattner | 43189d1 | 2002-11-17 20:07:45 +0000 | [diff] [blame] | 1010 | // Emit a 'ret' instruction |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1011 | BuildMI(BB, X86::RET, 0); |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
Chris Lattner | 55f6fab | 2003-01-16 18:07:23 +0000 | [diff] [blame] | 1014 | // getBlockAfter - Return the basic block which occurs lexically after the |
| 1015 | // specified one. |
| 1016 | static inline BasicBlock *getBlockAfter(BasicBlock *BB) { |
| 1017 | Function::iterator I = BB; ++I; // Get iterator to next block |
| 1018 | return I != BB->getParent()->end() ? &*I : 0; |
| 1019 | } |
| 1020 | |
Chris Lattner | 51b49a9 | 2002-11-02 19:45:49 +0000 | [diff] [blame] | 1021 | /// visitBranchInst - Handle conditional and unconditional branches here. Note |
| 1022 | /// that since code layout is frozen at this point, that if we are trying to |
| 1023 | /// 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] | 1024 | /// just make a fall-through (but we don't currently). |
Chris Lattner | 51b49a9 | 2002-11-02 19:45:49 +0000 | [diff] [blame] | 1025 | /// |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1026 | void ISel::visitBranchInst(BranchInst &BI) { |
Chris Lattner | 55f6fab | 2003-01-16 18:07:23 +0000 | [diff] [blame] | 1027 | BasicBlock *NextBB = getBlockAfter(BI.getParent()); // BB after current one |
| 1028 | |
| 1029 | if (!BI.isConditional()) { // Unconditional branch? |
Chris Lattner | cf93cdd | 2004-01-30 22:13:44 +0000 | [diff] [blame] | 1030 | if (BI.getSuccessor(0) != NextBB) |
Chris Lattner | 55f6fab | 2003-01-16 18:07:23 +0000 | [diff] [blame] | 1031 | BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(0)); |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 1032 | return; |
| 1033 | } |
| 1034 | |
| 1035 | // See if we can fold the setcc into the branch itself... |
| 1036 | SetCondInst *SCI = canFoldSetCCIntoBranch(BI.getCondition()); |
| 1037 | if (SCI == 0) { |
| 1038 | // Nope, cannot fold setcc into this branch. Emit a branch on a condition |
| 1039 | // computed some other way... |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 1040 | unsigned condReg = getReg(BI.getCondition()); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1041 | BuildMI(BB, X86::CMPri8, 2).addReg(condReg).addZImm(0); |
Chris Lattner | 55f6fab | 2003-01-16 18:07:23 +0000 | [diff] [blame] | 1042 | if (BI.getSuccessor(1) == NextBB) { |
| 1043 | if (BI.getSuccessor(0) != NextBB) |
| 1044 | BuildMI(BB, X86::JNE, 1).addPCDisp(BI.getSuccessor(0)); |
| 1045 | } else { |
| 1046 | BuildMI(BB, X86::JE, 1).addPCDisp(BI.getSuccessor(1)); |
| 1047 | |
| 1048 | if (BI.getSuccessor(0) != NextBB) |
| 1049 | BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(0)); |
| 1050 | } |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 1051 | return; |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1052 | } |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 1053 | |
| 1054 | unsigned OpNum = getSetCCNumber(SCI->getOpcode()); |
Chris Lattner | 58c41fe | 2003-08-24 19:19:47 +0000 | [diff] [blame] | 1055 | MachineBasicBlock::iterator MII = BB->end(); |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 1056 | OpNum = EmitComparison(OpNum, SCI->getOperand(0), SCI->getOperand(1), BB,MII); |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 1057 | |
| 1058 | const Type *CompTy = SCI->getOperand(0)->getType(); |
| 1059 | bool isSigned = CompTy->isSigned() && getClassB(CompTy) != cFP; |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 1060 | |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 1061 | |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 1062 | // LLVM -> X86 signed X86 unsigned |
| 1063 | // ----- ---------- ------------ |
| 1064 | // seteq -> je je |
| 1065 | // setne -> jne jne |
| 1066 | // setlt -> jl jb |
Chris Lattner | 55f6fab | 2003-01-16 18:07:23 +0000 | [diff] [blame] | 1067 | // setge -> jge jae |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 1068 | // setgt -> jg ja |
| 1069 | // setle -> jle jbe |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 1070 | // ---- |
| 1071 | // js // Used by comparison with 0 optimization |
| 1072 | // jns |
| 1073 | |
| 1074 | static const unsigned OpcodeTab[2][8] = { |
| 1075 | { X86::JE, X86::JNE, X86::JB, X86::JAE, X86::JA, X86::JBE, 0, 0 }, |
| 1076 | { X86::JE, X86::JNE, X86::JL, X86::JGE, X86::JG, X86::JLE, |
| 1077 | X86::JS, X86::JNS }, |
Chris Lattner | 6d40c19 | 2003-01-16 16:43:00 +0000 | [diff] [blame] | 1078 | }; |
| 1079 | |
Chris Lattner | 55f6fab | 2003-01-16 18:07:23 +0000 | [diff] [blame] | 1080 | if (BI.getSuccessor(0) != NextBB) { |
| 1081 | BuildMI(BB, OpcodeTab[isSigned][OpNum], 1).addPCDisp(BI.getSuccessor(0)); |
| 1082 | if (BI.getSuccessor(1) != NextBB) |
| 1083 | BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(1)); |
| 1084 | } else { |
| 1085 | // Change to the inverse condition... |
| 1086 | if (BI.getSuccessor(1) != NextBB) { |
| 1087 | OpNum ^= 1; |
| 1088 | BuildMI(BB, OpcodeTab[isSigned][OpNum], 1).addPCDisp(BI.getSuccessor(1)); |
| 1089 | } |
| 1090 | } |
Chris Lattner | 2df035b | 2002-11-02 19:27:56 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1093 | |
| 1094 | /// doCall - This emits an abstract call instruction, setting up the arguments |
| 1095 | /// and the return value as appropriate. For the actual function call itself, |
| 1096 | /// it inserts the specified CallMI instruction into the stream. |
| 1097 | /// |
| 1098 | void ISel::doCall(const ValueRecord &Ret, MachineInstr *CallMI, |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 1099 | const std::vector<ValueRecord> &Args) { |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1100 | |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 1101 | // Count how many bytes are to be pushed on the stack... |
| 1102 | unsigned NumBytes = 0; |
Misha Brukman | 0d2cf3a | 2002-12-04 19:22:53 +0000 | [diff] [blame] | 1103 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1104 | if (!Args.empty()) { |
| 1105 | for (unsigned i = 0, e = Args.size(); i != e; ++i) |
| 1106 | switch (getClassB(Args[i].Ty)) { |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 1107 | case cByte: case cShort: case cInt: |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 1108 | NumBytes += 4; break; |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 1109 | case cLong: |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 1110 | NumBytes += 8; break; |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 1111 | case cFP: |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 1112 | NumBytes += Args[i].Ty == Type::FloatTy ? 4 : 8; |
| 1113 | break; |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 1114 | default: assert(0 && "Unknown class!"); |
| 1115 | } |
| 1116 | |
| 1117 | // Adjust the stack pointer for the new arguments... |
| 1118 | BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addZImm(NumBytes); |
| 1119 | |
| 1120 | // Arguments go on the stack in reverse order, as specified by the ABI. |
| 1121 | unsigned ArgOffset = 0; |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1122 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
Chris Lattner | 5e2cb8b | 2003-08-04 02:12:48 +0000 | [diff] [blame] | 1123 | unsigned ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg; |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1124 | switch (getClassB(Args[i].Ty)) { |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 1125 | case cByte: |
| 1126 | case cShort: { |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 1127 | // Promote arg to 32 bits wide into a temporary register... |
| 1128 | unsigned R = makeAnotherReg(Type::UIntTy); |
| 1129 | promote32(R, Args[i]); |
Chris Lattner | e87331d | 2004-02-17 06:28:19 +0000 | [diff] [blame] | 1130 | addRegOffset(BuildMI(BB, X86::MOVmr32, 5), |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 1131 | X86::ESP, ArgOffset).addReg(R); |
| 1132 | break; |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 1133 | } |
| 1134 | case cInt: |
Chris Lattner | e87331d | 2004-02-17 06:28:19 +0000 | [diff] [blame] | 1135 | addRegOffset(BuildMI(BB, X86::MOVmr32, 5), |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 1136 | X86::ESP, ArgOffset).addReg(ArgReg); |
| 1137 | break; |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1138 | case cLong: |
Chris Lattner | e87331d | 2004-02-17 06:28:19 +0000 | [diff] [blame] | 1139 | addRegOffset(BuildMI(BB, X86::MOVmr32, 5), |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 1140 | X86::ESP, ArgOffset).addReg(ArgReg); |
Chris Lattner | e87331d | 2004-02-17 06:28:19 +0000 | [diff] [blame] | 1141 | addRegOffset(BuildMI(BB, X86::MOVmr32, 5), |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 1142 | X86::ESP, ArgOffset+4).addReg(ArgReg+1); |
| 1143 | ArgOffset += 4; // 8 byte entry, not 4. |
| 1144 | break; |
| 1145 | |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 1146 | case cFP: |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 1147 | if (Args[i].Ty == Type::FloatTy) { |
Alkis Evlogimenos | 8e475b8 | 2004-02-28 23:42:35 +0000 | [diff] [blame] | 1148 | addRegOffset(BuildMI(BB, X86::FSTm32, 5), |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 1149 | X86::ESP, ArgOffset).addReg(ArgReg); |
| 1150 | } else { |
| 1151 | assert(Args[i].Ty == Type::DoubleTy && "Unknown FP type!"); |
Alkis Evlogimenos | 8e475b8 | 2004-02-28 23:42:35 +0000 | [diff] [blame] | 1152 | addRegOffset(BuildMI(BB, X86::FSTm64, 5), |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 1153 | X86::ESP, ArgOffset).addReg(ArgReg); |
| 1154 | ArgOffset += 4; // 8 byte entry, not 4. |
| 1155 | } |
| 1156 | break; |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 1157 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1158 | default: assert(0 && "Unknown class!"); |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 1159 | } |
| 1160 | ArgOffset += 4; |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1161 | } |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1162 | } else { |
| 1163 | BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addZImm(0); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1164 | } |
Chris Lattner | 6e49a4b | 2002-12-13 14:13:27 +0000 | [diff] [blame] | 1165 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1166 | BB->push_back(CallMI); |
Misha Brukman | 0d2cf3a | 2002-12-04 19:22:53 +0000 | [diff] [blame] | 1167 | |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 1168 | BuildMI(BB, X86::ADJCALLSTACKUP, 1).addZImm(NumBytes); |
Chris Lattner | a324364 | 2002-12-04 23:45:28 +0000 | [diff] [blame] | 1169 | |
| 1170 | // If there is a return value, scavenge the result from the location the call |
| 1171 | // leaves it in... |
| 1172 | // |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1173 | if (Ret.Ty != Type::VoidTy) { |
| 1174 | unsigned DestClass = getClassB(Ret.Ty); |
| 1175 | switch (DestClass) { |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 1176 | case cByte: |
| 1177 | case cShort: |
| 1178 | case cInt: { |
| 1179 | // Integral results are in %eax, or the appropriate portion |
| 1180 | // thereof. |
| 1181 | static const unsigned regRegMove[] = { |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 1182 | X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 1183 | }; |
| 1184 | static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX }; |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1185 | BuildMI(BB, regRegMove[DestClass], 1, Ret.Reg).addReg(AReg[DestClass]); |
Chris Lattner | 4fa1acc | 2002-12-04 23:50:28 +0000 | [diff] [blame] | 1186 | break; |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 1187 | } |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1188 | case cFP: // Floating-point return values live in %ST(0) |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1189 | BuildMI(BB, X86::FpGETRESULT, 1, Ret.Reg); |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 1190 | break; |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1191 | case cLong: // Long values are left in EDX:EAX |
| 1192 | BuildMI(BB, X86::MOVrr32, 1, Ret.Reg).addReg(X86::EAX); |
| 1193 | BuildMI(BB, X86::MOVrr32, 1, Ret.Reg+1).addReg(X86::EDX); |
| 1194 | break; |
| 1195 | default: assert(0 && "Unknown class!"); |
Chris Lattner | 4fa1acc | 2002-12-04 23:50:28 +0000 | [diff] [blame] | 1196 | } |
Chris Lattner | a324364 | 2002-12-04 23:45:28 +0000 | [diff] [blame] | 1197 | } |
Brian Gaeke | fa8d571 | 2002-11-22 11:07:01 +0000 | [diff] [blame] | 1198 | } |
Chris Lattner | 2df035b | 2002-11-02 19:27:56 +0000 | [diff] [blame] | 1199 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1200 | |
| 1201 | /// visitCallInst - Push args on stack and do a procedure call instruction. |
| 1202 | void ISel::visitCallInst(CallInst &CI) { |
| 1203 | MachineInstr *TheCall; |
| 1204 | if (Function *F = CI.getCalledFunction()) { |
Chris Lattner | eca195e | 2003-05-08 19:44:13 +0000 | [diff] [blame] | 1205 | // Is it an intrinsic function call? |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 1206 | if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) { |
Chris Lattner | eca195e | 2003-05-08 19:44:13 +0000 | [diff] [blame] | 1207 | visitIntrinsicCall(ID, CI); // Special intrinsics are not handled here |
| 1208 | return; |
| 1209 | } |
| 1210 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1211 | // Emit a CALL instruction with PC-relative displacement. |
| 1212 | TheCall = BuildMI(X86::CALLpcrel32, 1).addGlobalAddress(F, true); |
| 1213 | } else { // Emit an indirect call... |
| 1214 | unsigned Reg = getReg(CI.getCalledValue()); |
| 1215 | TheCall = BuildMI(X86::CALLr32, 1).addReg(Reg); |
| 1216 | } |
| 1217 | |
| 1218 | std::vector<ValueRecord> Args; |
| 1219 | for (unsigned i = 1, e = CI.getNumOperands(); i != e; ++i) |
Chris Lattner | 5e2cb8b | 2003-08-04 02:12:48 +0000 | [diff] [blame] | 1220 | Args.push_back(ValueRecord(CI.getOperand(i))); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1221 | |
| 1222 | unsigned DestReg = CI.getType() != Type::VoidTy ? getReg(CI) : 0; |
| 1223 | doCall(ValueRecord(DestReg, CI.getType()), TheCall, Args); |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 1224 | } |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1225 | |
Chris Lattner | aeb54b8 | 2003-08-28 21:23:43 +0000 | [diff] [blame] | 1226 | |
Chris Lattner | 4482715 | 2003-12-28 09:47:19 +0000 | [diff] [blame] | 1227 | /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the |
| 1228 | /// function, lowering any calls to unknown intrinsic functions into the |
| 1229 | /// equivalent LLVM code. |
| 1230 | void ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) { |
| 1231 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 1232 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) |
| 1233 | if (CallInst *CI = dyn_cast<CallInst>(I++)) |
| 1234 | if (Function *F = CI->getCalledFunction()) |
| 1235 | switch (F->getIntrinsicID()) { |
Chris Lattner | aed386e | 2003-12-28 09:53:23 +0000 | [diff] [blame] | 1236 | case Intrinsic::not_intrinsic: |
Chris Lattner | 4482715 | 2003-12-28 09:47:19 +0000 | [diff] [blame] | 1237 | case Intrinsic::va_start: |
| 1238 | case Intrinsic::va_copy: |
| 1239 | case Intrinsic::va_end: |
Chris Lattner | 0e5b79c | 2004-02-15 01:04:03 +0000 | [diff] [blame] | 1240 | case Intrinsic::returnaddress: |
| 1241 | case Intrinsic::frameaddress: |
Chris Lattner | 915e5e5 | 2004-02-12 17:53:22 +0000 | [diff] [blame] | 1242 | case Intrinsic::memcpy: |
Chris Lattner | 2a0f224 | 2004-02-14 04:46:05 +0000 | [diff] [blame] | 1243 | case Intrinsic::memset: |
Chris Lattner | 4482715 | 2003-12-28 09:47:19 +0000 | [diff] [blame] | 1244 | // We directly implement these intrinsics |
| 1245 | break; |
| 1246 | default: |
| 1247 | // All other intrinsic calls we must lower. |
| 1248 | Instruction *Before = CI->getPrev(); |
Chris Lattner | f70e0c2 | 2003-12-28 21:23:38 +0000 | [diff] [blame] | 1249 | TM.getIntrinsicLowering().LowerIntrinsicCall(CI); |
Chris Lattner | 4482715 | 2003-12-28 09:47:19 +0000 | [diff] [blame] | 1250 | if (Before) { // Move iterator to instruction after call |
| 1251 | I = Before; ++I; |
| 1252 | } else { |
| 1253 | I = BB->begin(); |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | } |
| 1258 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 1259 | void ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) { |
Chris Lattner | eca195e | 2003-05-08 19:44:13 +0000 | [diff] [blame] | 1260 | unsigned TmpReg1, TmpReg2; |
| 1261 | switch (ID) { |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 1262 | case Intrinsic::va_start: |
Chris Lattner | eca195e | 2003-05-08 19:44:13 +0000 | [diff] [blame] | 1263 | // Get the address of the first vararg value... |
Chris Lattner | 7381506 | 2003-10-18 05:56:40 +0000 | [diff] [blame] | 1264 | TmpReg1 = getReg(CI); |
Chris Lattner | eca195e | 2003-05-08 19:44:13 +0000 | [diff] [blame] | 1265 | addFrameReference(BuildMI(BB, X86::LEAr32, 5, TmpReg1), VarArgsFrameIndex); |
Chris Lattner | eca195e | 2003-05-08 19:44:13 +0000 | [diff] [blame] | 1266 | return; |
| 1267 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 1268 | case Intrinsic::va_copy: |
Chris Lattner | 7381506 | 2003-10-18 05:56:40 +0000 | [diff] [blame] | 1269 | TmpReg1 = getReg(CI); |
| 1270 | TmpReg2 = getReg(CI.getOperand(1)); |
| 1271 | BuildMI(BB, X86::MOVrr32, 1, TmpReg1).addReg(TmpReg2); |
Chris Lattner | eca195e | 2003-05-08 19:44:13 +0000 | [diff] [blame] | 1272 | return; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 1273 | case Intrinsic::va_end: return; // Noop on X86 |
Chris Lattner | eca195e | 2003-05-08 19:44:13 +0000 | [diff] [blame] | 1274 | |
Chris Lattner | 0e5b79c | 2004-02-15 01:04:03 +0000 | [diff] [blame] | 1275 | case Intrinsic::returnaddress: |
| 1276 | case Intrinsic::frameaddress: |
| 1277 | TmpReg1 = getReg(CI); |
| 1278 | if (cast<Constant>(CI.getOperand(1))->isNullValue()) { |
| 1279 | if (ID == Intrinsic::returnaddress) { |
| 1280 | // Just load the return address |
Chris Lattner | e87331d | 2004-02-17 06:28:19 +0000 | [diff] [blame] | 1281 | addFrameReference(BuildMI(BB, X86::MOVrm32, 4, TmpReg1), |
Chris Lattner | 0e5b79c | 2004-02-15 01:04:03 +0000 | [diff] [blame] | 1282 | ReturnAddressIndex); |
| 1283 | } else { |
| 1284 | addFrameReference(BuildMI(BB, X86::LEAr32, 4, TmpReg1), |
| 1285 | ReturnAddressIndex, -4); |
| 1286 | } |
| 1287 | } else { |
| 1288 | // Values other than zero are not implemented yet. |
Chris Lattner | 6e173a0 | 2004-02-17 06:16:44 +0000 | [diff] [blame] | 1289 | BuildMI(BB, X86::MOVri32, 1, TmpReg1).addZImm(0); |
Chris Lattner | 0e5b79c | 2004-02-15 01:04:03 +0000 | [diff] [blame] | 1290 | } |
| 1291 | return; |
| 1292 | |
Chris Lattner | 915e5e5 | 2004-02-12 17:53:22 +0000 | [diff] [blame] | 1293 | case Intrinsic::memcpy: { |
| 1294 | assert(CI.getNumOperands() == 5 && "Illegal llvm.memcpy call!"); |
| 1295 | unsigned Align = 1; |
| 1296 | if (ConstantInt *AlignC = dyn_cast<ConstantInt>(CI.getOperand(4))) { |
| 1297 | Align = AlignC->getRawValue(); |
| 1298 | if (Align == 0) Align = 1; |
| 1299 | } |
| 1300 | |
| 1301 | // Turn the byte code into # iterations |
Chris Lattner | 915e5e5 | 2004-02-12 17:53:22 +0000 | [diff] [blame] | 1302 | unsigned CountReg; |
Chris Lattner | 2a0f224 | 2004-02-14 04:46:05 +0000 | [diff] [blame] | 1303 | unsigned Opcode; |
Chris Lattner | 915e5e5 | 2004-02-12 17:53:22 +0000 | [diff] [blame] | 1304 | switch (Align & 3) { |
| 1305 | case 2: // WORD aligned |
Chris Lattner | 0712283 | 2004-02-13 23:36:47 +0000 | [diff] [blame] | 1306 | if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) { |
| 1307 | CountReg = getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/2)); |
| 1308 | } else { |
| 1309 | CountReg = makeAnotherReg(Type::IntTy); |
Chris Lattner | 8dd8d26 | 2004-02-26 01:20:02 +0000 | [diff] [blame] | 1310 | unsigned ByteReg = getReg(CI.getOperand(3)); |
Chris Lattner | 7ddc3fb | 2004-02-17 06:24:02 +0000 | [diff] [blame] | 1311 | BuildMI(BB, X86::SHRri32, 2, CountReg).addReg(ByteReg).addZImm(1); |
Chris Lattner | 0712283 | 2004-02-13 23:36:47 +0000 | [diff] [blame] | 1312 | } |
Chris Lattner | 2a0f224 | 2004-02-14 04:46:05 +0000 | [diff] [blame] | 1313 | Opcode = X86::REP_MOVSW; |
Chris Lattner | 915e5e5 | 2004-02-12 17:53:22 +0000 | [diff] [blame] | 1314 | break; |
| 1315 | case 0: // DWORD aligned |
Chris Lattner | 0712283 | 2004-02-13 23:36:47 +0000 | [diff] [blame] | 1316 | if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) { |
| 1317 | CountReg = getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/4)); |
| 1318 | } else { |
| 1319 | CountReg = makeAnotherReg(Type::IntTy); |
Chris Lattner | 8dd8d26 | 2004-02-26 01:20:02 +0000 | [diff] [blame] | 1320 | unsigned ByteReg = getReg(CI.getOperand(3)); |
Chris Lattner | 7ddc3fb | 2004-02-17 06:24:02 +0000 | [diff] [blame] | 1321 | BuildMI(BB, X86::SHRri32, 2, CountReg).addReg(ByteReg).addZImm(2); |
Chris Lattner | 0712283 | 2004-02-13 23:36:47 +0000 | [diff] [blame] | 1322 | } |
Chris Lattner | 2a0f224 | 2004-02-14 04:46:05 +0000 | [diff] [blame] | 1323 | Opcode = X86::REP_MOVSD; |
Chris Lattner | 915e5e5 | 2004-02-12 17:53:22 +0000 | [diff] [blame] | 1324 | break; |
Chris Lattner | 8dd8d26 | 2004-02-26 01:20:02 +0000 | [diff] [blame] | 1325 | default: // BYTE aligned |
Chris Lattner | 0712283 | 2004-02-13 23:36:47 +0000 | [diff] [blame] | 1326 | CountReg = getReg(CI.getOperand(3)); |
Chris Lattner | 2a0f224 | 2004-02-14 04:46:05 +0000 | [diff] [blame] | 1327 | Opcode = X86::REP_MOVSB; |
Chris Lattner | 915e5e5 | 2004-02-12 17:53:22 +0000 | [diff] [blame] | 1328 | break; |
| 1329 | } |
| 1330 | |
| 1331 | // No matter what the alignment is, we put the source in ESI, the |
| 1332 | // destination in EDI, and the count in ECX. |
| 1333 | TmpReg1 = getReg(CI.getOperand(1)); |
| 1334 | TmpReg2 = getReg(CI.getOperand(2)); |
| 1335 | BuildMI(BB, X86::MOVrr32, 1, X86::ECX).addReg(CountReg); |
| 1336 | BuildMI(BB, X86::MOVrr32, 1, X86::EDI).addReg(TmpReg1); |
| 1337 | BuildMI(BB, X86::MOVrr32, 1, X86::ESI).addReg(TmpReg2); |
Chris Lattner | 2a0f224 | 2004-02-14 04:46:05 +0000 | [diff] [blame] | 1338 | BuildMI(BB, Opcode, 0); |
| 1339 | return; |
| 1340 | } |
| 1341 | case Intrinsic::memset: { |
| 1342 | assert(CI.getNumOperands() == 5 && "Illegal llvm.memset call!"); |
| 1343 | unsigned Align = 1; |
| 1344 | if (ConstantInt *AlignC = dyn_cast<ConstantInt>(CI.getOperand(4))) { |
| 1345 | Align = AlignC->getRawValue(); |
| 1346 | if (Align == 0) Align = 1; |
Chris Lattner | 915e5e5 | 2004-02-12 17:53:22 +0000 | [diff] [blame] | 1347 | } |
| 1348 | |
Chris Lattner | 2a0f224 | 2004-02-14 04:46:05 +0000 | [diff] [blame] | 1349 | // Turn the byte code into # iterations |
Chris Lattner | 2a0f224 | 2004-02-14 04:46:05 +0000 | [diff] [blame] | 1350 | unsigned CountReg; |
| 1351 | unsigned Opcode; |
| 1352 | if (ConstantInt *ValC = dyn_cast<ConstantInt>(CI.getOperand(2))) { |
| 1353 | unsigned Val = ValC->getRawValue() & 255; |
| 1354 | |
| 1355 | // If the value is a constant, then we can potentially use larger copies. |
| 1356 | switch (Align & 3) { |
| 1357 | case 2: // WORD aligned |
| 1358 | if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) { |
Chris Lattner | 300d0ed | 2004-02-14 06:00:36 +0000 | [diff] [blame] | 1359 | CountReg =getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/2)); |
Chris Lattner | 2a0f224 | 2004-02-14 04:46:05 +0000 | [diff] [blame] | 1360 | } else { |
| 1361 | CountReg = makeAnotherReg(Type::IntTy); |
Chris Lattner | 8dd8d26 | 2004-02-26 01:20:02 +0000 | [diff] [blame] | 1362 | unsigned ByteReg = getReg(CI.getOperand(3)); |
Chris Lattner | 7ddc3fb | 2004-02-17 06:24:02 +0000 | [diff] [blame] | 1363 | BuildMI(BB, X86::SHRri32, 2, CountReg).addReg(ByteReg).addZImm(1); |
Chris Lattner | 2a0f224 | 2004-02-14 04:46:05 +0000 | [diff] [blame] | 1364 | } |
Chris Lattner | 6e173a0 | 2004-02-17 06:16:44 +0000 | [diff] [blame] | 1365 | BuildMI(BB, X86::MOVri16, 1, X86::AX).addZImm((Val << 8) | Val); |
Chris Lattner | 2a0f224 | 2004-02-14 04:46:05 +0000 | [diff] [blame] | 1366 | Opcode = X86::REP_STOSW; |
| 1367 | break; |
| 1368 | case 0: // DWORD aligned |
| 1369 | if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) { |
Chris Lattner | 300d0ed | 2004-02-14 06:00:36 +0000 | [diff] [blame] | 1370 | CountReg =getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/4)); |
Chris Lattner | 2a0f224 | 2004-02-14 04:46:05 +0000 | [diff] [blame] | 1371 | } else { |
| 1372 | CountReg = makeAnotherReg(Type::IntTy); |
Chris Lattner | 8dd8d26 | 2004-02-26 01:20:02 +0000 | [diff] [blame] | 1373 | unsigned ByteReg = getReg(CI.getOperand(3)); |
Chris Lattner | 7ddc3fb | 2004-02-17 06:24:02 +0000 | [diff] [blame] | 1374 | BuildMI(BB, X86::SHRri32, 2, CountReg).addReg(ByteReg).addZImm(2); |
Chris Lattner | 2a0f224 | 2004-02-14 04:46:05 +0000 | [diff] [blame] | 1375 | } |
| 1376 | Val = (Val << 8) | Val; |
Chris Lattner | 6e173a0 | 2004-02-17 06:16:44 +0000 | [diff] [blame] | 1377 | BuildMI(BB, X86::MOVri32, 1, X86::EAX).addZImm((Val << 16) | Val); |
Chris Lattner | 2a0f224 | 2004-02-14 04:46:05 +0000 | [diff] [blame] | 1378 | Opcode = X86::REP_STOSD; |
| 1379 | break; |
Chris Lattner | 8dd8d26 | 2004-02-26 01:20:02 +0000 | [diff] [blame] | 1380 | default: // BYTE aligned |
Chris Lattner | 2a0f224 | 2004-02-14 04:46:05 +0000 | [diff] [blame] | 1381 | CountReg = getReg(CI.getOperand(3)); |
Chris Lattner | 6e173a0 | 2004-02-17 06:16:44 +0000 | [diff] [blame] | 1382 | BuildMI(BB, X86::MOVri8, 1, X86::AL).addZImm(Val); |
Chris Lattner | 2a0f224 | 2004-02-14 04:46:05 +0000 | [diff] [blame] | 1383 | Opcode = X86::REP_STOSB; |
| 1384 | break; |
| 1385 | } |
| 1386 | } else { |
| 1387 | // If it's not a constant value we are storing, just fall back. We could |
| 1388 | // try to be clever to form 16 bit and 32 bit values, but we don't yet. |
| 1389 | unsigned ValReg = getReg(CI.getOperand(2)); |
| 1390 | BuildMI(BB, X86::MOVrr8, 1, X86::AL).addReg(ValReg); |
| 1391 | CountReg = getReg(CI.getOperand(3)); |
| 1392 | Opcode = X86::REP_STOSB; |
| 1393 | } |
| 1394 | |
| 1395 | // No matter what the alignment is, we put the source in ESI, the |
| 1396 | // destination in EDI, and the count in ECX. |
| 1397 | TmpReg1 = getReg(CI.getOperand(1)); |
| 1398 | //TmpReg2 = getReg(CI.getOperand(2)); |
| 1399 | BuildMI(BB, X86::MOVrr32, 1, X86::ECX).addReg(CountReg); |
| 1400 | BuildMI(BB, X86::MOVrr32, 1, X86::EDI).addReg(TmpReg1); |
| 1401 | BuildMI(BB, Opcode, 0); |
Chris Lattner | 915e5e5 | 2004-02-12 17:53:22 +0000 | [diff] [blame] | 1402 | return; |
| 1403 | } |
| 1404 | |
Chris Lattner | 4482715 | 2003-12-28 09:47:19 +0000 | [diff] [blame] | 1405 | default: assert(0 && "Error: unknown intrinsics should have been lowered!"); |
Chris Lattner | eca195e | 2003-05-08 19:44:13 +0000 | [diff] [blame] | 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | |
Chris Lattner | b515f6d | 2003-05-08 20:49:25 +0000 | [diff] [blame] | 1410 | /// visitSimpleBinary - Implement simple binary operators for integral types... |
| 1411 | /// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or, 4 for |
| 1412 | /// Xor. |
| 1413 | void ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) { |
| 1414 | unsigned DestReg = getReg(B); |
| 1415 | MachineBasicBlock::iterator MI = BB->end(); |
| 1416 | emitSimpleBinaryOperation(BB, MI, B.getOperand(0), B.getOperand(1), |
| 1417 | OperatorClass, DestReg); |
| 1418 | } |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1419 | |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 1420 | /// emitSimpleBinaryOperation - Implement simple binary operators for integral |
| 1421 | /// types... OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for |
| 1422 | /// Or, 4 for Xor. |
Chris Lattner | 68aad93 | 2002-11-02 20:13:22 +0000 | [diff] [blame] | 1423 | /// |
Chris Lattner | b515f6d | 2003-05-08 20:49:25 +0000 | [diff] [blame] | 1424 | /// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary |
| 1425 | /// and constant expression support. |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 1426 | /// |
| 1427 | void ISel::emitSimpleBinaryOperation(MachineBasicBlock *MBB, |
Chris Lattner | baa58a5 | 2004-02-23 03:10:10 +0000 | [diff] [blame] | 1428 | MachineBasicBlock::iterator IP, |
Chris Lattner | b515f6d | 2003-05-08 20:49:25 +0000 | [diff] [blame] | 1429 | Value *Op0, Value *Op1, |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 1430 | unsigned OperatorClass, unsigned DestReg) { |
Chris Lattner | b515f6d | 2003-05-08 20:49:25 +0000 | [diff] [blame] | 1431 | unsigned Class = getClassB(Op0->getType()); |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 1432 | |
| 1433 | // sub 0, X -> neg X |
| 1434 | if (OperatorClass == 1 && Class != cLong) |
Chris Lattner | af70362 | 2004-02-02 18:56:30 +0000 | [diff] [blame] | 1435 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0)) { |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 1436 | if (CI->isNullValue()) { |
| 1437 | unsigned op1Reg = getReg(Op1, MBB, IP); |
| 1438 | switch (Class) { |
| 1439 | default: assert(0 && "Unknown class for this function!"); |
| 1440 | case cByte: |
| 1441 | BMI(MBB, IP, X86::NEGr8, 1, DestReg).addReg(op1Reg); |
| 1442 | return; |
| 1443 | case cShort: |
| 1444 | BMI(MBB, IP, X86::NEGr16, 1, DestReg).addReg(op1Reg); |
| 1445 | return; |
| 1446 | case cInt: |
| 1447 | BMI(MBB, IP, X86::NEGr32, 1, DestReg).addReg(op1Reg); |
| 1448 | return; |
| 1449 | } |
| 1450 | } |
Chris Lattner | 9f8fd6d | 2004-02-02 19:31:38 +0000 | [diff] [blame] | 1451 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(Op0)) |
| 1452 | if (CFP->isExactlyValue(-0.0)) { |
| 1453 | // -0.0 - X === -X |
| 1454 | unsigned op1Reg = getReg(Op1, MBB, IP); |
| 1455 | BMI(MBB, IP, X86::FCHS, 1, DestReg).addReg(op1Reg); |
| 1456 | return; |
| 1457 | } |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 1458 | |
Chris Lattner | 35333e1 | 2003-06-05 18:28:55 +0000 | [diff] [blame] | 1459 | if (!isa<ConstantInt>(Op1) || Class == cLong) { |
| 1460 | static const unsigned OpcodeTab[][4] = { |
| 1461 | // Arithmetic operators |
| 1462 | { X86::ADDrr8, X86::ADDrr16, X86::ADDrr32, X86::FpADD }, // ADD |
| 1463 | { X86::SUBrr8, X86::SUBrr16, X86::SUBrr32, X86::FpSUB }, // SUB |
| 1464 | |
| 1465 | // Bitwise operators |
| 1466 | { X86::ANDrr8, X86::ANDrr16, X86::ANDrr32, 0 }, // AND |
| 1467 | { X86:: ORrr8, X86:: ORrr16, X86:: ORrr32, 0 }, // OR |
| 1468 | { X86::XORrr8, X86::XORrr16, X86::XORrr32, 0 }, // XOR |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1469 | }; |
Chris Lattner | 35333e1 | 2003-06-05 18:28:55 +0000 | [diff] [blame] | 1470 | |
| 1471 | bool isLong = false; |
| 1472 | if (Class == cLong) { |
| 1473 | isLong = true; |
| 1474 | Class = cInt; // Bottom 32 bits are handled just like ints |
| 1475 | } |
| 1476 | |
| 1477 | unsigned Opcode = OpcodeTab[OperatorClass][Class]; |
| 1478 | assert(Opcode && "Floating point arguments to logical inst?"); |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 1479 | unsigned Op0r = getReg(Op0, MBB, IP); |
| 1480 | unsigned Op1r = getReg(Op1, MBB, IP); |
| 1481 | BMI(MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r); |
Chris Lattner | 35333e1 | 2003-06-05 18:28:55 +0000 | [diff] [blame] | 1482 | |
| 1483 | if (isLong) { // Handle the upper 32 bits of long values... |
| 1484 | static const unsigned TopTab[] = { |
| 1485 | X86::ADCrr32, X86::SBBrr32, X86::ANDrr32, X86::ORrr32, X86::XORrr32 |
| 1486 | }; |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 1487 | BMI(MBB, IP, TopTab[OperatorClass], 2, |
| 1488 | DestReg+1).addReg(Op0r+1).addReg(Op1r+1); |
Chris Lattner | 35333e1 | 2003-06-05 18:28:55 +0000 | [diff] [blame] | 1489 | } |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 1490 | return; |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1491 | } |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 1492 | |
| 1493 | // Special case: op Reg, <const> |
| 1494 | ConstantInt *Op1C = cast<ConstantInt>(Op1); |
| 1495 | unsigned Op0r = getReg(Op0, MBB, IP); |
| 1496 | |
| 1497 | // xor X, -1 -> not X |
| 1498 | if (OperatorClass == 4 && Op1C->isAllOnesValue()) { |
| 1499 | static unsigned const NOTTab[] = { X86::NOTr8, X86::NOTr16, X86::NOTr32 }; |
| 1500 | BMI(MBB, IP, NOTTab[Class], 1, DestReg).addReg(Op0r); |
| 1501 | return; |
| 1502 | } |
| 1503 | |
| 1504 | // add X, -1 -> dec X |
| 1505 | if (OperatorClass == 0 && Op1C->isAllOnesValue()) { |
| 1506 | static unsigned const DECTab[] = { X86::DECr8, X86::DECr16, X86::DECr32 }; |
| 1507 | BMI(MBB, IP, DECTab[Class], 1, DestReg).addReg(Op0r); |
| 1508 | return; |
| 1509 | } |
| 1510 | |
| 1511 | // add X, 1 -> inc X |
| 1512 | if (OperatorClass == 0 && Op1C->equalsInt(1)) { |
| 1513 | static unsigned const DECTab[] = { X86::INCr8, X86::INCr16, X86::INCr32 }; |
| 1514 | BMI(MBB, IP, DECTab[Class], 1, DestReg).addReg(Op0r); |
| 1515 | return; |
| 1516 | } |
| 1517 | |
| 1518 | static const unsigned OpcodeTab[][3] = { |
| 1519 | // Arithmetic operators |
| 1520 | { X86::ADDri8, X86::ADDri16, X86::ADDri32 }, // ADD |
| 1521 | { X86::SUBri8, X86::SUBri16, X86::SUBri32 }, // SUB |
| 1522 | |
| 1523 | // Bitwise operators |
| 1524 | { X86::ANDri8, X86::ANDri16, X86::ANDri32 }, // AND |
| 1525 | { X86:: ORri8, X86:: ORri16, X86:: ORri32 }, // OR |
| 1526 | { X86::XORri8, X86::XORri16, X86::XORri32 }, // XOR |
| 1527 | }; |
| 1528 | |
| 1529 | assert(Class < 3 && "General code handles 64-bit integer types!"); |
| 1530 | unsigned Opcode = OpcodeTab[OperatorClass][Class]; |
| 1531 | uint64_t Op1v = cast<ConstantInt>(Op1C)->getRawValue(); |
| 1532 | |
| 1533 | // Mask off any upper bits of the constant, if there are any... |
| 1534 | Op1v &= (1ULL << (8 << Class)) - 1; |
| 1535 | BMI(MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addZImm(Op1v); |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1538 | /// doMultiply - Emit appropriate instructions to multiply together the |
| 1539 | /// registers op0Reg and op1Reg, and put the result in DestReg. The type of the |
| 1540 | /// result should be given as DestTy. |
| 1541 | /// |
Chris Lattner | baa58a5 | 2004-02-23 03:10:10 +0000 | [diff] [blame] | 1542 | void ISel::doMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI, |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1543 | unsigned DestReg, const Type *DestTy, |
Chris Lattner | 8a307e8 | 2002-12-16 19:32:50 +0000 | [diff] [blame] | 1544 | unsigned op0Reg, unsigned op1Reg) { |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1545 | unsigned Class = getClass(DestTy); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1546 | switch (Class) { |
| 1547 | case cFP: // Floating point multiply |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1548 | BMI(BB, MBBI, X86::FpMUL, 2, DestReg).addReg(op0Reg).addReg(op1Reg); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1549 | return; |
Chris Lattner | 0f1c461 | 2003-06-21 17:16:58 +0000 | [diff] [blame] | 1550 | case cInt: |
| 1551 | case cShort: |
Chris Lattner | c01d123 | 2003-10-20 03:42:58 +0000 | [diff] [blame] | 1552 | BMI(BB, MBBI, Class == cInt ? X86::IMULrr32 : X86::IMULrr16, 2, DestReg) |
Chris Lattner | 0f1c461 | 2003-06-21 17:16:58 +0000 | [diff] [blame] | 1553 | .addReg(op0Reg).addReg(op1Reg); |
| 1554 | return; |
| 1555 | case cByte: |
| 1556 | // Must use the MUL instruction, which forces use of AL... |
| 1557 | BMI(MBB, MBBI, X86::MOVrr8, 1, X86::AL).addReg(op0Reg); |
| 1558 | BMI(MBB, MBBI, X86::MULr8, 1).addReg(op1Reg); |
| 1559 | BMI(MBB, MBBI, X86::MOVrr8, 1, DestReg).addReg(X86::AL); |
| 1560 | return; |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1561 | default: |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1562 | case cLong: assert(0 && "doMultiply cannot operate on LONG values!"); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1563 | } |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 1564 | } |
| 1565 | |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 1566 | // ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It |
| 1567 | // returns zero when the input is not exactly a power of two. |
| 1568 | static unsigned ExactLog2(unsigned Val) { |
| 1569 | if (Val == 0) return 0; |
| 1570 | unsigned Count = 0; |
| 1571 | while (Val != 1) { |
| 1572 | if (Val & 1) return 0; |
| 1573 | Val >>= 1; |
| 1574 | ++Count; |
| 1575 | } |
| 1576 | return Count+1; |
| 1577 | } |
| 1578 | |
| 1579 | void ISel::doMultiplyConst(MachineBasicBlock *MBB, |
Chris Lattner | baa58a5 | 2004-02-23 03:10:10 +0000 | [diff] [blame] | 1580 | MachineBasicBlock::iterator IP, |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 1581 | unsigned DestReg, const Type *DestTy, |
| 1582 | unsigned op0Reg, unsigned ConstRHS) { |
| 1583 | unsigned Class = getClass(DestTy); |
| 1584 | |
| 1585 | // If the element size is exactly a power of 2, use a shift to get it. |
| 1586 | if (unsigned Shift = ExactLog2(ConstRHS)) { |
| 1587 | switch (Class) { |
| 1588 | default: assert(0 && "Unknown class for this function!"); |
| 1589 | case cByte: |
Chris Lattner | 7ddc3fb | 2004-02-17 06:24:02 +0000 | [diff] [blame] | 1590 | BMI(MBB, IP, X86::SHLri32, 2, DestReg).addReg(op0Reg).addZImm(Shift-1); |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 1591 | return; |
| 1592 | case cShort: |
Chris Lattner | 7ddc3fb | 2004-02-17 06:24:02 +0000 | [diff] [blame] | 1593 | BMI(MBB, IP, X86::SHLri32, 2, DestReg).addReg(op0Reg).addZImm(Shift-1); |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 1594 | return; |
| 1595 | case cInt: |
Chris Lattner | 7ddc3fb | 2004-02-17 06:24:02 +0000 | [diff] [blame] | 1596 | BMI(MBB, IP, X86::SHLri32, 2, DestReg).addReg(op0Reg).addZImm(Shift-1); |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 1597 | return; |
| 1598 | } |
| 1599 | } |
Chris Lattner | c01d123 | 2003-10-20 03:42:58 +0000 | [diff] [blame] | 1600 | |
| 1601 | if (Class == cShort) { |
Chris Lattner | 55b5481 | 2004-02-17 04:26:43 +0000 | [diff] [blame] | 1602 | BMI(MBB, IP, X86::IMULrri16, 2, DestReg).addReg(op0Reg).addZImm(ConstRHS); |
Chris Lattner | c01d123 | 2003-10-20 03:42:58 +0000 | [diff] [blame] | 1603 | return; |
| 1604 | } else if (Class == cInt) { |
Chris Lattner | 55b5481 | 2004-02-17 04:26:43 +0000 | [diff] [blame] | 1605 | BMI(MBB, IP, X86::IMULrri32, 2, DestReg).addReg(op0Reg).addZImm(ConstRHS); |
Chris Lattner | c01d123 | 2003-10-20 03:42:58 +0000 | [diff] [blame] | 1606 | return; |
| 1607 | } |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 1608 | |
| 1609 | // Most general case, emit a normal multiply... |
Chris Lattner | 6e173a0 | 2004-02-17 06:16:44 +0000 | [diff] [blame] | 1610 | static const unsigned MOVriTab[] = { |
| 1611 | X86::MOVri8, X86::MOVri16, X86::MOVri32 |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 1612 | }; |
| 1613 | |
| 1614 | unsigned TmpReg = makeAnotherReg(DestTy); |
Chris Lattner | 6e173a0 | 2004-02-17 06:16:44 +0000 | [diff] [blame] | 1615 | BMI(MBB, IP, MOVriTab[Class], 1, TmpReg).addZImm(ConstRHS); |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 1616 | |
| 1617 | // Emit a MUL to multiply the register holding the index by |
| 1618 | // elementSize, putting the result in OffsetReg. |
| 1619 | doMultiply(MBB, IP, DestReg, DestTy, op0Reg, TmpReg); |
| 1620 | } |
| 1621 | |
Chris Lattner | ca9671d | 2002-11-02 20:28:58 +0000 | [diff] [blame] | 1622 | /// visitMul - Multiplies are not simple binary operators because they must deal |
| 1623 | /// with the EAX register explicitly. |
| 1624 | /// |
| 1625 | void ISel::visitMul(BinaryOperator &I) { |
Chris Lattner | 202a2d0 | 2002-12-13 13:07:42 +0000 | [diff] [blame] | 1626 | unsigned Op0Reg = getReg(I.getOperand(0)); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1627 | unsigned DestReg = getReg(I); |
| 1628 | |
| 1629 | // Simple scalar multiply? |
| 1630 | if (I.getType() != Type::LongTy && I.getType() != Type::ULongTy) { |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 1631 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1))) { |
| 1632 | unsigned Val = (unsigned)CI->getRawValue(); // Cannot be 64-bit constant |
| 1633 | MachineBasicBlock::iterator MBBI = BB->end(); |
| 1634 | doMultiplyConst(BB, MBBI, DestReg, I.getType(), Op0Reg, Val); |
| 1635 | } else { |
| 1636 | unsigned Op1Reg = getReg(I.getOperand(1)); |
| 1637 | MachineBasicBlock::iterator MBBI = BB->end(); |
| 1638 | doMultiply(BB, MBBI, DestReg, I.getType(), Op0Reg, Op1Reg); |
| 1639 | } |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1640 | } else { |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 1641 | unsigned Op1Reg = getReg(I.getOperand(1)); |
| 1642 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1643 | // Long value. We have to do things the hard way... |
| 1644 | // Multiply the two low parts... capturing carry into EDX |
| 1645 | BuildMI(BB, X86::MOVrr32, 1, X86::EAX).addReg(Op0Reg); |
| 1646 | BuildMI(BB, X86::MULr32, 1).addReg(Op1Reg); // AL*BL |
| 1647 | |
| 1648 | unsigned OverflowReg = makeAnotherReg(Type::UIntTy); |
| 1649 | BuildMI(BB, X86::MOVrr32, 1, DestReg).addReg(X86::EAX); // AL*BL |
| 1650 | BuildMI(BB, X86::MOVrr32, 1, OverflowReg).addReg(X86::EDX); // AL*BL >> 32 |
| 1651 | |
| 1652 | MachineBasicBlock::iterator MBBI = BB->end(); |
Chris Lattner | 034acf0 | 2003-06-21 18:15:27 +0000 | [diff] [blame] | 1653 | unsigned AHBLReg = makeAnotherReg(Type::UIntTy); // AH*BL |
Chris Lattner | c01d123 | 2003-10-20 03:42:58 +0000 | [diff] [blame] | 1654 | BMI(BB, MBBI, X86::IMULrr32, 2, AHBLReg).addReg(Op0Reg+1).addReg(Op1Reg); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1655 | |
| 1656 | unsigned AHBLplusOverflowReg = makeAnotherReg(Type::UIntTy); |
| 1657 | BuildMI(BB, X86::ADDrr32, 2, // AH*BL+(AL*BL >> 32) |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 1658 | AHBLplusOverflowReg).addReg(AHBLReg).addReg(OverflowReg); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1659 | |
| 1660 | MBBI = BB->end(); |
Chris Lattner | 034acf0 | 2003-06-21 18:15:27 +0000 | [diff] [blame] | 1661 | unsigned ALBHReg = makeAnotherReg(Type::UIntTy); // AL*BH |
Chris Lattner | c01d123 | 2003-10-20 03:42:58 +0000 | [diff] [blame] | 1662 | BMI(BB, MBBI, X86::IMULrr32, 2, ALBHReg).addReg(Op0Reg).addReg(Op1Reg+1); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1663 | |
| 1664 | BuildMI(BB, X86::ADDrr32, 2, // AL*BH + AH*BL + (AL*BL >> 32) |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 1665 | DestReg+1).addReg(AHBLplusOverflowReg).addReg(ALBHReg); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1666 | } |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 1667 | } |
Chris Lattner | ca9671d | 2002-11-02 20:28:58 +0000 | [diff] [blame] | 1668 | |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 1669 | |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 1670 | /// visitDivRem - Handle division and remainder instructions... these |
| 1671 | /// instruction both require the same instructions to be generated, they just |
| 1672 | /// select the result from a different register. Note that both of these |
| 1673 | /// instructions work differently for signed and unsigned operands. |
| 1674 | /// |
| 1675 | void ISel::visitDivRem(BinaryOperator &I) { |
Chris Lattner | cadff44 | 2003-10-23 17:21:43 +0000 | [diff] [blame] | 1676 | unsigned Op0Reg = getReg(I.getOperand(0)); |
| 1677 | unsigned Op1Reg = getReg(I.getOperand(1)); |
| 1678 | unsigned ResultReg = getReg(I); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1679 | |
Chris Lattner | cadff44 | 2003-10-23 17:21:43 +0000 | [diff] [blame] | 1680 | MachineBasicBlock::iterator IP = BB->end(); |
| 1681 | emitDivRemOperation(BB, IP, Op0Reg, Op1Reg, I.getOpcode() == Instruction::Div, |
| 1682 | I.getType(), ResultReg); |
| 1683 | } |
| 1684 | |
| 1685 | void ISel::emitDivRemOperation(MachineBasicBlock *BB, |
Chris Lattner | baa58a5 | 2004-02-23 03:10:10 +0000 | [diff] [blame] | 1686 | MachineBasicBlock::iterator IP, |
Chris Lattner | cadff44 | 2003-10-23 17:21:43 +0000 | [diff] [blame] | 1687 | unsigned Op0Reg, unsigned Op1Reg, bool isDiv, |
| 1688 | const Type *Ty, unsigned ResultReg) { |
| 1689 | unsigned Class = getClass(Ty); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1690 | switch (Class) { |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1691 | case cFP: // Floating point divide |
Chris Lattner | cadff44 | 2003-10-23 17:21:43 +0000 | [diff] [blame] | 1692 | if (isDiv) { |
Chris Lattner | 62b767b | 2003-11-18 17:47:05 +0000 | [diff] [blame] | 1693 | BMI(BB, IP, X86::FpDIV, 2, ResultReg).addReg(Op0Reg).addReg(Op1Reg); |
Chris Lattner | 5e2cb8b | 2003-08-04 02:12:48 +0000 | [diff] [blame] | 1694 | } else { // Floating point remainder... |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1695 | MachineInstr *TheCall = |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 1696 | BuildMI(X86::CALLpcrel32, 1).addExternalSymbol("fmod", true); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1697 | std::vector<ValueRecord> Args; |
Chris Lattner | cadff44 | 2003-10-23 17:21:43 +0000 | [diff] [blame] | 1698 | Args.push_back(ValueRecord(Op0Reg, Type::DoubleTy)); |
| 1699 | Args.push_back(ValueRecord(Op1Reg, Type::DoubleTy)); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1700 | doCall(ValueRecord(ResultReg, Type::DoubleTy), TheCall, Args); |
| 1701 | } |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1702 | return; |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1703 | case cLong: { |
| 1704 | static const char *FnName[] = |
| 1705 | { "__moddi3", "__divdi3", "__umoddi3", "__udivdi3" }; |
| 1706 | |
Chris Lattner | cadff44 | 2003-10-23 17:21:43 +0000 | [diff] [blame] | 1707 | unsigned NameIdx = Ty->isUnsigned()*2 + isDiv; |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1708 | MachineInstr *TheCall = |
| 1709 | BuildMI(X86::CALLpcrel32, 1).addExternalSymbol(FnName[NameIdx], true); |
| 1710 | |
| 1711 | std::vector<ValueRecord> Args; |
Chris Lattner | cadff44 | 2003-10-23 17:21:43 +0000 | [diff] [blame] | 1712 | Args.push_back(ValueRecord(Op0Reg, Type::LongTy)); |
| 1713 | Args.push_back(ValueRecord(Op1Reg, Type::LongTy)); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1714 | doCall(ValueRecord(ResultReg, Type::LongTy), TheCall, Args); |
| 1715 | return; |
| 1716 | } |
| 1717 | case cByte: case cShort: case cInt: |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 1718 | break; // Small integrals, handled below... |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1719 | default: assert(0 && "Unknown class!"); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1720 | } |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 1721 | |
| 1722 | static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX }; |
| 1723 | static const unsigned MovOpcode[]={ X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 }; |
Chris Lattner | 7ddc3fb | 2004-02-17 06:24:02 +0000 | [diff] [blame] | 1724 | static const unsigned SarOpcode[]={ X86::SARri8, X86::SARri16, X86::SARri32 }; |
Chris Lattner | 6e173a0 | 2004-02-17 06:16:44 +0000 | [diff] [blame] | 1725 | static const unsigned ClrOpcode[]={ X86::MOVri8, X86::MOVri16, X86::MOVri32 }; |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 1726 | static const unsigned ExtRegs[] ={ X86::AH , X86::DX , X86::EDX }; |
| 1727 | |
| 1728 | static const unsigned DivOpcode[][4] = { |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1729 | { X86::DIVr8 , X86::DIVr16 , X86::DIVr32 , 0 }, // Unsigned division |
| 1730 | { X86::IDIVr8, X86::IDIVr16, X86::IDIVr32, 0 }, // Signed division |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 1731 | }; |
| 1732 | |
Chris Lattner | cadff44 | 2003-10-23 17:21:43 +0000 | [diff] [blame] | 1733 | bool isSigned = Ty->isSigned(); |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 1734 | unsigned Reg = Regs[Class]; |
| 1735 | unsigned ExtReg = ExtRegs[Class]; |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 1736 | |
| 1737 | // Put the first operand into one of the A registers... |
Chris Lattner | 62b767b | 2003-11-18 17:47:05 +0000 | [diff] [blame] | 1738 | BMI(BB, IP, MovOpcode[Class], 1, Reg).addReg(Op0Reg); |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 1739 | |
| 1740 | if (isSigned) { |
| 1741 | // Emit a sign extension instruction... |
Chris Lattner | cadff44 | 2003-10-23 17:21:43 +0000 | [diff] [blame] | 1742 | unsigned ShiftResult = makeAnotherReg(Ty); |
Chris Lattner | 62b767b | 2003-11-18 17:47:05 +0000 | [diff] [blame] | 1743 | BMI(BB, IP, SarOpcode[Class], 2, ShiftResult).addReg(Op0Reg).addZImm(31); |
| 1744 | BMI(BB, IP, MovOpcode[Class], 1, ExtReg).addReg(ShiftResult); |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 1745 | } else { |
Alkis Evlogimenos | f998a7e | 2004-01-12 07:22:45 +0000 | [diff] [blame] | 1746 | // If unsigned, emit a zeroing instruction... (reg = 0) |
| 1747 | BMI(BB, IP, ClrOpcode[Class], 2, ExtReg).addZImm(0); |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 1748 | } |
| 1749 | |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 1750 | // Emit the appropriate divide or remainder instruction... |
Chris Lattner | 62b767b | 2003-11-18 17:47:05 +0000 | [diff] [blame] | 1751 | BMI(BB, IP, DivOpcode[isSigned][Class], 1).addReg(Op1Reg); |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 1752 | |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 1753 | // Figure out which register we want to pick the result out of... |
Chris Lattner | cadff44 | 2003-10-23 17:21:43 +0000 | [diff] [blame] | 1754 | unsigned DestReg = isDiv ? Reg : ExtReg; |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 1755 | |
Chris Lattner | f01729e | 2002-11-02 20:54:46 +0000 | [diff] [blame] | 1756 | // Put the result into the destination register... |
Chris Lattner | 62b767b | 2003-11-18 17:47:05 +0000 | [diff] [blame] | 1757 | BMI(BB, IP, MovOpcode[Class], 1, ResultReg).addReg(DestReg); |
Chris Lattner | ca9671d | 2002-11-02 20:28:58 +0000 | [diff] [blame] | 1758 | } |
Chris Lattner | e2954c8 | 2002-11-02 20:04:26 +0000 | [diff] [blame] | 1759 | |
Chris Lattner | 0692536 | 2002-11-17 21:56:38 +0000 | [diff] [blame] | 1760 | |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 1761 | /// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here |
| 1762 | /// for constant immediate shift values, and for constant immediate |
| 1763 | /// shift values equal to 1. Even the general case is sort of special, |
| 1764 | /// because the shift amount has to be in CL, not just any old register. |
| 1765 | /// |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1766 | void ISel::visitShiftInst(ShiftInst &I) { |
Brian Gaeke | dfcc9cf | 2003-11-22 06:49:41 +0000 | [diff] [blame] | 1767 | MachineBasicBlock::iterator IP = BB->end (); |
| 1768 | emitShiftOperation (BB, IP, I.getOperand (0), I.getOperand (1), |
| 1769 | I.getOpcode () == Instruction::Shl, I.getType (), |
| 1770 | getReg (I)); |
| 1771 | } |
| 1772 | |
| 1773 | /// emitShiftOperation - Common code shared between visitShiftInst and |
| 1774 | /// constant expression support. |
| 1775 | void ISel::emitShiftOperation(MachineBasicBlock *MBB, |
Chris Lattner | baa58a5 | 2004-02-23 03:10:10 +0000 | [diff] [blame] | 1776 | MachineBasicBlock::iterator IP, |
Brian Gaeke | dfcc9cf | 2003-11-22 06:49:41 +0000 | [diff] [blame] | 1777 | Value *Op, Value *ShiftAmount, bool isLeftShift, |
| 1778 | const Type *ResultTy, unsigned DestReg) { |
| 1779 | unsigned SrcReg = getReg (Op, MBB, IP); |
| 1780 | bool isSigned = ResultTy->isSigned (); |
| 1781 | unsigned Class = getClass (ResultTy); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1782 | |
| 1783 | static const unsigned ConstantOperand[][4] = { |
Alkis Evlogimenos | da474ad | 2004-02-28 23:46:44 +0000 | [diff] [blame] | 1784 | { X86::SHRri8, X86::SHRri16, X86::SHRri32, X86::SHRDrr32i8 }, // SHR |
| 1785 | { X86::SARri8, X86::SARri16, X86::SARri32, X86::SHRDrr32i8 }, // SAR |
| 1786 | { X86::SHLri8, X86::SHLri16, X86::SHLri32, X86::SHLDrr32i8 }, // SHL |
| 1787 | { X86::SHLri8, X86::SHLri16, X86::SHLri32, X86::SHLDrr32i8 }, // SAL = SHL |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1788 | }; |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 1789 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1790 | static const unsigned NonConstantOperand[][4] = { |
Alkis Evlogimenos | e35ba65 | 2004-02-27 06:57:05 +0000 | [diff] [blame] | 1791 | { X86::SHRrCL8, X86::SHRrCL16, X86::SHRrCL32 }, // SHR |
| 1792 | { X86::SARrCL8, X86::SARrCL16, X86::SARrCL32 }, // SAR |
| 1793 | { X86::SHLrCL8, X86::SHLrCL16, X86::SHLrCL32 }, // SHL |
| 1794 | { X86::SHLrCL8, X86::SHLrCL16, X86::SHLrCL32 }, // SAL = SHL |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1795 | }; |
Chris Lattner | 796df73 | 2002-11-02 00:44:25 +0000 | [diff] [blame] | 1796 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1797 | // Longs, as usual, are handled specially... |
| 1798 | if (Class == cLong) { |
| 1799 | // If we have a constant shift, we can generate much more efficient code |
| 1800 | // than otherwise... |
| 1801 | // |
Brian Gaeke | dfcc9cf | 2003-11-22 06:49:41 +0000 | [diff] [blame] | 1802 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) { |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1803 | unsigned Amount = CUI->getValue(); |
| 1804 | if (Amount < 32) { |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 1805 | const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned]; |
| 1806 | if (isLeftShift) { |
Brian Gaeke | dfcc9cf | 2003-11-22 06:49:41 +0000 | [diff] [blame] | 1807 | BMI(MBB, IP, Opc[3], 3, |
| 1808 | DestReg+1).addReg(SrcReg+1).addReg(SrcReg).addZImm(Amount); |
| 1809 | BMI(MBB, IP, Opc[2], 2, DestReg).addReg(SrcReg).addZImm(Amount); |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 1810 | } else { |
Brian Gaeke | dfcc9cf | 2003-11-22 06:49:41 +0000 | [diff] [blame] | 1811 | BMI(MBB, IP, Opc[3], 3, |
| 1812 | DestReg).addReg(SrcReg ).addReg(SrcReg+1).addZImm(Amount); |
| 1813 | BMI(MBB, IP, Opc[2], 2, DestReg+1).addReg(SrcReg+1).addZImm(Amount); |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 1814 | } |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1815 | } else { // Shifting more than 32 bits |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 1816 | Amount -= 32; |
| 1817 | if (isLeftShift) { |
Chris Lattner | 7ddc3fb | 2004-02-17 06:24:02 +0000 | [diff] [blame] | 1818 | BMI(MBB, IP, X86::SHLri32, 2, |
Brian Gaeke | dfcc9cf | 2003-11-22 06:49:41 +0000 | [diff] [blame] | 1819 | DestReg + 1).addReg(SrcReg).addZImm(Amount); |
Chris Lattner | 6e173a0 | 2004-02-17 06:16:44 +0000 | [diff] [blame] | 1820 | BMI(MBB, IP, X86::MOVri32, 1, |
Brian Gaeke | dfcc9cf | 2003-11-22 06:49:41 +0000 | [diff] [blame] | 1821 | DestReg).addZImm(0); |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 1822 | } else { |
Chris Lattner | 7ddc3fb | 2004-02-17 06:24:02 +0000 | [diff] [blame] | 1823 | unsigned Opcode = isSigned ? X86::SARri32 : X86::SHRri32; |
Brian Gaeke | dfcc9cf | 2003-11-22 06:49:41 +0000 | [diff] [blame] | 1824 | BMI(MBB, IP, Opcode, 2, DestReg).addReg(SrcReg+1).addZImm(Amount); |
Chris Lattner | 6e173a0 | 2004-02-17 06:16:44 +0000 | [diff] [blame] | 1825 | BMI(MBB, IP, X86::MOVri32, 1, DestReg+1).addZImm(0); |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 1826 | } |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1827 | } |
| 1828 | } else { |
Chris Lattner | 9171ef5 | 2003-06-01 01:56:54 +0000 | [diff] [blame] | 1829 | unsigned TmpReg = makeAnotherReg(Type::IntTy); |
| 1830 | |
| 1831 | if (!isLeftShift && isSigned) { |
| 1832 | // If this is a SHR of a Long, then we need to do funny sign extension |
| 1833 | // stuff. TmpReg gets the value to use as the high-part if we are |
| 1834 | // shifting more than 32 bits. |
Chris Lattner | 7ddc3fb | 2004-02-17 06:24:02 +0000 | [diff] [blame] | 1835 | BMI(MBB, IP, X86::SARri32, 2, TmpReg).addReg(SrcReg).addZImm(31); |
Chris Lattner | 9171ef5 | 2003-06-01 01:56:54 +0000 | [diff] [blame] | 1836 | } else { |
| 1837 | // Other shifts use a fixed zero value if the shift is more than 32 |
| 1838 | // bits. |
Chris Lattner | 6e173a0 | 2004-02-17 06:16:44 +0000 | [diff] [blame] | 1839 | BMI(MBB, IP, X86::MOVri32, 1, TmpReg).addZImm(0); |
Chris Lattner | 9171ef5 | 2003-06-01 01:56:54 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
| 1842 | // Initialize CL with the shift amount... |
Brian Gaeke | dfcc9cf | 2003-11-22 06:49:41 +0000 | [diff] [blame] | 1843 | unsigned ShiftAmountReg = getReg(ShiftAmount, MBB, IP); |
| 1844 | BMI(MBB, IP, X86::MOVrr8, 1, X86::CL).addReg(ShiftAmountReg); |
Chris Lattner | 9171ef5 | 2003-06-01 01:56:54 +0000 | [diff] [blame] | 1845 | |
| 1846 | unsigned TmpReg2 = makeAnotherReg(Type::IntTy); |
| 1847 | unsigned TmpReg3 = makeAnotherReg(Type::IntTy); |
| 1848 | if (isLeftShift) { |
| 1849 | // TmpReg2 = shld inHi, inLo |
Alkis Evlogimenos | e35ba65 | 2004-02-27 06:57:05 +0000 | [diff] [blame] | 1850 | BMI(MBB, IP, X86::SHLDrrCL32,2,TmpReg2).addReg(SrcReg+1).addReg(SrcReg); |
Chris Lattner | 9171ef5 | 2003-06-01 01:56:54 +0000 | [diff] [blame] | 1851 | // TmpReg3 = shl inLo, CL |
Alkis Evlogimenos | e35ba65 | 2004-02-27 06:57:05 +0000 | [diff] [blame] | 1852 | BMI(MBB, IP, X86::SHLrCL32, 1, TmpReg3).addReg(SrcReg); |
Chris Lattner | 9171ef5 | 2003-06-01 01:56:54 +0000 | [diff] [blame] | 1853 | |
| 1854 | // Set the flags to indicate whether the shift was by more than 32 bits. |
Brian Gaeke | dfcc9cf | 2003-11-22 06:49:41 +0000 | [diff] [blame] | 1855 | BMI(MBB, IP, X86::TESTri8, 2).addReg(X86::CL).addZImm(32); |
Chris Lattner | 9171ef5 | 2003-06-01 01:56:54 +0000 | [diff] [blame] | 1856 | |
| 1857 | // DestHi = (>32) ? TmpReg3 : TmpReg2; |
Brian Gaeke | dfcc9cf | 2003-11-22 06:49:41 +0000 | [diff] [blame] | 1858 | BMI(MBB, IP, X86::CMOVNErr32, 2, |
Chris Lattner | 9171ef5 | 2003-06-01 01:56:54 +0000 | [diff] [blame] | 1859 | DestReg+1).addReg(TmpReg2).addReg(TmpReg3); |
| 1860 | // DestLo = (>32) ? TmpReg : TmpReg3; |
Brian Gaeke | dfcc9cf | 2003-11-22 06:49:41 +0000 | [diff] [blame] | 1861 | BMI(MBB, IP, X86::CMOVNErr32, 2, |
| 1862 | DestReg).addReg(TmpReg3).addReg(TmpReg); |
Chris Lattner | 9171ef5 | 2003-06-01 01:56:54 +0000 | [diff] [blame] | 1863 | } else { |
| 1864 | // TmpReg2 = shrd inLo, inHi |
Alkis Evlogimenos | e35ba65 | 2004-02-27 06:57:05 +0000 | [diff] [blame] | 1865 | BMI(MBB, IP, X86::SHRDrrCL32,2,TmpReg2).addReg(SrcReg).addReg(SrcReg+1); |
Chris Lattner | 9171ef5 | 2003-06-01 01:56:54 +0000 | [diff] [blame] | 1866 | // TmpReg3 = s[ah]r inHi, CL |
Alkis Evlogimenos | e35ba65 | 2004-02-27 06:57:05 +0000 | [diff] [blame] | 1867 | BMI(MBB, IP, isSigned ? X86::SARrCL32 : X86::SHRrCL32, 1, TmpReg3) |
Chris Lattner | 9171ef5 | 2003-06-01 01:56:54 +0000 | [diff] [blame] | 1868 | .addReg(SrcReg+1); |
| 1869 | |
| 1870 | // Set the flags to indicate whether the shift was by more than 32 bits. |
Brian Gaeke | dfcc9cf | 2003-11-22 06:49:41 +0000 | [diff] [blame] | 1871 | BMI(MBB, IP, X86::TESTri8, 2).addReg(X86::CL).addZImm(32); |
Chris Lattner | 9171ef5 | 2003-06-01 01:56:54 +0000 | [diff] [blame] | 1872 | |
| 1873 | // DestLo = (>32) ? TmpReg3 : TmpReg2; |
Brian Gaeke | dfcc9cf | 2003-11-22 06:49:41 +0000 | [diff] [blame] | 1874 | BMI(MBB, IP, X86::CMOVNErr32, 2, |
Chris Lattner | 9171ef5 | 2003-06-01 01:56:54 +0000 | [diff] [blame] | 1875 | DestReg).addReg(TmpReg2).addReg(TmpReg3); |
| 1876 | |
| 1877 | // DestHi = (>32) ? TmpReg : TmpReg3; |
Brian Gaeke | dfcc9cf | 2003-11-22 06:49:41 +0000 | [diff] [blame] | 1878 | BMI(MBB, IP, X86::CMOVNErr32, 2, |
Chris Lattner | 9171ef5 | 2003-06-01 01:56:54 +0000 | [diff] [blame] | 1879 | DestReg+1).addReg(TmpReg3).addReg(TmpReg); |
| 1880 | } |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 1881 | } |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1882 | return; |
| 1883 | } |
Chris Lattner | e9913f2 | 2002-11-02 01:41:55 +0000 | [diff] [blame] | 1884 | |
Brian Gaeke | dfcc9cf | 2003-11-22 06:49:41 +0000 | [diff] [blame] | 1885 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) { |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1886 | // The shift amount is constant, guaranteed to be a ubyte. Get its value. |
| 1887 | assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?"); |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 1888 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1889 | const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned]; |
Brian Gaeke | dfcc9cf | 2003-11-22 06:49:41 +0000 | [diff] [blame] | 1890 | BMI(MBB, IP, Opc[Class], 2, |
| 1891 | DestReg).addReg(SrcReg).addZImm(CUI->getValue()); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1892 | } else { // The shift amount is non-constant. |
Brian Gaeke | dfcc9cf | 2003-11-22 06:49:41 +0000 | [diff] [blame] | 1893 | unsigned ShiftAmountReg = getReg (ShiftAmount, MBB, IP); |
| 1894 | BMI(MBB, IP, X86::MOVrr8, 1, X86::CL).addReg(ShiftAmountReg); |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 1895 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1896 | const unsigned *Opc = NonConstantOperand[isLeftShift*2+isSigned]; |
Brian Gaeke | dfcc9cf | 2003-11-22 06:49:41 +0000 | [diff] [blame] | 1897 | BMI(MBB, IP, Opc[Class], 1, DestReg).addReg(SrcReg); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1898 | } |
| 1899 | } |
Chris Lattner | b1761fc | 2002-11-02 01:15:18 +0000 | [diff] [blame] | 1900 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1901 | |
Chris Lattner | 6fc3c52 | 2002-11-17 21:11:55 +0000 | [diff] [blame] | 1902 | /// visitLoadInst - Implement LLVM load instructions in terms of the x86 'mov' |
Chris Lattner | e8f0d92 | 2002-12-24 00:03:11 +0000 | [diff] [blame] | 1903 | /// instruction. The load and store instructions are the only place where we |
| 1904 | /// need to worry about the memory layout of the target machine. |
Chris Lattner | 6fc3c52 | 2002-11-17 21:11:55 +0000 | [diff] [blame] | 1905 | /// |
| 1906 | void ISel::visitLoadInst(LoadInst &I) { |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1907 | unsigned DestReg = getReg(I); |
Chris Lattner | b6bac51 | 2004-02-25 06:13:04 +0000 | [diff] [blame] | 1908 | unsigned BaseReg = 0, Scale = 1, IndexReg = 0, Disp = 0; |
| 1909 | Value *Addr = I.getOperand(0); |
| 1910 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Addr)) { |
| 1911 | if (isGEPFoldable(BB, GEP->getOperand(0), GEP->op_begin()+1, GEP->op_end(), |
| 1912 | BaseReg, Scale, IndexReg, Disp)) |
| 1913 | Addr = 0; // Address is consumed! |
| 1914 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Addr)) { |
| 1915 | if (CE->getOpcode() == Instruction::GetElementPtr) |
| 1916 | if (isGEPFoldable(BB, CE->getOperand(0), CE->op_begin()+1, CE->op_end(), |
| 1917 | BaseReg, Scale, IndexReg, Disp)) |
| 1918 | Addr = 0; |
| 1919 | } |
| 1920 | |
| 1921 | if (Addr) { |
| 1922 | // If it's not foldable, reset addr mode. |
| 1923 | BaseReg = getReg(Addr); |
| 1924 | Scale = 1; IndexReg = 0; Disp = 0; |
| 1925 | } |
Chris Lattner | e8f0d92 | 2002-12-24 00:03:11 +0000 | [diff] [blame] | 1926 | |
Brian Gaeke | bfedb91 | 2003-07-17 21:30:06 +0000 | [diff] [blame] | 1927 | unsigned Class = getClassB(I.getType()); |
Chris Lattner | 6ac1d71 | 2003-10-20 04:48:06 +0000 | [diff] [blame] | 1928 | if (Class == cLong) { |
Chris Lattner | b6bac51 | 2004-02-25 06:13:04 +0000 | [diff] [blame] | 1929 | addFullAddress(BuildMI(BB, X86::MOVrm32, 4, DestReg), |
| 1930 | BaseReg, Scale, IndexReg, Disp); |
| 1931 | addFullAddress(BuildMI(BB, X86::MOVrm32, 4, DestReg+1), |
| 1932 | BaseReg, Scale, IndexReg, Disp+4); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 1933 | return; |
| 1934 | } |
Chris Lattner | 6fc3c52 | 2002-11-17 21:11:55 +0000 | [diff] [blame] | 1935 | |
Chris Lattner | 6ac1d71 | 2003-10-20 04:48:06 +0000 | [diff] [blame] | 1936 | static const unsigned Opcodes[] = { |
Alkis Evlogimenos | 8e475b8 | 2004-02-28 23:42:35 +0000 | [diff] [blame] | 1937 | X86::MOVrm8, X86::MOVrm16, X86::MOVrm32, X86::FLDm32 |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1938 | }; |
Chris Lattner | 6ac1d71 | 2003-10-20 04:48:06 +0000 | [diff] [blame] | 1939 | unsigned Opcode = Opcodes[Class]; |
Alkis Evlogimenos | 8e475b8 | 2004-02-28 23:42:35 +0000 | [diff] [blame] | 1940 | if (I.getType() == Type::DoubleTy) Opcode = X86::FLDm64; |
Chris Lattner | b6bac51 | 2004-02-25 06:13:04 +0000 | [diff] [blame] | 1941 | addFullAddress(BuildMI(BB, Opcode, 4, DestReg), |
| 1942 | BaseReg, Scale, IndexReg, Disp); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
Chris Lattner | 6fc3c52 | 2002-11-17 21:11:55 +0000 | [diff] [blame] | 1945 | /// visitStoreInst - Implement LLVM store instructions in terms of the x86 'mov' |
| 1946 | /// instruction. |
| 1947 | /// |
| 1948 | void ISel::visitStoreInst(StoreInst &I) { |
Chris Lattner | b6bac51 | 2004-02-25 06:13:04 +0000 | [diff] [blame] | 1949 | unsigned BaseReg = 0, Scale = 1, IndexReg = 0, Disp = 0; |
| 1950 | Value *Addr = I.getOperand(1); |
| 1951 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Addr)) { |
| 1952 | if (isGEPFoldable(BB, GEP->getOperand(0), GEP->op_begin()+1, GEP->op_end(), |
| 1953 | BaseReg, Scale, IndexReg, Disp)) |
| 1954 | Addr = 0; // Address is consumed! |
| 1955 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Addr)) { |
| 1956 | if (CE->getOpcode() == Instruction::GetElementPtr) |
| 1957 | if (isGEPFoldable(BB, CE->getOperand(0), CE->op_begin()+1, CE->op_end(), |
| 1958 | BaseReg, Scale, IndexReg, Disp)) |
| 1959 | Addr = 0; |
| 1960 | } |
| 1961 | |
| 1962 | if (Addr) { |
| 1963 | // If it's not foldable, reset addr mode. |
| 1964 | BaseReg = getReg(Addr); |
| 1965 | Scale = 1; IndexReg = 0; Disp = 0; |
| 1966 | } |
| 1967 | |
Chris Lattner | 6c09db2 | 2003-10-20 04:11:23 +0000 | [diff] [blame] | 1968 | const Type *ValTy = I.getOperand(0)->getType(); |
| 1969 | unsigned Class = getClassB(ValTy); |
Chris Lattner | 6ac1d71 | 2003-10-20 04:48:06 +0000 | [diff] [blame] | 1970 | |
Chris Lattner | 5a83096 | 2004-02-25 02:56:58 +0000 | [diff] [blame] | 1971 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(0))) { |
| 1972 | uint64_t Val = CI->getRawValue(); |
| 1973 | if (Class == cLong) { |
Chris Lattner | b6bac51 | 2004-02-25 06:13:04 +0000 | [diff] [blame] | 1974 | addFullAddress(BuildMI(BB, X86::MOVmi32, 5), |
| 1975 | BaseReg, Scale, IndexReg, Disp).addZImm(Val & ~0U); |
| 1976 | addFullAddress(BuildMI(BB, X86::MOVmi32, 5), |
| 1977 | BaseReg, Scale, IndexReg, Disp+4).addZImm(Val>>32); |
Chris Lattner | 5a83096 | 2004-02-25 02:56:58 +0000 | [diff] [blame] | 1978 | } else { |
| 1979 | static const unsigned Opcodes[] = { |
| 1980 | X86::MOVmi8, X86::MOVmi16, X86::MOVmi32 |
| 1981 | }; |
| 1982 | unsigned Opcode = Opcodes[Class]; |
Chris Lattner | b6bac51 | 2004-02-25 06:13:04 +0000 | [diff] [blame] | 1983 | addFullAddress(BuildMI(BB, Opcode, 5), |
| 1984 | BaseReg, Scale, IndexReg, Disp).addZImm(Val); |
Chris Lattner | 5a83096 | 2004-02-25 02:56:58 +0000 | [diff] [blame] | 1985 | } |
| 1986 | } else if (ConstantBool *CB = dyn_cast<ConstantBool>(I.getOperand(0))) { |
Chris Lattner | b6bac51 | 2004-02-25 06:13:04 +0000 | [diff] [blame] | 1987 | addFullAddress(BuildMI(BB, X86::MOVmi8, 5), |
| 1988 | BaseReg, Scale, IndexReg, Disp).addZImm(CB->getValue()); |
Chris Lattner | 5a83096 | 2004-02-25 02:56:58 +0000 | [diff] [blame] | 1989 | } else { |
| 1990 | if (Class == cLong) { |
| 1991 | unsigned ValReg = getReg(I.getOperand(0)); |
Chris Lattner | b6bac51 | 2004-02-25 06:13:04 +0000 | [diff] [blame] | 1992 | addFullAddress(BuildMI(BB, X86::MOVmr32, 5), |
| 1993 | BaseReg, Scale, IndexReg, Disp).addReg(ValReg); |
| 1994 | addFullAddress(BuildMI(BB, X86::MOVmr32, 5), |
| 1995 | BaseReg, Scale, IndexReg, Disp+4).addReg(ValReg+1); |
Chris Lattner | 5a83096 | 2004-02-25 02:56:58 +0000 | [diff] [blame] | 1996 | } else { |
| 1997 | unsigned ValReg = getReg(I.getOperand(0)); |
| 1998 | static const unsigned Opcodes[] = { |
Alkis Evlogimenos | 8e475b8 | 2004-02-28 23:42:35 +0000 | [diff] [blame] | 1999 | X86::MOVmr8, X86::MOVmr16, X86::MOVmr32, X86::FSTm32 |
Chris Lattner | 5a83096 | 2004-02-25 02:56:58 +0000 | [diff] [blame] | 2000 | }; |
| 2001 | unsigned Opcode = Opcodes[Class]; |
Alkis Evlogimenos | 8e475b8 | 2004-02-28 23:42:35 +0000 | [diff] [blame] | 2002 | if (ValTy == Type::DoubleTy) Opcode = X86::FSTm64; |
Chris Lattner | b6bac51 | 2004-02-25 06:13:04 +0000 | [diff] [blame] | 2003 | addFullAddress(BuildMI(BB, Opcode, 1+4), |
| 2004 | BaseReg, Scale, IndexReg, Disp).addReg(ValReg); |
Chris Lattner | 5a83096 | 2004-02-25 02:56:58 +0000 | [diff] [blame] | 2005 | } |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 2006 | } |
Chris Lattner | 6fc3c52 | 2002-11-17 21:11:55 +0000 | [diff] [blame] | 2007 | } |
| 2008 | |
| 2009 | |
Brian Gaeke | c11232a | 2002-11-26 10:43:30 +0000 | [diff] [blame] | 2010 | /// visitCastInst - Here we have various kinds of copying with or without |
| 2011 | /// sign extension going on. |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2012 | void ISel::visitCastInst(CastInst &CI) { |
Chris Lattner | f585447 | 2003-06-21 16:01:24 +0000 | [diff] [blame] | 2013 | Value *Op = CI.getOperand(0); |
| 2014 | // If this is a cast from a 32-bit integer to a Long type, and the only uses |
| 2015 | // of the case are GEP instructions, then the cast does not need to be |
| 2016 | // generated explicitly, it will be folded into the GEP. |
| 2017 | if (CI.getType() == Type::LongTy && |
| 2018 | (Op->getType() == Type::IntTy || Op->getType() == Type::UIntTy)) { |
| 2019 | bool AllUsesAreGEPs = true; |
| 2020 | for (Value::use_iterator I = CI.use_begin(), E = CI.use_end(); I != E; ++I) |
| 2021 | if (!isa<GetElementPtrInst>(*I)) { |
| 2022 | AllUsesAreGEPs = false; |
| 2023 | break; |
| 2024 | } |
| 2025 | |
| 2026 | // No need to codegen this cast if all users are getelementptr instrs... |
| 2027 | if (AllUsesAreGEPs) return; |
| 2028 | } |
| 2029 | |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 2030 | unsigned DestReg = getReg(CI); |
| 2031 | MachineBasicBlock::iterator MI = BB->end(); |
Chris Lattner | f585447 | 2003-06-21 16:01:24 +0000 | [diff] [blame] | 2032 | emitCastOperation(BB, MI, Op, CI.getType(), DestReg); |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 2033 | } |
| 2034 | |
| 2035 | /// emitCastOperation - Common code shared between visitCastInst and |
| 2036 | /// constant expression cast support. |
| 2037 | void ISel::emitCastOperation(MachineBasicBlock *BB, |
Chris Lattner | baa58a5 | 2004-02-23 03:10:10 +0000 | [diff] [blame] | 2038 | MachineBasicBlock::iterator IP, |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 2039 | Value *Src, const Type *DestTy, |
| 2040 | unsigned DestReg) { |
Chris Lattner | 3907d11 | 2003-04-23 17:57:48 +0000 | [diff] [blame] | 2041 | unsigned SrcReg = getReg(Src, BB, IP); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2042 | const Type *SrcTy = Src->getType(); |
| 2043 | unsigned SrcClass = getClassB(SrcTy); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2044 | unsigned DestClass = getClassB(DestTy); |
Chris Lattner | 7d25589 | 2002-12-13 11:31:59 +0000 | [diff] [blame] | 2045 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2046 | // Implement casts to bool by using compare on the operand followed by set if |
| 2047 | // not zero on the result. |
| 2048 | if (DestTy == Type::BoolTy) { |
Chris Lattner | 2077254 | 2003-06-01 03:38:24 +0000 | [diff] [blame] | 2049 | switch (SrcClass) { |
| 2050 | case cByte: |
| 2051 | BMI(BB, IP, X86::TESTrr8, 2).addReg(SrcReg).addReg(SrcReg); |
| 2052 | break; |
| 2053 | case cShort: |
| 2054 | BMI(BB, IP, X86::TESTrr16, 2).addReg(SrcReg).addReg(SrcReg); |
| 2055 | break; |
| 2056 | case cInt: |
| 2057 | BMI(BB, IP, X86::TESTrr32, 2).addReg(SrcReg).addReg(SrcReg); |
| 2058 | break; |
| 2059 | case cLong: { |
| 2060 | unsigned TmpReg = makeAnotherReg(Type::IntTy); |
| 2061 | BMI(BB, IP, X86::ORrr32, 2, TmpReg).addReg(SrcReg).addReg(SrcReg+1); |
| 2062 | break; |
| 2063 | } |
| 2064 | case cFP: |
Chris Lattner | 311ca2e | 2004-02-23 03:21:41 +0000 | [diff] [blame] | 2065 | BMI(BB, IP, X86::FTST, 1).addReg(SrcReg); |
| 2066 | BMI(BB, IP, X86::FNSTSWr8, 0); |
| 2067 | BMI(BB, IP, X86::SAHF, 1); |
| 2068 | break; |
Chris Lattner | 2077254 | 2003-06-01 03:38:24 +0000 | [diff] [blame] | 2069 | } |
| 2070 | |
| 2071 | // If the zero flag is not set, then the value is true, set the byte to |
| 2072 | // true. |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 2073 | BMI(BB, IP, X86::SETNEr, 1, DestReg); |
Chris Lattner | 94af414 | 2002-12-25 05:13:53 +0000 | [diff] [blame] | 2074 | return; |
| 2075 | } |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2076 | |
| 2077 | static const unsigned RegRegMove[] = { |
| 2078 | X86::MOVrr8, X86::MOVrr16, X86::MOVrr32, X86::FpMOV, X86::MOVrr32 |
| 2079 | }; |
| 2080 | |
| 2081 | // Implement casts between values of the same type class (as determined by |
| 2082 | // getClass) by using a register-to-register move. |
| 2083 | if (SrcClass == DestClass) { |
| 2084 | if (SrcClass <= cInt || (SrcClass == cFP && SrcTy == DestTy)) { |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 2085 | BMI(BB, IP, RegRegMove[SrcClass], 1, DestReg).addReg(SrcReg); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2086 | } else if (SrcClass == cFP) { |
| 2087 | if (SrcTy == Type::FloatTy) { // double -> float |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 2088 | assert(DestTy == Type::DoubleTy && "Unknown cFP member!"); |
| 2089 | BMI(BB, IP, X86::FpMOV, 1, DestReg).addReg(SrcReg); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2090 | } else { // float -> double |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 2091 | assert(SrcTy == Type::DoubleTy && DestTy == Type::FloatTy && |
| 2092 | "Unknown cFP member!"); |
| 2093 | // Truncate from double to float by storing to memory as short, then |
| 2094 | // reading it back. |
| 2095 | unsigned FltAlign = TM.getTargetData().getFloatAlignment(); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2096 | int FrameIdx = F->getFrameInfo()->CreateStackObject(4, FltAlign); |
Alkis Evlogimenos | 8e475b8 | 2004-02-28 23:42:35 +0000 | [diff] [blame] | 2097 | addFrameReference(BMI(BB, IP, X86::FSTm32, 5), FrameIdx).addReg(SrcReg); |
| 2098 | addFrameReference(BMI(BB, IP, X86::FLDm32, 5, DestReg), FrameIdx); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2099 | } |
| 2100 | } else if (SrcClass == cLong) { |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 2101 | BMI(BB, IP, X86::MOVrr32, 1, DestReg).addReg(SrcReg); |
| 2102 | BMI(BB, IP, X86::MOVrr32, 1, DestReg+1).addReg(SrcReg+1); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2103 | } else { |
Chris Lattner | c53544a | 2003-05-12 20:16:58 +0000 | [diff] [blame] | 2104 | assert(0 && "Cannot handle this type of cast instruction!"); |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 2105 | abort(); |
Brian Gaeke | d474e9c | 2002-12-06 10:49:33 +0000 | [diff] [blame] | 2106 | } |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2107 | return; |
| 2108 | } |
| 2109 | |
| 2110 | // Handle cast of SMALLER int to LARGER int using a move with sign extension |
| 2111 | // or zero extension, depending on whether the source type was signed. |
| 2112 | if (SrcClass <= cInt && (DestClass <= cInt || DestClass == cLong) && |
| 2113 | SrcClass < DestClass) { |
| 2114 | bool isLong = DestClass == cLong; |
| 2115 | if (isLong) DestClass = cInt; |
| 2116 | |
| 2117 | static const unsigned Opc[][4] = { |
| 2118 | { X86::MOVSXr16r8, X86::MOVSXr32r8, X86::MOVSXr32r16, X86::MOVrr32 }, // s |
| 2119 | { X86::MOVZXr16r8, X86::MOVZXr32r8, X86::MOVZXr32r16, X86::MOVrr32 } // u |
| 2120 | }; |
| 2121 | |
| 2122 | bool isUnsigned = SrcTy->isUnsigned(); |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 2123 | BMI(BB, IP, Opc[isUnsigned][SrcClass + DestClass - 1], 1, |
| 2124 | DestReg).addReg(SrcReg); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2125 | |
| 2126 | if (isLong) { // Handle upper 32 bits as appropriate... |
| 2127 | if (isUnsigned) // Zero out top bits... |
Chris Lattner | 6e173a0 | 2004-02-17 06:16:44 +0000 | [diff] [blame] | 2128 | BMI(BB, IP, X86::MOVri32, 1, DestReg+1).addZImm(0); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2129 | else // Sign extend bottom half... |
Chris Lattner | 7ddc3fb | 2004-02-17 06:24:02 +0000 | [diff] [blame] | 2130 | BMI(BB, IP, X86::SARri32, 2, DestReg+1).addReg(DestReg).addZImm(31); |
Brian Gaeke | d474e9c | 2002-12-06 10:49:33 +0000 | [diff] [blame] | 2131 | } |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2132 | return; |
| 2133 | } |
| 2134 | |
| 2135 | // Special case long -> int ... |
| 2136 | if (SrcClass == cLong && DestClass == cInt) { |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 2137 | BMI(BB, IP, X86::MOVrr32, 1, DestReg).addReg(SrcReg); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2138 | return; |
| 2139 | } |
| 2140 | |
| 2141 | // Handle cast of LARGER int to SMALLER int using a move to EAX followed by a |
| 2142 | // move out of AX or AL. |
| 2143 | if ((SrcClass <= cInt || SrcClass == cLong) && DestClass <= cInt |
| 2144 | && SrcClass > DestClass) { |
| 2145 | 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] | 2146 | BMI(BB, IP, RegRegMove[SrcClass], 1, AReg[SrcClass]).addReg(SrcReg); |
| 2147 | BMI(BB, IP, RegRegMove[DestClass], 1, DestReg).addReg(AReg[DestClass]); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2148 | return; |
| 2149 | } |
| 2150 | |
| 2151 | // Handle casts from integer to floating point now... |
| 2152 | if (DestClass == cFP) { |
Chris Lattner | 4d5a50a | 2003-05-12 20:36:13 +0000 | [diff] [blame] | 2153 | // Promote the integer to a type supported by FLD. We do this because there |
| 2154 | // are no unsigned FLD instructions, so we must promote an unsigned value to |
| 2155 | // a larger signed value, then use FLD on the larger value. |
| 2156 | // |
| 2157 | const Type *PromoteType = 0; |
| 2158 | unsigned PromoteOpcode; |
Chris Lattner | baa58a5 | 2004-02-23 03:10:10 +0000 | [diff] [blame] | 2159 | unsigned RealDestReg = DestReg; |
Chris Lattner | 4d5a50a | 2003-05-12 20:36:13 +0000 | [diff] [blame] | 2160 | switch (SrcTy->getPrimitiveID()) { |
| 2161 | case Type::BoolTyID: |
| 2162 | case Type::SByteTyID: |
| 2163 | // We don't have the facilities for directly loading byte sized data from |
| 2164 | // memory (even signed). Promote it to 16 bits. |
| 2165 | PromoteType = Type::ShortTy; |
| 2166 | PromoteOpcode = X86::MOVSXr16r8; |
| 2167 | break; |
| 2168 | case Type::UByteTyID: |
| 2169 | PromoteType = Type::ShortTy; |
| 2170 | PromoteOpcode = X86::MOVZXr16r8; |
| 2171 | break; |
| 2172 | case Type::UShortTyID: |
| 2173 | PromoteType = Type::IntTy; |
| 2174 | PromoteOpcode = X86::MOVZXr32r16; |
| 2175 | break; |
| 2176 | case Type::UIntTyID: { |
| 2177 | // Make a 64 bit temporary... and zero out the top of it... |
| 2178 | unsigned TmpReg = makeAnotherReg(Type::LongTy); |
| 2179 | BMI(BB, IP, X86::MOVrr32, 1, TmpReg).addReg(SrcReg); |
Chris Lattner | 6e173a0 | 2004-02-17 06:16:44 +0000 | [diff] [blame] | 2180 | BMI(BB, IP, X86::MOVri32, 1, TmpReg+1).addZImm(0); |
Chris Lattner | 4d5a50a | 2003-05-12 20:36:13 +0000 | [diff] [blame] | 2181 | SrcTy = Type::LongTy; |
| 2182 | SrcClass = cLong; |
| 2183 | SrcReg = TmpReg; |
| 2184 | break; |
| 2185 | } |
| 2186 | case Type::ULongTyID: |
Chris Lattner | baa58a5 | 2004-02-23 03:10:10 +0000 | [diff] [blame] | 2187 | // Don't fild into the read destination. |
| 2188 | DestReg = makeAnotherReg(Type::DoubleTy); |
| 2189 | break; |
Chris Lattner | 4d5a50a | 2003-05-12 20:36:13 +0000 | [diff] [blame] | 2190 | default: // No promotion needed... |
| 2191 | break; |
| 2192 | } |
| 2193 | |
| 2194 | if (PromoteType) { |
| 2195 | unsigned TmpReg = makeAnotherReg(PromoteType); |
Chris Lattner | baa58a5 | 2004-02-23 03:10:10 +0000 | [diff] [blame] | 2196 | unsigned Opc = SrcTy->isSigned() ? X86::MOVSXr16r8 : X86::MOVZXr16r8; |
| 2197 | BMI(BB, IP, Opc, 1, TmpReg).addReg(SrcReg); |
Chris Lattner | 4d5a50a | 2003-05-12 20:36:13 +0000 | [diff] [blame] | 2198 | SrcTy = PromoteType; |
| 2199 | SrcClass = getClass(PromoteType); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2200 | SrcReg = TmpReg; |
| 2201 | } |
| 2202 | |
| 2203 | // Spill the integer to memory and reload it from there... |
Chris Lattner | b6bac51 | 2004-02-25 06:13:04 +0000 | [diff] [blame] | 2204 | int FrameIdx = |
| 2205 | F->getFrameInfo()->CreateStackObject(SrcTy, TM.getTargetData()); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2206 | |
| 2207 | if (SrcClass == cLong) { |
Chris Lattner | e87331d | 2004-02-17 06:28:19 +0000 | [diff] [blame] | 2208 | addFrameReference(BMI(BB, IP, X86::MOVmr32, 5), FrameIdx).addReg(SrcReg); |
| 2209 | addFrameReference(BMI(BB, IP, X86::MOVmr32, 5), |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 2210 | FrameIdx, 4).addReg(SrcReg+1); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2211 | } else { |
Chris Lattner | e87331d | 2004-02-17 06:28:19 +0000 | [diff] [blame] | 2212 | static const unsigned Op1[] = { X86::MOVmr8, X86::MOVmr16, X86::MOVmr32 }; |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 2213 | addFrameReference(BMI(BB, IP, Op1[SrcClass], 5), FrameIdx).addReg(SrcReg); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2214 | } |
| 2215 | |
| 2216 | static const unsigned Op2[] = |
Alkis Evlogimenos | 8e475b8 | 2004-02-28 23:42:35 +0000 | [diff] [blame] | 2217 | { 0/*byte*/, X86::FILDm16, X86::FILDm32, 0/*FP*/, X86::FILDm64 }; |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 2218 | addFrameReference(BMI(BB, IP, Op2[SrcClass], 5, DestReg), FrameIdx); |
Chris Lattner | baa58a5 | 2004-02-23 03:10:10 +0000 | [diff] [blame] | 2219 | |
| 2220 | // We need special handling for unsigned 64-bit integer sources. If the |
| 2221 | // input number has the "sign bit" set, then we loaded it incorrectly as a |
| 2222 | // negative 64-bit number. In this case, add an offset value. |
| 2223 | if (SrcTy == Type::ULongTy) { |
| 2224 | // Emit a test instruction to see if the dynamic input value was signed. |
| 2225 | BMI(BB, IP, X86::TESTrr32, 2).addReg(SrcReg+1).addReg(SrcReg+1); |
| 2226 | |
Chris Lattner | b6bac51 | 2004-02-25 06:13:04 +0000 | [diff] [blame] | 2227 | // If the sign bit is set, get a pointer to an offset, otherwise get a |
| 2228 | // pointer to a zero. |
Chris Lattner | baa58a5 | 2004-02-23 03:10:10 +0000 | [diff] [blame] | 2229 | MachineConstantPool *CP = F->getConstantPool(); |
| 2230 | unsigned Zero = makeAnotherReg(Type::IntTy); |
Chris Lattner | b6bac51 | 2004-02-25 06:13:04 +0000 | [diff] [blame] | 2231 | Constant *Null = Constant::getNullValue(Type::UIntTy); |
Chris Lattner | baa58a5 | 2004-02-23 03:10:10 +0000 | [diff] [blame] | 2232 | addConstantPoolReference(BMI(BB, IP, X86::LEAr32, 5, Zero), |
Chris Lattner | b6bac51 | 2004-02-25 06:13:04 +0000 | [diff] [blame] | 2233 | CP->getConstantPoolIndex(Null)); |
Chris Lattner | baa58a5 | 2004-02-23 03:10:10 +0000 | [diff] [blame] | 2234 | unsigned Offset = makeAnotherReg(Type::IntTy); |
Chris Lattner | b6bac51 | 2004-02-25 06:13:04 +0000 | [diff] [blame] | 2235 | Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000); |
| 2236 | |
Chris Lattner | baa58a5 | 2004-02-23 03:10:10 +0000 | [diff] [blame] | 2237 | addConstantPoolReference(BMI(BB, IP, X86::LEAr32, 5, Offset), |
Chris Lattner | b6bac51 | 2004-02-25 06:13:04 +0000 | [diff] [blame] | 2238 | CP->getConstantPoolIndex(OffsetCst)); |
Chris Lattner | baa58a5 | 2004-02-23 03:10:10 +0000 | [diff] [blame] | 2239 | unsigned Addr = makeAnotherReg(Type::IntTy); |
| 2240 | BMI(BB, IP, X86::CMOVSrr32, 2, Addr).addReg(Zero).addReg(Offset); |
| 2241 | |
| 2242 | // Load the constant for an add. FIXME: this could make an 'fadd' that |
| 2243 | // reads directly from memory, but we don't support these yet. |
| 2244 | unsigned ConstReg = makeAnotherReg(Type::DoubleTy); |
Alkis Evlogimenos | 8e475b8 | 2004-02-28 23:42:35 +0000 | [diff] [blame] | 2245 | addDirectMem(BMI(BB, IP, X86::FLDm32, 4, ConstReg), Addr); |
Chris Lattner | baa58a5 | 2004-02-23 03:10:10 +0000 | [diff] [blame] | 2246 | |
| 2247 | BMI(BB, IP, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(DestReg); |
| 2248 | } |
| 2249 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2250 | return; |
| 2251 | } |
| 2252 | |
| 2253 | // Handle casts from floating point to integer now... |
| 2254 | if (SrcClass == cFP) { |
| 2255 | // Change the floating point control register to use "round towards zero" |
| 2256 | // mode when truncating to an integer value. |
| 2257 | // |
| 2258 | int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2); |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 2259 | addFrameReference(BMI(BB, IP, X86::FNSTCWm16, 4), CWFrameIdx); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2260 | |
| 2261 | // Load the old value of the high byte of the control word... |
| 2262 | unsigned HighPartOfCW = makeAnotherReg(Type::UByteTy); |
Chris Lattner | e87331d | 2004-02-17 06:28:19 +0000 | [diff] [blame] | 2263 | addFrameReference(BMI(BB, IP, X86::MOVrm8, 4, HighPartOfCW), CWFrameIdx, 1); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2264 | |
| 2265 | // Set the high part to be round to zero... |
Chris Lattner | 6e173a0 | 2004-02-17 06:16:44 +0000 | [diff] [blame] | 2266 | addFrameReference(BMI(BB, IP, X86::MOVmi8, 5), CWFrameIdx, 1).addZImm(12); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2267 | |
| 2268 | // Reload the modified control word now... |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 2269 | addFrameReference(BMI(BB, IP, X86::FLDCWm16, 4), CWFrameIdx); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2270 | |
| 2271 | // Restore the memory image of control word to original value |
Chris Lattner | e87331d | 2004-02-17 06:28:19 +0000 | [diff] [blame] | 2272 | addFrameReference(BMI(BB, IP, X86::MOVmr8, 5), |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 2273 | CWFrameIdx, 1).addReg(HighPartOfCW); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2274 | |
| 2275 | // We don't have the facilities for directly storing byte sized data to |
| 2276 | // memory. Promote it to 16 bits. We also must promote unsigned values to |
| 2277 | // larger classes because we only have signed FP stores. |
| 2278 | unsigned StoreClass = DestClass; |
| 2279 | const Type *StoreTy = DestTy; |
| 2280 | if (StoreClass == cByte || DestTy->isUnsigned()) |
| 2281 | switch (StoreClass) { |
| 2282 | case cByte: StoreTy = Type::ShortTy; StoreClass = cShort; break; |
| 2283 | case cShort: StoreTy = Type::IntTy; StoreClass = cInt; break; |
| 2284 | case cInt: StoreTy = Type::LongTy; StoreClass = cLong; break; |
Brian Gaeke | d461505 | 2003-07-18 20:23:43 +0000 | [diff] [blame] | 2285 | // The following treatment of cLong may not be perfectly right, |
| 2286 | // but it survives chains of casts of the form |
| 2287 | // double->ulong->double. |
| 2288 | case cLong: StoreTy = Type::LongTy; StoreClass = cLong; break; |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2289 | default: assert(0 && "Unknown store class!"); |
| 2290 | } |
| 2291 | |
| 2292 | // Spill the integer to memory and reload it from there... |
| 2293 | int FrameIdx = |
| 2294 | F->getFrameInfo()->CreateStackObject(StoreTy, TM.getTargetData()); |
| 2295 | |
| 2296 | static const unsigned Op1[] = |
Alkis Evlogimenos | 8e475b8 | 2004-02-28 23:42:35 +0000 | [diff] [blame] | 2297 | { 0, X86::FISTm16, X86::FISTm32, 0, X86::FISTPm64 }; |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 2298 | addFrameReference(BMI(BB, IP, Op1[StoreClass], 5), FrameIdx).addReg(SrcReg); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2299 | |
| 2300 | if (DestClass == cLong) { |
Chris Lattner | e87331d | 2004-02-17 06:28:19 +0000 | [diff] [blame] | 2301 | addFrameReference(BMI(BB, IP, X86::MOVrm32, 4, DestReg), FrameIdx); |
| 2302 | addFrameReference(BMI(BB, IP, X86::MOVrm32, 4, DestReg+1), FrameIdx, 4); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2303 | } else { |
Chris Lattner | e87331d | 2004-02-17 06:28:19 +0000 | [diff] [blame] | 2304 | static const unsigned Op2[] = { X86::MOVrm8, X86::MOVrm16, X86::MOVrm32 }; |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 2305 | addFrameReference(BMI(BB, IP, Op2[DestClass], 4, DestReg), FrameIdx); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2306 | } |
| 2307 | |
| 2308 | // Reload the original control word now... |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 2309 | addFrameReference(BMI(BB, IP, X86::FLDCWm16, 4), CWFrameIdx); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2310 | return; |
| 2311 | } |
| 2312 | |
Brian Gaeke | d474e9c | 2002-12-06 10:49:33 +0000 | [diff] [blame] | 2313 | // 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] | 2314 | assert(0 && "Unhandled cast instruction!"); |
Chris Lattner | 548f61d | 2003-04-23 17:22:12 +0000 | [diff] [blame] | 2315 | abort(); |
Brian Gaeke | fa8d571 | 2002-11-22 11:07:01 +0000 | [diff] [blame] | 2316 | } |
Brian Gaeke | a1719c9 | 2002-10-31 23:03:59 +0000 | [diff] [blame] | 2317 | |
Chris Lattner | 7381506 | 2003-10-18 05:56:40 +0000 | [diff] [blame] | 2318 | /// visitVANextInst - Implement the va_next instruction... |
Chris Lattner | eca195e | 2003-05-08 19:44:13 +0000 | [diff] [blame] | 2319 | /// |
Chris Lattner | 7381506 | 2003-10-18 05:56:40 +0000 | [diff] [blame] | 2320 | void ISel::visitVANextInst(VANextInst &I) { |
| 2321 | unsigned VAList = getReg(I.getOperand(0)); |
Chris Lattner | eca195e | 2003-05-08 19:44:13 +0000 | [diff] [blame] | 2322 | unsigned DestReg = getReg(I); |
| 2323 | |
Chris Lattner | eca195e | 2003-05-08 19:44:13 +0000 | [diff] [blame] | 2324 | unsigned Size; |
Chris Lattner | 7381506 | 2003-10-18 05:56:40 +0000 | [diff] [blame] | 2325 | switch (I.getArgType()->getPrimitiveID()) { |
Chris Lattner | eca195e | 2003-05-08 19:44:13 +0000 | [diff] [blame] | 2326 | default: |
| 2327 | std::cerr << I; |
Chris Lattner | 7381506 | 2003-10-18 05:56:40 +0000 | [diff] [blame] | 2328 | assert(0 && "Error: bad type for va_next instruction!"); |
Chris Lattner | eca195e | 2003-05-08 19:44:13 +0000 | [diff] [blame] | 2329 | return; |
| 2330 | case Type::PointerTyID: |
| 2331 | case Type::UIntTyID: |
| 2332 | case Type::IntTyID: |
| 2333 | Size = 4; |
Chris Lattner | eca195e | 2003-05-08 19:44:13 +0000 | [diff] [blame] | 2334 | break; |
| 2335 | case Type::ULongTyID: |
| 2336 | case Type::LongTyID: |
Chris Lattner | eca195e | 2003-05-08 19:44:13 +0000 | [diff] [blame] | 2337 | case Type::DoubleTyID: |
| 2338 | Size = 8; |
Chris Lattner | eca195e | 2003-05-08 19:44:13 +0000 | [diff] [blame] | 2339 | break; |
| 2340 | } |
| 2341 | |
| 2342 | // Increment the VAList pointer... |
Chris Lattner | 7381506 | 2003-10-18 05:56:40 +0000 | [diff] [blame] | 2343 | BuildMI(BB, X86::ADDri32, 2, DestReg).addReg(VAList).addZImm(Size); |
| 2344 | } |
Chris Lattner | eca195e | 2003-05-08 19:44:13 +0000 | [diff] [blame] | 2345 | |
Chris Lattner | 7381506 | 2003-10-18 05:56:40 +0000 | [diff] [blame] | 2346 | void ISel::visitVAArgInst(VAArgInst &I) { |
| 2347 | unsigned VAList = getReg(I.getOperand(0)); |
| 2348 | unsigned DestReg = getReg(I); |
| 2349 | |
| 2350 | switch (I.getType()->getPrimitiveID()) { |
| 2351 | default: |
| 2352 | std::cerr << I; |
| 2353 | assert(0 && "Error: bad type for va_next instruction!"); |
| 2354 | return; |
| 2355 | case Type::PointerTyID: |
| 2356 | case Type::UIntTyID: |
| 2357 | case Type::IntTyID: |
Chris Lattner | e87331d | 2004-02-17 06:28:19 +0000 | [diff] [blame] | 2358 | addDirectMem(BuildMI(BB, X86::MOVrm32, 4, DestReg), VAList); |
Chris Lattner | 7381506 | 2003-10-18 05:56:40 +0000 | [diff] [blame] | 2359 | break; |
| 2360 | case Type::ULongTyID: |
| 2361 | case Type::LongTyID: |
Chris Lattner | e87331d | 2004-02-17 06:28:19 +0000 | [diff] [blame] | 2362 | addDirectMem(BuildMI(BB, X86::MOVrm32, 4, DestReg), VAList); |
| 2363 | addRegOffset(BuildMI(BB, X86::MOVrm32, 4, DestReg+1), VAList, 4); |
Chris Lattner | 7381506 | 2003-10-18 05:56:40 +0000 | [diff] [blame] | 2364 | break; |
| 2365 | case Type::DoubleTyID: |
Alkis Evlogimenos | 8e475b8 | 2004-02-28 23:42:35 +0000 | [diff] [blame] | 2366 | addDirectMem(BuildMI(BB, X86::FLDm64, 4, DestReg), VAList); |
Chris Lattner | 7381506 | 2003-10-18 05:56:40 +0000 | [diff] [blame] | 2367 | break; |
| 2368 | } |
Chris Lattner | eca195e | 2003-05-08 19:44:13 +0000 | [diff] [blame] | 2369 | } |
| 2370 | |
| 2371 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2372 | void ISel::visitGetElementPtrInst(GetElementPtrInst &I) { |
Chris Lattner | b6bac51 | 2004-02-25 06:13:04 +0000 | [diff] [blame] | 2373 | // If this GEP instruction will be folded into all of its users, we don't need |
| 2374 | // to explicitly calculate it! |
| 2375 | unsigned A, B, C, D; |
| 2376 | if (isGEPFoldable(0, I.getOperand(0), I.op_begin()+1, I.op_end(), A,B,C,D)) { |
| 2377 | // Check all of the users of the instruction to see if they are loads and |
| 2378 | // stores. |
| 2379 | bool AllWillFold = true; |
| 2380 | for (Value::use_iterator UI = I.use_begin(), E = I.use_end(); UI != E; ++UI) |
| 2381 | if (cast<Instruction>(*UI)->getOpcode() != Instruction::Load) |
| 2382 | if (cast<Instruction>(*UI)->getOpcode() != Instruction::Store || |
| 2383 | cast<Instruction>(*UI)->getOperand(0) == &I) { |
| 2384 | AllWillFold = false; |
| 2385 | break; |
| 2386 | } |
| 2387 | |
| 2388 | // If the instruction is foldable, and will be folded into all users, don't |
| 2389 | // emit it! |
| 2390 | if (AllWillFold) return; |
| 2391 | } |
| 2392 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2393 | unsigned outputReg = getReg(I); |
Chris Lattner | 827832c | 2004-02-22 17:05:38 +0000 | [diff] [blame] | 2394 | emitGEPOperation(BB, BB->end(), I.getOperand(0), |
Brian Gaeke | 68b1edc | 2002-12-16 04:23:29 +0000 | [diff] [blame] | 2395 | I.op_begin()+1, I.op_end(), outputReg); |
Chris Lattner | c0812d8 | 2002-12-13 06:56:29 +0000 | [diff] [blame] | 2396 | } |
| 2397 | |
Chris Lattner | 985fe3d | 2004-02-25 03:45:50 +0000 | [diff] [blame] | 2398 | /// getGEPIndex - Inspect the getelementptr operands specified with GEPOps and |
| 2399 | /// GEPTypes (the derived types being stepped through at each level). On return |
| 2400 | /// from this function, if some indexes of the instruction are representable as |
| 2401 | /// an X86 lea instruction, the machine operands are put into the Ops |
| 2402 | /// instruction and the consumed indexes are poped from the GEPOps/GEPTypes |
| 2403 | /// lists. Otherwise, GEPOps.size() is returned. If this returns a an |
| 2404 | /// addressing mode that only partially consumes the input, the BaseReg input of |
| 2405 | /// the addressing mode must be left free. |
| 2406 | /// |
| 2407 | /// Note that there is one fewer entry in GEPTypes than there is in GEPOps. |
| 2408 | /// |
Chris Lattner | b6bac51 | 2004-02-25 06:13:04 +0000 | [diff] [blame] | 2409 | void ISel::getGEPIndex(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP, |
| 2410 | std::vector<Value*> &GEPOps, |
| 2411 | std::vector<const Type*> &GEPTypes, unsigned &BaseReg, |
| 2412 | unsigned &Scale, unsigned &IndexReg, unsigned &Disp) { |
| 2413 | const TargetData &TD = TM.getTargetData(); |
| 2414 | |
Chris Lattner | 985fe3d | 2004-02-25 03:45:50 +0000 | [diff] [blame] | 2415 | // Clear out the state we are working with... |
Chris Lattner | b6bac51 | 2004-02-25 06:13:04 +0000 | [diff] [blame] | 2416 | BaseReg = 0; // No base register |
| 2417 | Scale = 1; // Unit scale |
| 2418 | IndexReg = 0; // No index register |
| 2419 | Disp = 0; // No displacement |
| 2420 | |
Chris Lattner | 985fe3d | 2004-02-25 03:45:50 +0000 | [diff] [blame] | 2421 | // While there are GEP indexes that can be folded into the current address, |
| 2422 | // keep processing them. |
| 2423 | while (!GEPTypes.empty()) { |
| 2424 | if (const StructType *StTy = dyn_cast<StructType>(GEPTypes.back())) { |
| 2425 | // It's a struct access. CUI is the index into the structure, |
| 2426 | // which names the field. This index must have unsigned type. |
| 2427 | const ConstantUInt *CUI = cast<ConstantUInt>(GEPOps.back()); |
| 2428 | |
| 2429 | // Use the TargetData structure to pick out what the layout of the |
| 2430 | // structure is in memory. Since the structure index must be constant, we |
| 2431 | // can get its value and use it to find the right byte offset from the |
| 2432 | // StructLayout class's list of structure member offsets. |
Chris Lattner | b6bac51 | 2004-02-25 06:13:04 +0000 | [diff] [blame] | 2433 | Disp += TD.getStructLayout(StTy)->MemberOffsets[CUI->getValue()]; |
Chris Lattner | 985fe3d | 2004-02-25 03:45:50 +0000 | [diff] [blame] | 2434 | GEPOps.pop_back(); // Consume a GEP operand |
| 2435 | GEPTypes.pop_back(); |
| 2436 | } else { |
| 2437 | // It's an array or pointer access: [ArraySize x ElementType]. |
| 2438 | const SequentialType *SqTy = cast<SequentialType>(GEPTypes.back()); |
| 2439 | Value *idx = GEPOps.back(); |
| 2440 | |
| 2441 | // idx is the index into the array. Unlike with structure |
| 2442 | // indices, we may not know its actual value at code-generation |
| 2443 | // time. |
| 2444 | assert(idx->getType() == Type::LongTy && "Bad GEP array index!"); |
| 2445 | |
| 2446 | // If idx is a constant, fold it into the offset. |
Chris Lattner | 5f2c7b1 | 2004-02-25 07:00:55 +0000 | [diff] [blame] | 2447 | unsigned TypeSize = TD.getTypeSize(SqTy->getElementType()); |
Chris Lattner | 985fe3d | 2004-02-25 03:45:50 +0000 | [diff] [blame] | 2448 | if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(idx)) { |
Chris Lattner | 5f2c7b1 | 2004-02-25 07:00:55 +0000 | [diff] [blame] | 2449 | Disp += TypeSize*CSI->getValue(); |
Chris Lattner | 985fe3d | 2004-02-25 03:45:50 +0000 | [diff] [blame] | 2450 | } else { |
Chris Lattner | 5f2c7b1 | 2004-02-25 07:00:55 +0000 | [diff] [blame] | 2451 | // If the index reg is already taken, we can't handle this index. |
| 2452 | if (IndexReg) return; |
| 2453 | |
| 2454 | // If this is a size that we can handle, then add the index as |
| 2455 | switch (TypeSize) { |
| 2456 | case 1: case 2: case 4: case 8: |
| 2457 | // These are all acceptable scales on X86. |
| 2458 | Scale = TypeSize; |
| 2459 | break; |
| 2460 | default: |
| 2461 | // Otherwise, we can't handle this scale |
| 2462 | return; |
| 2463 | } |
| 2464 | |
| 2465 | if (CastInst *CI = dyn_cast<CastInst>(idx)) |
| 2466 | if (CI->getOperand(0)->getType() == Type::IntTy || |
| 2467 | CI->getOperand(0)->getType() == Type::UIntTy) |
| 2468 | idx = CI->getOperand(0); |
| 2469 | |
| 2470 | IndexReg = MBB ? getReg(idx, MBB, IP) : 1; |
Chris Lattner | 985fe3d | 2004-02-25 03:45:50 +0000 | [diff] [blame] | 2471 | } |
| 2472 | |
| 2473 | GEPOps.pop_back(); // Consume a GEP operand |
| 2474 | GEPTypes.pop_back(); |
| 2475 | } |
| 2476 | } |
Chris Lattner | b6bac51 | 2004-02-25 06:13:04 +0000 | [diff] [blame] | 2477 | |
| 2478 | // GEPTypes is empty, which means we have a single operand left. See if we |
| 2479 | // can set it as the base register. |
| 2480 | // |
| 2481 | // FIXME: When addressing modes are more powerful/correct, we could load |
| 2482 | // global addresses directly as 32-bit immediates. |
| 2483 | assert(BaseReg == 0); |
Chris Lattner | 5f2c7b1 | 2004-02-25 07:00:55 +0000 | [diff] [blame] | 2484 | BaseReg = MBB ? getReg(GEPOps[0], MBB, IP) : 1; |
Chris Lattner | b6bac51 | 2004-02-25 06:13:04 +0000 | [diff] [blame] | 2485 | GEPOps.pop_back(); // Consume the last GEP operand |
Chris Lattner | 985fe3d | 2004-02-25 03:45:50 +0000 | [diff] [blame] | 2486 | } |
| 2487 | |
| 2488 | |
Chris Lattner | b6bac51 | 2004-02-25 06:13:04 +0000 | [diff] [blame] | 2489 | /// isGEPFoldable - Return true if the specified GEP can be completely |
| 2490 | /// folded into the addressing mode of a load/store or lea instruction. |
| 2491 | bool ISel::isGEPFoldable(MachineBasicBlock *MBB, |
| 2492 | Value *Src, User::op_iterator IdxBegin, |
| 2493 | User::op_iterator IdxEnd, unsigned &BaseReg, |
| 2494 | unsigned &Scale, unsigned &IndexReg, unsigned &Disp) { |
Chris Lattner | 7ca0409 | 2004-02-22 17:35:42 +0000 | [diff] [blame] | 2495 | if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Src)) |
| 2496 | Src = CPR->getValue(); |
| 2497 | |
Chris Lattner | 3f1e8e7 | 2004-02-22 07:04:00 +0000 | [diff] [blame] | 2498 | std::vector<Value*> GEPOps; |
| 2499 | GEPOps.resize(IdxEnd-IdxBegin+1); |
| 2500 | GEPOps[0] = Src; |
| 2501 | std::copy(IdxBegin, IdxEnd, GEPOps.begin()+1); |
| 2502 | |
| 2503 | std::vector<const Type*> GEPTypes; |
| 2504 | GEPTypes.assign(gep_type_begin(Src->getType(), IdxBegin, IdxEnd), |
| 2505 | gep_type_end(Src->getType(), IdxBegin, IdxEnd)); |
| 2506 | |
Chris Lattner | b6bac51 | 2004-02-25 06:13:04 +0000 | [diff] [blame] | 2507 | MachineBasicBlock::iterator IP; |
| 2508 | if (MBB) IP = MBB->end(); |
| 2509 | getGEPIndex(MBB, IP, GEPOps, GEPTypes, BaseReg, Scale, IndexReg, Disp); |
| 2510 | |
| 2511 | // We can fold it away iff the getGEPIndex call eliminated all operands. |
| 2512 | return GEPOps.empty(); |
| 2513 | } |
| 2514 | |
| 2515 | void ISel::emitGEPOperation(MachineBasicBlock *MBB, |
| 2516 | MachineBasicBlock::iterator IP, |
| 2517 | Value *Src, User::op_iterator IdxBegin, |
| 2518 | User::op_iterator IdxEnd, unsigned TargetReg) { |
| 2519 | const TargetData &TD = TM.getTargetData(); |
| 2520 | if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Src)) |
| 2521 | Src = CPR->getValue(); |
| 2522 | |
| 2523 | std::vector<Value*> GEPOps; |
| 2524 | GEPOps.resize(IdxEnd-IdxBegin+1); |
| 2525 | GEPOps[0] = Src; |
| 2526 | std::copy(IdxBegin, IdxEnd, GEPOps.begin()+1); |
| 2527 | |
| 2528 | std::vector<const Type*> GEPTypes; |
| 2529 | GEPTypes.assign(gep_type_begin(Src->getType(), IdxBegin, IdxEnd), |
| 2530 | gep_type_end(Src->getType(), IdxBegin, IdxEnd)); |
Chris Lattner | 985fe3d | 2004-02-25 03:45:50 +0000 | [diff] [blame] | 2531 | |
Chris Lattner | 3f1e8e7 | 2004-02-22 07:04:00 +0000 | [diff] [blame] | 2532 | // Keep emitting instructions until we consume the entire GEP instruction. |
| 2533 | while (!GEPOps.empty()) { |
| 2534 | unsigned OldSize = GEPOps.size(); |
Chris Lattner | b6bac51 | 2004-02-25 06:13:04 +0000 | [diff] [blame] | 2535 | unsigned BaseReg, Scale, IndexReg, Disp; |
| 2536 | getGEPIndex(MBB, IP, GEPOps, GEPTypes, BaseReg, Scale, IndexReg, Disp); |
Chris Lattner | 3f1e8e7 | 2004-02-22 07:04:00 +0000 | [diff] [blame] | 2537 | |
Chris Lattner | 985fe3d | 2004-02-25 03:45:50 +0000 | [diff] [blame] | 2538 | if (GEPOps.size() != OldSize) { |
| 2539 | // getGEPIndex consumed some of the input. Build an LEA instruction here. |
Chris Lattner | b6bac51 | 2004-02-25 06:13:04 +0000 | [diff] [blame] | 2540 | unsigned NextTarget = 0; |
| 2541 | if (!GEPOps.empty()) { |
| 2542 | assert(BaseReg == 0 && |
| 2543 | "getGEPIndex should have left the base register open for chaining!"); |
| 2544 | NextTarget = BaseReg = makeAnotherReg(Type::UIntTy); |
Chris Lattner | 985fe3d | 2004-02-25 03:45:50 +0000 | [diff] [blame] | 2545 | } |
Chris Lattner | b6bac51 | 2004-02-25 06:13:04 +0000 | [diff] [blame] | 2546 | |
| 2547 | if (IndexReg == 0 && Disp == 0) |
| 2548 | BMI(MBB, IP, X86::MOVrr32, 1, TargetReg).addReg(BaseReg); |
| 2549 | else |
| 2550 | addFullAddress(BMI(MBB, IP, X86::LEAr32, 5, TargetReg), |
| 2551 | BaseReg, Scale, IndexReg, Disp); |
| 2552 | --IP; |
| 2553 | TargetReg = NextTarget; |
Chris Lattner | 985fe3d | 2004-02-25 03:45:50 +0000 | [diff] [blame] | 2554 | } else if (GEPTypes.empty()) { |
Chris Lattner | 3f1e8e7 | 2004-02-22 07:04:00 +0000 | [diff] [blame] | 2555 | // The getGEPIndex operation didn't want to build an LEA. Check to see if |
| 2556 | // all operands are consumed but the base pointer. If so, just load it |
| 2557 | // into the register. |
Chris Lattner | 7ca0409 | 2004-02-22 17:35:42 +0000 | [diff] [blame] | 2558 | if (GlobalValue *GV = dyn_cast<GlobalValue>(GEPOps[0])) { |
| 2559 | BMI(MBB, IP, X86::MOVri32, 1, TargetReg).addGlobalAddress(GV); |
| 2560 | } else { |
| 2561 | unsigned BaseReg = getReg(GEPOps[0], MBB, IP); |
| 2562 | BMI(MBB, IP, X86::MOVrr32, 1, TargetReg).addReg(BaseReg); |
| 2563 | } |
| 2564 | break; // we are now done |
Chris Lattner | b6bac51 | 2004-02-25 06:13:04 +0000 | [diff] [blame] | 2565 | |
Chris Lattner | 3f1e8e7 | 2004-02-22 07:04:00 +0000 | [diff] [blame] | 2566 | } else { |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 2567 | // It's an array or pointer access: [ArraySize x ElementType]. |
Chris Lattner | 3f1e8e7 | 2004-02-22 07:04:00 +0000 | [diff] [blame] | 2568 | const SequentialType *SqTy = cast<SequentialType>(GEPTypes.back()); |
| 2569 | Value *idx = GEPOps.back(); |
| 2570 | GEPOps.pop_back(); // Consume a GEP operand |
| 2571 | GEPTypes.pop_back(); |
Chris Lattner | 8a307e8 | 2002-12-16 19:32:50 +0000 | [diff] [blame] | 2572 | |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 2573 | // idx is the index into the array. Unlike with structure |
| 2574 | // indices, we may not know its actual value at code-generation |
| 2575 | // time. |
Chris Lattner | 8a307e8 | 2002-12-16 19:32:50 +0000 | [diff] [blame] | 2576 | assert(idx->getType() == Type::LongTy && "Bad GEP array index!"); |
| 2577 | |
Chris Lattner | f585447 | 2003-06-21 16:01:24 +0000 | [diff] [blame] | 2578 | // Most GEP instructions use a [cast (int/uint) to LongTy] as their |
| 2579 | // operand on X86. Handle this case directly now... |
| 2580 | if (CastInst *CI = dyn_cast<CastInst>(idx)) |
| 2581 | if (CI->getOperand(0)->getType() == Type::IntTy || |
| 2582 | CI->getOperand(0)->getType() == Type::UIntTy) |
| 2583 | idx = CI->getOperand(0); |
| 2584 | |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2585 | // We want to add BaseReg to(idxReg * sizeof ElementType). First, we |
Chris Lattner | 8a307e8 | 2002-12-16 19:32:50 +0000 | [diff] [blame] | 2586 | // must find the size of the pointed-to type (Not coincidentally, the next |
| 2587 | // type is the type of the elements in the array). |
Chris Lattner | 3f1e8e7 | 2004-02-22 07:04:00 +0000 | [diff] [blame] | 2588 | const Type *ElTy = SqTy->getElementType(); |
| 2589 | unsigned elementSize = TD.getTypeSize(ElTy); |
Chris Lattner | 8a307e8 | 2002-12-16 19:32:50 +0000 | [diff] [blame] | 2590 | |
| 2591 | // If idxReg is a constant, we don't need to perform the multiply! |
| 2592 | if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(idx)) { |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2593 | if (!CSI->isNullValue()) { |
Chris Lattner | 8a307e8 | 2002-12-16 19:32:50 +0000 | [diff] [blame] | 2594 | unsigned Offset = elementSize*CSI->getValue(); |
Chris Lattner | 3f1e8e7 | 2004-02-22 07:04:00 +0000 | [diff] [blame] | 2595 | unsigned Reg = makeAnotherReg(Type::UIntTy); |
| 2596 | BMI(MBB, IP, X86::ADDri32, 2, TargetReg).addReg(Reg).addZImm(Offset); |
| 2597 | --IP; // Insert the next instruction before this one. |
| 2598 | TargetReg = Reg; // Codegen the rest of the GEP into this |
Chris Lattner | 8a307e8 | 2002-12-16 19:32:50 +0000 | [diff] [blame] | 2599 | } |
| 2600 | } else if (elementSize == 1) { |
| 2601 | // If the element size is 1, we don't have to multiply, just add |
| 2602 | unsigned idxReg = getReg(idx, MBB, IP); |
Chris Lattner | 3f1e8e7 | 2004-02-22 07:04:00 +0000 | [diff] [blame] | 2603 | unsigned Reg = makeAnotherReg(Type::UIntTy); |
| 2604 | BMI(MBB, IP, X86::ADDrr32, 2, TargetReg).addReg(Reg).addReg(idxReg); |
| 2605 | --IP; // Insert the next instruction before this one. |
| 2606 | TargetReg = Reg; // Codegen the rest of the GEP into this |
Chris Lattner | 8a307e8 | 2002-12-16 19:32:50 +0000 | [diff] [blame] | 2607 | } else { |
| 2608 | unsigned idxReg = getReg(idx, MBB, IP); |
| 2609 | unsigned OffsetReg = makeAnotherReg(Type::UIntTy); |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 2610 | |
Chris Lattner | 3f1e8e7 | 2004-02-22 07:04:00 +0000 | [diff] [blame] | 2611 | // Make sure we can back the iterator up to point to the first |
| 2612 | // instruction emitted. |
| 2613 | MachineBasicBlock::iterator BeforeIt = IP; |
| 2614 | if (IP == MBB->begin()) |
| 2615 | BeforeIt = MBB->end(); |
| 2616 | else |
| 2617 | --BeforeIt; |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 2618 | doMultiplyConst(MBB, IP, OffsetReg, Type::IntTy, idxReg, elementSize); |
| 2619 | |
Chris Lattner | 8a307e8 | 2002-12-16 19:32:50 +0000 | [diff] [blame] | 2620 | // Emit an ADD to add OffsetReg to the basePtr. |
Chris Lattner | 3f1e8e7 | 2004-02-22 07:04:00 +0000 | [diff] [blame] | 2621 | unsigned Reg = makeAnotherReg(Type::UIntTy); |
| 2622 | BMI(MBB, IP, X86::ADDrr32, 2, TargetReg).addReg(Reg).addReg(OffsetReg); |
| 2623 | |
| 2624 | // Step to the first instruction of the multiply. |
| 2625 | if (BeforeIt == MBB->end()) |
| 2626 | IP = MBB->begin(); |
| 2627 | else |
| 2628 | IP = ++BeforeIt; |
| 2629 | |
| 2630 | TargetReg = Reg; // Codegen the rest of the GEP into this |
Chris Lattner | 8a307e8 | 2002-12-16 19:32:50 +0000 | [diff] [blame] | 2631 | } |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 2632 | } |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 2633 | } |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 2634 | } |
| 2635 | |
| 2636 | |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 2637 | /// visitAllocaInst - If this is a fixed size alloca, allocate space from the |
| 2638 | /// frame manager, otherwise do it the hard way. |
| 2639 | /// |
| 2640 | void ISel::visitAllocaInst(AllocaInst &I) { |
Brian Gaeke | e48ec01 | 2002-12-13 06:46:31 +0000 | [diff] [blame] | 2641 | // Find the data size of the alloca inst's getAllocatedType. |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 2642 | const Type *Ty = I.getAllocatedType(); |
| 2643 | unsigned TySize = TM.getTargetData().getTypeSize(Ty); |
| 2644 | |
| 2645 | // If this is a fixed size alloca in the entry block for the function, |
| 2646 | // statically stack allocate the space. |
| 2647 | // |
| 2648 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(I.getArraySize())) { |
| 2649 | if (I.getParent() == I.getParent()->getParent()->begin()) { |
| 2650 | TySize *= CUI->getValue(); // Get total allocated size... |
| 2651 | unsigned Alignment = TM.getTargetData().getTypeAlignment(Ty); |
| 2652 | |
| 2653 | // Create a new stack object using the frame manager... |
| 2654 | int FrameIdx = F->getFrameInfo()->CreateStackObject(TySize, Alignment); |
| 2655 | addFrameReference(BuildMI(BB, X86::LEAr32, 5, getReg(I)), FrameIdx); |
| 2656 | return; |
| 2657 | } |
| 2658 | } |
| 2659 | |
| 2660 | // Create a register to hold the temporary result of multiplying the type size |
| 2661 | // constant by the variable amount. |
| 2662 | unsigned TotalSizeReg = makeAnotherReg(Type::UIntTy); |
| 2663 | unsigned SrcReg1 = getReg(I.getArraySize()); |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 2664 | |
| 2665 | // TotalSizeReg = mul <numelements>, <TypeSize> |
| 2666 | MachineBasicBlock::iterator MBBI = BB->end(); |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 2667 | doMultiplyConst(BB, MBBI, TotalSizeReg, Type::UIntTy, SrcReg1, TySize); |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 2668 | |
| 2669 | // AddedSize = add <TotalSizeReg>, 15 |
| 2670 | unsigned AddedSizeReg = makeAnotherReg(Type::UIntTy); |
| 2671 | BuildMI(BB, X86::ADDri32, 2, AddedSizeReg).addReg(TotalSizeReg).addZImm(15); |
| 2672 | |
| 2673 | // AlignedSize = and <AddedSize>, ~15 |
| 2674 | unsigned AlignedSize = makeAnotherReg(Type::UIntTy); |
| 2675 | BuildMI(BB, X86::ANDri32, 2, AlignedSize).addReg(AddedSizeReg).addZImm(~15); |
| 2676 | |
Brian Gaeke | e48ec01 | 2002-12-13 06:46:31 +0000 | [diff] [blame] | 2677 | // Subtract size from stack pointer, thereby allocating some space. |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2678 | BuildMI(BB, X86::SUBrr32, 2, X86::ESP).addReg(X86::ESP).addReg(AlignedSize); |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 2679 | |
Brian Gaeke | e48ec01 | 2002-12-13 06:46:31 +0000 | [diff] [blame] | 2680 | // Put a pointer to the space into the result register, by copying |
| 2681 | // the stack pointer. |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 2682 | BuildMI(BB, X86::MOVrr32, 1, getReg(I)).addReg(X86::ESP); |
| 2683 | |
Misha Brukman | 48196b3 | 2003-05-03 02:18:17 +0000 | [diff] [blame] | 2684 | // Inform the Frame Information that we have just allocated a variable-sized |
Chris Lattner | 065faeb | 2002-12-28 20:24:02 +0000 | [diff] [blame] | 2685 | // object. |
| 2686 | F->getFrameInfo()->CreateVariableSizedObject(); |
Brian Gaeke | 20244b7 | 2002-12-12 15:33:40 +0000 | [diff] [blame] | 2687 | } |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2688 | |
| 2689 | /// visitMallocInst - Malloc instructions are code generated into direct calls |
| 2690 | /// to the library malloc. |
| 2691 | /// |
| 2692 | void ISel::visitMallocInst(MallocInst &I) { |
| 2693 | unsigned AllocSize = TM.getTargetData().getTypeSize(I.getAllocatedType()); |
| 2694 | unsigned Arg; |
| 2695 | |
| 2696 | if (ConstantUInt *C = dyn_cast<ConstantUInt>(I.getOperand(0))) { |
| 2697 | Arg = getReg(ConstantUInt::get(Type::UIntTy, C->getValue() * AllocSize)); |
| 2698 | } else { |
| 2699 | Arg = makeAnotherReg(Type::UIntTy); |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 2700 | unsigned Op0Reg = getReg(I.getOperand(0)); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2701 | MachineBasicBlock::iterator MBBI = BB->end(); |
Chris Lattner | b2acc51 | 2003-10-19 21:09:10 +0000 | [diff] [blame] | 2702 | doMultiplyConst(BB, MBBI, Arg, Type::UIntTy, Op0Reg, AllocSize); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2703 | } |
| 2704 | |
| 2705 | std::vector<ValueRecord> Args; |
| 2706 | Args.push_back(ValueRecord(Arg, Type::UIntTy)); |
| 2707 | MachineInstr *TheCall = BuildMI(X86::CALLpcrel32, |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 2708 | 1).addExternalSymbol("malloc", true); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2709 | doCall(ValueRecord(getReg(I), I.getType()), TheCall, Args); |
| 2710 | } |
| 2711 | |
| 2712 | |
| 2713 | /// visitFreeInst - Free instructions are code gen'd to call the free libc |
| 2714 | /// function. |
| 2715 | /// |
| 2716 | void ISel::visitFreeInst(FreeInst &I) { |
| 2717 | std::vector<ValueRecord> Args; |
Chris Lattner | 5e2cb8b | 2003-08-04 02:12:48 +0000 | [diff] [blame] | 2718 | Args.push_back(ValueRecord(I.getOperand(0))); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2719 | MachineInstr *TheCall = BuildMI(X86::CALLpcrel32, |
Misha Brukman | c8893fc | 2003-10-23 16:22:08 +0000 | [diff] [blame] | 2720 | 1).addExternalSymbol("free", true); |
Chris Lattner | 3e130a2 | 2003-01-13 00:32:26 +0000 | [diff] [blame] | 2721 | doCall(ValueRecord(0, Type::VoidTy), TheCall, Args); |
| 2722 | } |
| 2723 | |
Chris Lattner | d281de2 | 2003-07-26 23:49:58 +0000 | [diff] [blame] | 2724 | /// createX86SimpleInstructionSelector - This pass converts an LLVM function |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 2725 | /// 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] | 2726 | /// generated code sucks but the implementation is nice and simple. |
| 2727 | /// |
Chris Lattner | f70e0c2 | 2003-12-28 21:23:38 +0000 | [diff] [blame] | 2728 | FunctionPass *llvm::createX86SimpleInstructionSelector(TargetMachine &TM) { |
| 2729 | return new ISel(TM); |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 2730 | } |