blob: 0715e20c325c41eeace51d00cfeca8fc128ddf71 [file] [log] [blame]
Chris Lattner72614082002-10-25 22:55:53 +00001//===-- InstSelectSimple.cpp - A simple instruction selector for x86 ------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
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 Lattner72614082002-10-25 22:55:53 +00009//
Chris Lattner3e130a22003-01-13 00:32:26 +000010// This file defines a simple peephole instruction selector for the x86 target
Chris Lattner72614082002-10-25 22:55:53 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "X86.h"
Chris Lattner6fc3c522002-11-17 21:11:55 +000015#include "X86InstrBuilder.h"
Misha Brukmanc8893fc2003-10-23 16:22:08 +000016#include "X86InstrInfo.h"
17#include "llvm/Constants.h"
18#include "llvm/DerivedTypes.h"
Chris Lattner72614082002-10-25 22:55:53 +000019#include "llvm/Function.h"
Chris Lattner67580ed2003-05-13 20:21:19 +000020#include "llvm/Instructions.h"
Chris Lattner44827152003-12-28 09:47:19 +000021#include "llvm/IntrinsicLowering.h"
Misha Brukmanc8893fc2003-10-23 16:22:08 +000022#include "llvm/Pass.h"
23#include "llvm/CodeGen/MachineConstantPool.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner341a9372002-10-29 17:43:55 +000025#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner94af4142002-12-25 05:13:53 +000026#include "llvm/CodeGen/SSARegMap.h"
Misha Brukmand2cc0172002-11-20 00:58:23 +000027#include "llvm/Target/MRegisterInfo.h"
Misha Brukmanc8893fc2003-10-23 16:22:08 +000028#include "llvm/Target/TargetMachine.h"
Chris Lattner3f1e8e72004-02-22 07:04:00 +000029#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattner67580ed2003-05-13 20:21:19 +000030#include "llvm/Support/InstVisitor.h"
Chris Lattnercf93cdd2004-01-30 22:13:44 +000031#include "llvm/Support/CFG.h"
Chris Lattner986618e2004-02-22 19:47:26 +000032#include "Support/Statistic.h"
Chris Lattner44827152003-12-28 09:47:19 +000033using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000034
Chris Lattner986618e2004-02-22 19:47:26 +000035namespace {
36 Statistic<>
37 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
Chris Lattner427aeb42004-04-11 19:21:59 +000038
39 /// TypeClass - Used by the X86 backend to group LLVM types by their basic X86
40 /// Representation.
41 ///
42 enum TypeClass {
43 cByte, cShort, cInt, cFP, cLong
44 };
45}
46
47/// getClass - Turn a primitive type into a "class" number which is based on the
48/// size of the type, and whether or not it is floating point.
49///
50static inline TypeClass getClass(const Type *Ty) {
51 switch (Ty->getPrimitiveID()) {
52 case Type::SByteTyID:
53 case Type::UByteTyID: return cByte; // Byte operands are class #0
54 case Type::ShortTyID:
55 case Type::UShortTyID: return cShort; // Short operands are class #1
56 case Type::IntTyID:
57 case Type::UIntTyID:
58 case Type::PointerTyID: return cInt; // Int's and pointers are class #2
59
60 case Type::FloatTyID:
61 case Type::DoubleTyID: return cFP; // Floating Point is #3
62
63 case Type::LongTyID:
64 case Type::ULongTyID: return cLong; // Longs are class #4
65 default:
66 assert(0 && "Invalid type to getClass!");
67 return cByte; // not reached
68 }
69}
70
71// getClassB - Just like getClass, but treat boolean values as bytes.
72static inline TypeClass getClassB(const Type *Ty) {
73 if (Ty == Type::BoolTy) return cByte;
74 return getClass(Ty);
Chris Lattner986618e2004-02-22 19:47:26 +000075}
Chris Lattnercf93cdd2004-01-30 22:13:44 +000076
Chris Lattner72614082002-10-25 22:55:53 +000077namespace {
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000078 struct ISel : public FunctionPass, InstVisitor<ISel> {
79 TargetMachine &TM;
Chris Lattnereca195e2003-05-08 19:44:13 +000080 MachineFunction *F; // The function we are compiling into
81 MachineBasicBlock *BB; // The current MBB we are compiling
82 int VarArgsFrameIndex; // FrameIndex for start of varargs area
Chris Lattner0e5b79c2004-02-15 01:04:03 +000083 int ReturnAddressIndex; // FrameIndex for the return address
Chris Lattner72614082002-10-25 22:55:53 +000084
Chris Lattner72614082002-10-25 22:55:53 +000085 std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs
86
Chris Lattner333b2fa2002-12-13 10:09:43 +000087 // MBBMap - Mapping between LLVM BB -> Machine BB
88 std::map<const BasicBlock*, MachineBasicBlock*> MBBMap;
89
Chris Lattnerf70e0c22003-12-28 21:23:38 +000090 ISel(TargetMachine &tm) : TM(tm), F(0), BB(0) {}
Chris Lattner72614082002-10-25 22:55:53 +000091
92 /// runOnFunction - Top level implementation of instruction selection for
93 /// the entire function.
94 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000095 bool runOnFunction(Function &Fn) {
Chris Lattner44827152003-12-28 09:47:19 +000096 // First pass over the function, lower any unknown intrinsic functions
97 // with the IntrinsicLowering class.
98 LowerUnknownIntrinsicFunctionCalls(Fn);
99
Chris Lattner36b36032002-10-29 23:40:58 +0000100 F = &MachineFunction::construct(&Fn, TM);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000101
Chris Lattner065faeb2002-12-28 20:24:02 +0000102 // Create all of the machine basic blocks for the function...
Chris Lattner333b2fa2002-12-13 10:09:43 +0000103 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
104 F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I));
105
Chris Lattner14aa7fe2002-12-16 22:54:46 +0000106 BB = &F->front();
Chris Lattnerdbd73722003-05-06 21:32:22 +0000107
Chris Lattner0e5b79c2004-02-15 01:04:03 +0000108 // Set up a frame object for the return address. This is used by the
109 // llvm.returnaddress & llvm.frameaddress intrinisics.
110 ReturnAddressIndex = F->getFrameInfo()->CreateFixedObject(4, -4);
111
Chris Lattnerdbd73722003-05-06 21:32:22 +0000112 // Copy incoming arguments off of the stack...
Chris Lattner065faeb2002-12-28 20:24:02 +0000113 LoadArgumentsToVirtualRegs(Fn);
Chris Lattner14aa7fe2002-12-16 22:54:46 +0000114
Chris Lattner333b2fa2002-12-13 10:09:43 +0000115 // Instruction select everything except PHI nodes
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000116 visit(Fn);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000117
118 // Select the PHI nodes
119 SelectPHINodes();
120
Chris Lattner986618e2004-02-22 19:47:26 +0000121 // Insert the FP_REG_KILL instructions into blocks that need them.
122 InsertFPRegKills();
123
Chris Lattner72614082002-10-25 22:55:53 +0000124 RegMap.clear();
Chris Lattner333b2fa2002-12-13 10:09:43 +0000125 MBBMap.clear();
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000126 F = 0;
Chris Lattner2a865b02003-07-26 23:05:37 +0000127 // We always build a machine code representation for the function
128 return true;
Chris Lattner72614082002-10-25 22:55:53 +0000129 }
130
Chris Lattnerf0eb7be2002-12-15 21:13:40 +0000131 virtual const char *getPassName() const {
132 return "X86 Simple Instruction Selection";
133 }
134
Chris Lattner72614082002-10-25 22:55:53 +0000135 /// visitBasicBlock - This method is called when we are visiting a new basic
Chris Lattner33f53b52002-10-29 20:48:56 +0000136 /// block. This simply creates a new MachineBasicBlock to emit code into
137 /// and adds it to the current MachineFunction. Subsequent visit* for
138 /// instructions will be invoked for all instructions in the basic block.
Chris Lattner72614082002-10-25 22:55:53 +0000139 ///
140 void visitBasicBlock(BasicBlock &LLVM_BB) {
Chris Lattner333b2fa2002-12-13 10:09:43 +0000141 BB = MBBMap[&LLVM_BB];
Chris Lattner72614082002-10-25 22:55:53 +0000142 }
143
Chris Lattner44827152003-12-28 09:47:19 +0000144 /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
145 /// function, lowering any calls to unknown intrinsic functions into the
146 /// equivalent LLVM code.
Misha Brukman538607f2004-03-01 23:53:11 +0000147 ///
Chris Lattner44827152003-12-28 09:47:19 +0000148 void LowerUnknownIntrinsicFunctionCalls(Function &F);
149
Chris Lattner065faeb2002-12-28 20:24:02 +0000150 /// LoadArgumentsToVirtualRegs - Load all of the arguments to this function
151 /// from the stack into virtual registers.
152 ///
153 void LoadArgumentsToVirtualRegs(Function &F);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000154
155 /// SelectPHINodes - Insert machine code to generate phis. This is tricky
156 /// because we have to generate our sources into the source basic blocks,
157 /// not the current one.
158 ///
159 void SelectPHINodes();
160
Chris Lattner986618e2004-02-22 19:47:26 +0000161 /// InsertFPRegKills - Insert FP_REG_KILL instructions into basic blocks
162 /// that need them. This only occurs due to the floating point stackifier
163 /// not being aggressive enough to handle arbitrary global stackification.
164 ///
165 void InsertFPRegKills();
166
Chris Lattner72614082002-10-25 22:55:53 +0000167 // Visitation methods for various instructions. These methods simply emit
168 // fixed X86 code for each instruction.
169 //
Brian Gaekefa8d5712002-11-22 11:07:01 +0000170
171 // Control flow operators
Chris Lattner72614082002-10-25 22:55:53 +0000172 void visitReturnInst(ReturnInst &RI);
Chris Lattner2df035b2002-11-02 19:27:56 +0000173 void visitBranchInst(BranchInst &BI);
Chris Lattner3e130a22003-01-13 00:32:26 +0000174
175 struct ValueRecord {
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000176 Value *Val;
Chris Lattner3e130a22003-01-13 00:32:26 +0000177 unsigned Reg;
178 const Type *Ty;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000179 ValueRecord(unsigned R, const Type *T) : Val(0), Reg(R), Ty(T) {}
180 ValueRecord(Value *V) : Val(V), Reg(0), Ty(V->getType()) {}
Chris Lattner3e130a22003-01-13 00:32:26 +0000181 };
182 void doCall(const ValueRecord &Ret, MachineInstr *CallMI,
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000183 const std::vector<ValueRecord> &Args);
Brian Gaekefa8d5712002-11-22 11:07:01 +0000184 void visitCallInst(CallInst &I);
Brian Gaeked0fde302003-11-11 22:41:34 +0000185 void visitIntrinsicCall(Intrinsic::ID ID, CallInst &I);
Chris Lattnere2954c82002-11-02 20:04:26 +0000186
187 // Arithmetic operators
Chris Lattnerf01729e2002-11-02 20:54:46 +0000188 void visitSimpleBinary(BinaryOperator &B, unsigned OpcodeClass);
Chris Lattner68aad932002-11-02 20:13:22 +0000189 void visitAdd(BinaryOperator &B) { visitSimpleBinary(B, 0); }
190 void visitSub(BinaryOperator &B) { visitSimpleBinary(B, 1); }
Chris Lattnerca9671d2002-11-02 20:28:58 +0000191 void visitMul(BinaryOperator &B);
Chris Lattnere2954c82002-11-02 20:04:26 +0000192
Chris Lattnerf01729e2002-11-02 20:54:46 +0000193 void visitDiv(BinaryOperator &B) { visitDivRem(B); }
194 void visitRem(BinaryOperator &B) { visitDivRem(B); }
195 void visitDivRem(BinaryOperator &B);
196
Chris Lattnere2954c82002-11-02 20:04:26 +0000197 // Bitwise operators
Chris Lattner68aad932002-11-02 20:13:22 +0000198 void visitAnd(BinaryOperator &B) { visitSimpleBinary(B, 2); }
199 void visitOr (BinaryOperator &B) { visitSimpleBinary(B, 3); }
200 void visitXor(BinaryOperator &B) { visitSimpleBinary(B, 4); }
Chris Lattnere2954c82002-11-02 20:04:26 +0000201
Chris Lattner6d40c192003-01-16 16:43:00 +0000202 // Comparison operators...
203 void visitSetCondInst(SetCondInst &I);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000204 unsigned EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
205 MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000206 MachineBasicBlock::iterator MBBI);
Chris Lattner12d96a02004-03-30 21:22:00 +0000207 void visitSelectInst(SelectInst &SI);
208
Chris Lattnerb2acc512003-10-19 21:09:10 +0000209
Chris Lattner6fc3c522002-11-17 21:11:55 +0000210 // Memory Instructions
211 void visitLoadInst(LoadInst &I);
212 void visitStoreInst(StoreInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000213 void visitGetElementPtrInst(GetElementPtrInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000214 void visitAllocaInst(AllocaInst &I);
Chris Lattner3e130a22003-01-13 00:32:26 +0000215 void visitMallocInst(MallocInst &I);
216 void visitFreeInst(FreeInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000217
Chris Lattnere2954c82002-11-02 20:04:26 +0000218 // Other operators
Brian Gaekea1719c92002-10-31 23:03:59 +0000219 void visitShiftInst(ShiftInst &I);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000220 void visitPHINode(PHINode &I) {} // PHI nodes handled by second pass
Brian Gaekefa8d5712002-11-22 11:07:01 +0000221 void visitCastInst(CastInst &I);
Chris Lattner73815062003-10-18 05:56:40 +0000222 void visitVANextInst(VANextInst &I);
223 void visitVAArgInst(VAArgInst &I);
Chris Lattner72614082002-10-25 22:55:53 +0000224
225 void visitInstruction(Instruction &I) {
226 std::cerr << "Cannot instruction select: " << I;
227 abort();
228 }
229
Brian Gaeke95780cc2002-12-13 07:56:18 +0000230 /// promote32 - Make a value 32-bits wide, and put it somewhere.
Chris Lattner3e130a22003-01-13 00:32:26 +0000231 ///
232 void promote32(unsigned targetReg, const ValueRecord &VR);
233
Chris Lattner721d2d42004-03-08 01:18:36 +0000234 /// getAddressingMode - Get the addressing mode to use to address the
235 /// specified value. The returned value should be used with addFullAddress.
236 void getAddressingMode(Value *Addr, unsigned &BaseReg, unsigned &Scale,
237 unsigned &IndexReg, unsigned &Disp);
238
239
240 /// getGEPIndex - This is used to fold GEP instructions into X86 addressing
241 /// expressions.
Chris Lattnerb6bac512004-02-25 06:13:04 +0000242 void getGEPIndex(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
243 std::vector<Value*> &GEPOps,
244 std::vector<const Type*> &GEPTypes, unsigned &BaseReg,
245 unsigned &Scale, unsigned &IndexReg, unsigned &Disp);
246
247 /// isGEPFoldable - Return true if the specified GEP can be completely
248 /// folded into the addressing mode of a load/store or lea instruction.
249 bool isGEPFoldable(MachineBasicBlock *MBB,
250 Value *Src, User::op_iterator IdxBegin,
251 User::op_iterator IdxEnd, unsigned &BaseReg,
252 unsigned &Scale, unsigned &IndexReg, unsigned &Disp);
253
Chris Lattner3e130a22003-01-13 00:32:26 +0000254 /// emitGEPOperation - Common code shared between visitGetElementPtrInst and
255 /// constant expression GEP support.
256 ///
Chris Lattner827832c2004-02-22 17:05:38 +0000257 void emitGEPOperation(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
Chris Lattner333b2fa2002-12-13 10:09:43 +0000258 Value *Src, User::op_iterator IdxBegin,
Chris Lattnerc0812d82002-12-13 06:56:29 +0000259 User::op_iterator IdxEnd, unsigned TargetReg);
260
Chris Lattner548f61d2003-04-23 17:22:12 +0000261 /// emitCastOperation - Common code shared between visitCastInst and
262 /// constant expression cast support.
Misha Brukman538607f2004-03-01 23:53:11 +0000263 ///
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000264 void emitCastOperation(MachineBasicBlock *BB,MachineBasicBlock::iterator IP,
Chris Lattner548f61d2003-04-23 17:22:12 +0000265 Value *Src, const Type *DestTy, unsigned TargetReg);
266
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000267 /// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
268 /// and constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000269 ///
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000270 void emitSimpleBinaryOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000271 MachineBasicBlock::iterator IP,
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000272 Value *Op0, Value *Op1,
273 unsigned OperatorClass, unsigned TargetReg);
274
Chris Lattner6621ed92004-04-11 21:23:56 +0000275 /// emitBinaryFPOperation - This method handles emission of floating point
276 /// Add (0), Sub (1), Mul (2), and Div (3) operations.
277 void emitBinaryFPOperation(MachineBasicBlock *BB,
278 MachineBasicBlock::iterator IP,
279 Value *Op0, Value *Op1,
280 unsigned OperatorClass, unsigned TargetReg);
281
Chris Lattner462fa822004-04-11 20:56:28 +0000282 void emitMultiply(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
283 Value *Op0, Value *Op1, unsigned TargetReg);
284
285 void doMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI,
286 unsigned DestReg, const Type *DestTy,
287 unsigned Op0Reg, unsigned Op1Reg);
288 void doMultiplyConst(MachineBasicBlock *MBB,
289 MachineBasicBlock::iterator MBBI,
290 unsigned DestReg, const Type *DestTy,
291 unsigned Op0Reg, unsigned Op1Val);
292
Chris Lattnercadff442003-10-23 17:21:43 +0000293 void emitDivRemOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000294 MachineBasicBlock::iterator IP,
Chris Lattner462fa822004-04-11 20:56:28 +0000295 Value *Op0, Value *Op1, bool isDiv,
296 unsigned TargetReg);
Chris Lattnercadff442003-10-23 17:21:43 +0000297
Chris Lattner58c41fe2003-08-24 19:19:47 +0000298 /// emitSetCCOperation - Common code shared between visitSetCondInst and
299 /// constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000300 ///
Chris Lattner58c41fe2003-08-24 19:19:47 +0000301 void emitSetCCOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000302 MachineBasicBlock::iterator IP,
Chris Lattner58c41fe2003-08-24 19:19:47 +0000303 Value *Op0, Value *Op1, unsigned Opcode,
304 unsigned TargetReg);
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000305
306 /// emitShiftOperation - Common code shared between visitShiftInst and
307 /// constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000308 ///
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000309 void emitShiftOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000310 MachineBasicBlock::iterator IP,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000311 Value *Op, Value *ShiftAmount, bool isLeftShift,
312 const Type *ResultTy, unsigned DestReg);
313
Chris Lattner12d96a02004-03-30 21:22:00 +0000314 /// emitSelectOperation - Common code shared between visitSelectInst and the
315 /// constant expression support.
316 void emitSelectOperation(MachineBasicBlock *MBB,
317 MachineBasicBlock::iterator IP,
318 Value *Cond, Value *TrueVal, Value *FalseVal,
319 unsigned DestReg);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000320
Chris Lattnerc5291f52002-10-27 21:16:59 +0000321 /// copyConstantToRegister - Output the instructions required to put the
322 /// specified constant into the specified register.
323 ///
Chris Lattner8a307e82002-12-16 19:32:50 +0000324 void copyConstantToRegister(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000325 MachineBasicBlock::iterator MBBI,
Chris Lattner8a307e82002-12-16 19:32:50 +0000326 Constant *C, unsigned Reg);
Chris Lattnerc5291f52002-10-27 21:16:59 +0000327
Chris Lattner3e130a22003-01-13 00:32:26 +0000328 /// makeAnotherReg - This method returns the next register number we haven't
329 /// yet used.
330 ///
331 /// Long values are handled somewhat specially. They are always allocated
332 /// as pairs of 32 bit integer values. The register number returned is the
333 /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits
334 /// of the long value.
335 ///
Chris Lattnerc0812d82002-12-13 06:56:29 +0000336 unsigned makeAnotherReg(const Type *Ty) {
Chris Lattner7db1fa92003-07-30 05:33:48 +0000337 assert(dynamic_cast<const X86RegisterInfo*>(TM.getRegisterInfo()) &&
338 "Current target doesn't have X86 reg info??");
339 const X86RegisterInfo *MRI =
340 static_cast<const X86RegisterInfo*>(TM.getRegisterInfo());
Chris Lattner3e130a22003-01-13 00:32:26 +0000341 if (Ty == Type::LongTy || Ty == Type::ULongTy) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000342 const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy);
343 // Create the lower part
344 F->getSSARegMap()->createVirtualRegister(RC);
345 // Create the upper part.
346 return F->getSSARegMap()->createVirtualRegister(RC)-1;
Chris Lattner3e130a22003-01-13 00:32:26 +0000347 }
348
Chris Lattnerc0812d82002-12-13 06:56:29 +0000349 // Add the mapping of regnumber => reg class to MachineFunction
Chris Lattner7db1fa92003-07-30 05:33:48 +0000350 const TargetRegisterClass *RC = MRI->getRegClassForType(Ty);
Chris Lattner3e130a22003-01-13 00:32:26 +0000351 return F->getSSARegMap()->createVirtualRegister(RC);
Brian Gaeke20244b72002-12-12 15:33:40 +0000352 }
353
Chris Lattner72614082002-10-25 22:55:53 +0000354 /// getReg - This method turns an LLVM value into a register number. This
355 /// is guaranteed to produce the same register number for a particular value
356 /// every time it is queried.
357 ///
358 unsigned getReg(Value &V) { return getReg(&V); } // Allow references
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000359 unsigned getReg(Value *V) {
360 // Just append to the end of the current bb.
361 MachineBasicBlock::iterator It = BB->end();
362 return getReg(V, BB, It);
363 }
Brian Gaeke71794c02002-12-13 11:22:48 +0000364 unsigned getReg(Value *V, MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000365 MachineBasicBlock::iterator IPt) {
Chris Lattner427aeb42004-04-11 19:21:59 +0000366 // If this operand is a constant, emit the code to copy the constant into
367 // the register here...
368 //
369 if (Constant *C = dyn_cast<Constant>(V)) {
370 unsigned Reg = makeAnotherReg(V->getType());
371 copyConstantToRegister(MBB, IPt, C, Reg);
372 return Reg;
373 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
374 unsigned Reg = makeAnotherReg(V->getType());
375 // Move the address of the global into the register
376 BuildMI(*MBB, IPt, X86::MOV32ri, 1, Reg).addGlobalAddress(GV);
377 return Reg;
378 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
379 // Do not emit noop casts at all.
380 if (getClassB(CI->getType()) == getClassB(CI->getOperand(0)->getType()))
381 return getReg(CI->getOperand(0), MBB, IPt);
382 }
383
Chris Lattner72614082002-10-25 22:55:53 +0000384 unsigned &Reg = RegMap[V];
Misha Brukmand2cc0172002-11-20 00:58:23 +0000385 if (Reg == 0) {
Chris Lattnerc0812d82002-12-13 06:56:29 +0000386 Reg = makeAnotherReg(V->getType());
Misha Brukmand2cc0172002-11-20 00:58:23 +0000387 RegMap[V] = Reg;
Misha Brukmand2cc0172002-11-20 00:58:23 +0000388 }
Chris Lattner72614082002-10-25 22:55:53 +0000389
Chris Lattner72614082002-10-25 22:55:53 +0000390 return Reg;
391 }
Chris Lattner72614082002-10-25 22:55:53 +0000392 };
393}
394
Chris Lattnerc5291f52002-10-27 21:16:59 +0000395/// copyConstantToRegister - Output the instructions required to put the
396/// specified constant into the specified register.
397///
Chris Lattner8a307e82002-12-16 19:32:50 +0000398void ISel::copyConstantToRegister(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000399 MachineBasicBlock::iterator IP,
Chris Lattner8a307e82002-12-16 19:32:50 +0000400 Constant *C, unsigned R) {
Chris Lattnerc0812d82002-12-13 06:56:29 +0000401 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000402 unsigned Class = 0;
403 switch (CE->getOpcode()) {
404 case Instruction::GetElementPtr:
Brian Gaeke68b1edc2002-12-16 04:23:29 +0000405 emitGEPOperation(MBB, IP, CE->getOperand(0),
Chris Lattner333b2fa2002-12-13 10:09:43 +0000406 CE->op_begin()+1, CE->op_end(), R);
Chris Lattnerc0812d82002-12-13 06:56:29 +0000407 return;
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000408 case Instruction::Cast:
Chris Lattner548f61d2003-04-23 17:22:12 +0000409 emitCastOperation(MBB, IP, CE->getOperand(0), CE->getType(), R);
Chris Lattner4b12cde2003-04-21 21:33:44 +0000410 return;
Chris Lattnerc0812d82002-12-13 06:56:29 +0000411
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000412 case Instruction::Xor: ++Class; // FALL THROUGH
413 case Instruction::Or: ++Class; // FALL THROUGH
414 case Instruction::And: ++Class; // FALL THROUGH
415 case Instruction::Sub: ++Class; // FALL THROUGH
416 case Instruction::Add:
417 emitSimpleBinaryOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
418 Class, R);
419 return;
420
Chris Lattner462fa822004-04-11 20:56:28 +0000421 case Instruction::Mul:
422 emitMultiply(MBB, IP, CE->getOperand(0), CE->getOperand(1), R);
Chris Lattnercadff442003-10-23 17:21:43 +0000423 return;
Chris Lattner462fa822004-04-11 20:56:28 +0000424
Chris Lattnercadff442003-10-23 17:21:43 +0000425 case Instruction::Div:
Chris Lattner462fa822004-04-11 20:56:28 +0000426 case Instruction::Rem:
427 emitDivRemOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
428 CE->getOpcode() == Instruction::Div, R);
Chris Lattnercadff442003-10-23 17:21:43 +0000429 return;
Chris Lattnercadff442003-10-23 17:21:43 +0000430
Chris Lattner58c41fe2003-08-24 19:19:47 +0000431 case Instruction::SetNE:
432 case Instruction::SetEQ:
433 case Instruction::SetLT:
434 case Instruction::SetGT:
435 case Instruction::SetLE:
436 case Instruction::SetGE:
437 emitSetCCOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
438 CE->getOpcode(), R);
439 return;
440
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000441 case Instruction::Shl:
442 case Instruction::Shr:
443 emitShiftOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000444 CE->getOpcode() == Instruction::Shl, CE->getType(), R);
445 return;
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000446
Chris Lattner12d96a02004-03-30 21:22:00 +0000447 case Instruction::Select:
448 emitSelectOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
449 CE->getOperand(2), R);
450 return;
451
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000452 default:
453 std::cerr << "Offending expr: " << C << "\n";
Chris Lattnerb2acc512003-10-19 21:09:10 +0000454 assert(0 && "Constant expression not yet handled!\n");
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000455 }
Brian Gaeke20244b72002-12-12 15:33:40 +0000456 }
Chris Lattnerc5291f52002-10-27 21:16:59 +0000457
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000458 if (C->getType()->isIntegral()) {
Chris Lattner6b993cc2002-12-15 08:02:15 +0000459 unsigned Class = getClassB(C->getType());
Chris Lattner3e130a22003-01-13 00:32:26 +0000460
461 if (Class == cLong) {
462 // Copy the value into the register pair.
Chris Lattnerc07736a2003-07-23 15:22:26 +0000463 uint64_t Val = cast<ConstantInt>(C)->getRawValue();
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000464 BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addImm(Val & 0xFFFFFFFF);
465 BuildMI(*MBB, IP, X86::MOV32ri, 1, R+1).addImm(Val >> 32);
Chris Lattner3e130a22003-01-13 00:32:26 +0000466 return;
467 }
468
Chris Lattner94af4142002-12-25 05:13:53 +0000469 assert(Class <= cInt && "Type not handled yet!");
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000470
471 static const unsigned IntegralOpcodeTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000472 X86::MOV8ri, X86::MOV16ri, X86::MOV32ri
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000473 };
474
Chris Lattner6b993cc2002-12-15 08:02:15 +0000475 if (C->getType() == Type::BoolTy) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000476 BuildMI(*MBB, IP, X86::MOV8ri, 1, R).addImm(C == ConstantBool::True);
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000477 } else {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000478 ConstantInt *CI = cast<ConstantInt>(C);
Chris Lattneree352852004-02-29 07:22:16 +0000479 BuildMI(*MBB, IP, IntegralOpcodeTab[Class],1,R).addImm(CI->getRawValue());
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000480 }
Chris Lattner94af4142002-12-25 05:13:53 +0000481 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
Chris Lattneraf703622004-02-02 18:56:30 +0000482 if (CFP->isExactlyValue(+0.0))
Chris Lattneree352852004-02-29 07:22:16 +0000483 BuildMI(*MBB, IP, X86::FLD0, 0, R);
Chris Lattneraf703622004-02-02 18:56:30 +0000484 else if (CFP->isExactlyValue(+1.0))
Chris Lattneree352852004-02-29 07:22:16 +0000485 BuildMI(*MBB, IP, X86::FLD1, 0, R);
Chris Lattner94af4142002-12-25 05:13:53 +0000486 else {
Chris Lattner3e130a22003-01-13 00:32:26 +0000487 // Otherwise we need to spill the constant to memory...
488 MachineConstantPool *CP = F->getConstantPool();
489 unsigned CPI = CP->getConstantPoolIndex(CFP);
Chris Lattner6c09db22003-10-20 04:11:23 +0000490 const Type *Ty = CFP->getType();
491
492 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000493 unsigned LoadOpcode = Ty == Type::FloatTy ? X86::FLD32m : X86::FLD64m;
Chris Lattneree352852004-02-29 07:22:16 +0000494 addConstantPoolReference(BuildMI(*MBB, IP, LoadOpcode, 4, R), CPI);
Chris Lattner94af4142002-12-25 05:13:53 +0000495 }
496
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000497 } else if (isa<ConstantPointerNull>(C)) {
Brian Gaeke20244b72002-12-12 15:33:40 +0000498 // Copy zero (null pointer) to the register.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000499 BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addImm(0);
Chris Lattnerc0812d82002-12-13 06:56:29 +0000500 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000501 BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addGlobalAddress(CPR->getValue());
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000502 } else {
Brian Gaeke20244b72002-12-12 15:33:40 +0000503 std::cerr << "Offending constant: " << C << "\n";
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000504 assert(0 && "Type not handled yet!");
Chris Lattnerc5291f52002-10-27 21:16:59 +0000505 }
506}
507
Chris Lattner065faeb2002-12-28 20:24:02 +0000508/// LoadArgumentsToVirtualRegs - Load all of the arguments to this function from
509/// the stack into virtual registers.
510///
511void ISel::LoadArgumentsToVirtualRegs(Function &Fn) {
512 // Emit instructions to load the arguments... On entry to a function on the
513 // X86, the stack frame looks like this:
514 //
515 // [ESP] -- return address
Chris Lattner3e130a22003-01-13 00:32:26 +0000516 // [ESP + 4] -- first argument (leftmost lexically)
517 // [ESP + 8] -- second argument, if first argument is four bytes in size
Chris Lattner065faeb2002-12-28 20:24:02 +0000518 // ...
519 //
Chris Lattnerf158da22003-01-16 02:20:12 +0000520 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattneraa09b752002-12-28 21:08:28 +0000521 MachineFrameInfo *MFI = F->getFrameInfo();
Chris Lattner065faeb2002-12-28 20:24:02 +0000522
523 for (Function::aiterator I = Fn.abegin(), E = Fn.aend(); I != E; ++I) {
Chris Lattner427aeb42004-04-11 19:21:59 +0000524 bool ArgLive = !I->use_empty();
525 unsigned Reg = ArgLive ? getReg(*I) : 0;
Chris Lattner065faeb2002-12-28 20:24:02 +0000526 int FI; // Frame object index
Chris Lattner427aeb42004-04-11 19:21:59 +0000527
Chris Lattner065faeb2002-12-28 20:24:02 +0000528 switch (getClassB(I->getType())) {
529 case cByte:
Chris Lattner427aeb42004-04-11 19:21:59 +0000530 if (ArgLive) {
531 FI = MFI->CreateFixedObject(1, ArgOffset);
532 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Reg), FI);
533 }
Chris Lattner065faeb2002-12-28 20:24:02 +0000534 break;
535 case cShort:
Chris Lattner427aeb42004-04-11 19:21:59 +0000536 if (ArgLive) {
537 FI = MFI->CreateFixedObject(2, ArgOffset);
538 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Reg), FI);
539 }
Chris Lattner065faeb2002-12-28 20:24:02 +0000540 break;
541 case cInt:
Chris Lattner427aeb42004-04-11 19:21:59 +0000542 if (ArgLive) {
543 FI = MFI->CreateFixedObject(4, ArgOffset);
544 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Reg), FI);
545 }
Chris Lattner065faeb2002-12-28 20:24:02 +0000546 break;
Chris Lattner3e130a22003-01-13 00:32:26 +0000547 case cLong:
Chris Lattner427aeb42004-04-11 19:21:59 +0000548 if (ArgLive) {
549 FI = MFI->CreateFixedObject(8, ArgOffset);
550 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Reg), FI);
551 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Reg+1), FI, 4);
552 }
Chris Lattner3e130a22003-01-13 00:32:26 +0000553 ArgOffset += 4; // longs require 4 additional bytes
554 break;
Chris Lattner065faeb2002-12-28 20:24:02 +0000555 case cFP:
Chris Lattner427aeb42004-04-11 19:21:59 +0000556 if (ArgLive) {
557 unsigned Opcode;
558 if (I->getType() == Type::FloatTy) {
559 Opcode = X86::FLD32m;
560 FI = MFI->CreateFixedObject(4, ArgOffset);
561 } else {
562 Opcode = X86::FLD64m;
563 FI = MFI->CreateFixedObject(8, ArgOffset);
564 }
565 addFrameReference(BuildMI(BB, Opcode, 4, Reg), FI);
Chris Lattner065faeb2002-12-28 20:24:02 +0000566 }
Chris Lattner427aeb42004-04-11 19:21:59 +0000567 if (I->getType() == Type::DoubleTy)
568 ArgOffset += 4; // doubles require 4 additional bytes
Chris Lattner065faeb2002-12-28 20:24:02 +0000569 break;
570 default:
571 assert(0 && "Unhandled argument type!");
572 }
Chris Lattner3e130a22003-01-13 00:32:26 +0000573 ArgOffset += 4; // Each argument takes at least 4 bytes on the stack...
Chris Lattner065faeb2002-12-28 20:24:02 +0000574 }
Chris Lattnereca195e2003-05-08 19:44:13 +0000575
576 // If the function takes variable number of arguments, add a frame offset for
577 // the start of the first vararg value... this is used to expand
578 // llvm.va_start.
579 if (Fn.getFunctionType()->isVarArg())
580 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner065faeb2002-12-28 20:24:02 +0000581}
582
583
Chris Lattner333b2fa2002-12-13 10:09:43 +0000584/// SelectPHINodes - Insert machine code to generate phis. This is tricky
585/// because we have to generate our sources into the source basic blocks, not
586/// the current one.
587///
588void ISel::SelectPHINodes() {
Chris Lattner3501fea2003-01-14 22:00:31 +0000589 const TargetInstrInfo &TII = TM.getInstrInfo();
Chris Lattner333b2fa2002-12-13 10:09:43 +0000590 const Function &LF = *F->getFunction(); // The LLVM function...
591 for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) {
592 const BasicBlock *BB = I;
Chris Lattner168aa902004-02-29 07:10:16 +0000593 MachineBasicBlock &MBB = *MBBMap[I];
Chris Lattner333b2fa2002-12-13 10:09:43 +0000594
595 // Loop over all of the PHI nodes in the LLVM basic block...
Chris Lattner168aa902004-02-29 07:10:16 +0000596 MachineBasicBlock::iterator PHIInsertPoint = MBB.begin();
Chris Lattner333b2fa2002-12-13 10:09:43 +0000597 for (BasicBlock::const_iterator I = BB->begin();
Chris Lattnera81fc682003-10-19 00:26:11 +0000598 PHINode *PN = const_cast<PHINode*>(dyn_cast<PHINode>(I)); ++I) {
Chris Lattner3e130a22003-01-13 00:32:26 +0000599
Chris Lattner333b2fa2002-12-13 10:09:43 +0000600 // Create a new machine instr PHI node, and insert it.
Chris Lattner3e130a22003-01-13 00:32:26 +0000601 unsigned PHIReg = getReg(*PN);
Chris Lattner168aa902004-02-29 07:10:16 +0000602 MachineInstr *PhiMI = BuildMI(MBB, PHIInsertPoint,
603 X86::PHI, PN->getNumOperands(), PHIReg);
Chris Lattner3e130a22003-01-13 00:32:26 +0000604
605 MachineInstr *LongPhiMI = 0;
Chris Lattner168aa902004-02-29 07:10:16 +0000606 if (PN->getType() == Type::LongTy || PN->getType() == Type::ULongTy)
607 LongPhiMI = BuildMI(MBB, PHIInsertPoint,
608 X86::PHI, PN->getNumOperands(), PHIReg+1);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000609
Chris Lattnera6e73f12003-05-12 14:22:21 +0000610 // PHIValues - Map of blocks to incoming virtual registers. We use this
611 // so that we only initialize one incoming value for a particular block,
612 // even if the block has multiple entries in the PHI node.
613 //
614 std::map<MachineBasicBlock*, unsigned> PHIValues;
615
Chris Lattner333b2fa2002-12-13 10:09:43 +0000616 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
617 MachineBasicBlock *PredMBB = MBBMap[PN->getIncomingBlock(i)];
Chris Lattnera6e73f12003-05-12 14:22:21 +0000618 unsigned ValReg;
619 std::map<MachineBasicBlock*, unsigned>::iterator EntryIt =
620 PHIValues.lower_bound(PredMBB);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000621
Chris Lattnera6e73f12003-05-12 14:22:21 +0000622 if (EntryIt != PHIValues.end() && EntryIt->first == PredMBB) {
623 // We already inserted an initialization of the register for this
624 // predecessor. Recycle it.
625 ValReg = EntryIt->second;
626
627 } else {
Chris Lattnera81fc682003-10-19 00:26:11 +0000628 // Get the incoming value into a virtual register.
Chris Lattnera6e73f12003-05-12 14:22:21 +0000629 //
Chris Lattnera81fc682003-10-19 00:26:11 +0000630 Value *Val = PN->getIncomingValue(i);
631
632 // If this is a constant or GlobalValue, we may have to insert code
633 // into the basic block to compute it into a virtual register.
634 if (isa<Constant>(Val) || isa<GlobalValue>(Val)) {
Chris Lattner6f2ab042004-03-30 19:10:12 +0000635 if (isa<ConstantExpr>(Val)) {
636 // Because we don't want to clobber any values which might be in
637 // physical registers with the computation of this constant (which
638 // might be arbitrarily complex if it is a constant expression),
639 // just insert the computation at the top of the basic block.
640 MachineBasicBlock::iterator PI = PredMBB->begin();
641
642 // Skip over any PHI nodes though!
643 while (PI != PredMBB->end() && PI->getOpcode() == X86::PHI)
644 ++PI;
645
646 ValReg = getReg(Val, PredMBB, PI);
647 } else {
648 // Simple constants get emitted at the end of the basic block,
649 // before any terminator instructions. We "know" that the code to
650 // move a constant into a register will never clobber any flags.
651 ValReg = getReg(Val, PredMBB, PredMBB->getFirstTerminator());
652 }
Chris Lattnera81fc682003-10-19 00:26:11 +0000653 } else {
654 ValReg = getReg(Val);
655 }
Chris Lattnera6e73f12003-05-12 14:22:21 +0000656
657 // Remember that we inserted a value for this PHI for this predecessor
658 PHIValues.insert(EntryIt, std::make_pair(PredMBB, ValReg));
659 }
660
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000661 PhiMI->addRegOperand(ValReg);
Chris Lattner3e130a22003-01-13 00:32:26 +0000662 PhiMI->addMachineBasicBlockOperand(PredMBB);
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000663 if (LongPhiMI) {
664 LongPhiMI->addRegOperand(ValReg+1);
665 LongPhiMI->addMachineBasicBlockOperand(PredMBB);
666 }
Chris Lattner333b2fa2002-12-13 10:09:43 +0000667 }
Chris Lattner168aa902004-02-29 07:10:16 +0000668
669 // Now that we emitted all of the incoming values for the PHI node, make
670 // sure to reposition the InsertPoint after the PHI that we just added.
671 // This is needed because we might have inserted a constant into this
672 // block, right after the PHI's which is before the old insert point!
673 PHIInsertPoint = LongPhiMI ? LongPhiMI : PhiMI;
674 ++PHIInsertPoint;
Chris Lattner333b2fa2002-12-13 10:09:43 +0000675 }
676 }
677}
678
Chris Lattner986618e2004-02-22 19:47:26 +0000679/// RequiresFPRegKill - The floating point stackifier pass cannot insert
680/// compensation code on critical edges. As such, it requires that we kill all
681/// FP registers on the exit from any blocks that either ARE critical edges, or
682/// branch to a block that has incoming critical edges.
683///
684/// Note that this kill instruction will eventually be eliminated when
685/// restrictions in the stackifier are relaxed.
686///
687static bool RequiresFPRegKill(const BasicBlock *BB) {
688#if 0
689 for (succ_const_iterator SI = succ_begin(BB), E = succ_end(BB); SI!=E; ++SI) {
690 const BasicBlock *Succ = *SI;
691 pred_const_iterator PI = pred_begin(Succ), PE = pred_end(Succ);
692 ++PI; // Block have at least one predecessory
693 if (PI != PE) { // If it has exactly one, this isn't crit edge
694 // If this block has more than one predecessor, check all of the
695 // predecessors to see if they have multiple successors. If so, then the
696 // block we are analyzing needs an FPRegKill.
697 for (PI = pred_begin(Succ); PI != PE; ++PI) {
698 const BasicBlock *Pred = *PI;
699 succ_const_iterator SI2 = succ_begin(Pred);
700 ++SI2; // There must be at least one successor of this block.
701 if (SI2 != succ_end(Pred))
702 return true; // Yes, we must insert the kill on this edge.
703 }
704 }
705 }
706 // If we got this far, there is no need to insert the kill instruction.
707 return false;
708#else
709 return true;
710#endif
711}
712
713// InsertFPRegKills - Insert FP_REG_KILL instructions into basic blocks that
714// need them. This only occurs due to the floating point stackifier not being
715// aggressive enough to handle arbitrary global stackification.
716//
717// Currently we insert an FP_REG_KILL instruction into each block that uses or
718// defines a floating point virtual register.
719//
720// When the global register allocators (like linear scan) finally update live
721// variable analysis, we can keep floating point values in registers across
722// portions of the CFG that do not involve critical edges. This will be a big
723// win, but we are waiting on the global allocators before we can do this.
724//
725// With a bit of work, the floating point stackifier pass can be enhanced to
726// break critical edges as needed (to make a place to put compensation code),
727// but this will require some infrastructure improvements as well.
728//
729void ISel::InsertFPRegKills() {
730 SSARegMap &RegMap = *F->getSSARegMap();
Chris Lattner986618e2004-02-22 19:47:26 +0000731
732 for (MachineFunction::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
Chris Lattner986618e2004-02-22 19:47:26 +0000733 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000734 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
735 MachineOperand& MO = I->getOperand(i);
736 if (MO.isRegister() && MO.getReg()) {
737 unsigned Reg = MO.getReg();
Chris Lattner986618e2004-02-22 19:47:26 +0000738 if (MRegisterInfo::isVirtualRegister(Reg))
Chris Lattner65cf42d2004-02-23 07:29:45 +0000739 if (RegMap.getRegClass(Reg)->getSize() == 10)
740 goto UsesFPReg;
Chris Lattner986618e2004-02-22 19:47:26 +0000741 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000742 }
Chris Lattner65cf42d2004-02-23 07:29:45 +0000743 // If we haven't found an FP register use or def in this basic block, check
744 // to see if any of our successors has an FP PHI node, which will cause a
745 // copy to be inserted into this block.
Chris Lattnerfbc39d52004-02-23 07:42:19 +0000746 for (succ_const_iterator SI = succ_begin(BB->getBasicBlock()),
747 E = succ_end(BB->getBasicBlock()); SI != E; ++SI) {
748 MachineBasicBlock *SBB = MBBMap[*SI];
749 for (MachineBasicBlock::iterator I = SBB->begin();
750 I != SBB->end() && I->getOpcode() == X86::PHI; ++I) {
751 if (RegMap.getRegClass(I->getOperand(0).getReg())->getSize() == 10)
752 goto UsesFPReg;
Chris Lattner986618e2004-02-22 19:47:26 +0000753 }
Chris Lattnerfbc39d52004-02-23 07:42:19 +0000754 }
Chris Lattner65cf42d2004-02-23 07:29:45 +0000755 continue;
756 UsesFPReg:
757 // Okay, this block uses an FP register. If the block has successors (ie,
758 // it's not an unwind/return), insert the FP_REG_KILL instruction.
759 if (BB->getBasicBlock()->getTerminator()->getNumSuccessors() &&
760 RequiresFPRegKill(BB->getBasicBlock())) {
Chris Lattneree352852004-02-29 07:22:16 +0000761 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
Chris Lattner65cf42d2004-02-23 07:29:45 +0000762 ++NumFPKill;
Chris Lattner986618e2004-02-22 19:47:26 +0000763 }
764 }
765}
766
767
Chris Lattner307ecba2004-03-30 22:39:09 +0000768// canFoldSetCCIntoBranchOrSelect - Return the setcc instruction if we can fold
769// it into the conditional branch or select instruction which is the only user
770// of the cc instruction. This is the case if the conditional branch is the
771// only user of the setcc, and if the setcc is in the same basic block as the
772// conditional branch. We also don't handle long arguments below, so we reject
773// them here as well.
Chris Lattner6d40c192003-01-16 16:43:00 +0000774//
Chris Lattner307ecba2004-03-30 22:39:09 +0000775static SetCondInst *canFoldSetCCIntoBranchOrSelect(Value *V) {
Chris Lattner6d40c192003-01-16 16:43:00 +0000776 if (SetCondInst *SCI = dyn_cast<SetCondInst>(V))
Chris Lattner307ecba2004-03-30 22:39:09 +0000777 if (SCI->hasOneUse()) {
778 Instruction *User = cast<Instruction>(SCI->use_back());
779 if ((isa<BranchInst>(User) || isa<SelectInst>(User)) &&
780 SCI->getParent() == User->getParent() &&
Chris Lattner48c937e2004-04-06 17:34:50 +0000781 (getClassB(SCI->getOperand(0)->getType()) != cLong ||
782 SCI->getOpcode() == Instruction::SetEQ ||
783 SCI->getOpcode() == Instruction::SetNE))
Chris Lattner6d40c192003-01-16 16:43:00 +0000784 return SCI;
785 }
786 return 0;
787}
Chris Lattner333b2fa2002-12-13 10:09:43 +0000788
Chris Lattner6d40c192003-01-16 16:43:00 +0000789// Return a fixed numbering for setcc instructions which does not depend on the
790// order of the opcodes.
791//
792static unsigned getSetCCNumber(unsigned Opcode) {
793 switch(Opcode) {
794 default: assert(0 && "Unknown setcc instruction!");
795 case Instruction::SetEQ: return 0;
796 case Instruction::SetNE: return 1;
797 case Instruction::SetLT: return 2;
Chris Lattner55f6fab2003-01-16 18:07:23 +0000798 case Instruction::SetGE: return 3;
799 case Instruction::SetGT: return 4;
800 case Instruction::SetLE: return 5;
Chris Lattner6d40c192003-01-16 16:43:00 +0000801 }
802}
Chris Lattner06925362002-11-17 21:56:38 +0000803
Chris Lattner6d40c192003-01-16 16:43:00 +0000804// LLVM -> X86 signed X86 unsigned
805// ----- ---------- ------------
806// seteq -> sete sete
807// setne -> setne setne
808// setlt -> setl setb
Chris Lattner55f6fab2003-01-16 18:07:23 +0000809// setge -> setge setae
Chris Lattner6d40c192003-01-16 16:43:00 +0000810// setgt -> setg seta
811// setle -> setle setbe
Chris Lattnerb2acc512003-10-19 21:09:10 +0000812// ----
813// sets // Used by comparison with 0 optimization
814// setns
815static const unsigned SetCCOpcodeTab[2][8] = {
816 { X86::SETEr, X86::SETNEr, X86::SETBr, X86::SETAEr, X86::SETAr, X86::SETBEr,
817 0, 0 },
818 { X86::SETEr, X86::SETNEr, X86::SETLr, X86::SETGEr, X86::SETGr, X86::SETLEr,
819 X86::SETSr, X86::SETNSr },
Chris Lattner6d40c192003-01-16 16:43:00 +0000820};
821
Chris Lattnerb2acc512003-10-19 21:09:10 +0000822// EmitComparison - This function emits a comparison of the two operands,
823// returning the extended setcc code to use.
824unsigned ISel::EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
825 MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000826 MachineBasicBlock::iterator IP) {
Brian Gaeke1749d632002-11-07 17:59:21 +0000827 // The arguments are already supposed to be of the same type.
Chris Lattner6d40c192003-01-16 16:43:00 +0000828 const Type *CompTy = Op0->getType();
Chris Lattner3e130a22003-01-13 00:32:26 +0000829 unsigned Class = getClassB(CompTy);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000830 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattner333864d2003-06-05 19:30:30 +0000831
832 // Special case handling of: cmp R, i
Chris Lattnere80e6372004-04-06 16:02:27 +0000833 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
834 if (Class == cByte || Class == cShort || Class == cInt) {
835 unsigned Op1v = CI->getRawValue();
Chris Lattnerc07736a2003-07-23 15:22:26 +0000836
Chris Lattner333864d2003-06-05 19:30:30 +0000837 // Mask off any upper bits of the constant, if there are any...
838 Op1v &= (1ULL << (8 << Class)) - 1;
839
Chris Lattnerb2acc512003-10-19 21:09:10 +0000840 // If this is a comparison against zero, emit more efficient code. We
841 // can't handle unsigned comparisons against zero unless they are == or
842 // !=. These should have been strength reduced already anyway.
843 if (Op1v == 0 && (CompTy->isSigned() || OpNum < 2)) {
844 static const unsigned TESTTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000845 X86::TEST8rr, X86::TEST16rr, X86::TEST32rr
Chris Lattnerb2acc512003-10-19 21:09:10 +0000846 };
Chris Lattneree352852004-02-29 07:22:16 +0000847 BuildMI(*MBB, IP, TESTTab[Class], 2).addReg(Op0r).addReg(Op0r);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000848
849 if (OpNum == 2) return 6; // Map jl -> js
850 if (OpNum == 3) return 7; // Map jg -> jns
851 return OpNum;
Chris Lattner333864d2003-06-05 19:30:30 +0000852 }
Chris Lattnerb2acc512003-10-19 21:09:10 +0000853
854 static const unsigned CMPTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000855 X86::CMP8ri, X86::CMP16ri, X86::CMP32ri
Chris Lattnerb2acc512003-10-19 21:09:10 +0000856 };
857
Chris Lattneree352852004-02-29 07:22:16 +0000858 BuildMI(*MBB, IP, CMPTab[Class], 2).addReg(Op0r).addImm(Op1v);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000859 return OpNum;
Chris Lattnere80e6372004-04-06 16:02:27 +0000860 } else {
861 assert(Class == cLong && "Unknown integer class!");
862 unsigned LowCst = CI->getRawValue();
863 unsigned HiCst = CI->getRawValue() >> 32;
864 if (OpNum < 2) { // seteq, setne
865 unsigned LoTmp = Op0r;
866 if (LowCst != 0) {
867 LoTmp = makeAnotherReg(Type::IntTy);
868 BuildMI(*MBB, IP, X86::XOR32ri, 2, LoTmp).addReg(Op0r).addImm(LowCst);
869 }
870 unsigned HiTmp = Op0r+1;
871 if (HiCst != 0) {
872 HiTmp = makeAnotherReg(Type::IntTy);
Chris Lattner48c937e2004-04-06 17:34:50 +0000873 BuildMI(*MBB, IP, X86::XOR32ri, 2,HiTmp).addReg(Op0r+1).addImm(HiCst);
Chris Lattnere80e6372004-04-06 16:02:27 +0000874 }
875 unsigned FinalTmp = makeAnotherReg(Type::IntTy);
876 BuildMI(*MBB, IP, X86::OR32rr, 2, FinalTmp).addReg(LoTmp).addReg(HiTmp);
877 return OpNum;
Chris Lattner48c937e2004-04-06 17:34:50 +0000878 } else {
879 // Emit a sequence of code which compares the high and low parts once
880 // each, then uses a conditional move to handle the overflow case. For
881 // example, a setlt for long would generate code like this:
882 //
883 // AL = lo(op1) < lo(op2) // Signedness depends on operands
884 // BL = hi(op1) < hi(op2) // Always unsigned comparison
885 // dest = hi(op1) == hi(op2) ? AL : BL;
886 //
887
888 // FIXME: This would be much better if we had hierarchical register
889 // classes! Until then, hardcode registers so that we can deal with
890 // their aliases (because we don't have conditional byte moves).
891 //
892 BuildMI(*MBB, IP, X86::CMP32ri, 2).addReg(Op0r).addImm(LowCst);
893 BuildMI(*MBB, IP, SetCCOpcodeTab[0][OpNum], 0, X86::AL);
894 BuildMI(*MBB, IP, X86::CMP32ri, 2).addReg(Op0r+1).addImm(HiCst);
895 BuildMI(*MBB, IP, SetCCOpcodeTab[CompTy->isSigned()][OpNum], 0,X86::BL);
896 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::BH);
897 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::AH);
898 BuildMI(*MBB, IP, X86::CMOVE16rr, 2, X86::BX).addReg(X86::BX)
899 .addReg(X86::AX);
900 // NOTE: visitSetCondInst knows that the value is dumped into the BL
901 // register at this point for long values...
902 return OpNum;
Chris Lattnere80e6372004-04-06 16:02:27 +0000903 }
Chris Lattner333864d2003-06-05 19:30:30 +0000904 }
Chris Lattnere80e6372004-04-06 16:02:27 +0000905 }
Chris Lattner333864d2003-06-05 19:30:30 +0000906
Chris Lattner9f08a922004-02-03 18:54:04 +0000907 // Special case handling of comparison against +/- 0.0
908 if (ConstantFP *CFP = dyn_cast<ConstantFP>(Op1))
909 if (CFP->isExactlyValue(+0.0) || CFP->isExactlyValue(-0.0)) {
Chris Lattneree352852004-02-29 07:22:16 +0000910 BuildMI(*MBB, IP, X86::FTST, 1).addReg(Op0r);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000911 BuildMI(*MBB, IP, X86::FNSTSW8r, 0);
Chris Lattneree352852004-02-29 07:22:16 +0000912 BuildMI(*MBB, IP, X86::SAHF, 1);
Chris Lattner9f08a922004-02-03 18:54:04 +0000913 return OpNum;
914 }
915
Chris Lattner58c41fe2003-08-24 19:19:47 +0000916 unsigned Op1r = getReg(Op1, MBB, IP);
Chris Lattner3e130a22003-01-13 00:32:26 +0000917 switch (Class) {
918 default: assert(0 && "Unknown type class!");
919 // Emit: cmp <var1>, <var2> (do the comparison). We can
920 // compare 8-bit with 8-bit, 16-bit with 16-bit, 32-bit with
921 // 32-bit.
922 case cByte:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000923 BuildMI(*MBB, IP, X86::CMP8rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +0000924 break;
925 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000926 BuildMI(*MBB, IP, X86::CMP16rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +0000927 break;
928 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000929 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +0000930 break;
931 case cFP:
Chris Lattner8d2822e2004-04-12 01:43:36 +0000932 if (0) { // for processors prior to the P6
933 BuildMI(*MBB, IP, X86::FpUCOM, 2).addReg(Op0r).addReg(Op1r);
934 BuildMI(*MBB, IP, X86::FNSTSW8r, 0);
935 BuildMI(*MBB, IP, X86::SAHF, 1);
936 } else {
Chris Lattner133dbb12004-04-12 03:02:48 +0000937 BuildMI(*MBB, IP, X86::FpUCOMI, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner8d2822e2004-04-12 01:43:36 +0000938 }
Chris Lattner3e130a22003-01-13 00:32:26 +0000939 break;
940
941 case cLong:
942 if (OpNum < 2) { // seteq, setne
943 unsigned LoTmp = makeAnotherReg(Type::IntTy);
944 unsigned HiTmp = makeAnotherReg(Type::IntTy);
945 unsigned FinalTmp = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000946 BuildMI(*MBB, IP, X86::XOR32rr, 2, LoTmp).addReg(Op0r).addReg(Op1r);
947 BuildMI(*MBB, IP, X86::XOR32rr, 2, HiTmp).addReg(Op0r+1).addReg(Op1r+1);
948 BuildMI(*MBB, IP, X86::OR32rr, 2, FinalTmp).addReg(LoTmp).addReg(HiTmp);
Chris Lattner3e130a22003-01-13 00:32:26 +0000949 break; // Allow the sete or setne to be generated from flags set by OR
950 } else {
951 // Emit a sequence of code which compares the high and low parts once
952 // each, then uses a conditional move to handle the overflow case. For
953 // example, a setlt for long would generate code like this:
954 //
955 // AL = lo(op1) < lo(op2) // Signedness depends on operands
956 // BL = hi(op1) < hi(op2) // Always unsigned comparison
957 // dest = hi(op1) == hi(op2) ? AL : BL;
958 //
959
Chris Lattner6d40c192003-01-16 16:43:00 +0000960 // FIXME: This would be much better if we had hierarchical register
Chris Lattner3e130a22003-01-13 00:32:26 +0000961 // classes! Until then, hardcode registers so that we can deal with their
962 // aliases (because we don't have conditional byte moves).
963 //
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000964 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattneree352852004-02-29 07:22:16 +0000965 BuildMI(*MBB, IP, SetCCOpcodeTab[0][OpNum], 0, X86::AL);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000966 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r+1).addReg(Op1r+1);
Chris Lattneree352852004-02-29 07:22:16 +0000967 BuildMI(*MBB, IP, SetCCOpcodeTab[CompTy->isSigned()][OpNum], 0, X86::BL);
968 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::BH);
969 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::AH);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000970 BuildMI(*MBB, IP, X86::CMOVE16rr, 2, X86::BX).addReg(X86::BX)
Chris Lattneree352852004-02-29 07:22:16 +0000971 .addReg(X86::AX);
Chris Lattner6d40c192003-01-16 16:43:00 +0000972 // NOTE: visitSetCondInst knows that the value is dumped into the BL
973 // register at this point for long values...
Chris Lattnerb2acc512003-10-19 21:09:10 +0000974 return OpNum;
Chris Lattner3e130a22003-01-13 00:32:26 +0000975 }
976 }
Chris Lattnerb2acc512003-10-19 21:09:10 +0000977 return OpNum;
Chris Lattner6d40c192003-01-16 16:43:00 +0000978}
Chris Lattner3e130a22003-01-13 00:32:26 +0000979
Chris Lattner6d40c192003-01-16 16:43:00 +0000980/// SetCC instructions - Here we just emit boilerplate code to set a byte-sized
981/// register, then move it to wherever the result should be.
982///
983void ISel::visitSetCondInst(SetCondInst &I) {
Chris Lattner307ecba2004-03-30 22:39:09 +0000984 if (canFoldSetCCIntoBranchOrSelect(&I))
985 return; // Fold this into a branch or select.
Chris Lattner6d40c192003-01-16 16:43:00 +0000986
Chris Lattner6d40c192003-01-16 16:43:00 +0000987 unsigned DestReg = getReg(I);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000988 MachineBasicBlock::iterator MII = BB->end();
989 emitSetCCOperation(BB, MII, I.getOperand(0), I.getOperand(1), I.getOpcode(),
990 DestReg);
991}
Chris Lattner6d40c192003-01-16 16:43:00 +0000992
Chris Lattner58c41fe2003-08-24 19:19:47 +0000993/// emitSetCCOperation - Common code shared between visitSetCondInst and
994/// constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000995///
Chris Lattner58c41fe2003-08-24 19:19:47 +0000996void ISel::emitSetCCOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000997 MachineBasicBlock::iterator IP,
Chris Lattner58c41fe2003-08-24 19:19:47 +0000998 Value *Op0, Value *Op1, unsigned Opcode,
999 unsigned TargetReg) {
1000 unsigned OpNum = getSetCCNumber(Opcode);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001001 OpNum = EmitComparison(OpNum, Op0, Op1, MBB, IP);
Chris Lattner58c41fe2003-08-24 19:19:47 +00001002
Chris Lattnerb2acc512003-10-19 21:09:10 +00001003 const Type *CompTy = Op0->getType();
1004 unsigned CompClass = getClassB(CompTy);
1005 bool isSigned = CompTy->isSigned() && CompClass != cFP;
1006
1007 if (CompClass != cLong || OpNum < 2) {
Chris Lattner6d40c192003-01-16 16:43:00 +00001008 // Handle normal comparisons with a setcc instruction...
Chris Lattneree352852004-02-29 07:22:16 +00001009 BuildMI(*MBB, IP, SetCCOpcodeTab[isSigned][OpNum], 0, TargetReg);
Chris Lattner6d40c192003-01-16 16:43:00 +00001010 } else {
1011 // Handle long comparisons by copying the value which is already in BL into
1012 // the register we want...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001013 BuildMI(*MBB, IP, X86::MOV8rr, 1, TargetReg).addReg(X86::BL);
Chris Lattner6d40c192003-01-16 16:43:00 +00001014 }
Brian Gaeke1749d632002-11-07 17:59:21 +00001015}
Chris Lattner51b49a92002-11-02 19:45:49 +00001016
Chris Lattner12d96a02004-03-30 21:22:00 +00001017void ISel::visitSelectInst(SelectInst &SI) {
1018 unsigned DestReg = getReg(SI);
1019 MachineBasicBlock::iterator MII = BB->end();
1020 emitSelectOperation(BB, MII, SI.getCondition(), SI.getTrueValue(),
1021 SI.getFalseValue(), DestReg);
1022}
1023
1024/// emitSelect - Common code shared between visitSelectInst and the constant
1025/// expression support.
1026void ISel::emitSelectOperation(MachineBasicBlock *MBB,
1027 MachineBasicBlock::iterator IP,
1028 Value *Cond, Value *TrueVal, Value *FalseVal,
1029 unsigned DestReg) {
1030 unsigned SelectClass = getClassB(TrueVal->getType());
1031
1032 // We don't support 8-bit conditional moves. If we have incoming constants,
1033 // transform them into 16-bit constants to avoid having a run-time conversion.
1034 if (SelectClass == cByte) {
1035 if (Constant *T = dyn_cast<Constant>(TrueVal))
1036 TrueVal = ConstantExpr::getCast(T, Type::ShortTy);
1037 if (Constant *F = dyn_cast<Constant>(FalseVal))
1038 FalseVal = ConstantExpr::getCast(F, Type::ShortTy);
1039 }
1040
Chris Lattner82c5a992004-04-13 21:56:09 +00001041 unsigned TrueReg = getReg(TrueVal, MBB, IP);
1042 unsigned FalseReg = getReg(FalseVal, MBB, IP);
1043 if (TrueReg == FalseReg) {
1044 static const unsigned Opcode[] = {
1045 X86::MOV8rr, X86::MOV16rr, X86::MOV32rr, X86::FpMOV, X86::MOV32rr
1046 };
1047 BuildMI(*MBB, IP, Opcode[SelectClass], 1, DestReg).addReg(TrueReg);
1048 if (SelectClass == cLong)
1049 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg+1).addReg(TrueReg+1);
1050 return;
1051 }
1052
Chris Lattner307ecba2004-03-30 22:39:09 +00001053 unsigned Opcode;
1054 if (SetCondInst *SCI = canFoldSetCCIntoBranchOrSelect(Cond)) {
1055 // We successfully folded the setcc into the select instruction.
1056
1057 unsigned OpNum = getSetCCNumber(SCI->getOpcode());
1058 OpNum = EmitComparison(OpNum, SCI->getOperand(0), SCI->getOperand(1), MBB,
1059 IP);
1060
1061 const Type *CompTy = SCI->getOperand(0)->getType();
1062 bool isSigned = CompTy->isSigned() && getClassB(CompTy) != cFP;
1063
1064 // LLVM -> X86 signed X86 unsigned
1065 // ----- ---------- ------------
1066 // seteq -> cmovNE cmovNE
1067 // setne -> cmovE cmovE
1068 // setlt -> cmovGE cmovAE
1069 // setge -> cmovL cmovB
1070 // setgt -> cmovLE cmovBE
1071 // setle -> cmovG cmovA
1072 // ----
1073 // cmovNS // Used by comparison with 0 optimization
1074 // cmovS
1075
1076 switch (SelectClass) {
Chris Lattner352eb482004-03-31 22:03:35 +00001077 default: assert(0 && "Unknown value class!");
1078 case cFP: {
1079 // Annoyingly, we don't have a full set of floating point conditional
1080 // moves. :(
1081 static const unsigned OpcodeTab[2][8] = {
1082 { X86::FCMOVNE, X86::FCMOVE, X86::FCMOVAE, X86::FCMOVB,
1083 X86::FCMOVBE, X86::FCMOVA, 0, 0 },
1084 { X86::FCMOVNE, X86::FCMOVE, 0, 0, 0, 0, 0, 0 },
1085 };
1086 Opcode = OpcodeTab[isSigned][OpNum];
1087
1088 // If opcode == 0, we hit a case that we don't support. Output a setcc
1089 // and compare the result against zero.
1090 if (Opcode == 0) {
1091 unsigned CompClass = getClassB(CompTy);
1092 unsigned CondReg;
1093 if (CompClass != cLong || OpNum < 2) {
1094 CondReg = makeAnotherReg(Type::BoolTy);
1095 // Handle normal comparisons with a setcc instruction...
1096 BuildMI(*MBB, IP, SetCCOpcodeTab[isSigned][OpNum], 0, CondReg);
1097 } else {
1098 // Long comparisons end up in the BL register.
1099 CondReg = X86::BL;
1100 }
1101
Chris Lattner68626c22004-03-31 22:22:36 +00001102 BuildMI(*MBB, IP, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
Chris Lattner352eb482004-03-31 22:03:35 +00001103 Opcode = X86::FCMOVE;
1104 }
1105 break;
1106 }
Chris Lattner307ecba2004-03-30 22:39:09 +00001107 case cByte:
1108 case cShort: {
1109 static const unsigned OpcodeTab[2][8] = {
1110 { X86::CMOVNE16rr, X86::CMOVE16rr, X86::CMOVAE16rr, X86::CMOVB16rr,
1111 X86::CMOVBE16rr, X86::CMOVA16rr, 0, 0 },
1112 { X86::CMOVNE16rr, X86::CMOVE16rr, X86::CMOVGE16rr, X86::CMOVL16rr,
1113 X86::CMOVLE16rr, X86::CMOVG16rr, X86::CMOVNS16rr, X86::CMOVS16rr },
1114 };
1115 Opcode = OpcodeTab[isSigned][OpNum];
1116 break;
1117 }
1118 case cInt:
1119 case cLong: {
1120 static const unsigned OpcodeTab[2][8] = {
1121 { X86::CMOVNE32rr, X86::CMOVE32rr, X86::CMOVAE32rr, X86::CMOVB32rr,
1122 X86::CMOVBE32rr, X86::CMOVA32rr, 0, 0 },
1123 { X86::CMOVNE32rr, X86::CMOVE32rr, X86::CMOVGE32rr, X86::CMOVL32rr,
1124 X86::CMOVLE32rr, X86::CMOVG32rr, X86::CMOVNS32rr, X86::CMOVS32rr },
1125 };
1126 Opcode = OpcodeTab[isSigned][OpNum];
1127 break;
1128 }
1129 }
1130 } else {
1131 // Get the value being branched on, and use it to set the condition codes.
1132 unsigned CondReg = getReg(Cond, MBB, IP);
Chris Lattner68626c22004-03-31 22:22:36 +00001133 BuildMI(*MBB, IP, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
Chris Lattner307ecba2004-03-30 22:39:09 +00001134 switch (SelectClass) {
Chris Lattner352eb482004-03-31 22:03:35 +00001135 default: assert(0 && "Unknown value class!");
1136 case cFP: Opcode = X86::FCMOVE; break;
Chris Lattner307ecba2004-03-30 22:39:09 +00001137 case cByte:
Chris Lattner352eb482004-03-31 22:03:35 +00001138 case cShort: Opcode = X86::CMOVE16rr; break;
Chris Lattner307ecba2004-03-30 22:39:09 +00001139 case cInt:
Chris Lattner352eb482004-03-31 22:03:35 +00001140 case cLong: Opcode = X86::CMOVE32rr; break;
Chris Lattner307ecba2004-03-30 22:39:09 +00001141 }
1142 }
Chris Lattner12d96a02004-03-30 21:22:00 +00001143
Chris Lattner12d96a02004-03-30 21:22:00 +00001144 unsigned RealDestReg = DestReg;
Chris Lattner12d96a02004-03-30 21:22:00 +00001145
Chris Lattner12d96a02004-03-30 21:22:00 +00001146
1147 // Annoyingly enough, X86 doesn't HAVE 8-bit conditional moves. Because of
1148 // this, we have to promote the incoming values to 16 bits, perform a 16-bit
1149 // cmove, then truncate the result.
1150 if (SelectClass == cByte) {
1151 DestReg = makeAnotherReg(Type::ShortTy);
1152 if (getClassB(TrueVal->getType()) == cByte) {
1153 // Promote the true value, by storing it into AL, and reading from AX.
1154 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::AL).addReg(TrueReg);
1155 BuildMI(*MBB, IP, X86::MOV8ri, 1, X86::AH).addImm(0);
1156 TrueReg = makeAnotherReg(Type::ShortTy);
1157 BuildMI(*MBB, IP, X86::MOV16rr, 1, TrueReg).addReg(X86::AX);
1158 }
1159 if (getClassB(FalseVal->getType()) == cByte) {
1160 // Promote the true value, by storing it into CL, and reading from CX.
1161 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(FalseReg);
1162 BuildMI(*MBB, IP, X86::MOV8ri, 1, X86::CH).addImm(0);
1163 FalseReg = makeAnotherReg(Type::ShortTy);
1164 BuildMI(*MBB, IP, X86::MOV16rr, 1, FalseReg).addReg(X86::CX);
1165 }
1166 }
1167
1168 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(TrueReg).addReg(FalseReg);
1169
1170 switch (SelectClass) {
1171 case cByte:
1172 // We did the computation with 16-bit registers. Truncate back to our
1173 // result by copying into AX then copying out AL.
1174 BuildMI(*MBB, IP, X86::MOV16rr, 1, X86::AX).addReg(DestReg);
1175 BuildMI(*MBB, IP, X86::MOV8rr, 1, RealDestReg).addReg(X86::AL);
1176 break;
1177 case cLong:
1178 // Move the upper half of the value as well.
1179 BuildMI(*MBB, IP, Opcode, 2,DestReg+1).addReg(TrueReg+1).addReg(FalseReg+1);
1180 break;
1181 }
1182}
Chris Lattner58c41fe2003-08-24 19:19:47 +00001183
1184
1185
Brian Gaekec2505982002-11-30 11:57:28 +00001186/// promote32 - Emit instructions to turn a narrow operand into a 32-bit-wide
1187/// operand, in the specified target register.
Misha Brukman538607f2004-03-01 23:53:11 +00001188///
Chris Lattner3e130a22003-01-13 00:32:26 +00001189void ISel::promote32(unsigned targetReg, const ValueRecord &VR) {
1190 bool isUnsigned = VR.Ty->isUnsigned();
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001191
Chris Lattner29bf0622004-04-06 01:21:00 +00001192 Value *Val = VR.Val;
1193 const Type *Ty = VR.Ty;
Chris Lattner502e36c2004-04-06 01:25:33 +00001194 if (Val) {
Chris Lattner29bf0622004-04-06 01:21:00 +00001195 if (Constant *C = dyn_cast<Constant>(Val)) {
1196 Val = ConstantExpr::getCast(C, Type::IntTy);
1197 Ty = Type::IntTy;
1198 }
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001199
Chris Lattner502e36c2004-04-06 01:25:33 +00001200 // If this is a simple constant, just emit a MOVri directly to avoid the
1201 // copy.
1202 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
1203 int TheVal = CI->getRawValue() & 0xFFFFFFFF;
1204 BuildMI(BB, X86::MOV32ri, 1, targetReg).addImm(TheVal);
1205 return;
1206 }
1207 }
1208
Chris Lattner29bf0622004-04-06 01:21:00 +00001209 // Make sure we have the register number for this value...
1210 unsigned Reg = Val ? getReg(Val) : VR.Reg;
1211
1212 switch (getClassB(Ty)) {
Chris Lattner94af4142002-12-25 05:13:53 +00001213 case cByte:
1214 // Extend value into target register (8->32)
1215 if (isUnsigned)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001216 BuildMI(BB, X86::MOVZX32rr8, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001217 else
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001218 BuildMI(BB, X86::MOVSX32rr8, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001219 break;
1220 case cShort:
1221 // Extend value into target register (16->32)
1222 if (isUnsigned)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001223 BuildMI(BB, X86::MOVZX32rr16, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001224 else
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001225 BuildMI(BB, X86::MOVSX32rr16, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001226 break;
1227 case cInt:
1228 // Move value into target register (32->32)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001229 BuildMI(BB, X86::MOV32rr, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001230 break;
1231 default:
1232 assert(0 && "Unpromotable operand class in promote32");
1233 }
Brian Gaekec2505982002-11-30 11:57:28 +00001234}
Chris Lattnerc5291f52002-10-27 21:16:59 +00001235
Chris Lattner72614082002-10-25 22:55:53 +00001236/// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such,
1237/// we have the following possibilities:
1238///
1239/// ret void: No return value, simply emit a 'ret' instruction
1240/// ret sbyte, ubyte : Extend value into EAX and return
1241/// ret short, ushort: Extend value into EAX and return
1242/// ret int, uint : Move value into EAX and return
1243/// ret pointer : Move value into EAX and return
Chris Lattner06925362002-11-17 21:56:38 +00001244/// ret long, ulong : Move value into EAX/EDX and return
1245/// ret float/double : Top of FP stack
Chris Lattner72614082002-10-25 22:55:53 +00001246///
Chris Lattner3e130a22003-01-13 00:32:26 +00001247void ISel::visitReturnInst(ReturnInst &I) {
Chris Lattner94af4142002-12-25 05:13:53 +00001248 if (I.getNumOperands() == 0) {
1249 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
1250 return;
1251 }
1252
1253 Value *RetVal = I.getOperand(0);
Chris Lattner3e130a22003-01-13 00:32:26 +00001254 switch (getClassB(RetVal->getType())) {
Chris Lattner94af4142002-12-25 05:13:53 +00001255 case cByte: // integral return values: extend or move into EAX and return
1256 case cShort:
1257 case cInt:
Chris Lattner29bf0622004-04-06 01:21:00 +00001258 promote32(X86::EAX, ValueRecord(RetVal));
Chris Lattnerdbd73722003-05-06 21:32:22 +00001259 // Declare that EAX is live on exit
Chris Lattnerc2489032003-05-07 19:21:28 +00001260 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
Chris Lattner94af4142002-12-25 05:13:53 +00001261 break;
Chris Lattner29bf0622004-04-06 01:21:00 +00001262 case cFP: { // Floats & Doubles: Return in ST(0)
1263 unsigned RetReg = getReg(RetVal);
Chris Lattner3e130a22003-01-13 00:32:26 +00001264 BuildMI(BB, X86::FpSETRESULT, 1).addReg(RetReg);
Chris Lattnerdbd73722003-05-06 21:32:22 +00001265 // Declare that top-of-stack is live on exit
Chris Lattnerc2489032003-05-07 19:21:28 +00001266 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
Chris Lattner94af4142002-12-25 05:13:53 +00001267 break;
Chris Lattner29bf0622004-04-06 01:21:00 +00001268 }
1269 case cLong: {
1270 unsigned RetReg = getReg(RetVal);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001271 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(RetReg);
1272 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RetReg+1);
Chris Lattnerdbd73722003-05-06 21:32:22 +00001273 // Declare that EAX & EDX are live on exit
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001274 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
1275 .addReg(X86::ESP);
Chris Lattner3e130a22003-01-13 00:32:26 +00001276 break;
Chris Lattner29bf0622004-04-06 01:21:00 +00001277 }
Chris Lattner94af4142002-12-25 05:13:53 +00001278 default:
Chris Lattner3e130a22003-01-13 00:32:26 +00001279 visitInstruction(I);
Chris Lattner94af4142002-12-25 05:13:53 +00001280 }
Chris Lattner43189d12002-11-17 20:07:45 +00001281 // Emit a 'ret' instruction
Chris Lattner94af4142002-12-25 05:13:53 +00001282 BuildMI(BB, X86::RET, 0);
Chris Lattner72614082002-10-25 22:55:53 +00001283}
1284
Chris Lattner55f6fab2003-01-16 18:07:23 +00001285// getBlockAfter - Return the basic block which occurs lexically after the
1286// specified one.
1287static inline BasicBlock *getBlockAfter(BasicBlock *BB) {
1288 Function::iterator I = BB; ++I; // Get iterator to next block
1289 return I != BB->getParent()->end() ? &*I : 0;
1290}
1291
Chris Lattner51b49a92002-11-02 19:45:49 +00001292/// visitBranchInst - Handle conditional and unconditional branches here. Note
1293/// that since code layout is frozen at this point, that if we are trying to
1294/// jump to a block that is the immediate successor of the current block, we can
Chris Lattner6d40c192003-01-16 16:43:00 +00001295/// just make a fall-through (but we don't currently).
Chris Lattner51b49a92002-11-02 19:45:49 +00001296///
Chris Lattner94af4142002-12-25 05:13:53 +00001297void ISel::visitBranchInst(BranchInst &BI) {
Chris Lattner55f6fab2003-01-16 18:07:23 +00001298 BasicBlock *NextBB = getBlockAfter(BI.getParent()); // BB after current one
1299
1300 if (!BI.isConditional()) { // Unconditional branch?
Chris Lattnercf93cdd2004-01-30 22:13:44 +00001301 if (BI.getSuccessor(0) != NextBB)
Chris Lattner55f6fab2003-01-16 18:07:23 +00001302 BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(0));
Chris Lattner6d40c192003-01-16 16:43:00 +00001303 return;
1304 }
1305
1306 // See if we can fold the setcc into the branch itself...
Chris Lattner307ecba2004-03-30 22:39:09 +00001307 SetCondInst *SCI = canFoldSetCCIntoBranchOrSelect(BI.getCondition());
Chris Lattner6d40c192003-01-16 16:43:00 +00001308 if (SCI == 0) {
1309 // Nope, cannot fold setcc into this branch. Emit a branch on a condition
1310 // computed some other way...
Chris Lattner065faeb2002-12-28 20:24:02 +00001311 unsigned condReg = getReg(BI.getCondition());
Chris Lattner68626c22004-03-31 22:22:36 +00001312 BuildMI(BB, X86::TEST8rr, 2).addReg(condReg).addReg(condReg);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001313 if (BI.getSuccessor(1) == NextBB) {
1314 if (BI.getSuccessor(0) != NextBB)
1315 BuildMI(BB, X86::JNE, 1).addPCDisp(BI.getSuccessor(0));
1316 } else {
1317 BuildMI(BB, X86::JE, 1).addPCDisp(BI.getSuccessor(1));
1318
1319 if (BI.getSuccessor(0) != NextBB)
1320 BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(0));
1321 }
Chris Lattner6d40c192003-01-16 16:43:00 +00001322 return;
Chris Lattner94af4142002-12-25 05:13:53 +00001323 }
Chris Lattner6d40c192003-01-16 16:43:00 +00001324
1325 unsigned OpNum = getSetCCNumber(SCI->getOpcode());
Chris Lattner58c41fe2003-08-24 19:19:47 +00001326 MachineBasicBlock::iterator MII = BB->end();
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001327 OpNum = EmitComparison(OpNum, SCI->getOperand(0), SCI->getOperand(1), BB,MII);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001328
1329 const Type *CompTy = SCI->getOperand(0)->getType();
1330 bool isSigned = CompTy->isSigned() && getClassB(CompTy) != cFP;
Chris Lattner6d40c192003-01-16 16:43:00 +00001331
Chris Lattnerb2acc512003-10-19 21:09:10 +00001332
Chris Lattner6d40c192003-01-16 16:43:00 +00001333 // LLVM -> X86 signed X86 unsigned
1334 // ----- ---------- ------------
1335 // seteq -> je je
1336 // setne -> jne jne
1337 // setlt -> jl jb
Chris Lattner55f6fab2003-01-16 18:07:23 +00001338 // setge -> jge jae
Chris Lattner6d40c192003-01-16 16:43:00 +00001339 // setgt -> jg ja
1340 // setle -> jle jbe
Chris Lattnerb2acc512003-10-19 21:09:10 +00001341 // ----
1342 // js // Used by comparison with 0 optimization
1343 // jns
1344
1345 static const unsigned OpcodeTab[2][8] = {
1346 { X86::JE, X86::JNE, X86::JB, X86::JAE, X86::JA, X86::JBE, 0, 0 },
1347 { X86::JE, X86::JNE, X86::JL, X86::JGE, X86::JG, X86::JLE,
1348 X86::JS, X86::JNS },
Chris Lattner6d40c192003-01-16 16:43:00 +00001349 };
1350
Chris Lattner55f6fab2003-01-16 18:07:23 +00001351 if (BI.getSuccessor(0) != NextBB) {
1352 BuildMI(BB, OpcodeTab[isSigned][OpNum], 1).addPCDisp(BI.getSuccessor(0));
1353 if (BI.getSuccessor(1) != NextBB)
1354 BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(1));
1355 } else {
1356 // Change to the inverse condition...
1357 if (BI.getSuccessor(1) != NextBB) {
1358 OpNum ^= 1;
1359 BuildMI(BB, OpcodeTab[isSigned][OpNum], 1).addPCDisp(BI.getSuccessor(1));
1360 }
1361 }
Chris Lattner2df035b2002-11-02 19:27:56 +00001362}
1363
Chris Lattner3e130a22003-01-13 00:32:26 +00001364
1365/// doCall - This emits an abstract call instruction, setting up the arguments
1366/// and the return value as appropriate. For the actual function call itself,
1367/// it inserts the specified CallMI instruction into the stream.
1368///
1369void ISel::doCall(const ValueRecord &Ret, MachineInstr *CallMI,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001370 const std::vector<ValueRecord> &Args) {
Chris Lattner3e130a22003-01-13 00:32:26 +00001371
Chris Lattner065faeb2002-12-28 20:24:02 +00001372 // Count how many bytes are to be pushed on the stack...
1373 unsigned NumBytes = 0;
Misha Brukman0d2cf3a2002-12-04 19:22:53 +00001374
Chris Lattner3e130a22003-01-13 00:32:26 +00001375 if (!Args.empty()) {
1376 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1377 switch (getClassB(Args[i].Ty)) {
Chris Lattner065faeb2002-12-28 20:24:02 +00001378 case cByte: case cShort: case cInt:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001379 NumBytes += 4; break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001380 case cLong:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001381 NumBytes += 8; break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001382 case cFP:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001383 NumBytes += Args[i].Ty == Type::FloatTy ? 4 : 8;
1384 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001385 default: assert(0 && "Unknown class!");
1386 }
1387
1388 // Adjust the stack pointer for the new arguments...
Chris Lattneree352852004-02-29 07:22:16 +00001389 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(NumBytes);
Chris Lattner065faeb2002-12-28 20:24:02 +00001390
1391 // Arguments go on the stack in reverse order, as specified by the ABI.
1392 unsigned ArgOffset = 0;
Chris Lattner3e130a22003-01-13 00:32:26 +00001393 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattnerce6096f2004-03-01 02:34:08 +00001394 unsigned ArgReg;
Chris Lattner3e130a22003-01-13 00:32:26 +00001395 switch (getClassB(Args[i].Ty)) {
Chris Lattner065faeb2002-12-28 20:24:02 +00001396 case cByte:
Chris Lattner21585222004-03-01 02:42:43 +00001397 case cShort:
1398 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1399 // Zero/Sign extend constant, then stuff into memory.
1400 ConstantInt *Val = cast<ConstantInt>(Args[i].Val);
1401 Val = cast<ConstantInt>(ConstantExpr::getCast(Val, Type::IntTy));
1402 addRegOffset(BuildMI(BB, X86::MOV32mi, 5), X86::ESP, ArgOffset)
1403 .addImm(Val->getRawValue() & 0xFFFFFFFF);
1404 } else {
1405 // Promote arg to 32 bits wide into a temporary register...
1406 ArgReg = makeAnotherReg(Type::UIntTy);
1407 promote32(ArgReg, Args[i]);
1408 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1409 X86::ESP, ArgOffset).addReg(ArgReg);
1410 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001411 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001412 case cInt:
Chris Lattner21585222004-03-01 02:42:43 +00001413 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1414 unsigned Val = cast<ConstantInt>(Args[i].Val)->getRawValue();
1415 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1416 X86::ESP, ArgOffset).addImm(Val);
1417 } else {
1418 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1419 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1420 X86::ESP, ArgOffset).addReg(ArgReg);
1421 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001422 break;
Chris Lattner3e130a22003-01-13 00:32:26 +00001423 case cLong:
Chris Lattner92900a62004-04-06 03:23:00 +00001424 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1425 uint64_t Val = cast<ConstantInt>(Args[i].Val)->getRawValue();
1426 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1427 X86::ESP, ArgOffset).addImm(Val & ~0U);
1428 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1429 X86::ESP, ArgOffset+4).addImm(Val >> 32ULL);
1430 } else {
1431 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1432 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1433 X86::ESP, ArgOffset).addReg(ArgReg);
1434 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1435 X86::ESP, ArgOffset+4).addReg(ArgReg+1);
1436 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001437 ArgOffset += 4; // 8 byte entry, not 4.
1438 break;
1439
Chris Lattner065faeb2002-12-28 20:24:02 +00001440 case cFP:
Chris Lattnerce6096f2004-03-01 02:34:08 +00001441 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001442 if (Args[i].Ty == Type::FloatTy) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001443 addRegOffset(BuildMI(BB, X86::FST32m, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001444 X86::ESP, ArgOffset).addReg(ArgReg);
1445 } else {
1446 assert(Args[i].Ty == Type::DoubleTy && "Unknown FP type!");
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001447 addRegOffset(BuildMI(BB, X86::FST64m, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001448 X86::ESP, ArgOffset).addReg(ArgReg);
1449 ArgOffset += 4; // 8 byte entry, not 4.
1450 }
1451 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001452
Chris Lattner3e130a22003-01-13 00:32:26 +00001453 default: assert(0 && "Unknown class!");
Chris Lattner065faeb2002-12-28 20:24:02 +00001454 }
1455 ArgOffset += 4;
Chris Lattner94af4142002-12-25 05:13:53 +00001456 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001457 } else {
Chris Lattneree352852004-02-29 07:22:16 +00001458 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(0);
Chris Lattner94af4142002-12-25 05:13:53 +00001459 }
Chris Lattner6e49a4b2002-12-13 14:13:27 +00001460
Chris Lattner3e130a22003-01-13 00:32:26 +00001461 BB->push_back(CallMI);
Misha Brukman0d2cf3a2002-12-04 19:22:53 +00001462
Chris Lattneree352852004-02-29 07:22:16 +00001463 BuildMI(BB, X86::ADJCALLSTACKUP, 1).addImm(NumBytes);
Chris Lattnera3243642002-12-04 23:45:28 +00001464
1465 // If there is a return value, scavenge the result from the location the call
1466 // leaves it in...
1467 //
Chris Lattner3e130a22003-01-13 00:32:26 +00001468 if (Ret.Ty != Type::VoidTy) {
1469 unsigned DestClass = getClassB(Ret.Ty);
1470 switch (DestClass) {
Brian Gaeke20244b72002-12-12 15:33:40 +00001471 case cByte:
1472 case cShort:
1473 case cInt: {
1474 // Integral results are in %eax, or the appropriate portion
1475 // thereof.
1476 static const unsigned regRegMove[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001477 X86::MOV8rr, X86::MOV16rr, X86::MOV32rr
Brian Gaeke20244b72002-12-12 15:33:40 +00001478 };
1479 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX };
Chris Lattner3e130a22003-01-13 00:32:26 +00001480 BuildMI(BB, regRegMove[DestClass], 1, Ret.Reg).addReg(AReg[DestClass]);
Chris Lattner4fa1acc2002-12-04 23:50:28 +00001481 break;
Brian Gaeke20244b72002-12-12 15:33:40 +00001482 }
Chris Lattner94af4142002-12-25 05:13:53 +00001483 case cFP: // Floating-point return values live in %ST(0)
Chris Lattner3e130a22003-01-13 00:32:26 +00001484 BuildMI(BB, X86::FpGETRESULT, 1, Ret.Reg);
Brian Gaeke20244b72002-12-12 15:33:40 +00001485 break;
Chris Lattner3e130a22003-01-13 00:32:26 +00001486 case cLong: // Long values are left in EDX:EAX
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001487 BuildMI(BB, X86::MOV32rr, 1, Ret.Reg).addReg(X86::EAX);
1488 BuildMI(BB, X86::MOV32rr, 1, Ret.Reg+1).addReg(X86::EDX);
Chris Lattner3e130a22003-01-13 00:32:26 +00001489 break;
1490 default: assert(0 && "Unknown class!");
Chris Lattner4fa1acc2002-12-04 23:50:28 +00001491 }
Chris Lattnera3243642002-12-04 23:45:28 +00001492 }
Brian Gaekefa8d5712002-11-22 11:07:01 +00001493}
Chris Lattner2df035b2002-11-02 19:27:56 +00001494
Chris Lattner3e130a22003-01-13 00:32:26 +00001495
1496/// visitCallInst - Push args on stack and do a procedure call instruction.
1497void ISel::visitCallInst(CallInst &CI) {
1498 MachineInstr *TheCall;
1499 if (Function *F = CI.getCalledFunction()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001500 // Is it an intrinsic function call?
Brian Gaeked0fde302003-11-11 22:41:34 +00001501 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001502 visitIntrinsicCall(ID, CI); // Special intrinsics are not handled here
1503 return;
1504 }
1505
Chris Lattner3e130a22003-01-13 00:32:26 +00001506 // Emit a CALL instruction with PC-relative displacement.
1507 TheCall = BuildMI(X86::CALLpcrel32, 1).addGlobalAddress(F, true);
1508 } else { // Emit an indirect call...
1509 unsigned Reg = getReg(CI.getCalledValue());
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001510 TheCall = BuildMI(X86::CALL32r, 1).addReg(Reg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001511 }
1512
1513 std::vector<ValueRecord> Args;
1514 for (unsigned i = 1, e = CI.getNumOperands(); i != e; ++i)
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001515 Args.push_back(ValueRecord(CI.getOperand(i)));
Chris Lattner3e130a22003-01-13 00:32:26 +00001516
1517 unsigned DestReg = CI.getType() != Type::VoidTy ? getReg(CI) : 0;
1518 doCall(ValueRecord(DestReg, CI.getType()), TheCall, Args);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001519}
Chris Lattner3e130a22003-01-13 00:32:26 +00001520
Chris Lattneraeb54b82003-08-28 21:23:43 +00001521
Chris Lattner44827152003-12-28 09:47:19 +00001522/// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
1523/// function, lowering any calls to unknown intrinsic functions into the
1524/// equivalent LLVM code.
Misha Brukman538607f2004-03-01 23:53:11 +00001525///
Chris Lattner44827152003-12-28 09:47:19 +00001526void ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) {
1527 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1528 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
1529 if (CallInst *CI = dyn_cast<CallInst>(I++))
1530 if (Function *F = CI->getCalledFunction())
1531 switch (F->getIntrinsicID()) {
Chris Lattneraed386e2003-12-28 09:53:23 +00001532 case Intrinsic::not_intrinsic:
Chris Lattner317201d2004-03-13 00:24:00 +00001533 case Intrinsic::vastart:
1534 case Intrinsic::vacopy:
1535 case Intrinsic::vaend:
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001536 case Intrinsic::returnaddress:
1537 case Intrinsic::frameaddress:
Chris Lattner915e5e52004-02-12 17:53:22 +00001538 case Intrinsic::memcpy:
Chris Lattner2a0f2242004-02-14 04:46:05 +00001539 case Intrinsic::memset:
John Criswell4ffff9e2004-04-08 20:31:47 +00001540 case Intrinsic::readport:
1541 case Intrinsic::writeport:
Chris Lattner44827152003-12-28 09:47:19 +00001542 // We directly implement these intrinsics
1543 break;
John Criswelle5a4c152004-04-13 22:13:14 +00001544 case Intrinsic::readio: {
1545 // On X86, memory operations are in-order. Lower this intrinsic
1546 // into a volatile load.
1547 Instruction *Before = CI->getPrev();
1548 LoadInst * LI = new LoadInst (CI->getOperand(1), "", true, CI);
1549 CI->replaceAllUsesWith (LI);
1550 BB->getInstList().erase (CI);
John Criswelle5a4c152004-04-13 22:13:14 +00001551 break;
1552 }
1553 case Intrinsic::writeio: {
1554 // On X86, memory operations are in-order. Lower this intrinsic
1555 // into a volatile store.
1556 Instruction *Before = CI->getPrev();
1557 StoreInst * LI = new StoreInst (CI->getOperand(1),
1558 CI->getOperand(2), true, CI);
1559 CI->replaceAllUsesWith (LI);
1560 BB->getInstList().erase (CI);
John Criswelle5a4c152004-04-13 22:13:14 +00001561 break;
1562 }
Chris Lattner44827152003-12-28 09:47:19 +00001563 default:
1564 // All other intrinsic calls we must lower.
1565 Instruction *Before = CI->getPrev();
Chris Lattnerf70e0c22003-12-28 21:23:38 +00001566 TM.getIntrinsicLowering().LowerIntrinsicCall(CI);
Chris Lattner44827152003-12-28 09:47:19 +00001567 if (Before) { // Move iterator to instruction after call
1568 I = Before; ++I;
1569 } else {
1570 I = BB->begin();
1571 }
1572 }
1573
1574}
1575
Brian Gaeked0fde302003-11-11 22:41:34 +00001576void ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001577 unsigned TmpReg1, TmpReg2;
1578 switch (ID) {
Chris Lattner5634b9f2004-03-13 00:24:52 +00001579 case Intrinsic::vastart:
Chris Lattnereca195e2003-05-08 19:44:13 +00001580 // Get the address of the first vararg value...
Chris Lattner73815062003-10-18 05:56:40 +00001581 TmpReg1 = getReg(CI);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001582 addFrameReference(BuildMI(BB, X86::LEA32r, 5, TmpReg1), VarArgsFrameIndex);
Chris Lattnereca195e2003-05-08 19:44:13 +00001583 return;
1584
Chris Lattner5634b9f2004-03-13 00:24:52 +00001585 case Intrinsic::vacopy:
Chris Lattner73815062003-10-18 05:56:40 +00001586 TmpReg1 = getReg(CI);
1587 TmpReg2 = getReg(CI.getOperand(1));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001588 BuildMI(BB, X86::MOV32rr, 1, TmpReg1).addReg(TmpReg2);
Chris Lattnereca195e2003-05-08 19:44:13 +00001589 return;
Chris Lattner5634b9f2004-03-13 00:24:52 +00001590 case Intrinsic::vaend: return; // Noop on X86
Chris Lattnereca195e2003-05-08 19:44:13 +00001591
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001592 case Intrinsic::returnaddress:
1593 case Intrinsic::frameaddress:
1594 TmpReg1 = getReg(CI);
1595 if (cast<Constant>(CI.getOperand(1))->isNullValue()) {
1596 if (ID == Intrinsic::returnaddress) {
1597 // Just load the return address
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001598 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, TmpReg1),
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001599 ReturnAddressIndex);
1600 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001601 addFrameReference(BuildMI(BB, X86::LEA32r, 4, TmpReg1),
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001602 ReturnAddressIndex, -4);
1603 }
1604 } else {
1605 // Values other than zero are not implemented yet.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001606 BuildMI(BB, X86::MOV32ri, 1, TmpReg1).addImm(0);
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001607 }
1608 return;
1609
Chris Lattner915e5e52004-02-12 17:53:22 +00001610 case Intrinsic::memcpy: {
1611 assert(CI.getNumOperands() == 5 && "Illegal llvm.memcpy call!");
1612 unsigned Align = 1;
1613 if (ConstantInt *AlignC = dyn_cast<ConstantInt>(CI.getOperand(4))) {
1614 Align = AlignC->getRawValue();
1615 if (Align == 0) Align = 1;
1616 }
1617
1618 // Turn the byte code into # iterations
Chris Lattner915e5e52004-02-12 17:53:22 +00001619 unsigned CountReg;
Chris Lattner2a0f2242004-02-14 04:46:05 +00001620 unsigned Opcode;
Chris Lattner915e5e52004-02-12 17:53:22 +00001621 switch (Align & 3) {
1622 case 2: // WORD aligned
Chris Lattner07122832004-02-13 23:36:47 +00001623 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
1624 CountReg = getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/2));
1625 } else {
1626 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001627 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001628 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
Chris Lattner07122832004-02-13 23:36:47 +00001629 }
Chris Lattner2a0f2242004-02-14 04:46:05 +00001630 Opcode = X86::REP_MOVSW;
Chris Lattner915e5e52004-02-12 17:53:22 +00001631 break;
1632 case 0: // DWORD aligned
Chris Lattner07122832004-02-13 23:36:47 +00001633 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
1634 CountReg = getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/4));
1635 } else {
1636 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001637 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001638 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
Chris Lattner07122832004-02-13 23:36:47 +00001639 }
Chris Lattner2a0f2242004-02-14 04:46:05 +00001640 Opcode = X86::REP_MOVSD;
Chris Lattner915e5e52004-02-12 17:53:22 +00001641 break;
Chris Lattner8dd8d262004-02-26 01:20:02 +00001642 default: // BYTE aligned
Chris Lattner07122832004-02-13 23:36:47 +00001643 CountReg = getReg(CI.getOperand(3));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001644 Opcode = X86::REP_MOVSB;
Chris Lattner915e5e52004-02-12 17:53:22 +00001645 break;
1646 }
1647
1648 // No matter what the alignment is, we put the source in ESI, the
1649 // destination in EDI, and the count in ECX.
1650 TmpReg1 = getReg(CI.getOperand(1));
1651 TmpReg2 = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001652 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
1653 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
1654 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001655 BuildMI(BB, Opcode, 0);
1656 return;
1657 }
1658 case Intrinsic::memset: {
1659 assert(CI.getNumOperands() == 5 && "Illegal llvm.memset call!");
1660 unsigned Align = 1;
1661 if (ConstantInt *AlignC = dyn_cast<ConstantInt>(CI.getOperand(4))) {
1662 Align = AlignC->getRawValue();
1663 if (Align == 0) Align = 1;
Chris Lattner915e5e52004-02-12 17:53:22 +00001664 }
1665
Chris Lattner2a0f2242004-02-14 04:46:05 +00001666 // Turn the byte code into # iterations
Chris Lattner2a0f2242004-02-14 04:46:05 +00001667 unsigned CountReg;
1668 unsigned Opcode;
1669 if (ConstantInt *ValC = dyn_cast<ConstantInt>(CI.getOperand(2))) {
1670 unsigned Val = ValC->getRawValue() & 255;
1671
1672 // If the value is a constant, then we can potentially use larger copies.
1673 switch (Align & 3) {
1674 case 2: // WORD aligned
1675 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
Chris Lattner300d0ed2004-02-14 06:00:36 +00001676 CountReg =getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/2));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001677 } else {
1678 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001679 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001680 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001681 }
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001682 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001683 Opcode = X86::REP_STOSW;
1684 break;
1685 case 0: // DWORD aligned
1686 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
Chris Lattner300d0ed2004-02-14 06:00:36 +00001687 CountReg =getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/4));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001688 } else {
1689 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001690 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001691 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001692 }
1693 Val = (Val << 8) | Val;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001694 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001695 Opcode = X86::REP_STOSD;
1696 break;
Chris Lattner8dd8d262004-02-26 01:20:02 +00001697 default: // BYTE aligned
Chris Lattner2a0f2242004-02-14 04:46:05 +00001698 CountReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001699 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001700 Opcode = X86::REP_STOSB;
1701 break;
1702 }
1703 } else {
1704 // If it's not a constant value we are storing, just fall back. We could
1705 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
1706 unsigned ValReg = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001707 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001708 CountReg = getReg(CI.getOperand(3));
1709 Opcode = X86::REP_STOSB;
1710 }
1711
1712 // No matter what the alignment is, we put the source in ESI, the
1713 // destination in EDI, and the count in ECX.
1714 TmpReg1 = getReg(CI.getOperand(1));
1715 //TmpReg2 = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001716 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
1717 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001718 BuildMI(BB, Opcode, 0);
Chris Lattner915e5e52004-02-12 17:53:22 +00001719 return;
1720 }
1721
Chris Lattner87e18de2004-04-13 17:20:37 +00001722 case Intrinsic::readport: {
1723 // First, determine that the size of the operand falls within the acceptable
1724 // range for this architecture.
John Criswell4ffff9e2004-04-08 20:31:47 +00001725 //
Chris Lattner87e18de2004-04-13 17:20:37 +00001726 if (getClassB(CI.getOperand(1)->getType()) != cShort) {
John Criswellca6ea0f2004-04-08 22:39:13 +00001727 std::cerr << "llvm.readport: Address size is not 16 bits\n";
Chris Lattner87e18de2004-04-13 17:20:37 +00001728 exit(1);
John Criswellca6ea0f2004-04-08 22:39:13 +00001729 }
John Criswell4ffff9e2004-04-08 20:31:47 +00001730
John Criswell4ffff9e2004-04-08 20:31:47 +00001731 // Now, move the I/O port address into the DX register and use the IN
1732 // instruction to get the input data.
1733 //
Chris Lattner87e18de2004-04-13 17:20:37 +00001734 unsigned Class = getClass(CI.getCalledFunction()->getReturnType());
1735 unsigned DestReg = getReg(CI);
John Criswell4ffff9e2004-04-08 20:31:47 +00001736
Chris Lattner87e18de2004-04-13 17:20:37 +00001737 // If the port is a single-byte constant, use the immediate form.
1738 if (ConstantInt *C = dyn_cast<ConstantInt>(CI.getOperand(1)))
1739 if ((C->getRawValue() & 255) == C->getRawValue()) {
1740 switch (Class) {
1741 case cByte:
1742 BuildMI(BB, X86::IN8ri, 1).addImm((unsigned char)C->getRawValue());
1743 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AL);
1744 return;
1745 case cShort:
1746 BuildMI(BB, X86::IN16ri, 1).addImm((unsigned char)C->getRawValue());
1747 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AX);
1748 return;
1749 case cInt:
1750 BuildMI(BB, X86::IN32ri, 1).addImm((unsigned char)C->getRawValue());
1751 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::EAX);
1752 return;
1753 }
1754 }
1755
1756 unsigned Reg = getReg(CI.getOperand(1));
1757 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
1758 switch (Class) {
1759 case cByte:
1760 BuildMI(BB, X86::IN8rr, 0);
1761 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AL);
1762 break;
1763 case cShort:
1764 BuildMI(BB, X86::IN16rr, 0);
1765 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AX);
1766 break;
1767 case cInt:
1768 BuildMI(BB, X86::IN32rr, 0);
1769 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::EAX);
1770 break;
1771 default:
1772 std::cerr << "Cannot do input on this data type";
John Criswellca6ea0f2004-04-08 22:39:13 +00001773 exit (1);
1774 }
John Criswell4ffff9e2004-04-08 20:31:47 +00001775 return;
Chris Lattner87e18de2004-04-13 17:20:37 +00001776 }
John Criswell4ffff9e2004-04-08 20:31:47 +00001777
Chris Lattner87e18de2004-04-13 17:20:37 +00001778 case Intrinsic::writeport: {
1779 // First, determine that the size of the operand falls within the
1780 // acceptable range for this architecture.
1781 if (getClass(CI.getOperand(2)->getType()) != cShort) {
1782 std::cerr << "llvm.writeport: Address size is not 16 bits\n";
1783 exit(1);
1784 }
1785
1786 unsigned Class = getClassB(CI.getOperand(1)->getType());
1787 unsigned ValReg = getReg(CI.getOperand(1));
1788 switch (Class) {
1789 case cByte:
1790 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
1791 break;
1792 case cShort:
1793 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(ValReg);
1794 break;
1795 case cInt:
1796 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(ValReg);
1797 break;
1798 default:
1799 std::cerr << "llvm.writeport: invalid data type for X86 target";
1800 exit(1);
1801 }
1802
1803
1804 // If the port is a single-byte constant, use the immediate form.
1805 if (ConstantInt *C = dyn_cast<ConstantInt>(CI.getOperand(2)))
1806 if ((C->getRawValue() & 255) == C->getRawValue()) {
1807 static const unsigned O[] = { X86::OUT8ir, X86::OUT16ir, X86::OUT32ir };
1808 BuildMI(BB, O[Class], 1).addImm((unsigned char)C->getRawValue());
1809 return;
1810 }
1811
1812 // Otherwise, move the I/O port address into the DX register and the value
1813 // to write into the AL/AX/EAX register.
1814 static const unsigned Opc[] = { X86::OUT8rr, X86::OUT16rr, X86::OUT32rr };
1815 unsigned Reg = getReg(CI.getOperand(2));
1816 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
1817 BuildMI(BB, Opc[Class], 0);
1818 return;
1819 }
1820
Chris Lattner44827152003-12-28 09:47:19 +00001821 default: assert(0 && "Error: unknown intrinsics should have been lowered!");
Chris Lattnereca195e2003-05-08 19:44:13 +00001822 }
1823}
1824
Chris Lattner7dee5da2004-03-08 01:58:35 +00001825static bool isSafeToFoldLoadIntoInstruction(LoadInst &LI, Instruction &User) {
1826 if (LI.getParent() != User.getParent())
1827 return false;
1828 BasicBlock::iterator It = &LI;
1829 // Check all of the instructions between the load and the user. We should
1830 // really use alias analysis here, but for now we just do something simple.
1831 for (++It; It != BasicBlock::iterator(&User); ++It) {
1832 switch (It->getOpcode()) {
Chris Lattner85c84e72004-03-18 06:29:54 +00001833 case Instruction::Free:
Chris Lattner7dee5da2004-03-08 01:58:35 +00001834 case Instruction::Store:
1835 case Instruction::Call:
1836 case Instruction::Invoke:
1837 return false;
Chris Lattner133dbb12004-04-12 03:02:48 +00001838 case Instruction::Load:
1839 if (cast<LoadInst>(It)->isVolatile() && LI.isVolatile())
1840 return false;
1841 break;
Chris Lattner7dee5da2004-03-08 01:58:35 +00001842 }
1843 }
1844 return true;
1845}
1846
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001847/// visitSimpleBinary - Implement simple binary operators for integral types...
1848/// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or, 4 for
1849/// Xor.
Misha Brukman538607f2004-03-01 23:53:11 +00001850///
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001851void ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) {
1852 unsigned DestReg = getReg(B);
1853 MachineBasicBlock::iterator MI = BB->end();
Chris Lattner721d2d42004-03-08 01:18:36 +00001854 Value *Op0 = B.getOperand(0), *Op1 = B.getOperand(1);
1855
Chris Lattner7dee5da2004-03-08 01:58:35 +00001856 // Special case: op Reg, load [mem]
1857 if (isa<LoadInst>(Op0) && !isa<LoadInst>(Op1))
1858 if (!B.swapOperands())
1859 std::swap(Op0, Op1); // Make sure any loads are in the RHS.
1860
1861 unsigned Class = getClassB(B.getType());
Chris Lattner95157f72004-04-11 22:05:45 +00001862 if (isa<LoadInst>(Op1) && Class != cLong &&
Chris Lattner7dee5da2004-03-08 01:58:35 +00001863 isSafeToFoldLoadIntoInstruction(*cast<LoadInst>(Op1), B)) {
1864
Chris Lattner95157f72004-04-11 22:05:45 +00001865 unsigned Opcode;
1866 if (Class != cFP) {
1867 static const unsigned OpcodeTab[][3] = {
1868 // Arithmetic operators
1869 { X86::ADD8rm, X86::ADD16rm, X86::ADD32rm }, // ADD
1870 { X86::SUB8rm, X86::SUB16rm, X86::SUB32rm }, // SUB
1871
1872 // Bitwise operators
1873 { X86::AND8rm, X86::AND16rm, X86::AND32rm }, // AND
1874 { X86:: OR8rm, X86:: OR16rm, X86:: OR32rm }, // OR
1875 { X86::XOR8rm, X86::XOR16rm, X86::XOR32rm }, // XOR
1876 };
1877 Opcode = OpcodeTab[OperatorClass][Class];
1878 } else {
1879 static const unsigned OpcodeTab[][2] = {
1880 { X86::FADD32m, X86::FADD64m }, // ADD
1881 { X86::FSUB32m, X86::FSUB64m }, // SUB
1882 };
1883 const Type *Ty = Op0->getType();
1884 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
1885 Opcode = OpcodeTab[OperatorClass][Ty == Type::DoubleTy];
1886 }
Chris Lattner7dee5da2004-03-08 01:58:35 +00001887
1888 unsigned BaseReg, Scale, IndexReg, Disp;
1889 getAddressingMode(cast<LoadInst>(Op1)->getOperand(0), BaseReg,
1890 Scale, IndexReg, Disp);
1891
1892 unsigned Op0r = getReg(Op0);
1893 addFullAddress(BuildMI(BB, Opcode, 2, DestReg).addReg(Op0r),
1894 BaseReg, Scale, IndexReg, Disp);
1895 return;
1896 }
1897
Chris Lattner95157f72004-04-11 22:05:45 +00001898 // If this is a floating point subtract, check to see if we can fold the first
1899 // operand in.
1900 if (Class == cFP && OperatorClass == 1 &&
1901 isa<LoadInst>(Op0) &&
1902 isSafeToFoldLoadIntoInstruction(*cast<LoadInst>(Op0), B)) {
1903 const Type *Ty = Op0->getType();
1904 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
1905 unsigned Opcode = Ty == Type::FloatTy ? X86::FSUBR32m : X86::FSUBR64m;
1906
1907 unsigned BaseReg, Scale, IndexReg, Disp;
1908 getAddressingMode(cast<LoadInst>(Op0)->getOperand(0), BaseReg,
1909 Scale, IndexReg, Disp);
1910
1911 unsigned Op1r = getReg(Op1);
1912 addFullAddress(BuildMI(BB, Opcode, 2, DestReg).addReg(Op1r),
1913 BaseReg, Scale, IndexReg, Disp);
1914 return;
1915 }
1916
Chris Lattner721d2d42004-03-08 01:18:36 +00001917 emitSimpleBinaryOperation(BB, MI, Op0, Op1, OperatorClass, DestReg);
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001918}
Chris Lattner3e130a22003-01-13 00:32:26 +00001919
Chris Lattner6621ed92004-04-11 21:23:56 +00001920
1921/// emitBinaryFPOperation - This method handles emission of floating point
1922/// Add (0), Sub (1), Mul (2), and Div (3) operations.
1923void ISel::emitBinaryFPOperation(MachineBasicBlock *BB,
1924 MachineBasicBlock::iterator IP,
1925 Value *Op0, Value *Op1,
1926 unsigned OperatorClass, unsigned DestReg) {
1927
1928 // Special case: op Reg, <const fp>
1929 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1))
1930 if (!Op1C->isExactlyValue(+0.0) && !Op1C->isExactlyValue(+1.0)) {
1931 // Create a constant pool entry for this constant.
1932 MachineConstantPool *CP = F->getConstantPool();
1933 unsigned CPI = CP->getConstantPoolIndex(Op1C);
1934 const Type *Ty = Op1->getType();
1935
1936 static const unsigned OpcodeTab[][4] = {
1937 { X86::FADD32m, X86::FSUB32m, X86::FMUL32m, X86::FDIV32m }, // Float
1938 { X86::FADD64m, X86::FSUB64m, X86::FMUL64m, X86::FDIV64m }, // Double
1939 };
1940
1941 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
1942 unsigned Opcode = OpcodeTab[Ty != Type::FloatTy][OperatorClass];
1943 unsigned Op0r = getReg(Op0, BB, IP);
1944 addConstantPoolReference(BuildMI(*BB, IP, Opcode, 5,
1945 DestReg).addReg(Op0r), CPI);
1946 return;
1947 }
1948
Chris Lattner13c07fe2004-04-12 00:12:04 +00001949 // Special case: R1 = op <const fp>, R2
Chris Lattner6621ed92004-04-11 21:23:56 +00001950 if (ConstantFP *CFP = dyn_cast<ConstantFP>(Op0))
1951 if (CFP->isExactlyValue(-0.0) && OperatorClass == 1) {
1952 // -0.0 - X === -X
1953 unsigned op1Reg = getReg(Op1, BB, IP);
1954 BuildMI(*BB, IP, X86::FCHS, 1, DestReg).addReg(op1Reg);
1955 return;
1956 } else if (!CFP->isExactlyValue(+0.0) && !CFP->isExactlyValue(+1.0)) {
Chris Lattner13c07fe2004-04-12 00:12:04 +00001957 // R1 = op CST, R2 --> R1 = opr R2, CST
Chris Lattner6621ed92004-04-11 21:23:56 +00001958
1959 // Create a constant pool entry for this constant.
1960 MachineConstantPool *CP = F->getConstantPool();
1961 unsigned CPI = CP->getConstantPoolIndex(CFP);
1962 const Type *Ty = CFP->getType();
1963
1964 static const unsigned OpcodeTab[][4] = {
1965 { X86::FADD32m, X86::FSUBR32m, X86::FMUL32m, X86::FDIVR32m }, // Float
1966 { X86::FADD64m, X86::FSUBR64m, X86::FMUL64m, X86::FDIVR64m }, // Double
1967 };
1968
1969 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
1970 unsigned Opcode = OpcodeTab[Ty != Type::FloatTy][OperatorClass];
1971 unsigned Op1r = getReg(Op1, BB, IP);
1972 addConstantPoolReference(BuildMI(*BB, IP, Opcode, 5,
1973 DestReg).addReg(Op1r), CPI);
1974 return;
1975 }
1976
1977 // General case.
1978 static const unsigned OpcodeTab[4] = {
1979 X86::FpADD, X86::FpSUB, X86::FpMUL, X86::FpDIV
1980 };
1981
1982 unsigned Opcode = OpcodeTab[OperatorClass];
1983 unsigned Op0r = getReg(Op0, BB, IP);
1984 unsigned Op1r = getReg(Op1, BB, IP);
1985 BuildMI(*BB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r);
1986}
1987
Chris Lattnerb2acc512003-10-19 21:09:10 +00001988/// emitSimpleBinaryOperation - Implement simple binary operators for integral
1989/// types... OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for
1990/// Or, 4 for Xor.
Chris Lattner68aad932002-11-02 20:13:22 +00001991///
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001992/// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
1993/// and constant expression support.
Chris Lattnerb2acc512003-10-19 21:09:10 +00001994///
1995void ISel::emitSimpleBinaryOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00001996 MachineBasicBlock::iterator IP,
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001997 Value *Op0, Value *Op1,
Chris Lattnerb2acc512003-10-19 21:09:10 +00001998 unsigned OperatorClass, unsigned DestReg) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001999 unsigned Class = getClassB(Op0->getType());
Chris Lattnerb2acc512003-10-19 21:09:10 +00002000
Chris Lattner6621ed92004-04-11 21:23:56 +00002001 if (Class == cFP) {
2002 assert(OperatorClass < 2 && "No logical ops for FP!");
2003 emitBinaryFPOperation(MBB, IP, Op0, Op1, OperatorClass, DestReg);
2004 return;
2005 }
2006
Chris Lattnerb2acc512003-10-19 21:09:10 +00002007 // sub 0, X -> neg X
Chris Lattner48b0c972004-04-11 20:26:20 +00002008 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0))
2009 if (OperatorClass == 1 && CI->isNullValue()) {
2010 unsigned op1Reg = getReg(Op1, MBB, IP);
2011 static unsigned const NEGTab[] = {
2012 X86::NEG8r, X86::NEG16r, X86::NEG32r, 0, X86::NEG32r
2013 };
2014 BuildMI(*MBB, IP, NEGTab[Class], 1, DestReg).addReg(op1Reg);
2015
2016 if (Class == cLong) {
2017 // We just emitted: Dl = neg Sl
2018 // Now emit : T = addc Sh, 0
2019 // : Dh = neg T
2020 unsigned T = makeAnotherReg(Type::IntTy);
2021 BuildMI(*MBB, IP, X86::ADC32ri, 2, T).addReg(op1Reg+1).addImm(0);
2022 BuildMI(*MBB, IP, X86::NEG32r, 1, DestReg+1).addReg(T);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002023 }
Chris Lattner48b0c972004-04-11 20:26:20 +00002024 return;
2025 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002026
Chris Lattner48b0c972004-04-11 20:26:20 +00002027 // Special case: op Reg, <const int>
2028 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner721d2d42004-03-08 01:18:36 +00002029 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002030
Chris Lattner721d2d42004-03-08 01:18:36 +00002031 // xor X, -1 -> not X
2032 if (OperatorClass == 4 && Op1C->isAllOnesValue()) {
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002033 static unsigned const NOTTab[] = {
2034 X86::NOT8r, X86::NOT16r, X86::NOT32r, 0, X86::NOT32r
2035 };
Chris Lattner721d2d42004-03-08 01:18:36 +00002036 BuildMI(*MBB, IP, NOTTab[Class], 1, DestReg).addReg(Op0r);
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002037 if (Class == cLong) // Invert the top part too
2038 BuildMI(*MBB, IP, X86::NOT32r, 1, DestReg+1).addReg(Op0r+1);
Chris Lattner721d2d42004-03-08 01:18:36 +00002039 return;
2040 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002041
Chris Lattner721d2d42004-03-08 01:18:36 +00002042 // add X, -1 -> dec X
Chris Lattner06521672004-04-06 03:36:57 +00002043 if (OperatorClass == 0 && Op1C->isAllOnesValue() && Class != cLong) {
2044 // Note that we can't use dec for 64-bit decrements, because it does not
2045 // set the carry flag!
2046 static unsigned const DECTab[] = { X86::DEC8r, X86::DEC16r, X86::DEC32r };
Chris Lattner721d2d42004-03-08 01:18:36 +00002047 BuildMI(*MBB, IP, DECTab[Class], 1, DestReg).addReg(Op0r);
2048 return;
2049 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002050
Chris Lattner721d2d42004-03-08 01:18:36 +00002051 // add X, 1 -> inc X
Chris Lattner06521672004-04-06 03:36:57 +00002052 if (OperatorClass == 0 && Op1C->equalsInt(1) && Class != cLong) {
2053 // Note that we can't use inc for 64-bit increments, because it does not
2054 // set the carry flag!
2055 static unsigned const INCTab[] = { X86::INC8r, X86::INC16r, X86::INC32r };
Alkis Evlogimenosbee8a092004-04-02 18:11:32 +00002056 BuildMI(*MBB, IP, INCTab[Class], 1, DestReg).addReg(Op0r);
Chris Lattner721d2d42004-03-08 01:18:36 +00002057 return;
2058 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002059
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002060 static const unsigned OpcodeTab[][5] = {
Chris Lattner721d2d42004-03-08 01:18:36 +00002061 // Arithmetic operators
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002062 { X86::ADD8ri, X86::ADD16ri, X86::ADD32ri, 0, X86::ADD32ri }, // ADD
2063 { X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, X86::SUB32ri }, // SUB
Chris Lattnerb2acc512003-10-19 21:09:10 +00002064
Chris Lattner721d2d42004-03-08 01:18:36 +00002065 // Bitwise operators
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002066 { X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, X86::AND32ri }, // AND
2067 { X86:: OR8ri, X86:: OR16ri, X86:: OR32ri, 0, X86::OR32ri }, // OR
2068 { X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, X86::XOR32ri }, // XOR
Chris Lattner721d2d42004-03-08 01:18:36 +00002069 };
2070
Chris Lattner721d2d42004-03-08 01:18:36 +00002071 unsigned Opcode = OpcodeTab[OperatorClass][Class];
Chris Lattner33f7fa32004-04-06 03:15:53 +00002072 unsigned Op1l = cast<ConstantInt>(Op1C)->getRawValue();
Chris Lattner721d2d42004-03-08 01:18:36 +00002073
Chris Lattner33f7fa32004-04-06 03:15:53 +00002074 if (Class != cLong) {
2075 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addImm(Op1l);
2076 return;
Chris Lattner6621ed92004-04-11 21:23:56 +00002077 }
2078
2079 // If this is a long value and the high or low bits have a special
2080 // property, emit some special cases.
2081 unsigned Op1h = cast<ConstantInt>(Op1C)->getRawValue() >> 32LL;
2082
2083 // If the constant is zero in the low 32-bits, just copy the low part
2084 // across and apply the normal 32-bit operation to the high parts. There
2085 // will be no carry or borrow into the top.
2086 if (Op1l == 0) {
2087 if (OperatorClass != 2) // All but and...
2088 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg).addReg(Op0r);
2089 else
2090 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
2091 BuildMI(*MBB, IP, OpcodeTab[OperatorClass][cLong], 2, DestReg+1)
2092 .addReg(Op0r+1).addImm(Op1h);
Chris Lattner33f7fa32004-04-06 03:15:53 +00002093 return;
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002094 }
Chris Lattner6621ed92004-04-11 21:23:56 +00002095
2096 // If this is a logical operation and the top 32-bits are zero, just
2097 // operate on the lower 32.
2098 if (Op1h == 0 && OperatorClass > 1) {
2099 BuildMI(*MBB, IP, OpcodeTab[OperatorClass][cLong], 2, DestReg)
2100 .addReg(Op0r).addImm(Op1l);
2101 if (OperatorClass != 2) // All but and
2102 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg+1).addReg(Op0r+1);
2103 else
2104 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
2105 return;
2106 }
2107
2108 // TODO: We could handle lots of other special cases here, such as AND'ing
2109 // with 0xFFFFFFFF00000000 -> noop, etc.
2110
2111 // Otherwise, code generate the full operation with a constant.
2112 static const unsigned TopTab[] = {
2113 X86::ADC32ri, X86::SBB32ri, X86::AND32ri, X86::OR32ri, X86::XOR32ri
2114 };
2115
2116 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addImm(Op1l);
2117 BuildMI(*MBB, IP, TopTab[OperatorClass], 2, DestReg+1)
2118 .addReg(Op0r+1).addImm(Op1h);
2119 return;
Chris Lattner721d2d42004-03-08 01:18:36 +00002120 }
2121
2122 // Finally, handle the general case now.
Chris Lattner7ba92302004-04-06 02:13:25 +00002123 static const unsigned OpcodeTab[][5] = {
Chris Lattner721d2d42004-03-08 01:18:36 +00002124 // Arithmetic operators
Chris Lattner6621ed92004-04-11 21:23:56 +00002125 { X86::ADD8rr, X86::ADD16rr, X86::ADD32rr, 0, X86::ADD32rr }, // ADD
2126 { X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, 0, X86::SUB32rr }, // SUB
Chris Lattner721d2d42004-03-08 01:18:36 +00002127
Chris Lattnerb2acc512003-10-19 21:09:10 +00002128 // Bitwise operators
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002129 { X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, X86::AND32rr }, // AND
2130 { X86:: OR8rr, X86:: OR16rr, X86:: OR32rr, 0, X86:: OR32rr }, // OR
2131 { X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, X86::XOR32rr }, // XOR
Chris Lattnerb2acc512003-10-19 21:09:10 +00002132 };
Chris Lattner721d2d42004-03-08 01:18:36 +00002133
Chris Lattnerb2acc512003-10-19 21:09:10 +00002134 unsigned Opcode = OpcodeTab[OperatorClass][Class];
Chris Lattner721d2d42004-03-08 01:18:36 +00002135 unsigned Op0r = getReg(Op0, MBB, IP);
2136 unsigned Op1r = getReg(Op1, MBB, IP);
2137 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r);
2138
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002139 if (Class == cLong) { // Handle the upper 32 bits of long values...
Chris Lattner721d2d42004-03-08 01:18:36 +00002140 static const unsigned TopTab[] = {
2141 X86::ADC32rr, X86::SBB32rr, X86::AND32rr, X86::OR32rr, X86::XOR32rr
2142 };
2143 BuildMI(*MBB, IP, TopTab[OperatorClass], 2,
2144 DestReg+1).addReg(Op0r+1).addReg(Op1r+1);
2145 }
Chris Lattnere2954c82002-11-02 20:04:26 +00002146}
2147
Chris Lattner3e130a22003-01-13 00:32:26 +00002148/// doMultiply - Emit appropriate instructions to multiply together the
2149/// registers op0Reg and op1Reg, and put the result in DestReg. The type of the
2150/// result should be given as DestTy.
2151///
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002152void ISel::doMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI,
Chris Lattner3e130a22003-01-13 00:32:26 +00002153 unsigned DestReg, const Type *DestTy,
Chris Lattner8a307e82002-12-16 19:32:50 +00002154 unsigned op0Reg, unsigned op1Reg) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002155 unsigned Class = getClass(DestTy);
Chris Lattner94af4142002-12-25 05:13:53 +00002156 switch (Class) {
Chris Lattner0f1c4612003-06-21 17:16:58 +00002157 case cInt:
2158 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002159 BuildMI(*MBB, MBBI, Class == cInt ? X86::IMUL32rr:X86::IMUL16rr, 2, DestReg)
Chris Lattner0f1c4612003-06-21 17:16:58 +00002160 .addReg(op0Reg).addReg(op1Reg);
2161 return;
2162 case cByte:
2163 // Must use the MUL instruction, which forces use of AL...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002164 BuildMI(*MBB, MBBI, X86::MOV8rr, 1, X86::AL).addReg(op0Reg);
2165 BuildMI(*MBB, MBBI, X86::MUL8r, 1).addReg(op1Reg);
2166 BuildMI(*MBB, MBBI, X86::MOV8rr, 1, DestReg).addReg(X86::AL);
Chris Lattner0f1c4612003-06-21 17:16:58 +00002167 return;
Chris Lattner94af4142002-12-25 05:13:53 +00002168 default:
Chris Lattner3e130a22003-01-13 00:32:26 +00002169 case cLong: assert(0 && "doMultiply cannot operate on LONG values!");
Chris Lattner94af4142002-12-25 05:13:53 +00002170 }
Brian Gaeke20244b72002-12-12 15:33:40 +00002171}
2172
Chris Lattnerb2acc512003-10-19 21:09:10 +00002173// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
2174// returns zero when the input is not exactly a power of two.
2175static unsigned ExactLog2(unsigned Val) {
2176 if (Val == 0) return 0;
2177 unsigned Count = 0;
2178 while (Val != 1) {
2179 if (Val & 1) return 0;
2180 Val >>= 1;
2181 ++Count;
2182 }
2183 return Count+1;
2184}
2185
Chris Lattner462fa822004-04-11 20:56:28 +00002186
2187/// doMultiplyConst - This function is specialized to efficiently codegen an 8,
2188/// 16, or 32-bit integer multiply by a constant.
Chris Lattnerb2acc512003-10-19 21:09:10 +00002189void ISel::doMultiplyConst(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002190 MachineBasicBlock::iterator IP,
Chris Lattnerb2acc512003-10-19 21:09:10 +00002191 unsigned DestReg, const Type *DestTy,
2192 unsigned op0Reg, unsigned ConstRHS) {
Chris Lattner6ab06d52004-04-06 04:55:43 +00002193 static const unsigned MOVrrTab[] = {X86::MOV8rr, X86::MOV16rr, X86::MOV32rr};
2194 static const unsigned MOVriTab[] = {X86::MOV8ri, X86::MOV16ri, X86::MOV32ri};
2195
Chris Lattnerb2acc512003-10-19 21:09:10 +00002196 unsigned Class = getClass(DestTy);
2197
Chris Lattner6ab06d52004-04-06 04:55:43 +00002198 if (ConstRHS == 0) {
2199 BuildMI(*MBB, IP, MOVriTab[Class], 1, DestReg).addImm(0);
2200 return;
2201 } else if (ConstRHS == 1) {
2202 BuildMI(*MBB, IP, MOVrrTab[Class], 1, DestReg).addReg(op0Reg);
2203 return;
2204 }
2205
Chris Lattnerb2acc512003-10-19 21:09:10 +00002206 // If the element size is exactly a power of 2, use a shift to get it.
2207 if (unsigned Shift = ExactLog2(ConstRHS)) {
2208 switch (Class) {
2209 default: assert(0 && "Unknown class for this function!");
2210 case cByte:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002211 BuildMI(*MBB, IP, X86::SHL32ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002212 return;
2213 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002214 BuildMI(*MBB, IP, X86::SHL32ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002215 return;
2216 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002217 BuildMI(*MBB, IP, X86::SHL32ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002218 return;
2219 }
2220 }
Chris Lattnerc01d1232003-10-20 03:42:58 +00002221
2222 if (Class == cShort) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002223 BuildMI(*MBB, IP, X86::IMUL16rri,2,DestReg).addReg(op0Reg).addImm(ConstRHS);
Chris Lattnerc01d1232003-10-20 03:42:58 +00002224 return;
2225 } else if (Class == cInt) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002226 BuildMI(*MBB, IP, X86::IMUL32rri,2,DestReg).addReg(op0Reg).addImm(ConstRHS);
Chris Lattnerc01d1232003-10-20 03:42:58 +00002227 return;
2228 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002229
2230 // Most general case, emit a normal multiply...
Chris Lattnerb2acc512003-10-19 21:09:10 +00002231 unsigned TmpReg = makeAnotherReg(DestTy);
Chris Lattneree352852004-02-29 07:22:16 +00002232 BuildMI(*MBB, IP, MOVriTab[Class], 1, TmpReg).addImm(ConstRHS);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002233
2234 // Emit a MUL to multiply the register holding the index by
2235 // elementSize, putting the result in OffsetReg.
2236 doMultiply(MBB, IP, DestReg, DestTy, op0Reg, TmpReg);
2237}
2238
Chris Lattnerca9671d2002-11-02 20:28:58 +00002239/// visitMul - Multiplies are not simple binary operators because they must deal
2240/// with the EAX register explicitly.
2241///
2242void ISel::visitMul(BinaryOperator &I) {
Chris Lattner462fa822004-04-11 20:56:28 +00002243 unsigned ResultReg = getReg(I);
2244
Chris Lattner95157f72004-04-11 22:05:45 +00002245 Value *Op0 = I.getOperand(0);
2246 Value *Op1 = I.getOperand(1);
2247
2248 // Fold loads into floating point multiplies.
2249 if (getClass(Op0->getType()) == cFP) {
2250 if (isa<LoadInst>(Op0) && !isa<LoadInst>(Op1))
2251 if (!I.swapOperands())
2252 std::swap(Op0, Op1); // Make sure any loads are in the RHS.
2253 if (LoadInst *LI = dyn_cast<LoadInst>(Op1))
2254 if (isSafeToFoldLoadIntoInstruction(*LI, I)) {
2255 const Type *Ty = Op0->getType();
2256 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2257 unsigned Opcode = Ty == Type::FloatTy ? X86::FMUL32m : X86::FMUL64m;
2258
2259 unsigned BaseReg, Scale, IndexReg, Disp;
2260 getAddressingMode(LI->getOperand(0), BaseReg,
2261 Scale, IndexReg, Disp);
2262
2263 unsigned Op0r = getReg(Op0);
2264 addFullAddress(BuildMI(BB, Opcode, 2, ResultReg).addReg(Op0r),
2265 BaseReg, Scale, IndexReg, Disp);
2266 return;
2267 }
2268 }
2269
Chris Lattner462fa822004-04-11 20:56:28 +00002270 MachineBasicBlock::iterator IP = BB->end();
Chris Lattner95157f72004-04-11 22:05:45 +00002271 emitMultiply(BB, IP, Op0, Op1, ResultReg);
Chris Lattner462fa822004-04-11 20:56:28 +00002272}
2273
2274void ISel::emitMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
2275 Value *Op0, Value *Op1, unsigned DestReg) {
2276 MachineBasicBlock &BB = *MBB;
2277 TypeClass Class = getClass(Op0->getType());
Chris Lattner3e130a22003-01-13 00:32:26 +00002278
2279 // Simple scalar multiply?
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002280 unsigned Op0Reg = getReg(Op0, &BB, IP);
Chris Lattner462fa822004-04-11 20:56:28 +00002281 switch (Class) {
2282 case cByte:
2283 case cShort:
2284 case cInt:
2285 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner462fa822004-04-11 20:56:28 +00002286 unsigned Val = (unsigned)CI->getRawValue(); // Isn't a 64-bit constant
2287 doMultiplyConst(&BB, IP, DestReg, Op0->getType(), Op0Reg, Val);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002288 } else {
Chris Lattner462fa822004-04-11 20:56:28 +00002289 unsigned Op1Reg = getReg(Op1, &BB, IP);
2290 doMultiply(&BB, IP, DestReg, Op1->getType(), Op0Reg, Op1Reg);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002291 }
Chris Lattner462fa822004-04-11 20:56:28 +00002292 return;
2293 case cFP:
Chris Lattner6621ed92004-04-11 21:23:56 +00002294 emitBinaryFPOperation(MBB, IP, Op0, Op1, 2, DestReg);
2295 return;
Chris Lattner462fa822004-04-11 20:56:28 +00002296 case cLong:
2297 break;
2298 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002299
Chris Lattner462fa822004-04-11 20:56:28 +00002300 // Long value. We have to do things the hard way...
2301 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
2302 unsigned CLow = CI->getRawValue();
2303 unsigned CHi = CI->getRawValue() >> 32;
2304
2305 if (CLow == 0) {
2306 // If the low part of the constant is all zeros, things are simple.
2307 BuildMI(BB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
2308 doMultiplyConst(&BB, IP, DestReg+1, Type::UIntTy, Op0Reg, CHi);
2309 return;
2310 }
2311
2312 // Multiply the two low parts... capturing carry into EDX
2313 unsigned OverflowReg = 0;
2314 if (CLow == 1) {
2315 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg).addReg(Op0Reg);
Chris Lattner028adc42004-04-06 04:29:36 +00002316 } else {
Chris Lattner462fa822004-04-11 20:56:28 +00002317 unsigned Op1RegL = makeAnotherReg(Type::UIntTy);
2318 OverflowReg = makeAnotherReg(Type::UIntTy);
2319 BuildMI(BB, IP, X86::MOV32ri, 1, Op1RegL).addImm(CLow);
2320 BuildMI(BB, IP, X86::MOV32rr, 1, X86::EAX).addReg(Op0Reg);
2321 BuildMI(BB, IP, X86::MUL32r, 1).addReg(Op1RegL); // AL*BL
Chris Lattner028adc42004-04-06 04:29:36 +00002322
Chris Lattner462fa822004-04-11 20:56:28 +00002323 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg).addReg(X86::EAX); // AL*BL
2324 BuildMI(BB, IP, X86::MOV32rr, 1,
2325 OverflowReg).addReg(X86::EDX); // AL*BL >> 32
2326 }
2327
2328 unsigned AHBLReg = makeAnotherReg(Type::UIntTy); // AH*BL
2329 doMultiplyConst(&BB, IP, AHBLReg, Type::UIntTy, Op0Reg+1, CLow);
2330
2331 unsigned AHBLplusOverflowReg;
2332 if (OverflowReg) {
2333 AHBLplusOverflowReg = makeAnotherReg(Type::UIntTy);
2334 BuildMI(BB, IP, X86::ADD32rr, 2, // AH*BL+(AL*BL >> 32)
Chris Lattner028adc42004-04-06 04:29:36 +00002335 AHBLplusOverflowReg).addReg(AHBLReg).addReg(OverflowReg);
Chris Lattner462fa822004-04-11 20:56:28 +00002336 } else {
2337 AHBLplusOverflowReg = AHBLReg;
2338 }
2339
2340 if (CHi == 0) {
2341 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg+1).addReg(AHBLplusOverflowReg);
2342 } else {
Chris Lattner028adc42004-04-06 04:29:36 +00002343 unsigned ALBHReg = makeAnotherReg(Type::UIntTy); // AL*BH
Chris Lattner462fa822004-04-11 20:56:28 +00002344 doMultiplyConst(&BB, IP, ALBHReg, Type::UIntTy, Op0Reg, CHi);
Chris Lattner028adc42004-04-06 04:29:36 +00002345
Chris Lattner462fa822004-04-11 20:56:28 +00002346 BuildMI(BB, IP, X86::ADD32rr, 2, // AL*BH + AH*BL + (AL*BL >> 32)
Chris Lattner028adc42004-04-06 04:29:36 +00002347 DestReg+1).addReg(AHBLplusOverflowReg).addReg(ALBHReg);
2348 }
Chris Lattner462fa822004-04-11 20:56:28 +00002349 return;
Chris Lattner3e130a22003-01-13 00:32:26 +00002350 }
Chris Lattner462fa822004-04-11 20:56:28 +00002351
2352 // General 64x64 multiply
2353
2354 unsigned Op1Reg = getReg(Op1, &BB, IP);
2355 // Multiply the two low parts... capturing carry into EDX
2356 BuildMI(BB, IP, X86::MOV32rr, 1, X86::EAX).addReg(Op0Reg);
2357 BuildMI(BB, IP, X86::MUL32r, 1).addReg(Op1Reg); // AL*BL
2358
2359 unsigned OverflowReg = makeAnotherReg(Type::UIntTy);
2360 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg).addReg(X86::EAX); // AL*BL
2361 BuildMI(BB, IP, X86::MOV32rr, 1,
2362 OverflowReg).addReg(X86::EDX); // AL*BL >> 32
2363
2364 unsigned AHBLReg = makeAnotherReg(Type::UIntTy); // AH*BL
2365 BuildMI(BB, IP, X86::IMUL32rr, 2,
2366 AHBLReg).addReg(Op0Reg+1).addReg(Op1Reg);
2367
2368 unsigned AHBLplusOverflowReg = makeAnotherReg(Type::UIntTy);
2369 BuildMI(BB, IP, X86::ADD32rr, 2, // AH*BL+(AL*BL >> 32)
2370 AHBLplusOverflowReg).addReg(AHBLReg).addReg(OverflowReg);
2371
2372 unsigned ALBHReg = makeAnotherReg(Type::UIntTy); // AL*BH
2373 BuildMI(BB, IP, X86::IMUL32rr, 2,
2374 ALBHReg).addReg(Op0Reg).addReg(Op1Reg+1);
2375
2376 BuildMI(BB, IP, X86::ADD32rr, 2, // AL*BH + AH*BL + (AL*BL >> 32)
2377 DestReg+1).addReg(AHBLplusOverflowReg).addReg(ALBHReg);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002378}
Chris Lattnerca9671d2002-11-02 20:28:58 +00002379
Chris Lattner06925362002-11-17 21:56:38 +00002380
Chris Lattnerf01729e2002-11-02 20:54:46 +00002381/// visitDivRem - Handle division and remainder instructions... these
2382/// instruction both require the same instructions to be generated, they just
2383/// select the result from a different register. Note that both of these
2384/// instructions work differently for signed and unsigned operands.
2385///
2386void ISel::visitDivRem(BinaryOperator &I) {
Chris Lattnercadff442003-10-23 17:21:43 +00002387 unsigned ResultReg = getReg(I);
Chris Lattner95157f72004-04-11 22:05:45 +00002388 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2389
2390 // Fold loads into floating point divides.
2391 if (getClass(Op0->getType()) == cFP) {
2392 if (LoadInst *LI = dyn_cast<LoadInst>(Op1))
2393 if (isSafeToFoldLoadIntoInstruction(*LI, I)) {
2394 const Type *Ty = Op0->getType();
2395 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2396 unsigned Opcode = Ty == Type::FloatTy ? X86::FDIV32m : X86::FDIV64m;
2397
2398 unsigned BaseReg, Scale, IndexReg, Disp;
2399 getAddressingMode(LI->getOperand(0), BaseReg,
2400 Scale, IndexReg, Disp);
2401
2402 unsigned Op0r = getReg(Op0);
2403 addFullAddress(BuildMI(BB, Opcode, 2, ResultReg).addReg(Op0r),
2404 BaseReg, Scale, IndexReg, Disp);
2405 return;
2406 }
2407
2408 if (LoadInst *LI = dyn_cast<LoadInst>(Op0))
2409 if (isSafeToFoldLoadIntoInstruction(*LI, I)) {
2410 const Type *Ty = Op0->getType();
2411 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2412 unsigned Opcode = Ty == Type::FloatTy ? X86::FDIVR32m : X86::FDIVR64m;
2413
2414 unsigned BaseReg, Scale, IndexReg, Disp;
2415 getAddressingMode(LI->getOperand(0), BaseReg,
2416 Scale, IndexReg, Disp);
2417
2418 unsigned Op1r = getReg(Op1);
2419 addFullAddress(BuildMI(BB, Opcode, 2, ResultReg).addReg(Op1r),
2420 BaseReg, Scale, IndexReg, Disp);
2421 return;
2422 }
2423 }
2424
Chris Lattner94af4142002-12-25 05:13:53 +00002425
Chris Lattnercadff442003-10-23 17:21:43 +00002426 MachineBasicBlock::iterator IP = BB->end();
Chris Lattner95157f72004-04-11 22:05:45 +00002427 emitDivRemOperation(BB, IP, Op0, Op1,
Chris Lattner462fa822004-04-11 20:56:28 +00002428 I.getOpcode() == Instruction::Div, ResultReg);
Chris Lattnercadff442003-10-23 17:21:43 +00002429}
2430
2431void ISel::emitDivRemOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002432 MachineBasicBlock::iterator IP,
Chris Lattner462fa822004-04-11 20:56:28 +00002433 Value *Op0, Value *Op1, bool isDiv,
2434 unsigned ResultReg) {
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002435 const Type *Ty = Op0->getType();
2436 unsigned Class = getClass(Ty);
Chris Lattner94af4142002-12-25 05:13:53 +00002437 switch (Class) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002438 case cFP: // Floating point divide
Chris Lattnercadff442003-10-23 17:21:43 +00002439 if (isDiv) {
Chris Lattner6621ed92004-04-11 21:23:56 +00002440 emitBinaryFPOperation(BB, IP, Op0, Op1, 3, ResultReg);
2441 return;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00002442 } else { // Floating point remainder...
Chris Lattner462fa822004-04-11 20:56:28 +00002443 unsigned Op0Reg = getReg(Op0, BB, IP);
2444 unsigned Op1Reg = getReg(Op1, BB, IP);
Chris Lattner3e130a22003-01-13 00:32:26 +00002445 MachineInstr *TheCall =
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002446 BuildMI(X86::CALLpcrel32, 1).addExternalSymbol("fmod", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00002447 std::vector<ValueRecord> Args;
Chris Lattnercadff442003-10-23 17:21:43 +00002448 Args.push_back(ValueRecord(Op0Reg, Type::DoubleTy));
2449 Args.push_back(ValueRecord(Op1Reg, Type::DoubleTy));
Chris Lattner3e130a22003-01-13 00:32:26 +00002450 doCall(ValueRecord(ResultReg, Type::DoubleTy), TheCall, Args);
2451 }
Chris Lattner94af4142002-12-25 05:13:53 +00002452 return;
Chris Lattner3e130a22003-01-13 00:32:26 +00002453 case cLong: {
2454 static const char *FnName[] =
2455 { "__moddi3", "__divdi3", "__umoddi3", "__udivdi3" };
Chris Lattner462fa822004-04-11 20:56:28 +00002456 unsigned Op0Reg = getReg(Op0, BB, IP);
2457 unsigned Op1Reg = getReg(Op1, BB, IP);
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002458 unsigned NameIdx = Ty->isUnsigned()*2 + isDiv;
Chris Lattner3e130a22003-01-13 00:32:26 +00002459 MachineInstr *TheCall =
2460 BuildMI(X86::CALLpcrel32, 1).addExternalSymbol(FnName[NameIdx], true);
2461
2462 std::vector<ValueRecord> Args;
Chris Lattnercadff442003-10-23 17:21:43 +00002463 Args.push_back(ValueRecord(Op0Reg, Type::LongTy));
2464 Args.push_back(ValueRecord(Op1Reg, Type::LongTy));
Chris Lattner3e130a22003-01-13 00:32:26 +00002465 doCall(ValueRecord(ResultReg, Type::LongTy), TheCall, Args);
2466 return;
2467 }
2468 case cByte: case cShort: case cInt:
Misha Brukmancf00c4a2003-10-10 17:57:28 +00002469 break; // Small integrals, handled below...
Chris Lattner3e130a22003-01-13 00:32:26 +00002470 default: assert(0 && "Unknown class!");
Chris Lattner94af4142002-12-25 05:13:53 +00002471 }
Chris Lattnerf01729e2002-11-02 20:54:46 +00002472
2473 static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX };
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002474 static const unsigned MovOpcode[]={ X86::MOV8rr, X86::MOV16rr, X86::MOV32rr };
2475 static const unsigned SarOpcode[]={ X86::SAR8ri, X86::SAR16ri, X86::SAR32ri };
2476 static const unsigned ClrOpcode[]={ X86::MOV8ri, X86::MOV16ri, X86::MOV32ri };
Chris Lattnerf01729e2002-11-02 20:54:46 +00002477 static const unsigned ExtRegs[] ={ X86::AH , X86::DX , X86::EDX };
2478
2479 static const unsigned DivOpcode[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002480 { X86::DIV8r , X86::DIV16r , X86::DIV32r , 0 }, // Unsigned division
2481 { X86::IDIV8r, X86::IDIV16r, X86::IDIV32r, 0 }, // Signed division
Chris Lattnerf01729e2002-11-02 20:54:46 +00002482 };
2483
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002484 bool isSigned = Ty->isSigned();
Chris Lattnerf01729e2002-11-02 20:54:46 +00002485 unsigned Reg = Regs[Class];
2486 unsigned ExtReg = ExtRegs[Class];
Chris Lattnerf01729e2002-11-02 20:54:46 +00002487
2488 // Put the first operand into one of the A registers...
Chris Lattner462fa822004-04-11 20:56:28 +00002489 unsigned Op0Reg = getReg(Op0, BB, IP);
2490 unsigned Op1Reg = getReg(Op1, BB, IP);
Chris Lattneree352852004-02-29 07:22:16 +00002491 BuildMI(*BB, IP, MovOpcode[Class], 1, Reg).addReg(Op0Reg);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002492
2493 if (isSigned) {
2494 // Emit a sign extension instruction...
Chris Lattner462fa822004-04-11 20:56:28 +00002495 unsigned ShiftResult = makeAnotherReg(Op0->getType());
Chris Lattneree352852004-02-29 07:22:16 +00002496 BuildMI(*BB, IP, SarOpcode[Class], 2,ShiftResult).addReg(Op0Reg).addImm(31);
2497 BuildMI(*BB, IP, MovOpcode[Class], 1, ExtReg).addReg(ShiftResult);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002498 } else {
Alkis Evlogimenosf998a7e2004-01-12 07:22:45 +00002499 // If unsigned, emit a zeroing instruction... (reg = 0)
Chris Lattneree352852004-02-29 07:22:16 +00002500 BuildMI(*BB, IP, ClrOpcode[Class], 2, ExtReg).addImm(0);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002501 }
2502
Chris Lattner06925362002-11-17 21:56:38 +00002503 // Emit the appropriate divide or remainder instruction...
Chris Lattneree352852004-02-29 07:22:16 +00002504 BuildMI(*BB, IP, DivOpcode[isSigned][Class], 1).addReg(Op1Reg);
Chris Lattner06925362002-11-17 21:56:38 +00002505
Chris Lattnerf01729e2002-11-02 20:54:46 +00002506 // Figure out which register we want to pick the result out of...
Chris Lattnercadff442003-10-23 17:21:43 +00002507 unsigned DestReg = isDiv ? Reg : ExtReg;
Chris Lattnerf01729e2002-11-02 20:54:46 +00002508
Chris Lattnerf01729e2002-11-02 20:54:46 +00002509 // Put the result into the destination register...
Chris Lattneree352852004-02-29 07:22:16 +00002510 BuildMI(*BB, IP, MovOpcode[Class], 1, ResultReg).addReg(DestReg);
Chris Lattnerca9671d2002-11-02 20:28:58 +00002511}
Chris Lattnere2954c82002-11-02 20:04:26 +00002512
Chris Lattner06925362002-11-17 21:56:38 +00002513
Brian Gaekea1719c92002-10-31 23:03:59 +00002514/// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here
2515/// for constant immediate shift values, and for constant immediate
2516/// shift values equal to 1. Even the general case is sort of special,
2517/// because the shift amount has to be in CL, not just any old register.
2518///
Chris Lattner3e130a22003-01-13 00:32:26 +00002519void ISel::visitShiftInst(ShiftInst &I) {
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002520 MachineBasicBlock::iterator IP = BB->end ();
2521 emitShiftOperation (BB, IP, I.getOperand (0), I.getOperand (1),
2522 I.getOpcode () == Instruction::Shl, I.getType (),
2523 getReg (I));
2524}
2525
2526/// emitShiftOperation - Common code shared between visitShiftInst and
2527/// constant expression support.
2528void ISel::emitShiftOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002529 MachineBasicBlock::iterator IP,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002530 Value *Op, Value *ShiftAmount, bool isLeftShift,
2531 const Type *ResultTy, unsigned DestReg) {
2532 unsigned SrcReg = getReg (Op, MBB, IP);
2533 bool isSigned = ResultTy->isSigned ();
2534 unsigned Class = getClass (ResultTy);
Chris Lattner3e130a22003-01-13 00:32:26 +00002535
2536 static const unsigned ConstantOperand[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002537 { X86::SHR8ri, X86::SHR16ri, X86::SHR32ri, X86::SHRD32rri8 }, // SHR
2538 { X86::SAR8ri, X86::SAR16ri, X86::SAR32ri, X86::SHRD32rri8 }, // SAR
2539 { X86::SHL8ri, X86::SHL16ri, X86::SHL32ri, X86::SHLD32rri8 }, // SHL
2540 { X86::SHL8ri, X86::SHL16ri, X86::SHL32ri, X86::SHLD32rri8 }, // SAL = SHL
Chris Lattner3e130a22003-01-13 00:32:26 +00002541 };
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002542
Chris Lattner3e130a22003-01-13 00:32:26 +00002543 static const unsigned NonConstantOperand[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002544 { X86::SHR8rCL, X86::SHR16rCL, X86::SHR32rCL }, // SHR
2545 { X86::SAR8rCL, X86::SAR16rCL, X86::SAR32rCL }, // SAR
2546 { X86::SHL8rCL, X86::SHL16rCL, X86::SHL32rCL }, // SHL
2547 { X86::SHL8rCL, X86::SHL16rCL, X86::SHL32rCL }, // SAL = SHL
Chris Lattner3e130a22003-01-13 00:32:26 +00002548 };
Chris Lattner796df732002-11-02 00:44:25 +00002549
Chris Lattner3e130a22003-01-13 00:32:26 +00002550 // Longs, as usual, are handled specially...
2551 if (Class == cLong) {
2552 // If we have a constant shift, we can generate much more efficient code
2553 // than otherwise...
2554 //
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002555 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002556 unsigned Amount = CUI->getValue();
2557 if (Amount < 32) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002558 const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned];
2559 if (isLeftShift) {
Chris Lattneree352852004-02-29 07:22:16 +00002560 BuildMI(*MBB, IP, Opc[3], 3,
2561 DestReg+1).addReg(SrcReg+1).addReg(SrcReg).addImm(Amount);
2562 BuildMI(*MBB, IP, Opc[2], 2, DestReg).addReg(SrcReg).addImm(Amount);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002563 } else {
Chris Lattneree352852004-02-29 07:22:16 +00002564 BuildMI(*MBB, IP, Opc[3], 3,
2565 DestReg).addReg(SrcReg ).addReg(SrcReg+1).addImm(Amount);
2566 BuildMI(*MBB, IP, Opc[2],2,DestReg+1).addReg(SrcReg+1).addImm(Amount);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002567 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002568 } else { // Shifting more than 32 bits
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002569 Amount -= 32;
2570 if (isLeftShift) {
Chris Lattner722070e2004-04-06 03:42:38 +00002571 if (Amount != 0) {
2572 BuildMI(*MBB, IP, X86::SHL32ri, 2,
2573 DestReg + 1).addReg(SrcReg).addImm(Amount);
2574 } else {
2575 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg+1).addReg(SrcReg);
2576 }
2577 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002578 } else {
Chris Lattner722070e2004-04-06 03:42:38 +00002579 if (Amount != 0) {
2580 BuildMI(*MBB, IP, isSigned ? X86::SAR32ri : X86::SHR32ri, 2,
2581 DestReg).addReg(SrcReg+1).addImm(Amount);
2582 } else {
2583 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg+1);
2584 }
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002585 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002586 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002587 }
2588 } else {
Chris Lattner9171ef52003-06-01 01:56:54 +00002589 unsigned TmpReg = makeAnotherReg(Type::IntTy);
2590
2591 if (!isLeftShift && isSigned) {
2592 // If this is a SHR of a Long, then we need to do funny sign extension
2593 // stuff. TmpReg gets the value to use as the high-part if we are
2594 // shifting more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002595 BuildMI(*MBB, IP, X86::SAR32ri, 2, TmpReg).addReg(SrcReg).addImm(31);
Chris Lattner9171ef52003-06-01 01:56:54 +00002596 } else {
2597 // Other shifts use a fixed zero value if the shift is more than 32
2598 // bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002599 BuildMI(*MBB, IP, X86::MOV32ri, 1, TmpReg).addImm(0);
Chris Lattner9171ef52003-06-01 01:56:54 +00002600 }
2601
2602 // Initialize CL with the shift amount...
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002603 unsigned ShiftAmountReg = getReg(ShiftAmount, MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002604 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002605
2606 unsigned TmpReg2 = makeAnotherReg(Type::IntTy);
2607 unsigned TmpReg3 = makeAnotherReg(Type::IntTy);
2608 if (isLeftShift) {
2609 // TmpReg2 = shld inHi, inLo
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002610 BuildMI(*MBB, IP, X86::SHLD32rrCL,2,TmpReg2).addReg(SrcReg+1)
Chris Lattneree352852004-02-29 07:22:16 +00002611 .addReg(SrcReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002612 // TmpReg3 = shl inLo, CL
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002613 BuildMI(*MBB, IP, X86::SHL32rCL, 1, TmpReg3).addReg(SrcReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002614
2615 // Set the flags to indicate whether the shift was by more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002616 BuildMI(*MBB, IP, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Chris Lattner9171ef52003-06-01 01:56:54 +00002617
2618 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002619 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00002620 DestReg+1).addReg(TmpReg2).addReg(TmpReg3);
2621 // DestLo = (>32) ? TmpReg : TmpReg3;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002622 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002623 DestReg).addReg(TmpReg3).addReg(TmpReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002624 } else {
2625 // TmpReg2 = shrd inLo, inHi
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002626 BuildMI(*MBB, IP, X86::SHRD32rrCL,2,TmpReg2).addReg(SrcReg)
Chris Lattneree352852004-02-29 07:22:16 +00002627 .addReg(SrcReg+1);
Chris Lattner9171ef52003-06-01 01:56:54 +00002628 // TmpReg3 = s[ah]r inHi, CL
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002629 BuildMI(*MBB, IP, isSigned ? X86::SAR32rCL : X86::SHR32rCL, 1, TmpReg3)
Chris Lattner9171ef52003-06-01 01:56:54 +00002630 .addReg(SrcReg+1);
2631
2632 // Set the flags to indicate whether the shift was by more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002633 BuildMI(*MBB, IP, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Chris Lattner9171ef52003-06-01 01:56:54 +00002634
2635 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002636 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00002637 DestReg).addReg(TmpReg2).addReg(TmpReg3);
2638
2639 // DestHi = (>32) ? TmpReg : TmpReg3;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002640 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00002641 DestReg+1).addReg(TmpReg3).addReg(TmpReg);
2642 }
Brian Gaekea1719c92002-10-31 23:03:59 +00002643 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002644 return;
2645 }
Chris Lattnere9913f22002-11-02 01:41:55 +00002646
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002647 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002648 // The shift amount is constant, guaranteed to be a ubyte. Get its value.
2649 assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?");
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002650
Chris Lattner3e130a22003-01-13 00:32:26 +00002651 const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned];
Chris Lattneree352852004-02-29 07:22:16 +00002652 BuildMI(*MBB, IP, Opc[Class], 2,
2653 DestReg).addReg(SrcReg).addImm(CUI->getValue());
Chris Lattner3e130a22003-01-13 00:32:26 +00002654 } else { // The shift amount is non-constant.
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002655 unsigned ShiftAmountReg = getReg (ShiftAmount, MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002656 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002657
Chris Lattner3e130a22003-01-13 00:32:26 +00002658 const unsigned *Opc = NonConstantOperand[isLeftShift*2+isSigned];
Chris Lattneree352852004-02-29 07:22:16 +00002659 BuildMI(*MBB, IP, Opc[Class], 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002660 }
2661}
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002662
Chris Lattner3e130a22003-01-13 00:32:26 +00002663
Chris Lattner721d2d42004-03-08 01:18:36 +00002664void ISel::getAddressingMode(Value *Addr, unsigned &BaseReg, unsigned &Scale,
2665 unsigned &IndexReg, unsigned &Disp) {
2666 BaseReg = 0; Scale = 1; IndexReg = 0; Disp = 0;
2667 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Addr)) {
2668 if (isGEPFoldable(BB, GEP->getOperand(0), GEP->op_begin()+1, GEP->op_end(),
2669 BaseReg, Scale, IndexReg, Disp))
2670 return;
2671 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Addr)) {
2672 if (CE->getOpcode() == Instruction::GetElementPtr)
2673 if (isGEPFoldable(BB, CE->getOperand(0), CE->op_begin()+1, CE->op_end(),
2674 BaseReg, Scale, IndexReg, Disp))
2675 return;
2676 }
2677
2678 // If it's not foldable, reset addr mode.
2679 BaseReg = getReg(Addr);
2680 Scale = 1; IndexReg = 0; Disp = 0;
2681}
2682
2683
Chris Lattner6fc3c522002-11-17 21:11:55 +00002684/// visitLoadInst - Implement LLVM load instructions in terms of the x86 'mov'
Chris Lattnere8f0d922002-12-24 00:03:11 +00002685/// instruction. The load and store instructions are the only place where we
2686/// need to worry about the memory layout of the target machine.
Chris Lattner6fc3c522002-11-17 21:11:55 +00002687///
2688void ISel::visitLoadInst(LoadInst &I) {
Chris Lattner7dee5da2004-03-08 01:58:35 +00002689 // Check to see if this load instruction is going to be folded into a binary
2690 // instruction, like add. If so, we don't want to emit it. Wouldn't a real
2691 // pattern matching instruction selector be nice?
Chris Lattner95157f72004-04-11 22:05:45 +00002692 unsigned Class = getClassB(I.getType());
Chris Lattnerfeac3e12004-04-11 23:21:26 +00002693 if (I.hasOneUse()) {
Chris Lattner7dee5da2004-03-08 01:58:35 +00002694 Instruction *User = cast<Instruction>(I.use_back());
2695 switch (User->getOpcode()) {
Chris Lattnerfeac3e12004-04-11 23:21:26 +00002696 case Instruction::Cast:
2697 // If this is a cast from a signed-integer type to a floating point type,
2698 // fold the cast here.
2699 if (getClass(User->getType()) == cFP &&
2700 (I.getType() == Type::ShortTy || I.getType() == Type::IntTy ||
2701 I.getType() == Type::LongTy)) {
2702 unsigned DestReg = getReg(User);
2703 static const unsigned Opcode[] = {
2704 0/*BYTE*/, X86::FILD16m, X86::FILD32m, 0/*FP*/, X86::FILD64m
2705 };
Chris Lattnerfeac3e12004-04-11 23:21:26 +00002706 unsigned BaseReg = 0, Scale = 1, IndexReg = 0, Disp = 0;
2707 getAddressingMode(I.getOperand(0), BaseReg, Scale, IndexReg, Disp);
2708 addFullAddress(BuildMI(BB, Opcode[Class], 5, DestReg),
2709 BaseReg, Scale, IndexReg, Disp);
2710 return;
2711 } else {
2712 User = 0;
2713 }
2714 break;
Chris Lattner13c07fe2004-04-12 00:12:04 +00002715
Chris Lattner7dee5da2004-03-08 01:58:35 +00002716 case Instruction::Add:
2717 case Instruction::Sub:
2718 case Instruction::And:
2719 case Instruction::Or:
2720 case Instruction::Xor:
Chris Lattnerfeac3e12004-04-11 23:21:26 +00002721 if (Class == cLong) User = 0;
Chris Lattner7dee5da2004-03-08 01:58:35 +00002722 break;
Chris Lattner95157f72004-04-11 22:05:45 +00002723 case Instruction::Mul:
2724 case Instruction::Div:
Chris Lattner13c07fe2004-04-12 00:12:04 +00002725 if (Class != cFP) User = 0;
Chris Lattnerfeac3e12004-04-11 23:21:26 +00002726 break; // Folding only implemented for floating point.
Chris Lattner95157f72004-04-11 22:05:45 +00002727 default: User = 0; break;
Chris Lattner7dee5da2004-03-08 01:58:35 +00002728 }
2729
2730 if (User) {
2731 // Okay, we found a user. If the load is the first operand and there is
2732 // no second operand load, reverse the operand ordering. Note that this
2733 // can fail for a subtract (ie, no change will be made).
2734 if (!isa<LoadInst>(User->getOperand(1)))
2735 cast<BinaryOperator>(User)->swapOperands();
2736
2737 // Okay, now that everything is set up, if this load is used by the second
2738 // operand, and if there are no instructions that invalidate the load
2739 // before the binary operator, eliminate the load.
2740 if (User->getOperand(1) == &I &&
2741 isSafeToFoldLoadIntoInstruction(I, *User))
2742 return; // Eliminate the load!
Chris Lattner95157f72004-04-11 22:05:45 +00002743
2744 // If this is a floating point sub or div, we won't be able to swap the
2745 // operands, but we will still be able to eliminate the load.
2746 if (Class == cFP && User->getOperand(0) == &I &&
2747 !isa<LoadInst>(User->getOperand(1)) &&
2748 (User->getOpcode() == Instruction::Sub ||
2749 User->getOpcode() == Instruction::Div) &&
2750 isSafeToFoldLoadIntoInstruction(I, *User))
2751 return; // Eliminate the load!
Chris Lattner7dee5da2004-03-08 01:58:35 +00002752 }
2753 }
2754
Chris Lattner94af4142002-12-25 05:13:53 +00002755 unsigned DestReg = getReg(I);
Chris Lattnerb6bac512004-02-25 06:13:04 +00002756 unsigned BaseReg = 0, Scale = 1, IndexReg = 0, Disp = 0;
Chris Lattner721d2d42004-03-08 01:18:36 +00002757 getAddressingMode(I.getOperand(0), BaseReg, Scale, IndexReg, Disp);
Chris Lattnere8f0d922002-12-24 00:03:11 +00002758
Chris Lattner6ac1d712003-10-20 04:48:06 +00002759 if (Class == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002760 addFullAddress(BuildMI(BB, X86::MOV32rm, 4, DestReg),
Chris Lattnerb6bac512004-02-25 06:13:04 +00002761 BaseReg, Scale, IndexReg, Disp);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002762 addFullAddress(BuildMI(BB, X86::MOV32rm, 4, DestReg+1),
Chris Lattnerb6bac512004-02-25 06:13:04 +00002763 BaseReg, Scale, IndexReg, Disp+4);
Chris Lattner94af4142002-12-25 05:13:53 +00002764 return;
2765 }
Chris Lattner6fc3c522002-11-17 21:11:55 +00002766
Chris Lattner6ac1d712003-10-20 04:48:06 +00002767 static const unsigned Opcodes[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002768 X86::MOV8rm, X86::MOV16rm, X86::MOV32rm, X86::FLD32m
Chris Lattner3e130a22003-01-13 00:32:26 +00002769 };
Chris Lattner6ac1d712003-10-20 04:48:06 +00002770 unsigned Opcode = Opcodes[Class];
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002771 if (I.getType() == Type::DoubleTy) Opcode = X86::FLD64m;
Chris Lattnerb6bac512004-02-25 06:13:04 +00002772 addFullAddress(BuildMI(BB, Opcode, 4, DestReg),
2773 BaseReg, Scale, IndexReg, Disp);
Chris Lattner3e130a22003-01-13 00:32:26 +00002774}
2775
Chris Lattner6fc3c522002-11-17 21:11:55 +00002776/// visitStoreInst - Implement LLVM store instructions in terms of the x86 'mov'
2777/// instruction.
2778///
2779void ISel::visitStoreInst(StoreInst &I) {
Chris Lattner721d2d42004-03-08 01:18:36 +00002780 unsigned BaseReg, Scale, IndexReg, Disp;
2781 getAddressingMode(I.getOperand(1), BaseReg, Scale, IndexReg, Disp);
Chris Lattnerb6bac512004-02-25 06:13:04 +00002782
Chris Lattner6c09db22003-10-20 04:11:23 +00002783 const Type *ValTy = I.getOperand(0)->getType();
2784 unsigned Class = getClassB(ValTy);
Chris Lattner6ac1d712003-10-20 04:48:06 +00002785
Chris Lattner5a830962004-02-25 02:56:58 +00002786 if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(0))) {
2787 uint64_t Val = CI->getRawValue();
2788 if (Class == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002789 addFullAddress(BuildMI(BB, X86::MOV32mi, 5),
Chris Lattneree352852004-02-29 07:22:16 +00002790 BaseReg, Scale, IndexReg, Disp).addImm(Val & ~0U);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002791 addFullAddress(BuildMI(BB, X86::MOV32mi, 5),
Chris Lattneree352852004-02-29 07:22:16 +00002792 BaseReg, Scale, IndexReg, Disp+4).addImm(Val>>32);
Chris Lattner5a830962004-02-25 02:56:58 +00002793 } else {
2794 static const unsigned Opcodes[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002795 X86::MOV8mi, X86::MOV16mi, X86::MOV32mi
Chris Lattner5a830962004-02-25 02:56:58 +00002796 };
2797 unsigned Opcode = Opcodes[Class];
Chris Lattnerb6bac512004-02-25 06:13:04 +00002798 addFullAddress(BuildMI(BB, Opcode, 5),
Chris Lattneree352852004-02-29 07:22:16 +00002799 BaseReg, Scale, IndexReg, Disp).addImm(Val);
Chris Lattner5a830962004-02-25 02:56:58 +00002800 }
2801 } else if (ConstantBool *CB = dyn_cast<ConstantBool>(I.getOperand(0))) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002802 addFullAddress(BuildMI(BB, X86::MOV8mi, 5),
Chris Lattneree352852004-02-29 07:22:16 +00002803 BaseReg, Scale, IndexReg, Disp).addImm(CB->getValue());
Chris Lattner5a830962004-02-25 02:56:58 +00002804 } else {
2805 if (Class == cLong) {
2806 unsigned ValReg = getReg(I.getOperand(0));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002807 addFullAddress(BuildMI(BB, X86::MOV32mr, 5),
Chris Lattnerb6bac512004-02-25 06:13:04 +00002808 BaseReg, Scale, IndexReg, Disp).addReg(ValReg);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002809 addFullAddress(BuildMI(BB, X86::MOV32mr, 5),
Chris Lattnerb6bac512004-02-25 06:13:04 +00002810 BaseReg, Scale, IndexReg, Disp+4).addReg(ValReg+1);
Chris Lattner5a830962004-02-25 02:56:58 +00002811 } else {
2812 unsigned ValReg = getReg(I.getOperand(0));
2813 static const unsigned Opcodes[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002814 X86::MOV8mr, X86::MOV16mr, X86::MOV32mr, X86::FST32m
Chris Lattner5a830962004-02-25 02:56:58 +00002815 };
2816 unsigned Opcode = Opcodes[Class];
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002817 if (ValTy == Type::DoubleTy) Opcode = X86::FST64m;
Chris Lattnerb6bac512004-02-25 06:13:04 +00002818 addFullAddress(BuildMI(BB, Opcode, 1+4),
2819 BaseReg, Scale, IndexReg, Disp).addReg(ValReg);
Chris Lattner5a830962004-02-25 02:56:58 +00002820 }
Chris Lattner94af4142002-12-25 05:13:53 +00002821 }
Chris Lattner6fc3c522002-11-17 21:11:55 +00002822}
2823
2824
Misha Brukman538607f2004-03-01 23:53:11 +00002825/// visitCastInst - Here we have various kinds of copying with or without sign
2826/// extension going on.
2827///
Chris Lattner3e130a22003-01-13 00:32:26 +00002828void ISel::visitCastInst(CastInst &CI) {
Chris Lattnerf5854472003-06-21 16:01:24 +00002829 Value *Op = CI.getOperand(0);
Chris Lattner427aeb42004-04-11 19:21:59 +00002830
Chris Lattner99382862004-04-12 00:23:04 +00002831 unsigned SrcClass = getClassB(Op->getType());
2832 unsigned DestClass = getClassB(CI.getType());
2833 // Noop casts are not emitted: getReg will return the source operand as the
2834 // register to use for any uses of the noop cast.
2835 if (DestClass == SrcClass)
Chris Lattner427aeb42004-04-11 19:21:59 +00002836 return;
2837
Chris Lattnerf5854472003-06-21 16:01:24 +00002838 // If this is a cast from a 32-bit integer to a Long type, and the only uses
2839 // of the case are GEP instructions, then the cast does not need to be
2840 // generated explicitly, it will be folded into the GEP.
Chris Lattner99382862004-04-12 00:23:04 +00002841 if (DestClass == cLong && SrcClass == cInt) {
Chris Lattnerf5854472003-06-21 16:01:24 +00002842 bool AllUsesAreGEPs = true;
2843 for (Value::use_iterator I = CI.use_begin(), E = CI.use_end(); I != E; ++I)
2844 if (!isa<GetElementPtrInst>(*I)) {
2845 AllUsesAreGEPs = false;
2846 break;
2847 }
2848
2849 // No need to codegen this cast if all users are getelementptr instrs...
2850 if (AllUsesAreGEPs) return;
2851 }
2852
Chris Lattner99382862004-04-12 00:23:04 +00002853 // If this cast converts a load from a short,int, or long integer to a FP
2854 // value, we will have folded this cast away.
2855 if (DestClass == cFP && isa<LoadInst>(Op) && Op->hasOneUse() &&
2856 (Op->getType() == Type::ShortTy || Op->getType() == Type::IntTy ||
2857 Op->getType() == Type::LongTy))
2858 return;
2859
2860
Chris Lattner548f61d2003-04-23 17:22:12 +00002861 unsigned DestReg = getReg(CI);
2862 MachineBasicBlock::iterator MI = BB->end();
Chris Lattnerf5854472003-06-21 16:01:24 +00002863 emitCastOperation(BB, MI, Op, CI.getType(), DestReg);
Chris Lattner548f61d2003-04-23 17:22:12 +00002864}
2865
Misha Brukman538607f2004-03-01 23:53:11 +00002866/// emitCastOperation - Common code shared between visitCastInst and constant
2867/// expression cast support.
2868///
Chris Lattner548f61d2003-04-23 17:22:12 +00002869void ISel::emitCastOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002870 MachineBasicBlock::iterator IP,
Chris Lattner548f61d2003-04-23 17:22:12 +00002871 Value *Src, const Type *DestTy,
2872 unsigned DestReg) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002873 const Type *SrcTy = Src->getType();
2874 unsigned SrcClass = getClassB(SrcTy);
Chris Lattner3e130a22003-01-13 00:32:26 +00002875 unsigned DestClass = getClassB(DestTy);
Chris Lattnerfeac3e12004-04-11 23:21:26 +00002876 unsigned SrcReg = getReg(Src, BB, IP);
2877
Chris Lattner3e130a22003-01-13 00:32:26 +00002878 // Implement casts to bool by using compare on the operand followed by set if
2879 // not zero on the result.
2880 if (DestTy == Type::BoolTy) {
Chris Lattner20772542003-06-01 03:38:24 +00002881 switch (SrcClass) {
2882 case cByte:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002883 BuildMI(*BB, IP, X86::TEST8rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00002884 break;
2885 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002886 BuildMI(*BB, IP, X86::TEST16rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00002887 break;
2888 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002889 BuildMI(*BB, IP, X86::TEST32rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00002890 break;
2891 case cLong: {
2892 unsigned TmpReg = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002893 BuildMI(*BB, IP, X86::OR32rr, 2, TmpReg).addReg(SrcReg).addReg(SrcReg+1);
Chris Lattner20772542003-06-01 03:38:24 +00002894 break;
2895 }
2896 case cFP:
Chris Lattneree352852004-02-29 07:22:16 +00002897 BuildMI(*BB, IP, X86::FTST, 1).addReg(SrcReg);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002898 BuildMI(*BB, IP, X86::FNSTSW8r, 0);
Chris Lattneree352852004-02-29 07:22:16 +00002899 BuildMI(*BB, IP, X86::SAHF, 1);
Chris Lattner311ca2e2004-02-23 03:21:41 +00002900 break;
Chris Lattner20772542003-06-01 03:38:24 +00002901 }
2902
2903 // If the zero flag is not set, then the value is true, set the byte to
2904 // true.
Chris Lattneree352852004-02-29 07:22:16 +00002905 BuildMI(*BB, IP, X86::SETNEr, 1, DestReg);
Chris Lattner94af4142002-12-25 05:13:53 +00002906 return;
2907 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002908
2909 static const unsigned RegRegMove[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002910 X86::MOV8rr, X86::MOV16rr, X86::MOV32rr, X86::FpMOV, X86::MOV32rr
Chris Lattner3e130a22003-01-13 00:32:26 +00002911 };
2912
2913 // Implement casts between values of the same type class (as determined by
2914 // getClass) by using a register-to-register move.
2915 if (SrcClass == DestClass) {
2916 if (SrcClass <= cInt || (SrcClass == cFP && SrcTy == DestTy)) {
Chris Lattneree352852004-02-29 07:22:16 +00002917 BuildMI(*BB, IP, RegRegMove[SrcClass], 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002918 } else if (SrcClass == cFP) {
2919 if (SrcTy == Type::FloatTy) { // double -> float
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002920 assert(DestTy == Type::DoubleTy && "Unknown cFP member!");
Chris Lattneree352852004-02-29 07:22:16 +00002921 BuildMI(*BB, IP, X86::FpMOV, 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002922 } else { // float -> double
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002923 assert(SrcTy == Type::DoubleTy && DestTy == Type::FloatTy &&
2924 "Unknown cFP member!");
2925 // Truncate from double to float by storing to memory as short, then
2926 // reading it back.
2927 unsigned FltAlign = TM.getTargetData().getFloatAlignment();
Chris Lattner3e130a22003-01-13 00:32:26 +00002928 int FrameIdx = F->getFrameInfo()->CreateStackObject(4, FltAlign);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002929 addFrameReference(BuildMI(*BB, IP, X86::FST32m, 5), FrameIdx).addReg(SrcReg);
2930 addFrameReference(BuildMI(*BB, IP, X86::FLD32m, 5, DestReg), FrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00002931 }
2932 } else if (SrcClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002933 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg);
2934 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg+1).addReg(SrcReg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00002935 } else {
Chris Lattnerc53544a2003-05-12 20:16:58 +00002936 assert(0 && "Cannot handle this type of cast instruction!");
Chris Lattner548f61d2003-04-23 17:22:12 +00002937 abort();
Brian Gaeked474e9c2002-12-06 10:49:33 +00002938 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002939 return;
2940 }
2941
2942 // Handle cast of SMALLER int to LARGER int using a move with sign extension
2943 // or zero extension, depending on whether the source type was signed.
2944 if (SrcClass <= cInt && (DestClass <= cInt || DestClass == cLong) &&
2945 SrcClass < DestClass) {
2946 bool isLong = DestClass == cLong;
2947 if (isLong) DestClass = cInt;
2948
2949 static const unsigned Opc[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002950 { X86::MOVSX16rr8, X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOV32rr }, // s
2951 { X86::MOVZX16rr8, X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOV32rr } // u
Chris Lattner3e130a22003-01-13 00:32:26 +00002952 };
2953
2954 bool isUnsigned = SrcTy->isUnsigned();
Chris Lattneree352852004-02-29 07:22:16 +00002955 BuildMI(*BB, IP, Opc[isUnsigned][SrcClass + DestClass - 1], 1,
Chris Lattner548f61d2003-04-23 17:22:12 +00002956 DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002957
2958 if (isLong) { // Handle upper 32 bits as appropriate...
2959 if (isUnsigned) // Zero out top bits...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002960 BuildMI(*BB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
Chris Lattner3e130a22003-01-13 00:32:26 +00002961 else // Sign extend bottom half...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002962 BuildMI(*BB, IP, X86::SAR32ri, 2, DestReg+1).addReg(DestReg).addImm(31);
Brian Gaeked474e9c2002-12-06 10:49:33 +00002963 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002964 return;
2965 }
2966
2967 // Special case long -> int ...
2968 if (SrcClass == cLong && DestClass == cInt) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002969 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002970 return;
2971 }
2972
2973 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by a
2974 // move out of AX or AL.
2975 if ((SrcClass <= cInt || SrcClass == cLong) && DestClass <= cInt
2976 && SrcClass > DestClass) {
2977 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX, 0, X86::EAX };
Chris Lattneree352852004-02-29 07:22:16 +00002978 BuildMI(*BB, IP, RegRegMove[SrcClass], 1, AReg[SrcClass]).addReg(SrcReg);
2979 BuildMI(*BB, IP, RegRegMove[DestClass], 1, DestReg).addReg(AReg[DestClass]);
Chris Lattner3e130a22003-01-13 00:32:26 +00002980 return;
2981 }
2982
2983 // Handle casts from integer to floating point now...
2984 if (DestClass == cFP) {
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002985 // Promote the integer to a type supported by FLD. We do this because there
2986 // are no unsigned FLD instructions, so we must promote an unsigned value to
2987 // a larger signed value, then use FLD on the larger value.
2988 //
2989 const Type *PromoteType = 0;
Chris Lattner85aa7092004-04-10 18:32:01 +00002990 unsigned PromoteOpcode = 0;
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002991 unsigned RealDestReg = DestReg;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002992 switch (SrcTy->getPrimitiveID()) {
2993 case Type::BoolTyID:
2994 case Type::SByteTyID:
2995 // We don't have the facilities for directly loading byte sized data from
2996 // memory (even signed). Promote it to 16 bits.
2997 PromoteType = Type::ShortTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002998 PromoteOpcode = X86::MOVSX16rr8;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002999 break;
3000 case Type::UByteTyID:
3001 PromoteType = Type::ShortTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003002 PromoteOpcode = X86::MOVZX16rr8;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003003 break;
3004 case Type::UShortTyID:
3005 PromoteType = Type::IntTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003006 PromoteOpcode = X86::MOVZX32rr16;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003007 break;
3008 case Type::UIntTyID: {
3009 // Make a 64 bit temporary... and zero out the top of it...
3010 unsigned TmpReg = makeAnotherReg(Type::LongTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003011 BuildMI(*BB, IP, X86::MOV32rr, 1, TmpReg).addReg(SrcReg);
3012 BuildMI(*BB, IP, X86::MOV32ri, 1, TmpReg+1).addImm(0);
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003013 SrcTy = Type::LongTy;
3014 SrcClass = cLong;
3015 SrcReg = TmpReg;
3016 break;
3017 }
3018 case Type::ULongTyID:
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003019 // Don't fild into the read destination.
3020 DestReg = makeAnotherReg(Type::DoubleTy);
3021 break;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003022 default: // No promotion needed...
3023 break;
3024 }
3025
3026 if (PromoteType) {
3027 unsigned TmpReg = makeAnotherReg(PromoteType);
Chris Lattner7b92de1e2004-04-06 19:29:36 +00003028 BuildMI(*BB, IP, PromoteOpcode, 1, TmpReg).addReg(SrcReg);
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003029 SrcTy = PromoteType;
3030 SrcClass = getClass(PromoteType);
Chris Lattner3e130a22003-01-13 00:32:26 +00003031 SrcReg = TmpReg;
3032 }
3033
3034 // Spill the integer to memory and reload it from there...
Chris Lattnerb6bac512004-02-25 06:13:04 +00003035 int FrameIdx =
3036 F->getFrameInfo()->CreateStackObject(SrcTy, TM.getTargetData());
Chris Lattner3e130a22003-01-13 00:32:26 +00003037
3038 if (SrcClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003039 addFrameReference(BuildMI(*BB, IP, X86::MOV32mr, 5),
Chris Lattneree352852004-02-29 07:22:16 +00003040 FrameIdx).addReg(SrcReg);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003041 addFrameReference(BuildMI(*BB, IP, X86::MOV32mr, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003042 FrameIdx, 4).addReg(SrcReg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00003043 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003044 static const unsigned Op1[] = { X86::MOV8mr, X86::MOV16mr, X86::MOV32mr };
Chris Lattneree352852004-02-29 07:22:16 +00003045 addFrameReference(BuildMI(*BB, IP, Op1[SrcClass], 5),
3046 FrameIdx).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003047 }
3048
3049 static const unsigned Op2[] =
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003050 { 0/*byte*/, X86::FILD16m, X86::FILD32m, 0/*FP*/, X86::FILD64m };
Chris Lattneree352852004-02-29 07:22:16 +00003051 addFrameReference(BuildMI(*BB, IP, Op2[SrcClass], 5, DestReg), FrameIdx);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003052
3053 // We need special handling for unsigned 64-bit integer sources. If the
3054 // input number has the "sign bit" set, then we loaded it incorrectly as a
3055 // negative 64-bit number. In this case, add an offset value.
3056 if (SrcTy == Type::ULongTy) {
3057 // Emit a test instruction to see if the dynamic input value was signed.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003058 BuildMI(*BB, IP, X86::TEST32rr, 2).addReg(SrcReg+1).addReg(SrcReg+1);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003059
Chris Lattnerb6bac512004-02-25 06:13:04 +00003060 // If the sign bit is set, get a pointer to an offset, otherwise get a
3061 // pointer to a zero.
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003062 MachineConstantPool *CP = F->getConstantPool();
3063 unsigned Zero = makeAnotherReg(Type::IntTy);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003064 Constant *Null = Constant::getNullValue(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003065 addConstantPoolReference(BuildMI(*BB, IP, X86::LEA32r, 5, Zero),
Chris Lattnerb6bac512004-02-25 06:13:04 +00003066 CP->getConstantPoolIndex(Null));
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003067 unsigned Offset = makeAnotherReg(Type::IntTy);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003068 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
3069
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003070 addConstantPoolReference(BuildMI(*BB, IP, X86::LEA32r, 5, Offset),
Chris Lattnerb6bac512004-02-25 06:13:04 +00003071 CP->getConstantPoolIndex(OffsetCst));
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003072 unsigned Addr = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003073 BuildMI(*BB, IP, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003074
3075 // Load the constant for an add. FIXME: this could make an 'fadd' that
3076 // reads directly from memory, but we don't support these yet.
3077 unsigned ConstReg = makeAnotherReg(Type::DoubleTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003078 addDirectMem(BuildMI(*BB, IP, X86::FLD32m, 4, ConstReg), Addr);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003079
Chris Lattneree352852004-02-29 07:22:16 +00003080 BuildMI(*BB, IP, X86::FpADD, 2, RealDestReg)
3081 .addReg(ConstReg).addReg(DestReg);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003082 }
3083
Chris Lattner3e130a22003-01-13 00:32:26 +00003084 return;
3085 }
3086
3087 // Handle casts from floating point to integer now...
3088 if (SrcClass == cFP) {
3089 // Change the floating point control register to use "round towards zero"
3090 // mode when truncating to an integer value.
3091 //
3092 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003093 addFrameReference(BuildMI(*BB, IP, X86::FNSTCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003094
3095 // Load the old value of the high byte of the control word...
3096 unsigned HighPartOfCW = makeAnotherReg(Type::UByteTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003097 addFrameReference(BuildMI(*BB, IP, X86::MOV8rm, 4, HighPartOfCW),
Chris Lattneree352852004-02-29 07:22:16 +00003098 CWFrameIdx, 1);
Chris Lattner3e130a22003-01-13 00:32:26 +00003099
3100 // Set the high part to be round to zero...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003101 addFrameReference(BuildMI(*BB, IP, X86::MOV8mi, 5),
Chris Lattneree352852004-02-29 07:22:16 +00003102 CWFrameIdx, 1).addImm(12);
Chris Lattner3e130a22003-01-13 00:32:26 +00003103
3104 // Reload the modified control word now...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003105 addFrameReference(BuildMI(*BB, IP, X86::FLDCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003106
3107 // Restore the memory image of control word to original value
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003108 addFrameReference(BuildMI(*BB, IP, X86::MOV8mr, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003109 CWFrameIdx, 1).addReg(HighPartOfCW);
Chris Lattner3e130a22003-01-13 00:32:26 +00003110
3111 // We don't have the facilities for directly storing byte sized data to
3112 // memory. Promote it to 16 bits. We also must promote unsigned values to
3113 // larger classes because we only have signed FP stores.
3114 unsigned StoreClass = DestClass;
3115 const Type *StoreTy = DestTy;
3116 if (StoreClass == cByte || DestTy->isUnsigned())
3117 switch (StoreClass) {
3118 case cByte: StoreTy = Type::ShortTy; StoreClass = cShort; break;
3119 case cShort: StoreTy = Type::IntTy; StoreClass = cInt; break;
3120 case cInt: StoreTy = Type::LongTy; StoreClass = cLong; break;
Brian Gaeked4615052003-07-18 20:23:43 +00003121 // The following treatment of cLong may not be perfectly right,
3122 // but it survives chains of casts of the form
3123 // double->ulong->double.
3124 case cLong: StoreTy = Type::LongTy; StoreClass = cLong; break;
Chris Lattner3e130a22003-01-13 00:32:26 +00003125 default: assert(0 && "Unknown store class!");
3126 }
3127
3128 // Spill the integer to memory and reload it from there...
3129 int FrameIdx =
3130 F->getFrameInfo()->CreateStackObject(StoreTy, TM.getTargetData());
3131
3132 static const unsigned Op1[] =
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003133 { 0, X86::FIST16m, X86::FIST32m, 0, X86::FISTP64m };
Chris Lattneree352852004-02-29 07:22:16 +00003134 addFrameReference(BuildMI(*BB, IP, Op1[StoreClass], 5),
3135 FrameIdx).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003136
3137 if (DestClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003138 addFrameReference(BuildMI(*BB, IP, X86::MOV32rm, 4, DestReg), FrameIdx);
3139 addFrameReference(BuildMI(*BB, IP, X86::MOV32rm, 4, DestReg+1),
Chris Lattneree352852004-02-29 07:22:16 +00003140 FrameIdx, 4);
Chris Lattner3e130a22003-01-13 00:32:26 +00003141 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003142 static const unsigned Op2[] = { X86::MOV8rm, X86::MOV16rm, X86::MOV32rm };
Chris Lattneree352852004-02-29 07:22:16 +00003143 addFrameReference(BuildMI(*BB, IP, Op2[DestClass], 4, DestReg), FrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003144 }
3145
3146 // Reload the original control word now...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003147 addFrameReference(BuildMI(*BB, IP, X86::FLDCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003148 return;
3149 }
3150
Brian Gaeked474e9c2002-12-06 10:49:33 +00003151 // Anything we haven't handled already, we can't (yet) handle at all.
Chris Lattnerc53544a2003-05-12 20:16:58 +00003152 assert(0 && "Unhandled cast instruction!");
Chris Lattner548f61d2003-04-23 17:22:12 +00003153 abort();
Brian Gaekefa8d5712002-11-22 11:07:01 +00003154}
Brian Gaekea1719c92002-10-31 23:03:59 +00003155
Chris Lattner73815062003-10-18 05:56:40 +00003156/// visitVANextInst - Implement the va_next instruction...
Chris Lattnereca195e2003-05-08 19:44:13 +00003157///
Chris Lattner73815062003-10-18 05:56:40 +00003158void ISel::visitVANextInst(VANextInst &I) {
3159 unsigned VAList = getReg(I.getOperand(0));
Chris Lattnereca195e2003-05-08 19:44:13 +00003160 unsigned DestReg = getReg(I);
3161
Chris Lattnereca195e2003-05-08 19:44:13 +00003162 unsigned Size;
Chris Lattner73815062003-10-18 05:56:40 +00003163 switch (I.getArgType()->getPrimitiveID()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00003164 default:
3165 std::cerr << I;
Chris Lattner73815062003-10-18 05:56:40 +00003166 assert(0 && "Error: bad type for va_next instruction!");
Chris Lattnereca195e2003-05-08 19:44:13 +00003167 return;
3168 case Type::PointerTyID:
3169 case Type::UIntTyID:
3170 case Type::IntTyID:
3171 Size = 4;
Chris Lattnereca195e2003-05-08 19:44:13 +00003172 break;
3173 case Type::ULongTyID:
3174 case Type::LongTyID:
Chris Lattnereca195e2003-05-08 19:44:13 +00003175 case Type::DoubleTyID:
3176 Size = 8;
Chris Lattnereca195e2003-05-08 19:44:13 +00003177 break;
3178 }
3179
3180 // Increment the VAList pointer...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003181 BuildMI(BB, X86::ADD32ri, 2, DestReg).addReg(VAList).addImm(Size);
Chris Lattner73815062003-10-18 05:56:40 +00003182}
Chris Lattnereca195e2003-05-08 19:44:13 +00003183
Chris Lattner73815062003-10-18 05:56:40 +00003184void ISel::visitVAArgInst(VAArgInst &I) {
3185 unsigned VAList = getReg(I.getOperand(0));
3186 unsigned DestReg = getReg(I);
3187
3188 switch (I.getType()->getPrimitiveID()) {
3189 default:
3190 std::cerr << I;
3191 assert(0 && "Error: bad type for va_next instruction!");
3192 return;
3193 case Type::PointerTyID:
3194 case Type::UIntTyID:
3195 case Type::IntTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003196 addDirectMem(BuildMI(BB, X86::MOV32rm, 4, DestReg), VAList);
Chris Lattner73815062003-10-18 05:56:40 +00003197 break;
3198 case Type::ULongTyID:
3199 case Type::LongTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003200 addDirectMem(BuildMI(BB, X86::MOV32rm, 4, DestReg), VAList);
3201 addRegOffset(BuildMI(BB, X86::MOV32rm, 4, DestReg+1), VAList, 4);
Chris Lattner73815062003-10-18 05:56:40 +00003202 break;
3203 case Type::DoubleTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003204 addDirectMem(BuildMI(BB, X86::FLD64m, 4, DestReg), VAList);
Chris Lattner73815062003-10-18 05:56:40 +00003205 break;
3206 }
Chris Lattnereca195e2003-05-08 19:44:13 +00003207}
3208
Misha Brukman538607f2004-03-01 23:53:11 +00003209/// visitGetElementPtrInst - instruction-select GEP instructions
3210///
Chris Lattner3e130a22003-01-13 00:32:26 +00003211void ISel::visitGetElementPtrInst(GetElementPtrInst &I) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00003212 // If this GEP instruction will be folded into all of its users, we don't need
3213 // to explicitly calculate it!
3214 unsigned A, B, C, D;
3215 if (isGEPFoldable(0, I.getOperand(0), I.op_begin()+1, I.op_end(), A,B,C,D)) {
3216 // Check all of the users of the instruction to see if they are loads and
3217 // stores.
3218 bool AllWillFold = true;
3219 for (Value::use_iterator UI = I.use_begin(), E = I.use_end(); UI != E; ++UI)
3220 if (cast<Instruction>(*UI)->getOpcode() != Instruction::Load)
3221 if (cast<Instruction>(*UI)->getOpcode() != Instruction::Store ||
3222 cast<Instruction>(*UI)->getOperand(0) == &I) {
3223 AllWillFold = false;
3224 break;
3225 }
3226
3227 // If the instruction is foldable, and will be folded into all users, don't
3228 // emit it!
3229 if (AllWillFold) return;
3230 }
3231
Chris Lattner3e130a22003-01-13 00:32:26 +00003232 unsigned outputReg = getReg(I);
Chris Lattner827832c2004-02-22 17:05:38 +00003233 emitGEPOperation(BB, BB->end(), I.getOperand(0),
Brian Gaeke68b1edc2002-12-16 04:23:29 +00003234 I.op_begin()+1, I.op_end(), outputReg);
Chris Lattnerc0812d82002-12-13 06:56:29 +00003235}
3236
Chris Lattner985fe3d2004-02-25 03:45:50 +00003237/// getGEPIndex - Inspect the getelementptr operands specified with GEPOps and
3238/// GEPTypes (the derived types being stepped through at each level). On return
3239/// from this function, if some indexes of the instruction are representable as
3240/// an X86 lea instruction, the machine operands are put into the Ops
3241/// instruction and the consumed indexes are poped from the GEPOps/GEPTypes
3242/// lists. Otherwise, GEPOps.size() is returned. If this returns a an
3243/// addressing mode that only partially consumes the input, the BaseReg input of
3244/// the addressing mode must be left free.
3245///
3246/// Note that there is one fewer entry in GEPTypes than there is in GEPOps.
3247///
Chris Lattnerb6bac512004-02-25 06:13:04 +00003248void ISel::getGEPIndex(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
3249 std::vector<Value*> &GEPOps,
3250 std::vector<const Type*> &GEPTypes, unsigned &BaseReg,
3251 unsigned &Scale, unsigned &IndexReg, unsigned &Disp) {
3252 const TargetData &TD = TM.getTargetData();
3253
Chris Lattner985fe3d2004-02-25 03:45:50 +00003254 // Clear out the state we are working with...
Chris Lattnerb6bac512004-02-25 06:13:04 +00003255 BaseReg = 0; // No base register
3256 Scale = 1; // Unit scale
3257 IndexReg = 0; // No index register
3258 Disp = 0; // No displacement
3259
Chris Lattner985fe3d2004-02-25 03:45:50 +00003260 // While there are GEP indexes that can be folded into the current address,
3261 // keep processing them.
3262 while (!GEPTypes.empty()) {
3263 if (const StructType *StTy = dyn_cast<StructType>(GEPTypes.back())) {
3264 // It's a struct access. CUI is the index into the structure,
3265 // which names the field. This index must have unsigned type.
3266 const ConstantUInt *CUI = cast<ConstantUInt>(GEPOps.back());
3267
3268 // Use the TargetData structure to pick out what the layout of the
3269 // structure is in memory. Since the structure index must be constant, we
3270 // can get its value and use it to find the right byte offset from the
3271 // StructLayout class's list of structure member offsets.
Chris Lattnerb6bac512004-02-25 06:13:04 +00003272 Disp += TD.getStructLayout(StTy)->MemberOffsets[CUI->getValue()];
Chris Lattner985fe3d2004-02-25 03:45:50 +00003273 GEPOps.pop_back(); // Consume a GEP operand
3274 GEPTypes.pop_back();
3275 } else {
3276 // It's an array or pointer access: [ArraySize x ElementType].
3277 const SequentialType *SqTy = cast<SequentialType>(GEPTypes.back());
3278 Value *idx = GEPOps.back();
3279
3280 // idx is the index into the array. Unlike with structure
3281 // indices, we may not know its actual value at code-generation
3282 // time.
Chris Lattner985fe3d2004-02-25 03:45:50 +00003283
3284 // If idx is a constant, fold it into the offset.
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003285 unsigned TypeSize = TD.getTypeSize(SqTy->getElementType());
Chris Lattner985fe3d2004-02-25 03:45:50 +00003286 if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(idx)) {
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003287 Disp += TypeSize*CSI->getValue();
Chris Lattner28977af2004-04-05 01:30:19 +00003288 } else if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(idx)) {
3289 Disp += TypeSize*CUI->getValue();
Chris Lattner985fe3d2004-02-25 03:45:50 +00003290 } else {
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003291 // If the index reg is already taken, we can't handle this index.
3292 if (IndexReg) return;
3293
3294 // If this is a size that we can handle, then add the index as
3295 switch (TypeSize) {
3296 case 1: case 2: case 4: case 8:
3297 // These are all acceptable scales on X86.
3298 Scale = TypeSize;
3299 break;
3300 default:
3301 // Otherwise, we can't handle this scale
3302 return;
3303 }
3304
3305 if (CastInst *CI = dyn_cast<CastInst>(idx))
3306 if (CI->getOperand(0)->getType() == Type::IntTy ||
3307 CI->getOperand(0)->getType() == Type::UIntTy)
3308 idx = CI->getOperand(0);
3309
3310 IndexReg = MBB ? getReg(idx, MBB, IP) : 1;
Chris Lattner985fe3d2004-02-25 03:45:50 +00003311 }
3312
3313 GEPOps.pop_back(); // Consume a GEP operand
3314 GEPTypes.pop_back();
3315 }
3316 }
Chris Lattnerb6bac512004-02-25 06:13:04 +00003317
3318 // GEPTypes is empty, which means we have a single operand left. See if we
3319 // can set it as the base register.
3320 //
3321 // FIXME: When addressing modes are more powerful/correct, we could load
3322 // global addresses directly as 32-bit immediates.
3323 assert(BaseReg == 0);
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003324 BaseReg = MBB ? getReg(GEPOps[0], MBB, IP) : 1;
Chris Lattnerb6bac512004-02-25 06:13:04 +00003325 GEPOps.pop_back(); // Consume the last GEP operand
Chris Lattner985fe3d2004-02-25 03:45:50 +00003326}
3327
3328
Chris Lattnerb6bac512004-02-25 06:13:04 +00003329/// isGEPFoldable - Return true if the specified GEP can be completely
3330/// folded into the addressing mode of a load/store or lea instruction.
3331bool ISel::isGEPFoldable(MachineBasicBlock *MBB,
3332 Value *Src, User::op_iterator IdxBegin,
3333 User::op_iterator IdxEnd, unsigned &BaseReg,
3334 unsigned &Scale, unsigned &IndexReg, unsigned &Disp) {
Chris Lattner7ca04092004-02-22 17:35:42 +00003335 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Src))
3336 Src = CPR->getValue();
3337
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003338 std::vector<Value*> GEPOps;
3339 GEPOps.resize(IdxEnd-IdxBegin+1);
3340 GEPOps[0] = Src;
3341 std::copy(IdxBegin, IdxEnd, GEPOps.begin()+1);
3342
3343 std::vector<const Type*> GEPTypes;
3344 GEPTypes.assign(gep_type_begin(Src->getType(), IdxBegin, IdxEnd),
3345 gep_type_end(Src->getType(), IdxBegin, IdxEnd));
3346
Chris Lattnerb6bac512004-02-25 06:13:04 +00003347 MachineBasicBlock::iterator IP;
3348 if (MBB) IP = MBB->end();
3349 getGEPIndex(MBB, IP, GEPOps, GEPTypes, BaseReg, Scale, IndexReg, Disp);
3350
3351 // We can fold it away iff the getGEPIndex call eliminated all operands.
3352 return GEPOps.empty();
3353}
3354
3355void ISel::emitGEPOperation(MachineBasicBlock *MBB,
3356 MachineBasicBlock::iterator IP,
3357 Value *Src, User::op_iterator IdxBegin,
3358 User::op_iterator IdxEnd, unsigned TargetReg) {
3359 const TargetData &TD = TM.getTargetData();
3360 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Src))
3361 Src = CPR->getValue();
3362
3363 std::vector<Value*> GEPOps;
3364 GEPOps.resize(IdxEnd-IdxBegin+1);
3365 GEPOps[0] = Src;
3366 std::copy(IdxBegin, IdxEnd, GEPOps.begin()+1);
3367
3368 std::vector<const Type*> GEPTypes;
3369 GEPTypes.assign(gep_type_begin(Src->getType(), IdxBegin, IdxEnd),
3370 gep_type_end(Src->getType(), IdxBegin, IdxEnd));
Chris Lattner985fe3d2004-02-25 03:45:50 +00003371
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003372 // Keep emitting instructions until we consume the entire GEP instruction.
3373 while (!GEPOps.empty()) {
3374 unsigned OldSize = GEPOps.size();
Chris Lattnerb6bac512004-02-25 06:13:04 +00003375 unsigned BaseReg, Scale, IndexReg, Disp;
3376 getGEPIndex(MBB, IP, GEPOps, GEPTypes, BaseReg, Scale, IndexReg, Disp);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003377
Chris Lattner985fe3d2004-02-25 03:45:50 +00003378 if (GEPOps.size() != OldSize) {
3379 // getGEPIndex consumed some of the input. Build an LEA instruction here.
Chris Lattnerb6bac512004-02-25 06:13:04 +00003380 unsigned NextTarget = 0;
3381 if (!GEPOps.empty()) {
3382 assert(BaseReg == 0 &&
3383 "getGEPIndex should have left the base register open for chaining!");
3384 NextTarget = BaseReg = makeAnotherReg(Type::UIntTy);
Chris Lattner985fe3d2004-02-25 03:45:50 +00003385 }
Chris Lattnerb6bac512004-02-25 06:13:04 +00003386
3387 if (IndexReg == 0 && Disp == 0)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003388 BuildMI(*MBB, IP, X86::MOV32rr, 1, TargetReg).addReg(BaseReg);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003389 else
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003390 addFullAddress(BuildMI(*MBB, IP, X86::LEA32r, 5, TargetReg),
Chris Lattnerb6bac512004-02-25 06:13:04 +00003391 BaseReg, Scale, IndexReg, Disp);
3392 --IP;
3393 TargetReg = NextTarget;
Chris Lattner985fe3d2004-02-25 03:45:50 +00003394 } else if (GEPTypes.empty()) {
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003395 // The getGEPIndex operation didn't want to build an LEA. Check to see if
3396 // all operands are consumed but the base pointer. If so, just load it
3397 // into the register.
Chris Lattner7ca04092004-02-22 17:35:42 +00003398 if (GlobalValue *GV = dyn_cast<GlobalValue>(GEPOps[0])) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003399 BuildMI(*MBB, IP, X86::MOV32ri, 1, TargetReg).addGlobalAddress(GV);
Chris Lattner7ca04092004-02-22 17:35:42 +00003400 } else {
3401 unsigned BaseReg = getReg(GEPOps[0], MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003402 BuildMI(*MBB, IP, X86::MOV32rr, 1, TargetReg).addReg(BaseReg);
Chris Lattner7ca04092004-02-22 17:35:42 +00003403 }
3404 break; // we are now done
Chris Lattnerb6bac512004-02-25 06:13:04 +00003405
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003406 } else {
Brian Gaeke20244b72002-12-12 15:33:40 +00003407 // It's an array or pointer access: [ArraySize x ElementType].
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003408 const SequentialType *SqTy = cast<SequentialType>(GEPTypes.back());
3409 Value *idx = GEPOps.back();
3410 GEPOps.pop_back(); // Consume a GEP operand
3411 GEPTypes.pop_back();
Chris Lattner8a307e82002-12-16 19:32:50 +00003412
Chris Lattner28977af2004-04-05 01:30:19 +00003413 // Many GEP instructions use a [cast (int/uint) to LongTy] as their
Chris Lattnerf5854472003-06-21 16:01:24 +00003414 // operand on X86. Handle this case directly now...
3415 if (CastInst *CI = dyn_cast<CastInst>(idx))
3416 if (CI->getOperand(0)->getType() == Type::IntTy ||
3417 CI->getOperand(0)->getType() == Type::UIntTy)
3418 idx = CI->getOperand(0);
3419
Chris Lattner3e130a22003-01-13 00:32:26 +00003420 // We want to add BaseReg to(idxReg * sizeof ElementType). First, we
Chris Lattner8a307e82002-12-16 19:32:50 +00003421 // must find the size of the pointed-to type (Not coincidentally, the next
3422 // type is the type of the elements in the array).
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003423 const Type *ElTy = SqTy->getElementType();
3424 unsigned elementSize = TD.getTypeSize(ElTy);
Chris Lattner8a307e82002-12-16 19:32:50 +00003425
3426 // If idxReg is a constant, we don't need to perform the multiply!
Chris Lattner28977af2004-04-05 01:30:19 +00003427 if (ConstantInt *CSI = dyn_cast<ConstantInt>(idx)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00003428 if (!CSI->isNullValue()) {
Chris Lattner28977af2004-04-05 01:30:19 +00003429 unsigned Offset = elementSize*CSI->getRawValue();
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003430 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003431 BuildMI(*MBB, IP, X86::ADD32ri, 2, TargetReg)
Chris Lattneree352852004-02-29 07:22:16 +00003432 .addReg(Reg).addImm(Offset);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003433 --IP; // Insert the next instruction before this one.
3434 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00003435 }
3436 } else if (elementSize == 1) {
3437 // If the element size is 1, we don't have to multiply, just add
3438 unsigned idxReg = getReg(idx, MBB, IP);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003439 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003440 BuildMI(*MBB, IP, X86::ADD32rr, 2,TargetReg).addReg(Reg).addReg(idxReg);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003441 --IP; // Insert the next instruction before this one.
3442 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00003443 } else {
3444 unsigned idxReg = getReg(idx, MBB, IP);
3445 unsigned OffsetReg = makeAnotherReg(Type::UIntTy);
Chris Lattnerb2acc512003-10-19 21:09:10 +00003446
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003447 // Make sure we can back the iterator up to point to the first
3448 // instruction emitted.
3449 MachineBasicBlock::iterator BeforeIt = IP;
3450 if (IP == MBB->begin())
3451 BeforeIt = MBB->end();
3452 else
3453 --BeforeIt;
Chris Lattnerb2acc512003-10-19 21:09:10 +00003454 doMultiplyConst(MBB, IP, OffsetReg, Type::IntTy, idxReg, elementSize);
3455
Chris Lattner8a307e82002-12-16 19:32:50 +00003456 // Emit an ADD to add OffsetReg to the basePtr.
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003457 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003458 BuildMI(*MBB, IP, X86::ADD32rr, 2, TargetReg)
Chris Lattneree352852004-02-29 07:22:16 +00003459 .addReg(Reg).addReg(OffsetReg);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003460
3461 // Step to the first instruction of the multiply.
3462 if (BeforeIt == MBB->end())
3463 IP = MBB->begin();
3464 else
3465 IP = ++BeforeIt;
3466
3467 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00003468 }
Brian Gaeke20244b72002-12-12 15:33:40 +00003469 }
Brian Gaeke20244b72002-12-12 15:33:40 +00003470 }
Brian Gaeke20244b72002-12-12 15:33:40 +00003471}
3472
3473
Chris Lattner065faeb2002-12-28 20:24:02 +00003474/// visitAllocaInst - If this is a fixed size alloca, allocate space from the
3475/// frame manager, otherwise do it the hard way.
3476///
3477void ISel::visitAllocaInst(AllocaInst &I) {
Brian Gaekee48ec012002-12-13 06:46:31 +00003478 // Find the data size of the alloca inst's getAllocatedType.
Chris Lattner065faeb2002-12-28 20:24:02 +00003479 const Type *Ty = I.getAllocatedType();
3480 unsigned TySize = TM.getTargetData().getTypeSize(Ty);
3481
3482 // If this is a fixed size alloca in the entry block for the function,
3483 // statically stack allocate the space.
3484 //
3485 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(I.getArraySize())) {
3486 if (I.getParent() == I.getParent()->getParent()->begin()) {
3487 TySize *= CUI->getValue(); // Get total allocated size...
3488 unsigned Alignment = TM.getTargetData().getTypeAlignment(Ty);
3489
3490 // Create a new stack object using the frame manager...
3491 int FrameIdx = F->getFrameInfo()->CreateStackObject(TySize, Alignment);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003492 addFrameReference(BuildMI(BB, X86::LEA32r, 5, getReg(I)), FrameIdx);
Chris Lattner065faeb2002-12-28 20:24:02 +00003493 return;
3494 }
3495 }
3496
3497 // Create a register to hold the temporary result of multiplying the type size
3498 // constant by the variable amount.
3499 unsigned TotalSizeReg = makeAnotherReg(Type::UIntTy);
3500 unsigned SrcReg1 = getReg(I.getArraySize());
Chris Lattner065faeb2002-12-28 20:24:02 +00003501
3502 // TotalSizeReg = mul <numelements>, <TypeSize>
3503 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattnerb2acc512003-10-19 21:09:10 +00003504 doMultiplyConst(BB, MBBI, TotalSizeReg, Type::UIntTy, SrcReg1, TySize);
Chris Lattner065faeb2002-12-28 20:24:02 +00003505
3506 // AddedSize = add <TotalSizeReg>, 15
3507 unsigned AddedSizeReg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003508 BuildMI(BB, X86::ADD32ri, 2, AddedSizeReg).addReg(TotalSizeReg).addImm(15);
Chris Lattner065faeb2002-12-28 20:24:02 +00003509
3510 // AlignedSize = and <AddedSize>, ~15
3511 unsigned AlignedSize = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003512 BuildMI(BB, X86::AND32ri, 2, AlignedSize).addReg(AddedSizeReg).addImm(~15);
Chris Lattner065faeb2002-12-28 20:24:02 +00003513
Brian Gaekee48ec012002-12-13 06:46:31 +00003514 // Subtract size from stack pointer, thereby allocating some space.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003515 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(AlignedSize);
Chris Lattner065faeb2002-12-28 20:24:02 +00003516
Brian Gaekee48ec012002-12-13 06:46:31 +00003517 // Put a pointer to the space into the result register, by copying
3518 // the stack pointer.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003519 BuildMI(BB, X86::MOV32rr, 1, getReg(I)).addReg(X86::ESP);
Chris Lattner065faeb2002-12-28 20:24:02 +00003520
Misha Brukman48196b32003-05-03 02:18:17 +00003521 // Inform the Frame Information that we have just allocated a variable-sized
Chris Lattner065faeb2002-12-28 20:24:02 +00003522 // object.
3523 F->getFrameInfo()->CreateVariableSizedObject();
Brian Gaeke20244b72002-12-12 15:33:40 +00003524}
Chris Lattner3e130a22003-01-13 00:32:26 +00003525
3526/// visitMallocInst - Malloc instructions are code generated into direct calls
3527/// to the library malloc.
3528///
3529void ISel::visitMallocInst(MallocInst &I) {
3530 unsigned AllocSize = TM.getTargetData().getTypeSize(I.getAllocatedType());
3531 unsigned Arg;
3532
3533 if (ConstantUInt *C = dyn_cast<ConstantUInt>(I.getOperand(0))) {
3534 Arg = getReg(ConstantUInt::get(Type::UIntTy, C->getValue() * AllocSize));
3535 } else {
3536 Arg = makeAnotherReg(Type::UIntTy);
Chris Lattnerb2acc512003-10-19 21:09:10 +00003537 unsigned Op0Reg = getReg(I.getOperand(0));
Chris Lattner3e130a22003-01-13 00:32:26 +00003538 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattnerb2acc512003-10-19 21:09:10 +00003539 doMultiplyConst(BB, MBBI, Arg, Type::UIntTy, Op0Reg, AllocSize);
Chris Lattner3e130a22003-01-13 00:32:26 +00003540 }
3541
3542 std::vector<ValueRecord> Args;
3543 Args.push_back(ValueRecord(Arg, Type::UIntTy));
3544 MachineInstr *TheCall = BuildMI(X86::CALLpcrel32,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003545 1).addExternalSymbol("malloc", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00003546 doCall(ValueRecord(getReg(I), I.getType()), TheCall, Args);
3547}
3548
3549
3550/// visitFreeInst - Free instructions are code gen'd to call the free libc
3551/// function.
3552///
3553void ISel::visitFreeInst(FreeInst &I) {
3554 std::vector<ValueRecord> Args;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00003555 Args.push_back(ValueRecord(I.getOperand(0)));
Chris Lattner3e130a22003-01-13 00:32:26 +00003556 MachineInstr *TheCall = BuildMI(X86::CALLpcrel32,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003557 1).addExternalSymbol("free", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00003558 doCall(ValueRecord(0, Type::VoidTy), TheCall, Args);
3559}
3560
Chris Lattnerd281de22003-07-26 23:49:58 +00003561/// createX86SimpleInstructionSelector - This pass converts an LLVM function
Chris Lattnerb4f68ed2002-10-29 22:37:54 +00003562/// into a machine code representation is a very simple peep-hole fashion. The
Chris Lattner72614082002-10-25 22:55:53 +00003563/// generated code sucks but the implementation is nice and simple.
3564///
Chris Lattnerf70e0c22003-12-28 21:23:38 +00003565FunctionPass *llvm::createX86SimpleInstructionSelector(TargetMachine &TM) {
3566 return new ISel(TM);
Chris Lattner72614082002-10-25 22:55:53 +00003567}