blob: e5898a801b1604b1ef45493e3313676985f07063 [file] [log] [blame]
Misha Brukman91b5ca82004-07-26 18:45:48 +00001//===-- X86ISelSimple.cpp - A simple instruction selector for x86 ---------===//
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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"
Misha Brukmanc8893fc2003-10-23 16:22:08 +000021#include "llvm/Pass.h"
Chris Lattner30483732004-06-20 07:49:54 +000022#include "llvm/CodeGen/IntrinsicLowering.h"
Misha Brukmanc8893fc2003-10-23 16:22:08 +000023#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 Lattner5434b422005-04-30 04:12:40 +000029#include "llvm/Target/TargetOptions.h"
Chris Lattner3f1e8e72004-02-22 07:04:00 +000030#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattner67580ed2003-05-13 20:21:19 +000031#include "llvm/Support/InstVisitor.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000032#include "llvm/ADT/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) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000051 switch (Ty->getTypeID()) {
Chris Lattner427aeb42004-04-11 19:21:59 +000052 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 {
Misha Brukmaneae1bf12004-09-21 18:21:21 +000078 struct X86ISel : public FunctionPass, InstVisitor<X86ISel> {
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000079 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 Lattnercb2fd552004-05-13 07:40:27 +000090 // AllocaMap - Mapping from fixed sized alloca instructions to the
91 // FrameIndex for the alloca.
92 std::map<AllocaInst*, unsigned> AllocaMap;
93
Misha Brukmaneae1bf12004-09-21 18:21:21 +000094 X86ISel(TargetMachine &tm) : TM(tm), F(0), BB(0) {}
Chris Lattner72614082002-10-25 22:55:53 +000095
96 /// runOnFunction - Top level implementation of instruction selection for
97 /// the entire function.
98 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000099 bool runOnFunction(Function &Fn) {
Chris Lattner11cf7aa2004-12-17 00:07:46 +0000100 // Lazily create a stack slot for the return address if needed.
Chris Lattner8cdbc352004-12-17 00:46:51 +0000101 ReturnAddressIndex = 0;
Chris Lattner11cf7aa2004-12-17 00:07:46 +0000102
Chris Lattner44827152003-12-28 09:47:19 +0000103 // First pass over the function, lower any unknown intrinsic functions
104 // with the IntrinsicLowering class.
105 LowerUnknownIntrinsicFunctionCalls(Fn);
106
Chris Lattner36b36032002-10-29 23:40:58 +0000107 F = &MachineFunction::construct(&Fn, TM);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000108
Chris Lattner065faeb2002-12-28 20:24:02 +0000109 // Create all of the machine basic blocks for the function...
Chris Lattner333b2fa2002-12-13 10:09:43 +0000110 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
111 F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I));
112
Chris Lattner14aa7fe2002-12-16 22:54:46 +0000113 BB = &F->front();
Chris Lattnerdbd73722003-05-06 21:32:22 +0000114
Chris Lattnerdbd73722003-05-06 21:32:22 +0000115 // Copy incoming arguments off of the stack...
Chris Lattner065faeb2002-12-28 20:24:02 +0000116 LoadArgumentsToVirtualRegs(Fn);
Chris Lattner14aa7fe2002-12-16 22:54:46 +0000117
Chris Lattnerc0354c92004-12-13 17:23:11 +0000118 // If this is main, emit special code.
119 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
120 EmitSpecialCodeForMain();
121
Chris Lattner333b2fa2002-12-13 10:09:43 +0000122 // Instruction select everything except PHI nodes
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000123 visit(Fn);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000124
125 // Select the PHI nodes
126 SelectPHINodes();
127
Chris Lattner986618e2004-02-22 19:47:26 +0000128 // Insert the FP_REG_KILL instructions into blocks that need them.
129 InsertFPRegKills();
130
Chris Lattner72614082002-10-25 22:55:53 +0000131 RegMap.clear();
Chris Lattner333b2fa2002-12-13 10:09:43 +0000132 MBBMap.clear();
Chris Lattnercb2fd552004-05-13 07:40:27 +0000133 AllocaMap.clear();
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000134 F = 0;
Chris Lattner2a865b02003-07-26 23:05:37 +0000135 // We always build a machine code representation for the function
136 return true;
Chris Lattner72614082002-10-25 22:55:53 +0000137 }
138
Chris Lattnerf0eb7be2002-12-15 21:13:40 +0000139 virtual const char *getPassName() const {
140 return "X86 Simple Instruction Selection";
141 }
142
Chris Lattnerc0354c92004-12-13 17:23:11 +0000143 /// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
144 /// the main function.
145 void EmitSpecialCodeForMain();
146
Chris Lattner72614082002-10-25 22:55:53 +0000147 /// visitBasicBlock - This method is called when we are visiting a new basic
Chris Lattner33f53b52002-10-29 20:48:56 +0000148 /// block. This simply creates a new MachineBasicBlock to emit code into
149 /// and adds it to the current MachineFunction. Subsequent visit* for
150 /// instructions will be invoked for all instructions in the basic block.
Chris Lattner72614082002-10-25 22:55:53 +0000151 ///
152 void visitBasicBlock(BasicBlock &LLVM_BB) {
Chris Lattner333b2fa2002-12-13 10:09:43 +0000153 BB = MBBMap[&LLVM_BB];
Chris Lattner72614082002-10-25 22:55:53 +0000154 }
155
Chris Lattner44827152003-12-28 09:47:19 +0000156 /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
157 /// function, lowering any calls to unknown intrinsic functions into the
158 /// equivalent LLVM code.
Misha Brukman538607f2004-03-01 23:53:11 +0000159 ///
Chris Lattner44827152003-12-28 09:47:19 +0000160 void LowerUnknownIntrinsicFunctionCalls(Function &F);
161
Chris Lattner065faeb2002-12-28 20:24:02 +0000162 /// LoadArgumentsToVirtualRegs - Load all of the arguments to this function
163 /// from the stack into virtual registers.
164 ///
165 void LoadArgumentsToVirtualRegs(Function &F);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000166
167 /// SelectPHINodes - Insert machine code to generate phis. This is tricky
168 /// because we have to generate our sources into the source basic blocks,
169 /// not the current one.
170 ///
171 void SelectPHINodes();
172
Chris Lattner986618e2004-02-22 19:47:26 +0000173 /// InsertFPRegKills - Insert FP_REG_KILL instructions into basic blocks
174 /// that need them. This only occurs due to the floating point stackifier
175 /// not being aggressive enough to handle arbitrary global stackification.
176 ///
177 void InsertFPRegKills();
178
Chris Lattner72614082002-10-25 22:55:53 +0000179 // Visitation methods for various instructions. These methods simply emit
180 // fixed X86 code for each instruction.
181 //
Brian Gaekefa8d5712002-11-22 11:07:01 +0000182
183 // Control flow operators
Chris Lattner72614082002-10-25 22:55:53 +0000184 void visitReturnInst(ReturnInst &RI);
Chris Lattner2df035b2002-11-02 19:27:56 +0000185 void visitBranchInst(BranchInst &BI);
Chris Lattner30483b02004-10-16 18:13:05 +0000186 void visitUnreachableInst(UnreachableInst &UI) {}
Chris Lattner3e130a22003-01-13 00:32:26 +0000187
188 struct ValueRecord {
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000189 Value *Val;
Chris Lattner3e130a22003-01-13 00:32:26 +0000190 unsigned Reg;
191 const Type *Ty;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000192 ValueRecord(unsigned R, const Type *T) : Val(0), Reg(R), Ty(T) {}
193 ValueRecord(Value *V) : Val(V), Reg(0), Ty(V->getType()) {}
Chris Lattner3e130a22003-01-13 00:32:26 +0000194 };
195 void doCall(const ValueRecord &Ret, MachineInstr *CallMI,
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000196 const std::vector<ValueRecord> &Args);
Brian Gaekefa8d5712002-11-22 11:07:01 +0000197 void visitCallInst(CallInst &I);
Brian Gaeked0fde302003-11-11 22:41:34 +0000198 void visitIntrinsicCall(Intrinsic::ID ID, CallInst &I);
Chris Lattnere2954c82002-11-02 20:04:26 +0000199
200 // Arithmetic operators
Chris Lattnerf01729e2002-11-02 20:54:46 +0000201 void visitSimpleBinary(BinaryOperator &B, unsigned OpcodeClass);
Chris Lattner68aad932002-11-02 20:13:22 +0000202 void visitAdd(BinaryOperator &B) { visitSimpleBinary(B, 0); }
203 void visitSub(BinaryOperator &B) { visitSimpleBinary(B, 1); }
Chris Lattnerca9671d2002-11-02 20:28:58 +0000204 void visitMul(BinaryOperator &B);
Chris Lattnere2954c82002-11-02 20:04:26 +0000205
Chris Lattnerf01729e2002-11-02 20:54:46 +0000206 void visitDiv(BinaryOperator &B) { visitDivRem(B); }
207 void visitRem(BinaryOperator &B) { visitDivRem(B); }
208 void visitDivRem(BinaryOperator &B);
209
Chris Lattnere2954c82002-11-02 20:04:26 +0000210 // Bitwise operators
Chris Lattner68aad932002-11-02 20:13:22 +0000211 void visitAnd(BinaryOperator &B) { visitSimpleBinary(B, 2); }
212 void visitOr (BinaryOperator &B) { visitSimpleBinary(B, 3); }
213 void visitXor(BinaryOperator &B) { visitSimpleBinary(B, 4); }
Chris Lattnere2954c82002-11-02 20:04:26 +0000214
Chris Lattner6d40c192003-01-16 16:43:00 +0000215 // Comparison operators...
216 void visitSetCondInst(SetCondInst &I);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000217 unsigned EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
218 MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000219 MachineBasicBlock::iterator MBBI);
Chris Lattner12d96a02004-03-30 21:22:00 +0000220 void visitSelectInst(SelectInst &SI);
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000221
222
Chris Lattner6fc3c522002-11-17 21:11:55 +0000223 // Memory Instructions
224 void visitLoadInst(LoadInst &I);
225 void visitStoreInst(StoreInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000226 void visitGetElementPtrInst(GetElementPtrInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000227 void visitAllocaInst(AllocaInst &I);
Chris Lattner3e130a22003-01-13 00:32:26 +0000228 void visitMallocInst(MallocInst &I);
229 void visitFreeInst(FreeInst &I);
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000230
Chris Lattnere2954c82002-11-02 20:04:26 +0000231 // Other operators
Brian Gaekea1719c92002-10-31 23:03:59 +0000232 void visitShiftInst(ShiftInst &I);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000233 void visitPHINode(PHINode &I) {} // PHI nodes handled by second pass
Brian Gaekefa8d5712002-11-22 11:07:01 +0000234 void visitCastInst(CastInst &I);
Chris Lattner73815062003-10-18 05:56:40 +0000235 void visitVANextInst(VANextInst &I);
236 void visitVAArgInst(VAArgInst &I);
Chris Lattner72614082002-10-25 22:55:53 +0000237
238 void visitInstruction(Instruction &I) {
239 std::cerr << "Cannot instruction select: " << I;
240 abort();
241 }
242
Brian Gaeke95780cc2002-12-13 07:56:18 +0000243 /// promote32 - Make a value 32-bits wide, and put it somewhere.
Chris Lattner3e130a22003-01-13 00:32:26 +0000244 ///
245 void promote32(unsigned targetReg, const ValueRecord &VR);
246
Chris Lattner721d2d42004-03-08 01:18:36 +0000247 /// getAddressingMode - Get the addressing mode to use to address the
248 /// specified value. The returned value should be used with addFullAddress.
Reid Spencerfc989e12004-08-30 00:13:26 +0000249 void getAddressingMode(Value *Addr, X86AddressMode &AM);
Chris Lattner721d2d42004-03-08 01:18:36 +0000250
251
252 /// getGEPIndex - This is used to fold GEP instructions into X86 addressing
253 /// expressions.
Chris Lattnerb6bac512004-02-25 06:13:04 +0000254 void getGEPIndex(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
255 std::vector<Value*> &GEPOps,
Reid Spencerfc989e12004-08-30 00:13:26 +0000256 std::vector<const Type*> &GEPTypes,
257 X86AddressMode &AM);
Chris Lattnerb6bac512004-02-25 06:13:04 +0000258
259 /// isGEPFoldable - Return true if the specified GEP can be completely
260 /// folded into the addressing mode of a load/store or lea instruction.
261 bool isGEPFoldable(MachineBasicBlock *MBB,
262 Value *Src, User::op_iterator IdxBegin,
Reid Spencerfc989e12004-08-30 00:13:26 +0000263 User::op_iterator IdxEnd, X86AddressMode &AM);
Chris Lattnerb6bac512004-02-25 06:13:04 +0000264
Chris Lattner3e130a22003-01-13 00:32:26 +0000265 /// emitGEPOperation - Common code shared between visitGetElementPtrInst and
266 /// constant expression GEP support.
267 ///
Chris Lattner827832c2004-02-22 17:05:38 +0000268 void emitGEPOperation(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
Chris Lattner333b2fa2002-12-13 10:09:43 +0000269 Value *Src, User::op_iterator IdxBegin,
Chris Lattnerc0812d82002-12-13 06:56:29 +0000270 User::op_iterator IdxEnd, unsigned TargetReg);
271
Chris Lattner548f61d2003-04-23 17:22:12 +0000272 /// emitCastOperation - Common code shared between visitCastInst and
273 /// constant expression cast support.
Misha Brukman538607f2004-03-01 23:53:11 +0000274 ///
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000275 void emitCastOperation(MachineBasicBlock *BB,MachineBasicBlock::iterator IP,
Chris Lattner548f61d2003-04-23 17:22:12 +0000276 Value *Src, const Type *DestTy, unsigned TargetReg);
277
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000278 /// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
279 /// and constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000280 ///
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000281 void emitSimpleBinaryOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000282 MachineBasicBlock::iterator IP,
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000283 Value *Op0, Value *Op1,
284 unsigned OperatorClass, unsigned TargetReg);
285
Chris Lattner6621ed92004-04-11 21:23:56 +0000286 /// emitBinaryFPOperation - This method handles emission of floating point
287 /// Add (0), Sub (1), Mul (2), and Div (3) operations.
288 void emitBinaryFPOperation(MachineBasicBlock *BB,
289 MachineBasicBlock::iterator IP,
290 Value *Op0, Value *Op1,
291 unsigned OperatorClass, unsigned TargetReg);
292
Chris Lattner462fa822004-04-11 20:56:28 +0000293 void emitMultiply(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
294 Value *Op0, Value *Op1, unsigned TargetReg);
295
296 void doMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI,
297 unsigned DestReg, const Type *DestTy,
298 unsigned Op0Reg, unsigned Op1Reg);
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000299 void doMultiplyConst(MachineBasicBlock *MBB,
Chris Lattner462fa822004-04-11 20:56:28 +0000300 MachineBasicBlock::iterator MBBI,
301 unsigned DestReg, const Type *DestTy,
302 unsigned Op0Reg, unsigned Op1Val);
303
Chris Lattnercadff442003-10-23 17:21:43 +0000304 void emitDivRemOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000305 MachineBasicBlock::iterator IP,
Chris Lattner462fa822004-04-11 20:56:28 +0000306 Value *Op0, Value *Op1, bool isDiv,
307 unsigned TargetReg);
Chris Lattnercadff442003-10-23 17:21:43 +0000308
Chris Lattner58c41fe2003-08-24 19:19:47 +0000309 /// emitSetCCOperation - Common code shared between visitSetCondInst and
310 /// constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000311 ///
Chris Lattner58c41fe2003-08-24 19:19:47 +0000312 void emitSetCCOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000313 MachineBasicBlock::iterator IP,
Chris Lattner58c41fe2003-08-24 19:19:47 +0000314 Value *Op0, Value *Op1, unsigned Opcode,
315 unsigned TargetReg);
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000316
317 /// emitShiftOperation - Common code shared between visitShiftInst and
318 /// constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000319 ///
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000320 void emitShiftOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000321 MachineBasicBlock::iterator IP,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000322 Value *Op, Value *ShiftAmount, bool isLeftShift,
323 const Type *ResultTy, unsigned DestReg);
Chris Lattnerce7cafa2004-11-13 20:48:57 +0000324
325 // Emit code for a 'SHLD DestReg, Op0, Op1, Amt' operation, where Amt is a
326 // constant.
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000327 void doSHLDConst(MachineBasicBlock *MBB,
Chris Lattnerce7cafa2004-11-13 20:48:57 +0000328 MachineBasicBlock::iterator MBBI,
329 unsigned DestReg, unsigned Op0Reg, unsigned Op1Reg,
330 unsigned Op1Val);
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000331
Chris Lattner12d96a02004-03-30 21:22:00 +0000332 /// emitSelectOperation - Common code shared between visitSelectInst and the
333 /// constant expression support.
334 void emitSelectOperation(MachineBasicBlock *MBB,
335 MachineBasicBlock::iterator IP,
336 Value *Cond, Value *TrueVal, Value *FalseVal,
337 unsigned DestReg);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000338
Chris Lattnerc5291f52002-10-27 21:16:59 +0000339 /// copyConstantToRegister - Output the instructions required to put the
340 /// specified constant into the specified register.
341 ///
Chris Lattner8a307e82002-12-16 19:32:50 +0000342 void copyConstantToRegister(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000343 MachineBasicBlock::iterator MBBI,
Chris Lattner8a307e82002-12-16 19:32:50 +0000344 Constant *C, unsigned Reg);
Chris Lattnerc5291f52002-10-27 21:16:59 +0000345
Chris Lattner01cdb1b2004-06-11 05:33:49 +0000346 void emitUCOMr(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI,
347 unsigned LHS, unsigned RHS);
348
Chris Lattner3e130a22003-01-13 00:32:26 +0000349 /// makeAnotherReg - This method returns the next register number we haven't
350 /// yet used.
351 ///
352 /// Long values are handled somewhat specially. They are always allocated
353 /// as pairs of 32 bit integer values. The register number returned is the
354 /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits
355 /// of the long value.
356 ///
Chris Lattnerc0812d82002-12-13 06:56:29 +0000357 unsigned makeAnotherReg(const Type *Ty) {
Chris Lattner7db1fa92003-07-30 05:33:48 +0000358 assert(dynamic_cast<const X86RegisterInfo*>(TM.getRegisterInfo()) &&
359 "Current target doesn't have X86 reg info??");
360 const X86RegisterInfo *MRI =
361 static_cast<const X86RegisterInfo*>(TM.getRegisterInfo());
Chris Lattner3e130a22003-01-13 00:32:26 +0000362 if (Ty == Type::LongTy || Ty == Type::ULongTy) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000363 const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy);
364 // Create the lower part
365 F->getSSARegMap()->createVirtualRegister(RC);
366 // Create the upper part.
367 return F->getSSARegMap()->createVirtualRegister(RC)-1;
Chris Lattner3e130a22003-01-13 00:32:26 +0000368 }
369
Chris Lattnerc0812d82002-12-13 06:56:29 +0000370 // Add the mapping of regnumber => reg class to MachineFunction
Chris Lattner7db1fa92003-07-30 05:33:48 +0000371 const TargetRegisterClass *RC = MRI->getRegClassForType(Ty);
Chris Lattner3e130a22003-01-13 00:32:26 +0000372 return F->getSSARegMap()->createVirtualRegister(RC);
Brian Gaeke20244b72002-12-12 15:33:40 +0000373 }
374
Chris Lattnercb2fd552004-05-13 07:40:27 +0000375 /// getReg - This method turns an LLVM value into a register number.
Chris Lattner72614082002-10-25 22:55:53 +0000376 ///
377 unsigned getReg(Value &V) { return getReg(&V); } // Allow references
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000378 unsigned getReg(Value *V) {
379 // Just append to the end of the current bb.
380 MachineBasicBlock::iterator It = BB->end();
381 return getReg(V, BB, It);
382 }
Brian Gaeke71794c02002-12-13 11:22:48 +0000383 unsigned getReg(Value *V, MachineBasicBlock *MBB,
Chris Lattnercb2fd552004-05-13 07:40:27 +0000384 MachineBasicBlock::iterator IPt);
Chris Lattner427aeb42004-04-11 19:21:59 +0000385
Chris Lattnercb2fd552004-05-13 07:40:27 +0000386 /// getFixedSizedAllocaFI - Return the frame index for a fixed sized alloca
387 /// that is to be statically allocated with the initial stack frame
388 /// adjustment.
389 unsigned getFixedSizedAllocaFI(AllocaInst *AI);
Chris Lattner72614082002-10-25 22:55:53 +0000390 };
391}
392
Chris Lattnercb2fd552004-05-13 07:40:27 +0000393/// dyn_castFixedAlloca - If the specified value is a fixed size alloca
394/// instruction in the entry block, return it. Otherwise, return a null
395/// pointer.
396static AllocaInst *dyn_castFixedAlloca(Value *V) {
397 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
398 BasicBlock *BB = AI->getParent();
399 if (isa<ConstantUInt>(AI->getArraySize()) && BB ==&BB->getParent()->front())
400 return AI;
401 }
402 return 0;
403}
404
405/// getReg - This method turns an LLVM value into a register number.
406///
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000407unsigned X86ISel::getReg(Value *V, MachineBasicBlock *MBB,
408 MachineBasicBlock::iterator IPt) {
Chris Lattnercb2fd552004-05-13 07:40:27 +0000409 // If this operand is a constant, emit the code to copy the constant into
410 // the register here...
Chris Lattnercb2fd552004-05-13 07:40:27 +0000411 if (Constant *C = dyn_cast<Constant>(V)) {
412 unsigned Reg = makeAnotherReg(V->getType());
413 copyConstantToRegister(MBB, IPt, C, Reg);
414 return Reg;
Chris Lattnercb2fd552004-05-13 07:40:27 +0000415 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
Chris Lattner8b486a12004-06-29 00:14:38 +0000416 // Do not emit noop casts at all, unless it's a double -> float cast.
417 if (getClassB(CI->getType()) == getClassB(CI->getOperand(0)->getType()) &&
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000418 (CI->getType() != Type::FloatTy ||
Chris Lattner8b486a12004-06-29 00:14:38 +0000419 CI->getOperand(0)->getType() != Type::DoubleTy))
Chris Lattnercb2fd552004-05-13 07:40:27 +0000420 return getReg(CI->getOperand(0), MBB, IPt);
421 } else if (AllocaInst *AI = dyn_castFixedAlloca(V)) {
422 // If the alloca address couldn't be folded into the instruction addressing,
423 // emit an explicit LEA as appropriate.
424 unsigned Reg = makeAnotherReg(V->getType());
425 unsigned FI = getFixedSizedAllocaFI(AI);
426 addFrameReference(BuildMI(*MBB, IPt, X86::LEA32r, 4, Reg), FI);
427 return Reg;
428 }
429
430 unsigned &Reg = RegMap[V];
431 if (Reg == 0) {
432 Reg = makeAnotherReg(V->getType());
433 RegMap[V] = Reg;
434 }
435
436 return Reg;
437}
438
439/// getFixedSizedAllocaFI - Return the frame index for a fixed sized alloca
440/// that is to be statically allocated with the initial stack frame
441/// adjustment.
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000442unsigned X86ISel::getFixedSizedAllocaFI(AllocaInst *AI) {
Chris Lattnercb2fd552004-05-13 07:40:27 +0000443 // Already computed this?
444 std::map<AllocaInst*, unsigned>::iterator I = AllocaMap.lower_bound(AI);
445 if (I != AllocaMap.end() && I->first == AI) return I->second;
446
447 const Type *Ty = AI->getAllocatedType();
448 ConstantUInt *CUI = cast<ConstantUInt>(AI->getArraySize());
449 unsigned TySize = TM.getTargetData().getTypeSize(Ty);
450 TySize *= CUI->getValue(); // Get total allocated size...
451 unsigned Alignment = TM.getTargetData().getTypeAlignment(Ty);
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000452
Chris Lattnercb2fd552004-05-13 07:40:27 +0000453 // Create a new stack object using the frame manager...
454 int FrameIdx = F->getFrameInfo()->CreateStackObject(TySize, Alignment);
455 AllocaMap.insert(I, std::make_pair(AI, FrameIdx));
456 return FrameIdx;
457}
458
459
Chris Lattnerc5291f52002-10-27 21:16:59 +0000460/// copyConstantToRegister - Output the instructions required to put the
461/// specified constant into the specified register.
462///
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000463void X86ISel::copyConstantToRegister(MachineBasicBlock *MBB,
464 MachineBasicBlock::iterator IP,
465 Constant *C, unsigned R) {
Chris Lattner30483b02004-10-16 18:13:05 +0000466 if (isa<UndefValue>(C)) {
467 switch (getClassB(C->getType())) {
468 case cFP:
469 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
470 BuildMI(*MBB, IP, X86::FLD0, 0, R);
471 return;
472 case cLong:
473 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, R+1);
474 // FALL THROUGH
475 default:
476 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, R);
477 return;
478 }
479 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000480 unsigned Class = 0;
481 switch (CE->getOpcode()) {
482 case Instruction::GetElementPtr:
Brian Gaeke68b1edc2002-12-16 04:23:29 +0000483 emitGEPOperation(MBB, IP, CE->getOperand(0),
Chris Lattner333b2fa2002-12-13 10:09:43 +0000484 CE->op_begin()+1, CE->op_end(), R);
Chris Lattnerc0812d82002-12-13 06:56:29 +0000485 return;
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000486 case Instruction::Cast:
Chris Lattner548f61d2003-04-23 17:22:12 +0000487 emitCastOperation(MBB, IP, CE->getOperand(0), CE->getType(), R);
Chris Lattner4b12cde2003-04-21 21:33:44 +0000488 return;
Chris Lattnerc0812d82002-12-13 06:56:29 +0000489
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000490 case Instruction::Xor: ++Class; // FALL THROUGH
491 case Instruction::Or: ++Class; // FALL THROUGH
492 case Instruction::And: ++Class; // FALL THROUGH
493 case Instruction::Sub: ++Class; // FALL THROUGH
494 case Instruction::Add:
495 emitSimpleBinaryOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
496 Class, R);
497 return;
498
Chris Lattner462fa822004-04-11 20:56:28 +0000499 case Instruction::Mul:
500 emitMultiply(MBB, IP, CE->getOperand(0), CE->getOperand(1), R);
Chris Lattnercadff442003-10-23 17:21:43 +0000501 return;
Chris Lattner462fa822004-04-11 20:56:28 +0000502
Chris Lattnercadff442003-10-23 17:21:43 +0000503 case Instruction::Div:
Chris Lattner462fa822004-04-11 20:56:28 +0000504 case Instruction::Rem:
505 emitDivRemOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
506 CE->getOpcode() == Instruction::Div, R);
Chris Lattnercadff442003-10-23 17:21:43 +0000507 return;
Chris Lattnercadff442003-10-23 17:21:43 +0000508
Chris Lattner58c41fe2003-08-24 19:19:47 +0000509 case Instruction::SetNE:
510 case Instruction::SetEQ:
511 case Instruction::SetLT:
512 case Instruction::SetGT:
513 case Instruction::SetLE:
514 case Instruction::SetGE:
515 emitSetCCOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
516 CE->getOpcode(), R);
517 return;
518
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000519 case Instruction::Shl:
520 case Instruction::Shr:
521 emitShiftOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000522 CE->getOpcode() == Instruction::Shl, CE->getType(), R);
523 return;
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000524
Chris Lattner12d96a02004-03-30 21:22:00 +0000525 case Instruction::Select:
526 emitSelectOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
527 CE->getOperand(2), R);
528 return;
529
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000530 default:
Chris Lattner76e2df22004-07-15 02:14:30 +0000531 std::cerr << "Offending expr: " << *C << "\n";
Chris Lattnerb2acc512003-10-19 21:09:10 +0000532 assert(0 && "Constant expression not yet handled!\n");
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000533 }
Brian Gaeke20244b72002-12-12 15:33:40 +0000534 }
Chris Lattnerc5291f52002-10-27 21:16:59 +0000535
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000536 if (C->getType()->isIntegral()) {
Chris Lattner6b993cc2002-12-15 08:02:15 +0000537 unsigned Class = getClassB(C->getType());
Chris Lattner3e130a22003-01-13 00:32:26 +0000538
539 if (Class == cLong) {
540 // Copy the value into the register pair.
Chris Lattnerc07736a2003-07-23 15:22:26 +0000541 uint64_t Val = cast<ConstantInt>(C)->getRawValue();
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000542 BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addImm(Val & 0xFFFFFFFF);
543 BuildMI(*MBB, IP, X86::MOV32ri, 1, R+1).addImm(Val >> 32);
Chris Lattner3e130a22003-01-13 00:32:26 +0000544 return;
545 }
546
Chris Lattner94af4142002-12-25 05:13:53 +0000547 assert(Class <= cInt && "Type not handled yet!");
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000548
549 static const unsigned IntegralOpcodeTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000550 X86::MOV8ri, X86::MOV16ri, X86::MOV32ri
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000551 };
552
Chris Lattner6b993cc2002-12-15 08:02:15 +0000553 if (C->getType() == Type::BoolTy) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000554 BuildMI(*MBB, IP, X86::MOV8ri, 1, R).addImm(C == ConstantBool::True);
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000555 } else {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000556 ConstantInt *CI = cast<ConstantInt>(C);
Chris Lattneree352852004-02-29 07:22:16 +0000557 BuildMI(*MBB, IP, IntegralOpcodeTab[Class],1,R).addImm(CI->getRawValue());
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000558 }
Chris Lattner94af4142002-12-25 05:13:53 +0000559 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
Chris Lattneraf703622004-02-02 18:56:30 +0000560 if (CFP->isExactlyValue(+0.0))
Chris Lattneree352852004-02-29 07:22:16 +0000561 BuildMI(*MBB, IP, X86::FLD0, 0, R);
Chris Lattneraf703622004-02-02 18:56:30 +0000562 else if (CFP->isExactlyValue(+1.0))
Chris Lattneree352852004-02-29 07:22:16 +0000563 BuildMI(*MBB, IP, X86::FLD1, 0, R);
Chris Lattner6ac95f92005-01-06 21:19:16 +0000564 else if (CFP->isExactlyValue(-0.0)) {
565 unsigned Tmp = makeAnotherReg(Type::DoubleTy);
566 BuildMI(*MBB, IP, X86::FLD0, 0, Tmp);
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000567 BuildMI(*MBB, IP, X86::FCHS, 1, R).addReg(Tmp);
Chris Lattner6ac95f92005-01-06 21:19:16 +0000568 } else if (CFP->isExactlyValue(-1.0)) {
569 unsigned Tmp = makeAnotherReg(Type::DoubleTy);
570 BuildMI(*MBB, IP, X86::FLD1, 0, Tmp);
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000571 BuildMI(*MBB, IP, X86::FCHS, 1, R).addReg(Tmp);
Chris Lattner6ac95f92005-01-06 21:19:16 +0000572 } else { // FIXME: PI, other native values
Chris Lattner5384b382005-01-05 16:30:14 +0000573 // FIXME: 2*PI -> LDPI + FADD
574
575 // Otherwise we need to spill the constant to memory.
Chris Lattner3e130a22003-01-13 00:32:26 +0000576 MachineConstantPool *CP = F->getConstantPool();
Chris Lattner5384b382005-01-05 16:30:14 +0000577
Chris Lattner6c09db22003-10-20 04:11:23 +0000578 const Type *Ty = CFP->getType();
579
Chris Lattner5384b382005-01-05 16:30:14 +0000580 // If a FP immediate is precise when represented as a float, we put it
581 // into the constant pool as a float, even if it's is statically typed as
582 // a double.
583 if (Ty == Type::DoubleTy)
584 if (CFP->isExactlyValue((float)CFP->getValue())) {
585 Ty = Type::FloatTy;
586 CFP = cast<ConstantFP>(ConstantExpr::getCast(CFP, Ty));
587 }
588
589 unsigned CPI = CP->getConstantPoolIndex(CFP);
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000590
Chris Lattner6c09db22003-10-20 04:11:23 +0000591 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000592 unsigned LoadOpcode = Ty == Type::FloatTy ? X86::FLD32m : X86::FLD64m;
Chris Lattneree352852004-02-29 07:22:16 +0000593 addConstantPoolReference(BuildMI(*MBB, IP, LoadOpcode, 4, R), CPI);
Chris Lattner94af4142002-12-25 05:13:53 +0000594 }
595
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000596 } else if (isa<ConstantPointerNull>(C)) {
Brian Gaeke20244b72002-12-12 15:33:40 +0000597 // Copy zero (null pointer) to the register.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000598 BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addImm(0);
Reid Spencer8863f182004-07-18 00:38:32 +0000599 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) {
600 BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addGlobalAddress(GV);
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000601 } else {
Chris Lattner76e2df22004-07-15 02:14:30 +0000602 std::cerr << "Offending constant: " << *C << "\n";
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000603 assert(0 && "Type not handled yet!");
Chris Lattnerc5291f52002-10-27 21:16:59 +0000604 }
605}
606
Chris Lattner065faeb2002-12-28 20:24:02 +0000607/// LoadArgumentsToVirtualRegs - Load all of the arguments to this function from
608/// the stack into virtual registers.
609///
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000610void X86ISel::LoadArgumentsToVirtualRegs(Function &Fn) {
Chris Lattner065faeb2002-12-28 20:24:02 +0000611 // Emit instructions to load the arguments... On entry to a function on the
612 // X86, the stack frame looks like this:
613 //
614 // [ESP] -- return address
Chris Lattner3e130a22003-01-13 00:32:26 +0000615 // [ESP + 4] -- first argument (leftmost lexically)
616 // [ESP + 8] -- second argument, if first argument is four bytes in size
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000617 // ...
Chris Lattner065faeb2002-12-28 20:24:02 +0000618 //
Chris Lattnerf158da22003-01-16 02:20:12 +0000619 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattneraa09b752002-12-28 21:08:28 +0000620 MachineFrameInfo *MFI = F->getFrameInfo();
Chris Lattner065faeb2002-12-28 20:24:02 +0000621
Chris Lattner4c52f0e2005-04-09 15:23:56 +0000622 for (Function::arg_iterator I = Fn.arg_begin(), E = Fn.arg_end();
623 I != E; ++I) {
Chris Lattner427aeb42004-04-11 19:21:59 +0000624 bool ArgLive = !I->use_empty();
625 unsigned Reg = ArgLive ? getReg(*I) : 0;
Chris Lattner065faeb2002-12-28 20:24:02 +0000626 int FI; // Frame object index
Chris Lattner427aeb42004-04-11 19:21:59 +0000627
Chris Lattner065faeb2002-12-28 20:24:02 +0000628 switch (getClassB(I->getType())) {
629 case cByte:
Chris Lattner427aeb42004-04-11 19:21:59 +0000630 if (ArgLive) {
631 FI = MFI->CreateFixedObject(1, ArgOffset);
632 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Reg), FI);
633 }
Chris Lattner065faeb2002-12-28 20:24:02 +0000634 break;
635 case cShort:
Chris Lattner427aeb42004-04-11 19:21:59 +0000636 if (ArgLive) {
637 FI = MFI->CreateFixedObject(2, ArgOffset);
638 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Reg), FI);
639 }
Chris Lattner065faeb2002-12-28 20:24:02 +0000640 break;
641 case cInt:
Chris Lattner427aeb42004-04-11 19:21:59 +0000642 if (ArgLive) {
643 FI = MFI->CreateFixedObject(4, ArgOffset);
644 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Reg), FI);
645 }
Chris Lattner065faeb2002-12-28 20:24:02 +0000646 break;
Chris Lattner3e130a22003-01-13 00:32:26 +0000647 case cLong:
Chris Lattner427aeb42004-04-11 19:21:59 +0000648 if (ArgLive) {
649 FI = MFI->CreateFixedObject(8, ArgOffset);
650 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Reg), FI);
651 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Reg+1), FI, 4);
652 }
Chris Lattner3e130a22003-01-13 00:32:26 +0000653 ArgOffset += 4; // longs require 4 additional bytes
654 break;
Chris Lattner065faeb2002-12-28 20:24:02 +0000655 case cFP:
Chris Lattner427aeb42004-04-11 19:21:59 +0000656 if (ArgLive) {
657 unsigned Opcode;
658 if (I->getType() == Type::FloatTy) {
659 Opcode = X86::FLD32m;
660 FI = MFI->CreateFixedObject(4, ArgOffset);
661 } else {
662 Opcode = X86::FLD64m;
663 FI = MFI->CreateFixedObject(8, ArgOffset);
664 }
665 addFrameReference(BuildMI(BB, Opcode, 4, Reg), FI);
Chris Lattner065faeb2002-12-28 20:24:02 +0000666 }
Chris Lattner427aeb42004-04-11 19:21:59 +0000667 if (I->getType() == Type::DoubleTy)
668 ArgOffset += 4; // doubles require 4 additional bytes
Chris Lattner065faeb2002-12-28 20:24:02 +0000669 break;
670 default:
671 assert(0 && "Unhandled argument type!");
672 }
Chris Lattner3e130a22003-01-13 00:32:26 +0000673 ArgOffset += 4; // Each argument takes at least 4 bytes on the stack...
Chris Lattner065faeb2002-12-28 20:24:02 +0000674 }
Chris Lattnereca195e2003-05-08 19:44:13 +0000675
676 // If the function takes variable number of arguments, add a frame offset for
677 // the start of the first vararg value... this is used to expand
678 // llvm.va_start.
679 if (Fn.getFunctionType()->isVarArg())
680 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner4c52f0e2005-04-09 15:23:56 +0000681
682 // Finally, inform the compiler what our live-outs will be, aka, what we will
683 // be returning in registers.
684 if (Fn.getReturnType() != Type::VoidTy)
685 switch (getClassB(Fn.getReturnType())) {
686 default: assert(0 && "Unknown type!");
687 case cByte:
688 case cShort:
689 case cInt:
690 F->addLiveOut(X86::EAX);
691 break;
692 case cLong:
693 F->addLiveOut(X86::EAX);
694 F->addLiveOut(X86::EDX);
695 break;
696 case cFP:
697 F->addLiveOut(X86::ST0);
698 break;
699 }
Chris Lattner065faeb2002-12-28 20:24:02 +0000700}
701
Chris Lattnerc0354c92004-12-13 17:23:11 +0000702/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
703/// the main function.
704void X86ISel::EmitSpecialCodeForMain() {
705 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
706 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
707 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000708
Chris Lattnerc0354c92004-12-13 17:23:11 +0000709 // Set the high part to be 64-bit precision.
710 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
711 CWFrameIdx, 1).addImm(2);
712
713 // Reload the modified control word now.
714 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
715}
Chris Lattner065faeb2002-12-28 20:24:02 +0000716
Chris Lattner333b2fa2002-12-13 10:09:43 +0000717/// SelectPHINodes - Insert machine code to generate phis. This is tricky
718/// because we have to generate our sources into the source basic blocks, not
719/// the current one.
720///
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000721void X86ISel::SelectPHINodes() {
Chris Lattnerd029cd22004-06-02 05:55:25 +0000722 const TargetInstrInfo &TII = *TM.getInstrInfo();
Chris Lattner333b2fa2002-12-13 10:09:43 +0000723 const Function &LF = *F->getFunction(); // The LLVM function...
724 for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) {
725 const BasicBlock *BB = I;
Chris Lattner168aa902004-02-29 07:10:16 +0000726 MachineBasicBlock &MBB = *MBBMap[I];
Chris Lattner333b2fa2002-12-13 10:09:43 +0000727
728 // Loop over all of the PHI nodes in the LLVM basic block...
Chris Lattner168aa902004-02-29 07:10:16 +0000729 MachineBasicBlock::iterator PHIInsertPoint = MBB.begin();
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000730 for (BasicBlock::const_iterator I = BB->begin(); isa<PHINode>(I); ++I) {
731 PHINode *PN = const_cast<PHINode*>(dyn_cast<PHINode>(I));
Chris Lattner3e130a22003-01-13 00:32:26 +0000732
Chris Lattner333b2fa2002-12-13 10:09:43 +0000733 // Create a new machine instr PHI node, and insert it.
Chris Lattner3e130a22003-01-13 00:32:26 +0000734 unsigned PHIReg = getReg(*PN);
Chris Lattner168aa902004-02-29 07:10:16 +0000735 MachineInstr *PhiMI = BuildMI(MBB, PHIInsertPoint,
736 X86::PHI, PN->getNumOperands(), PHIReg);
Chris Lattner3e130a22003-01-13 00:32:26 +0000737
738 MachineInstr *LongPhiMI = 0;
Chris Lattner168aa902004-02-29 07:10:16 +0000739 if (PN->getType() == Type::LongTy || PN->getType() == Type::ULongTy)
740 LongPhiMI = BuildMI(MBB, PHIInsertPoint,
741 X86::PHI, PN->getNumOperands(), PHIReg+1);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000742
Chris Lattnera6e73f12003-05-12 14:22:21 +0000743 // PHIValues - Map of blocks to incoming virtual registers. We use this
744 // so that we only initialize one incoming value for a particular block,
745 // even if the block has multiple entries in the PHI node.
746 //
747 std::map<MachineBasicBlock*, unsigned> PHIValues;
748
Chris Lattner333b2fa2002-12-13 10:09:43 +0000749 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
750 MachineBasicBlock *PredMBB = MBBMap[PN->getIncomingBlock(i)];
Chris Lattnera6e73f12003-05-12 14:22:21 +0000751 unsigned ValReg;
752 std::map<MachineBasicBlock*, unsigned>::iterator EntryIt =
753 PHIValues.lower_bound(PredMBB);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000754
Chris Lattnera6e73f12003-05-12 14:22:21 +0000755 if (EntryIt != PHIValues.end() && EntryIt->first == PredMBB) {
756 // We already inserted an initialization of the register for this
757 // predecessor. Recycle it.
758 ValReg = EntryIt->second;
759
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000760 } else {
Chris Lattnera81fc682003-10-19 00:26:11 +0000761 // Get the incoming value into a virtual register.
Chris Lattnera6e73f12003-05-12 14:22:21 +0000762 //
Chris Lattnera81fc682003-10-19 00:26:11 +0000763 Value *Val = PN->getIncomingValue(i);
764
765 // If this is a constant or GlobalValue, we may have to insert code
766 // into the basic block to compute it into a virtual register.
Reid Spencer8863f182004-07-18 00:38:32 +0000767 if ((isa<Constant>(Val) && !isa<ConstantExpr>(Val))) {
Chris Lattnercb2fd552004-05-13 07:40:27 +0000768 // Simple constants get emitted at the end of the basic block,
769 // before any terminator instructions. We "know" that the code to
770 // move a constant into a register will never clobber any flags.
771 ValReg = getReg(Val, PredMBB, PredMBB->getFirstTerminator());
Chris Lattnera81fc682003-10-19 00:26:11 +0000772 } else {
Chris Lattnercb2fd552004-05-13 07:40:27 +0000773 // Because we don't want to clobber any values which might be in
774 // physical registers with the computation of this constant (which
775 // might be arbitrarily complex if it is a constant expression),
776 // just insert the computation at the top of the basic block.
777 MachineBasicBlock::iterator PI = PredMBB->begin();
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000778
Chris Lattnercb2fd552004-05-13 07:40:27 +0000779 // Skip over any PHI nodes though!
780 while (PI != PredMBB->end() && PI->getOpcode() == X86::PHI)
781 ++PI;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000782
Chris Lattnercb2fd552004-05-13 07:40:27 +0000783 ValReg = getReg(Val, PredMBB, PI);
Chris Lattnera81fc682003-10-19 00:26:11 +0000784 }
Chris Lattnera6e73f12003-05-12 14:22:21 +0000785
786 // Remember that we inserted a value for this PHI for this predecessor
787 PHIValues.insert(EntryIt, std::make_pair(PredMBB, ValReg));
788 }
789
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000790 PhiMI->addRegOperand(ValReg);
Chris Lattner3e130a22003-01-13 00:32:26 +0000791 PhiMI->addMachineBasicBlockOperand(PredMBB);
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000792 if (LongPhiMI) {
793 LongPhiMI->addRegOperand(ValReg+1);
794 LongPhiMI->addMachineBasicBlockOperand(PredMBB);
795 }
Chris Lattner333b2fa2002-12-13 10:09:43 +0000796 }
Chris Lattner168aa902004-02-29 07:10:16 +0000797
798 // Now that we emitted all of the incoming values for the PHI node, make
799 // sure to reposition the InsertPoint after the PHI that we just added.
800 // This is needed because we might have inserted a constant into this
801 // block, right after the PHI's which is before the old insert point!
802 PHIInsertPoint = LongPhiMI ? LongPhiMI : PhiMI;
803 ++PHIInsertPoint;
Chris Lattner333b2fa2002-12-13 10:09:43 +0000804 }
805 }
806}
807
Chris Lattner986618e2004-02-22 19:47:26 +0000808/// RequiresFPRegKill - The floating point stackifier pass cannot insert
809/// compensation code on critical edges. As such, it requires that we kill all
810/// FP registers on the exit from any blocks that either ARE critical edges, or
811/// branch to a block that has incoming critical edges.
812///
813/// Note that this kill instruction will eventually be eliminated when
814/// restrictions in the stackifier are relaxed.
815///
Brian Gaeke1afe7732004-04-28 04:45:55 +0000816static bool RequiresFPRegKill(const MachineBasicBlock *MBB) {
Chris Lattner986618e2004-02-22 19:47:26 +0000817#if 0
Brian Gaeke1afe7732004-04-28 04:45:55 +0000818 const BasicBlock *BB = MBB->getBasicBlock ();
Chris Lattner986618e2004-02-22 19:47:26 +0000819 for (succ_const_iterator SI = succ_begin(BB), E = succ_end(BB); SI!=E; ++SI) {
820 const BasicBlock *Succ = *SI;
821 pred_const_iterator PI = pred_begin(Succ), PE = pred_end(Succ);
822 ++PI; // Block have at least one predecessory
823 if (PI != PE) { // If it has exactly one, this isn't crit edge
824 // If this block has more than one predecessor, check all of the
825 // predecessors to see if they have multiple successors. If so, then the
826 // block we are analyzing needs an FPRegKill.
827 for (PI = pred_begin(Succ); PI != PE; ++PI) {
828 const BasicBlock *Pred = *PI;
829 succ_const_iterator SI2 = succ_begin(Pred);
830 ++SI2; // There must be at least one successor of this block.
831 if (SI2 != succ_end(Pred))
832 return true; // Yes, we must insert the kill on this edge.
833 }
834 }
835 }
836 // If we got this far, there is no need to insert the kill instruction.
837 return false;
838#else
839 return true;
840#endif
841}
842
843// InsertFPRegKills - Insert FP_REG_KILL instructions into basic blocks that
844// need them. This only occurs due to the floating point stackifier not being
845// aggressive enough to handle arbitrary global stackification.
846//
847// Currently we insert an FP_REG_KILL instruction into each block that uses or
848// defines a floating point virtual register.
849//
850// When the global register allocators (like linear scan) finally update live
851// variable analysis, we can keep floating point values in registers across
852// portions of the CFG that do not involve critical edges. This will be a big
853// win, but we are waiting on the global allocators before we can do this.
854//
855// With a bit of work, the floating point stackifier pass can be enhanced to
856// break critical edges as needed (to make a place to put compensation code),
857// but this will require some infrastructure improvements as well.
858//
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000859void X86ISel::InsertFPRegKills() {
Chris Lattner986618e2004-02-22 19:47:26 +0000860 SSARegMap &RegMap = *F->getSSARegMap();
Chris Lattner986618e2004-02-22 19:47:26 +0000861
862 for (MachineFunction::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
Chris Lattner986618e2004-02-22 19:47:26 +0000863 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000864 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
865 MachineOperand& MO = I->getOperand(i);
866 if (MO.isRegister() && MO.getReg()) {
867 unsigned Reg = MO.getReg();
Chris Lattner223d4c42004-12-03 05:13:15 +0000868 if (MRegisterInfo::isVirtualRegister(Reg)) {
869 unsigned RegSize = RegMap.getRegClass(Reg)->getSize();
870 if (RegSize == 10 || RegSize == 8)
Chris Lattner65cf42d2004-02-23 07:29:45 +0000871 goto UsesFPReg;
Chris Lattner223d4c42004-12-03 05:13:15 +0000872 }
Chris Lattner986618e2004-02-22 19:47:26 +0000873 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000874 }
Chris Lattner65cf42d2004-02-23 07:29:45 +0000875 // If we haven't found an FP register use or def in this basic block, check
876 // to see if any of our successors has an FP PHI node, which will cause a
877 // copy to be inserted into this block.
Brian Gaeke235aa5e2004-04-28 04:34:16 +0000878 for (MachineBasicBlock::const_succ_iterator SI = BB->succ_begin(),
879 SE = BB->succ_end(); SI != SE; ++SI) {
880 MachineBasicBlock *SBB = *SI;
Chris Lattnerfbc39d52004-02-23 07:42:19 +0000881 for (MachineBasicBlock::iterator I = SBB->begin();
882 I != SBB->end() && I->getOpcode() == X86::PHI; ++I) {
Chris Lattner39869242004-12-02 17:57:21 +0000883 const TargetRegisterClass *RC =
884 RegMap.getRegClass(I->getOperand(0).getReg());
885 if (RC->getSize() == 10 || RC->getSize() == 8)
Chris Lattnerfbc39d52004-02-23 07:42:19 +0000886 goto UsesFPReg;
Chris Lattner986618e2004-02-22 19:47:26 +0000887 }
Chris Lattnerfbc39d52004-02-23 07:42:19 +0000888 }
Chris Lattner65cf42d2004-02-23 07:29:45 +0000889 continue;
890 UsesFPReg:
891 // Okay, this block uses an FP register. If the block has successors (ie,
892 // it's not an unwind/return), insert the FP_REG_KILL instruction.
Chris Lattner5384b382005-01-05 16:30:14 +0000893 if (BB->succ_size() && RequiresFPRegKill(BB)) {
Chris Lattneree352852004-02-29 07:22:16 +0000894 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
Chris Lattner65cf42d2004-02-23 07:29:45 +0000895 ++NumFPKill;
Chris Lattner986618e2004-02-22 19:47:26 +0000896 }
897 }
898}
899
900
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000901void X86ISel::getAddressingMode(Value *Addr, X86AddressMode &AM) {
Reid Spencerfc989e12004-08-30 00:13:26 +0000902 AM.BaseType = X86AddressMode::RegBase;
903 AM.Base.Reg = 0; AM.Scale = 1; AM.IndexReg = 0; AM.Disp = 0;
Chris Lattner9f1b5312004-05-13 15:12:43 +0000904 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Addr)) {
905 if (isGEPFoldable(BB, GEP->getOperand(0), GEP->op_begin()+1, GEP->op_end(),
Reid Spencerfc989e12004-08-30 00:13:26 +0000906 AM))
Chris Lattner9f1b5312004-05-13 15:12:43 +0000907 return;
908 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Addr)) {
909 if (CE->getOpcode() == Instruction::GetElementPtr)
910 if (isGEPFoldable(BB, CE->getOperand(0), CE->op_begin()+1, CE->op_end(),
Reid Spencerfc989e12004-08-30 00:13:26 +0000911 AM))
Chris Lattner9f1b5312004-05-13 15:12:43 +0000912 return;
Reid Spencerfc989e12004-08-30 00:13:26 +0000913 } else if (AllocaInst *AI = dyn_castFixedAlloca(Addr)) {
914 AM.BaseType = X86AddressMode::FrameIndexBase;
915 AM.Base.FrameIndex = getFixedSizedAllocaFI(AI);
916 return;
Chris Lattner358a9022004-10-15 05:05:29 +0000917 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(Addr)) {
918 AM.GV = GV;
919 return;
Chris Lattner9f1b5312004-05-13 15:12:43 +0000920 }
921
922 // If it's not foldable, reset addr mode.
Reid Spencerfc989e12004-08-30 00:13:26 +0000923 AM.BaseType = X86AddressMode::RegBase;
924 AM.Base.Reg = getReg(Addr);
925 AM.Scale = 1; AM.IndexReg = 0; AM.Disp = 0;
Chris Lattner9f1b5312004-05-13 15:12:43 +0000926}
927
Chris Lattner307ecba2004-03-30 22:39:09 +0000928// canFoldSetCCIntoBranchOrSelect - Return the setcc instruction if we can fold
929// it into the conditional branch or select instruction which is the only user
930// of the cc instruction. This is the case if the conditional branch is the
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000931// only user of the setcc. We also don't handle long arguments below, so we
Chris Lattnera6f9fe62004-06-18 00:29:22 +0000932// reject them here as well.
Chris Lattner6d40c192003-01-16 16:43:00 +0000933//
Chris Lattner307ecba2004-03-30 22:39:09 +0000934static SetCondInst *canFoldSetCCIntoBranchOrSelect(Value *V) {
Chris Lattner6d40c192003-01-16 16:43:00 +0000935 if (SetCondInst *SCI = dyn_cast<SetCondInst>(V))
Chris Lattner307ecba2004-03-30 22:39:09 +0000936 if (SCI->hasOneUse()) {
937 Instruction *User = cast<Instruction>(SCI->use_back());
Tanya Lattner9855b842004-12-01 18:27:03 +0000938 if ((isa<BranchInst>(User) || isa<SelectInst>(User)) &&
939 (getClassB(SCI->getOperand(0)->getType()) != cLong ||
940 SCI->getOpcode() == Instruction::SetEQ ||
941 SCI->getOpcode() == Instruction::SetNE) &&
942 (isa<BranchInst>(User) || User->getOperand(0) == V))
Chris Lattner6d40c192003-01-16 16:43:00 +0000943 return SCI;
944 }
945 return 0;
946}
Chris Lattner333b2fa2002-12-13 10:09:43 +0000947
Chris Lattner6d40c192003-01-16 16:43:00 +0000948// Return a fixed numbering for setcc instructions which does not depend on the
949// order of the opcodes.
950//
951static unsigned getSetCCNumber(unsigned Opcode) {
952 switch(Opcode) {
953 default: assert(0 && "Unknown setcc instruction!");
954 case Instruction::SetEQ: return 0;
955 case Instruction::SetNE: return 1;
956 case Instruction::SetLT: return 2;
Chris Lattner55f6fab2003-01-16 18:07:23 +0000957 case Instruction::SetGE: return 3;
958 case Instruction::SetGT: return 4;
959 case Instruction::SetLE: return 5;
Chris Lattner6d40c192003-01-16 16:43:00 +0000960 }
961}
Chris Lattner06925362002-11-17 21:56:38 +0000962
Chris Lattner6d40c192003-01-16 16:43:00 +0000963// LLVM -> X86 signed X86 unsigned
964// ----- ---------- ------------
965// seteq -> sete sete
966// setne -> setne setne
967// setlt -> setl setb
Chris Lattner55f6fab2003-01-16 18:07:23 +0000968// setge -> setge setae
Chris Lattner6d40c192003-01-16 16:43:00 +0000969// setgt -> setg seta
970// setle -> setle setbe
Chris Lattnerb2acc512003-10-19 21:09:10 +0000971// ----
972// sets // Used by comparison with 0 optimization
973// setns
974static const unsigned SetCCOpcodeTab[2][8] = {
975 { X86::SETEr, X86::SETNEr, X86::SETBr, X86::SETAEr, X86::SETAr, X86::SETBEr,
976 0, 0 },
977 { X86::SETEr, X86::SETNEr, X86::SETLr, X86::SETGEr, X86::SETGr, X86::SETLEr,
978 X86::SETSr, X86::SETNSr },
Chris Lattner6d40c192003-01-16 16:43:00 +0000979};
980
Chris Lattner01cdb1b2004-06-11 05:33:49 +0000981/// emitUCOMr - In the future when we support processors before the P6, this
982/// wraps the logic for emitting an FUCOMr vs FUCOMIr.
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000983void X86ISel::emitUCOMr(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
984 unsigned LHS, unsigned RHS) {
Chris Lattner01cdb1b2004-06-11 05:33:49 +0000985 if (0) { // for processors prior to the P6
986 BuildMI(*MBB, IP, X86::FUCOMr, 2).addReg(LHS).addReg(RHS);
987 BuildMI(*MBB, IP, X86::FNSTSW8r, 0);
988 BuildMI(*MBB, IP, X86::SAHF, 1);
989 } else {
990 BuildMI(*MBB, IP, X86::FUCOMIr, 2).addReg(LHS).addReg(RHS);
991 }
992}
993
Chris Lattnerb2acc512003-10-19 21:09:10 +0000994// EmitComparison - This function emits a comparison of the two operands,
995// returning the extended setcc code to use.
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000996unsigned X86ISel::EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
997 MachineBasicBlock *MBB,
998 MachineBasicBlock::iterator IP) {
Brian Gaeke1749d632002-11-07 17:59:21 +0000999 // The arguments are already supposed to be of the same type.
Chris Lattner6d40c192003-01-16 16:43:00 +00001000 const Type *CompTy = Op0->getType();
Chris Lattner3e130a22003-01-13 00:32:26 +00001001 unsigned Class = getClassB(CompTy);
Chris Lattner333864d2003-06-05 19:30:30 +00001002
1003 // Special case handling of: cmp R, i
Chris Lattner260195d2004-05-07 19:55:55 +00001004 if (isa<ConstantPointerNull>(Op1)) {
Chris Lattnerde95c9e2004-10-17 06:10:40 +00001005 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattner260195d2004-05-07 19:55:55 +00001006 if (OpNum < 2) // seteq/setne -> test
1007 BuildMI(*MBB, IP, X86::TEST32rr, 2).addReg(Op0r).addReg(Op0r);
1008 else
1009 BuildMI(*MBB, IP, X86::CMP32ri, 2).addReg(Op0r).addImm(0);
1010 return OpNum;
1011
1012 } else if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
Chris Lattnere80e6372004-04-06 16:02:27 +00001013 if (Class == cByte || Class == cShort || Class == cInt) {
1014 unsigned Op1v = CI->getRawValue();
Chris Lattnerc07736a2003-07-23 15:22:26 +00001015
Chris Lattner333864d2003-06-05 19:30:30 +00001016 // Mask off any upper bits of the constant, if there are any...
1017 Op1v &= (1ULL << (8 << Class)) - 1;
1018
Chris Lattnerb2acc512003-10-19 21:09:10 +00001019 // If this is a comparison against zero, emit more efficient code. We
1020 // can't handle unsigned comparisons against zero unless they are == or
1021 // !=. These should have been strength reduced already anyway.
1022 if (Op1v == 0 && (CompTy->isSigned() || OpNum < 2)) {
Chris Lattnerde95c9e2004-10-17 06:10:40 +00001023
1024 // If this is a comparison against zero and the LHS is an and of a
1025 // register with a constant, use the test to do the and.
1026 if (Instruction *Op0I = dyn_cast<Instruction>(Op0))
1027 if (Op0I->getOpcode() == Instruction::And && Op0->hasOneUse() &&
1028 isa<ConstantInt>(Op0I->getOperand(1))) {
1029 static const unsigned TESTTab[] = {
1030 X86::TEST8ri, X86::TEST16ri, X86::TEST32ri
1031 };
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001032
Chris Lattnerde95c9e2004-10-17 06:10:40 +00001033 // Emit test X, i
1034 unsigned LHS = getReg(Op0I->getOperand(0), MBB, IP);
1035 unsigned Imm =
1036 cast<ConstantInt>(Op0I->getOperand(1))->getRawValue();
1037 BuildMI(*MBB, IP, TESTTab[Class], 2).addReg(LHS).addImm(Imm);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001038
Chris Lattnerde95c9e2004-10-17 06:10:40 +00001039 if (OpNum == 2) return 6; // Map jl -> js
1040 if (OpNum == 3) return 7; // Map jg -> jns
1041 return OpNum;
1042 }
1043
1044 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001045 static const unsigned TESTTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001046 X86::TEST8rr, X86::TEST16rr, X86::TEST32rr
Chris Lattnerb2acc512003-10-19 21:09:10 +00001047 };
Chris Lattneree352852004-02-29 07:22:16 +00001048 BuildMI(*MBB, IP, TESTTab[Class], 2).addReg(Op0r).addReg(Op0r);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001049
1050 if (OpNum == 2) return 6; // Map jl -> js
1051 if (OpNum == 3) return 7; // Map jg -> jns
1052 return OpNum;
Chris Lattner333864d2003-06-05 19:30:30 +00001053 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001054
1055 static const unsigned CMPTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001056 X86::CMP8ri, X86::CMP16ri, X86::CMP32ri
Chris Lattnerb2acc512003-10-19 21:09:10 +00001057 };
1058
Chris Lattnerde95c9e2004-10-17 06:10:40 +00001059 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattneree352852004-02-29 07:22:16 +00001060 BuildMI(*MBB, IP, CMPTab[Class], 2).addReg(Op0r).addImm(Op1v);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001061 return OpNum;
Chris Lattnere80e6372004-04-06 16:02:27 +00001062 } else {
Chris Lattnerde95c9e2004-10-17 06:10:40 +00001063 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattnere80e6372004-04-06 16:02:27 +00001064 assert(Class == cLong && "Unknown integer class!");
1065 unsigned LowCst = CI->getRawValue();
1066 unsigned HiCst = CI->getRawValue() >> 32;
1067 if (OpNum < 2) { // seteq, setne
1068 unsigned LoTmp = Op0r;
1069 if (LowCst != 0) {
1070 LoTmp = makeAnotherReg(Type::IntTy);
1071 BuildMI(*MBB, IP, X86::XOR32ri, 2, LoTmp).addReg(Op0r).addImm(LowCst);
1072 }
1073 unsigned HiTmp = Op0r+1;
1074 if (HiCst != 0) {
1075 HiTmp = makeAnotherReg(Type::IntTy);
Chris Lattner48c937e2004-04-06 17:34:50 +00001076 BuildMI(*MBB, IP, X86::XOR32ri, 2,HiTmp).addReg(Op0r+1).addImm(HiCst);
Chris Lattnere80e6372004-04-06 16:02:27 +00001077 }
1078 unsigned FinalTmp = makeAnotherReg(Type::IntTy);
1079 BuildMI(*MBB, IP, X86::OR32rr, 2, FinalTmp).addReg(LoTmp).addReg(HiTmp);
1080 return OpNum;
Chris Lattner48c937e2004-04-06 17:34:50 +00001081 } else {
Tanya Lattner9855b842004-12-01 18:27:03 +00001082 // Emit a sequence of code which compares the high and low parts once
1083 // each, then uses a conditional move to handle the overflow case. For
1084 // example, a setlt for long would generate code like this:
1085 //
1086 // AL = lo(op1) < lo(op2) // Always unsigned comparison
1087 // BL = hi(op1) < hi(op2) // Signedness depends on operands
1088 // dest = hi(op1) == hi(op2) ? BL : AL;
1089 //
1090
1091 // FIXME: This would be much better if we had hierarchical register
1092 // classes! Until then, hardcode registers so that we can deal with
1093 // their aliases (because we don't have conditional byte moves).
1094 //
1095 BuildMI(*MBB, IP, X86::CMP32ri, 2).addReg(Op0r).addImm(LowCst);
1096 BuildMI(*MBB, IP, SetCCOpcodeTab[0][OpNum], 0, X86::AL);
1097 BuildMI(*MBB, IP, X86::CMP32ri, 2).addReg(Op0r+1).addImm(HiCst);
1098 BuildMI(*MBB, IP, SetCCOpcodeTab[CompTy->isSigned()][OpNum], 0,X86::BL);
1099 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::BH);
1100 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::AH);
1101 BuildMI(*MBB, IP, X86::CMOVE16rr, 2, X86::BX).addReg(X86::BX)
1102 .addReg(X86::AX);
1103 // NOTE: visitSetCondInst knows that the value is dumped into the BL
1104 // register at this point for long values...
Chris Lattner48c937e2004-04-06 17:34:50 +00001105 return OpNum;
Chris Lattnere80e6372004-04-06 16:02:27 +00001106 }
Chris Lattner333864d2003-06-05 19:30:30 +00001107 }
Chris Lattnere80e6372004-04-06 16:02:27 +00001108 }
Chris Lattner333864d2003-06-05 19:30:30 +00001109
Chris Lattnerde95c9e2004-10-17 06:10:40 +00001110 unsigned Op0r = getReg(Op0, MBB, IP);
1111
Chris Lattner9f08a922004-02-03 18:54:04 +00001112 // Special case handling of comparison against +/- 0.0
1113 if (ConstantFP *CFP = dyn_cast<ConstantFP>(Op1))
1114 if (CFP->isExactlyValue(+0.0) || CFP->isExactlyValue(-0.0)) {
Chris Lattneree352852004-02-29 07:22:16 +00001115 BuildMI(*MBB, IP, X86::FTST, 1).addReg(Op0r);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001116 BuildMI(*MBB, IP, X86::FNSTSW8r, 0);
Chris Lattneree352852004-02-29 07:22:16 +00001117 BuildMI(*MBB, IP, X86::SAHF, 1);
Chris Lattner9f08a922004-02-03 18:54:04 +00001118 return OpNum;
1119 }
1120
Chris Lattner58c41fe2003-08-24 19:19:47 +00001121 unsigned Op1r = getReg(Op1, MBB, IP);
Chris Lattner3e130a22003-01-13 00:32:26 +00001122 switch (Class) {
1123 default: assert(0 && "Unknown type class!");
1124 // Emit: cmp <var1>, <var2> (do the comparison). We can
1125 // compare 8-bit with 8-bit, 16-bit with 16-bit, 32-bit with
1126 // 32-bit.
1127 case cByte:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001128 BuildMI(*MBB, IP, X86::CMP8rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +00001129 break;
1130 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001131 BuildMI(*MBB, IP, X86::CMP16rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +00001132 break;
1133 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001134 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +00001135 break;
1136 case cFP:
Chris Lattner01cdb1b2004-06-11 05:33:49 +00001137 emitUCOMr(MBB, IP, Op0r, Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +00001138 break;
1139
1140 case cLong:
1141 if (OpNum < 2) { // seteq, setne
1142 unsigned LoTmp = makeAnotherReg(Type::IntTy);
1143 unsigned HiTmp = makeAnotherReg(Type::IntTy);
1144 unsigned FinalTmp = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001145 BuildMI(*MBB, IP, X86::XOR32rr, 2, LoTmp).addReg(Op0r).addReg(Op1r);
1146 BuildMI(*MBB, IP, X86::XOR32rr, 2, HiTmp).addReg(Op0r+1).addReg(Op1r+1);
1147 BuildMI(*MBB, IP, X86::OR32rr, 2, FinalTmp).addReg(LoTmp).addReg(HiTmp);
Chris Lattner3e130a22003-01-13 00:32:26 +00001148 break; // Allow the sete or setne to be generated from flags set by OR
1149 } else {
1150 // Emit a sequence of code which compares the high and low parts once
1151 // each, then uses a conditional move to handle the overflow case. For
1152 // example, a setlt for long would generate code like this:
1153 //
1154 // AL = lo(op1) < lo(op2) // Signedness depends on operands
1155 // BL = hi(op1) < hi(op2) // Always unsigned comparison
Chris Lattner9984fd02004-05-09 23:16:33 +00001156 // dest = hi(op1) == hi(op2) ? BL : AL;
Chris Lattner3e130a22003-01-13 00:32:26 +00001157 //
1158
Chris Lattner6d40c192003-01-16 16:43:00 +00001159 // FIXME: This would be much better if we had hierarchical register
Chris Lattner3e130a22003-01-13 00:32:26 +00001160 // classes! Until then, hardcode registers so that we can deal with their
1161 // aliases (because we don't have conditional byte moves).
1162 //
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001163 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattneree352852004-02-29 07:22:16 +00001164 BuildMI(*MBB, IP, SetCCOpcodeTab[0][OpNum], 0, X86::AL);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001165 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r+1).addReg(Op1r+1);
Chris Lattneree352852004-02-29 07:22:16 +00001166 BuildMI(*MBB, IP, SetCCOpcodeTab[CompTy->isSigned()][OpNum], 0, X86::BL);
1167 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::BH);
1168 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::AH);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001169 BuildMI(*MBB, IP, X86::CMOVE16rr, 2, X86::BX).addReg(X86::BX)
Chris Lattneree352852004-02-29 07:22:16 +00001170 .addReg(X86::AX);
Chris Lattner6d40c192003-01-16 16:43:00 +00001171 // NOTE: visitSetCondInst knows that the value is dumped into the BL
1172 // register at this point for long values...
Chris Lattnerb2acc512003-10-19 21:09:10 +00001173 return OpNum;
Chris Lattner3e130a22003-01-13 00:32:26 +00001174 }
1175 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001176 return OpNum;
Chris Lattner6d40c192003-01-16 16:43:00 +00001177}
Chris Lattner3e130a22003-01-13 00:32:26 +00001178
Chris Lattner6d40c192003-01-16 16:43:00 +00001179/// SetCC instructions - Here we just emit boilerplate code to set a byte-sized
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001180/// register, then move it to wherever the result should be.
Chris Lattner6d40c192003-01-16 16:43:00 +00001181///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001182void X86ISel::visitSetCondInst(SetCondInst &I) {
Chris Lattner307ecba2004-03-30 22:39:09 +00001183 if (canFoldSetCCIntoBranchOrSelect(&I))
1184 return; // Fold this into a branch or select.
Chris Lattner6d40c192003-01-16 16:43:00 +00001185
Chris Lattner6d40c192003-01-16 16:43:00 +00001186 unsigned DestReg = getReg(I);
Chris Lattner58c41fe2003-08-24 19:19:47 +00001187 MachineBasicBlock::iterator MII = BB->end();
1188 emitSetCCOperation(BB, MII, I.getOperand(0), I.getOperand(1), I.getOpcode(),
1189 DestReg);
1190}
Chris Lattner6d40c192003-01-16 16:43:00 +00001191
Chris Lattner58c41fe2003-08-24 19:19:47 +00001192/// emitSetCCOperation - Common code shared between visitSetCondInst and
1193/// constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +00001194///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001195void X86ISel::emitSetCCOperation(MachineBasicBlock *MBB,
1196 MachineBasicBlock::iterator IP,
1197 Value *Op0, Value *Op1, unsigned Opcode,
1198 unsigned TargetReg) {
Chris Lattner58c41fe2003-08-24 19:19:47 +00001199 unsigned OpNum = getSetCCNumber(Opcode);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001200 OpNum = EmitComparison(OpNum, Op0, Op1, MBB, IP);
Chris Lattner58c41fe2003-08-24 19:19:47 +00001201
Chris Lattnerb2acc512003-10-19 21:09:10 +00001202 const Type *CompTy = Op0->getType();
1203 unsigned CompClass = getClassB(CompTy);
1204 bool isSigned = CompTy->isSigned() && CompClass != cFP;
1205
Tanya Lattner9855b842004-12-01 18:27:03 +00001206 if (CompClass != cLong || OpNum < 2) {
1207 // Handle normal comparisons with a setcc instruction...
1208 BuildMI(*MBB, IP, SetCCOpcodeTab[isSigned][OpNum], 0, TargetReg);
1209 } else {
1210 // Handle long comparisons by copying the value which is already in BL into
1211 // the register we want...
1212 BuildMI(*MBB, IP, X86::MOV8rr, 1, TargetReg).addReg(X86::BL);
1213 }
Brian Gaeke1749d632002-11-07 17:59:21 +00001214}
Chris Lattner51b49a92002-11-02 19:45:49 +00001215
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001216void X86ISel::visitSelectInst(SelectInst &SI) {
Chris Lattner12d96a02004-03-30 21:22:00 +00001217 unsigned DestReg = getReg(SI);
1218 MachineBasicBlock::iterator MII = BB->end();
1219 emitSelectOperation(BB, MII, SI.getCondition(), SI.getTrueValue(),
1220 SI.getFalseValue(), DestReg);
1221}
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001222
Chris Lattner12d96a02004-03-30 21:22:00 +00001223/// emitSelect - Common code shared between visitSelectInst and the constant
1224/// expression support.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001225void X86ISel::emitSelectOperation(MachineBasicBlock *MBB,
1226 MachineBasicBlock::iterator IP,
1227 Value *Cond, Value *TrueVal, Value *FalseVal,
1228 unsigned DestReg) {
Chris Lattner12d96a02004-03-30 21:22:00 +00001229 unsigned SelectClass = getClassB(TrueVal->getType());
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001230
Chris Lattner12d96a02004-03-30 21:22:00 +00001231 // We don't support 8-bit conditional moves. If we have incoming constants,
1232 // transform them into 16-bit constants to avoid having a run-time conversion.
1233 if (SelectClass == cByte) {
1234 if (Constant *T = dyn_cast<Constant>(TrueVal))
1235 TrueVal = ConstantExpr::getCast(T, Type::ShortTy);
1236 if (Constant *F = dyn_cast<Constant>(FalseVal))
1237 FalseVal = ConstantExpr::getCast(F, Type::ShortTy);
1238 }
1239
Chris Lattner82c5a992004-04-13 21:56:09 +00001240 unsigned TrueReg = getReg(TrueVal, MBB, IP);
1241 unsigned FalseReg = getReg(FalseVal, MBB, IP);
1242 if (TrueReg == FalseReg) {
1243 static const unsigned Opcode[] = {
1244 X86::MOV8rr, X86::MOV16rr, X86::MOV32rr, X86::FpMOV, X86::MOV32rr
1245 };
1246 BuildMI(*MBB, IP, Opcode[SelectClass], 1, DestReg).addReg(TrueReg);
1247 if (SelectClass == cLong)
1248 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg+1).addReg(TrueReg+1);
1249 return;
1250 }
1251
Chris Lattner307ecba2004-03-30 22:39:09 +00001252 unsigned Opcode;
1253 if (SetCondInst *SCI = canFoldSetCCIntoBranchOrSelect(Cond)) {
1254 // We successfully folded the setcc into the select instruction.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001255
Chris Lattner307ecba2004-03-30 22:39:09 +00001256 unsigned OpNum = getSetCCNumber(SCI->getOpcode());
1257 OpNum = EmitComparison(OpNum, SCI->getOperand(0), SCI->getOperand(1), MBB,
1258 IP);
1259
1260 const Type *CompTy = SCI->getOperand(0)->getType();
1261 bool isSigned = CompTy->isSigned() && getClassB(CompTy) != cFP;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001262
Chris Lattner307ecba2004-03-30 22:39:09 +00001263 // LLVM -> X86 signed X86 unsigned
1264 // ----- ---------- ------------
1265 // seteq -> cmovNE cmovNE
1266 // setne -> cmovE cmovE
1267 // setlt -> cmovGE cmovAE
1268 // setge -> cmovL cmovB
1269 // setgt -> cmovLE cmovBE
1270 // setle -> cmovG cmovA
1271 // ----
1272 // cmovNS // Used by comparison with 0 optimization
1273 // cmovS
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001274
Chris Lattner307ecba2004-03-30 22:39:09 +00001275 switch (SelectClass) {
Chris Lattner352eb482004-03-31 22:03:35 +00001276 default: assert(0 && "Unknown value class!");
1277 case cFP: {
1278 // Annoyingly, we don't have a full set of floating point conditional
1279 // moves. :(
1280 static const unsigned OpcodeTab[2][8] = {
1281 { X86::FCMOVNE, X86::FCMOVE, X86::FCMOVAE, X86::FCMOVB,
1282 X86::FCMOVBE, X86::FCMOVA, 0, 0 },
1283 { X86::FCMOVNE, X86::FCMOVE, 0, 0, 0, 0, 0, 0 },
1284 };
1285 Opcode = OpcodeTab[isSigned][OpNum];
1286
1287 // If opcode == 0, we hit a case that we don't support. Output a setcc
1288 // and compare the result against zero.
1289 if (Opcode == 0) {
1290 unsigned CompClass = getClassB(CompTy);
1291 unsigned CondReg;
1292 if (CompClass != cLong || OpNum < 2) {
1293 CondReg = makeAnotherReg(Type::BoolTy);
1294 // Handle normal comparisons with a setcc instruction...
1295 BuildMI(*MBB, IP, SetCCOpcodeTab[isSigned][OpNum], 0, CondReg);
1296 } else {
1297 // Long comparisons end up in the BL register.
1298 CondReg = X86::BL;
1299 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001300
Chris Lattner68626c22004-03-31 22:22:36 +00001301 BuildMI(*MBB, IP, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
Chris Lattner352eb482004-03-31 22:03:35 +00001302 Opcode = X86::FCMOVE;
1303 }
1304 break;
1305 }
Chris Lattner307ecba2004-03-30 22:39:09 +00001306 case cByte:
1307 case cShort: {
1308 static const unsigned OpcodeTab[2][8] = {
1309 { X86::CMOVNE16rr, X86::CMOVE16rr, X86::CMOVAE16rr, X86::CMOVB16rr,
1310 X86::CMOVBE16rr, X86::CMOVA16rr, 0, 0 },
1311 { X86::CMOVNE16rr, X86::CMOVE16rr, X86::CMOVGE16rr, X86::CMOVL16rr,
1312 X86::CMOVLE16rr, X86::CMOVG16rr, X86::CMOVNS16rr, X86::CMOVS16rr },
1313 };
1314 Opcode = OpcodeTab[isSigned][OpNum];
1315 break;
1316 }
1317 case cInt:
1318 case cLong: {
1319 static const unsigned OpcodeTab[2][8] = {
1320 { X86::CMOVNE32rr, X86::CMOVE32rr, X86::CMOVAE32rr, X86::CMOVB32rr,
1321 X86::CMOVBE32rr, X86::CMOVA32rr, 0, 0 },
1322 { X86::CMOVNE32rr, X86::CMOVE32rr, X86::CMOVGE32rr, X86::CMOVL32rr,
1323 X86::CMOVLE32rr, X86::CMOVG32rr, X86::CMOVNS32rr, X86::CMOVS32rr },
1324 };
1325 Opcode = OpcodeTab[isSigned][OpNum];
1326 break;
1327 }
1328 }
1329 } else {
1330 // Get the value being branched on, and use it to set the condition codes.
1331 unsigned CondReg = getReg(Cond, MBB, IP);
Chris Lattner68626c22004-03-31 22:22:36 +00001332 BuildMI(*MBB, IP, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
Chris Lattner307ecba2004-03-30 22:39:09 +00001333 switch (SelectClass) {
Chris Lattner352eb482004-03-31 22:03:35 +00001334 default: assert(0 && "Unknown value class!");
1335 case cFP: Opcode = X86::FCMOVE; break;
Chris Lattner307ecba2004-03-30 22:39:09 +00001336 case cByte:
Chris Lattner352eb482004-03-31 22:03:35 +00001337 case cShort: Opcode = X86::CMOVE16rr; break;
Chris Lattner307ecba2004-03-30 22:39:09 +00001338 case cInt:
Chris Lattner352eb482004-03-31 22:03:35 +00001339 case cLong: Opcode = X86::CMOVE32rr; break;
Chris Lattner307ecba2004-03-30 22:39:09 +00001340 }
1341 }
Chris Lattner12d96a02004-03-30 21:22:00 +00001342
Chris Lattner12d96a02004-03-30 21:22:00 +00001343 unsigned RealDestReg = DestReg;
Chris Lattner12d96a02004-03-30 21:22:00 +00001344
Chris Lattner12d96a02004-03-30 21:22:00 +00001345
1346 // Annoyingly enough, X86 doesn't HAVE 8-bit conditional moves. Because of
1347 // this, we have to promote the incoming values to 16 bits, perform a 16-bit
1348 // cmove, then truncate the result.
1349 if (SelectClass == cByte) {
1350 DestReg = makeAnotherReg(Type::ShortTy);
1351 if (getClassB(TrueVal->getType()) == cByte) {
1352 // Promote the true value, by storing it into AL, and reading from AX.
1353 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::AL).addReg(TrueReg);
1354 BuildMI(*MBB, IP, X86::MOV8ri, 1, X86::AH).addImm(0);
1355 TrueReg = makeAnotherReg(Type::ShortTy);
1356 BuildMI(*MBB, IP, X86::MOV16rr, 1, TrueReg).addReg(X86::AX);
1357 }
1358 if (getClassB(FalseVal->getType()) == cByte) {
1359 // Promote the true value, by storing it into CL, and reading from CX.
1360 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(FalseReg);
1361 BuildMI(*MBB, IP, X86::MOV8ri, 1, X86::CH).addImm(0);
1362 FalseReg = makeAnotherReg(Type::ShortTy);
1363 BuildMI(*MBB, IP, X86::MOV16rr, 1, FalseReg).addReg(X86::CX);
1364 }
1365 }
1366
1367 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(TrueReg).addReg(FalseReg);
1368
1369 switch (SelectClass) {
1370 case cByte:
1371 // We did the computation with 16-bit registers. Truncate back to our
1372 // result by copying into AX then copying out AL.
1373 BuildMI(*MBB, IP, X86::MOV16rr, 1, X86::AX).addReg(DestReg);
1374 BuildMI(*MBB, IP, X86::MOV8rr, 1, RealDestReg).addReg(X86::AL);
1375 break;
1376 case cLong:
1377 // Move the upper half of the value as well.
1378 BuildMI(*MBB, IP, Opcode, 2,DestReg+1).addReg(TrueReg+1).addReg(FalseReg+1);
1379 break;
1380 }
1381}
Chris Lattner58c41fe2003-08-24 19:19:47 +00001382
1383
1384
Brian Gaekec2505982002-11-30 11:57:28 +00001385/// promote32 - Emit instructions to turn a narrow operand into a 32-bit-wide
1386/// operand, in the specified target register.
Misha Brukman538607f2004-03-01 23:53:11 +00001387///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001388void X86ISel::promote32(unsigned targetReg, const ValueRecord &VR) {
Chris Lattner9984fd02004-05-09 23:16:33 +00001389 bool isUnsigned = VR.Ty->isUnsigned() || VR.Ty == Type::BoolTy;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001390
Chris Lattner29bf0622004-04-06 01:21:00 +00001391 Value *Val = VR.Val;
1392 const Type *Ty = VR.Ty;
Chris Lattner502e36c2004-04-06 01:25:33 +00001393 if (Val) {
Chris Lattner29bf0622004-04-06 01:21:00 +00001394 if (Constant *C = dyn_cast<Constant>(Val)) {
1395 Val = ConstantExpr::getCast(C, Type::IntTy);
1396 Ty = Type::IntTy;
1397 }
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001398
Chris Lattner502e36c2004-04-06 01:25:33 +00001399 // If this is a simple constant, just emit a MOVri directly to avoid the
1400 // copy.
1401 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
1402 int TheVal = CI->getRawValue() & 0xFFFFFFFF;
Chris Lattner2b10b082004-05-12 16:35:04 +00001403 BuildMI(BB, X86::MOV32ri, 1, targetReg).addImm(TheVal);
Chris Lattner502e36c2004-04-06 01:25:33 +00001404 return;
1405 }
1406 }
1407
Chris Lattner29bf0622004-04-06 01:21:00 +00001408 // Make sure we have the register number for this value...
1409 unsigned Reg = Val ? getReg(Val) : VR.Reg;
1410
1411 switch (getClassB(Ty)) {
Chris Lattner94af4142002-12-25 05:13:53 +00001412 case cByte:
1413 // Extend value into target register (8->32)
1414 if (isUnsigned)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001415 BuildMI(BB, X86::MOVZX32rr8, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001416 else
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001417 BuildMI(BB, X86::MOVSX32rr8, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001418 break;
1419 case cShort:
1420 // Extend value into target register (16->32)
1421 if (isUnsigned)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001422 BuildMI(BB, X86::MOVZX32rr16, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001423 else
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001424 BuildMI(BB, X86::MOVSX32rr16, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001425 break;
1426 case cInt:
1427 // Move value into target register (32->32)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001428 BuildMI(BB, X86::MOV32rr, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001429 break;
1430 default:
1431 assert(0 && "Unpromotable operand class in promote32");
1432 }
Brian Gaekec2505982002-11-30 11:57:28 +00001433}
Chris Lattnerc5291f52002-10-27 21:16:59 +00001434
Chris Lattner72614082002-10-25 22:55:53 +00001435/// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such,
1436/// we have the following possibilities:
1437///
1438/// ret void: No return value, simply emit a 'ret' instruction
1439/// ret sbyte, ubyte : Extend value into EAX and return
1440/// ret short, ushort: Extend value into EAX and return
1441/// ret int, uint : Move value into EAX and return
1442/// ret pointer : Move value into EAX and return
Chris Lattner06925362002-11-17 21:56:38 +00001443/// ret long, ulong : Move value into EAX/EDX and return
1444/// ret float/double : Top of FP stack
Chris Lattner72614082002-10-25 22:55:53 +00001445///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001446void X86ISel::visitReturnInst(ReturnInst &I) {
Chris Lattner94af4142002-12-25 05:13:53 +00001447 if (I.getNumOperands() == 0) {
1448 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
1449 return;
1450 }
1451
1452 Value *RetVal = I.getOperand(0);
Chris Lattner3e130a22003-01-13 00:32:26 +00001453 switch (getClassB(RetVal->getType())) {
Chris Lattner94af4142002-12-25 05:13:53 +00001454 case cByte: // integral return values: extend or move into EAX and return
1455 case cShort:
1456 case cInt:
Chris Lattner29bf0622004-04-06 01:21:00 +00001457 promote32(X86::EAX, ValueRecord(RetVal));
Chris Lattner94af4142002-12-25 05:13:53 +00001458 break;
Chris Lattner29bf0622004-04-06 01:21:00 +00001459 case cFP: { // Floats & Doubles: Return in ST(0)
1460 unsigned RetReg = getReg(RetVal);
Chris Lattner3e130a22003-01-13 00:32:26 +00001461 BuildMI(BB, X86::FpSETRESULT, 1).addReg(RetReg);
Chris Lattner94af4142002-12-25 05:13:53 +00001462 break;
Chris Lattner29bf0622004-04-06 01:21:00 +00001463 }
1464 case cLong: {
1465 unsigned RetReg = getReg(RetVal);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001466 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(RetReg);
1467 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RetReg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00001468 break;
Chris Lattner29bf0622004-04-06 01:21:00 +00001469 }
Chris Lattner94af4142002-12-25 05:13:53 +00001470 default:
Chris Lattner3e130a22003-01-13 00:32:26 +00001471 visitInstruction(I);
Chris Lattner94af4142002-12-25 05:13:53 +00001472 }
Chris Lattner43189d12002-11-17 20:07:45 +00001473 // Emit a 'ret' instruction
Chris Lattner94af4142002-12-25 05:13:53 +00001474 BuildMI(BB, X86::RET, 0);
Chris Lattner72614082002-10-25 22:55:53 +00001475}
1476
Chris Lattner55f6fab2003-01-16 18:07:23 +00001477// getBlockAfter - Return the basic block which occurs lexically after the
1478// specified one.
1479static inline BasicBlock *getBlockAfter(BasicBlock *BB) {
1480 Function::iterator I = BB; ++I; // Get iterator to next block
1481 return I != BB->getParent()->end() ? &*I : 0;
1482}
1483
Chris Lattner51b49a92002-11-02 19:45:49 +00001484/// visitBranchInst - Handle conditional and unconditional branches here. Note
1485/// that since code layout is frozen at this point, that if we are trying to
1486/// jump to a block that is the immediate successor of the current block, we can
Chris Lattner6d40c192003-01-16 16:43:00 +00001487/// just make a fall-through (but we don't currently).
Chris Lattner51b49a92002-11-02 19:45:49 +00001488///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001489void X86ISel::visitBranchInst(BranchInst &BI) {
Brian Gaekeea9ca672004-04-28 04:19:37 +00001490 // Update machine-CFG edges
1491 BB->addSuccessor (MBBMap[BI.getSuccessor(0)]);
1492 if (BI.isConditional())
1493 BB->addSuccessor (MBBMap[BI.getSuccessor(1)]);
1494
Chris Lattner55f6fab2003-01-16 18:07:23 +00001495 BasicBlock *NextBB = getBlockAfter(BI.getParent()); // BB after current one
1496
1497 if (!BI.isConditional()) { // Unconditional branch?
Chris Lattnercf93cdd2004-01-30 22:13:44 +00001498 if (BI.getSuccessor(0) != NextBB)
Brian Gaeke9f088e42004-05-14 06:54:56 +00001499 BuildMI(BB, X86::JMP, 1).addMBB(MBBMap[BI.getSuccessor(0)]);
Chris Lattner6d40c192003-01-16 16:43:00 +00001500 return;
1501 }
1502
1503 // See if we can fold the setcc into the branch itself...
Chris Lattner307ecba2004-03-30 22:39:09 +00001504 SetCondInst *SCI = canFoldSetCCIntoBranchOrSelect(BI.getCondition());
Chris Lattner6d40c192003-01-16 16:43:00 +00001505 if (SCI == 0) {
1506 // Nope, cannot fold setcc into this branch. Emit a branch on a condition
1507 // computed some other way...
Chris Lattner065faeb2002-12-28 20:24:02 +00001508 unsigned condReg = getReg(BI.getCondition());
Chris Lattner68626c22004-03-31 22:22:36 +00001509 BuildMI(BB, X86::TEST8rr, 2).addReg(condReg).addReg(condReg);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001510 if (BI.getSuccessor(1) == NextBB) {
1511 if (BI.getSuccessor(0) != NextBB)
Brian Gaeke9f088e42004-05-14 06:54:56 +00001512 BuildMI(BB, X86::JNE, 1).addMBB(MBBMap[BI.getSuccessor(0)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001513 } else {
Brian Gaeke9f088e42004-05-14 06:54:56 +00001514 BuildMI(BB, X86::JE, 1).addMBB(MBBMap[BI.getSuccessor(1)]);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001515
Chris Lattner55f6fab2003-01-16 18:07:23 +00001516 if (BI.getSuccessor(0) != NextBB)
Brian Gaeke9f088e42004-05-14 06:54:56 +00001517 BuildMI(BB, X86::JMP, 1).addMBB(MBBMap[BI.getSuccessor(0)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001518 }
Chris Lattner6d40c192003-01-16 16:43:00 +00001519 return;
Chris Lattner94af4142002-12-25 05:13:53 +00001520 }
Chris Lattner6d40c192003-01-16 16:43:00 +00001521
1522 unsigned OpNum = getSetCCNumber(SCI->getOpcode());
Chris Lattner58c41fe2003-08-24 19:19:47 +00001523 MachineBasicBlock::iterator MII = BB->end();
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001524 OpNum = EmitComparison(OpNum, SCI->getOperand(0), SCI->getOperand(1), BB,MII);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001525
1526 const Type *CompTy = SCI->getOperand(0)->getType();
1527 bool isSigned = CompTy->isSigned() && getClassB(CompTy) != cFP;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001528
Chris Lattnerb2acc512003-10-19 21:09:10 +00001529
Chris Lattner6d40c192003-01-16 16:43:00 +00001530 // LLVM -> X86 signed X86 unsigned
1531 // ----- ---------- ------------
1532 // seteq -> je je
1533 // setne -> jne jne
1534 // setlt -> jl jb
Chris Lattner55f6fab2003-01-16 18:07:23 +00001535 // setge -> jge jae
Chris Lattner6d40c192003-01-16 16:43:00 +00001536 // setgt -> jg ja
1537 // setle -> jle jbe
Chris Lattnerb2acc512003-10-19 21:09:10 +00001538 // ----
1539 // js // Used by comparison with 0 optimization
1540 // jns
1541
1542 static const unsigned OpcodeTab[2][8] = {
1543 { X86::JE, X86::JNE, X86::JB, X86::JAE, X86::JA, X86::JBE, 0, 0 },
1544 { X86::JE, X86::JNE, X86::JL, X86::JGE, X86::JG, X86::JLE,
1545 X86::JS, X86::JNS },
Chris Lattner6d40c192003-01-16 16:43:00 +00001546 };
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001547
Chris Lattner55f6fab2003-01-16 18:07:23 +00001548 if (BI.getSuccessor(0) != NextBB) {
Brian Gaeke9f088e42004-05-14 06:54:56 +00001549 BuildMI(BB, OpcodeTab[isSigned][OpNum], 1)
1550 .addMBB(MBBMap[BI.getSuccessor(0)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001551 if (BI.getSuccessor(1) != NextBB)
Brian Gaeke9f088e42004-05-14 06:54:56 +00001552 BuildMI(BB, X86::JMP, 1).addMBB(MBBMap[BI.getSuccessor(1)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001553 } else {
1554 // Change to the inverse condition...
1555 if (BI.getSuccessor(1) != NextBB) {
1556 OpNum ^= 1;
Brian Gaeke9f088e42004-05-14 06:54:56 +00001557 BuildMI(BB, OpcodeTab[isSigned][OpNum], 1)
1558 .addMBB(MBBMap[BI.getSuccessor(1)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001559 }
1560 }
Chris Lattner2df035b2002-11-02 19:27:56 +00001561}
1562
Chris Lattner3e130a22003-01-13 00:32:26 +00001563
1564/// doCall - This emits an abstract call instruction, setting up the arguments
1565/// and the return value as appropriate. For the actual function call itself,
1566/// it inserts the specified CallMI instruction into the stream.
1567///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001568void X86ISel::doCall(const ValueRecord &Ret, MachineInstr *CallMI,
1569 const std::vector<ValueRecord> &Args) {
Chris Lattner065faeb2002-12-28 20:24:02 +00001570 // Count how many bytes are to be pushed on the stack...
1571 unsigned NumBytes = 0;
Misha Brukman0d2cf3a2002-12-04 19:22:53 +00001572
Chris Lattner3e130a22003-01-13 00:32:26 +00001573 if (!Args.empty()) {
1574 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1575 switch (getClassB(Args[i].Ty)) {
Chris Lattner065faeb2002-12-28 20:24:02 +00001576 case cByte: case cShort: case cInt:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001577 NumBytes += 4; break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001578 case cLong:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001579 NumBytes += 8; break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001580 case cFP:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001581 NumBytes += Args[i].Ty == Type::FloatTy ? 4 : 8;
1582 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001583 default: assert(0 && "Unknown class!");
1584 }
1585
1586 // Adjust the stack pointer for the new arguments...
Chris Lattneree352852004-02-29 07:22:16 +00001587 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(NumBytes);
Chris Lattner065faeb2002-12-28 20:24:02 +00001588
1589 // Arguments go on the stack in reverse order, as specified by the ABI.
1590 unsigned ArgOffset = 0;
Chris Lattner3e130a22003-01-13 00:32:26 +00001591 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattnerce6096f2004-03-01 02:34:08 +00001592 unsigned ArgReg;
Chris Lattner3e130a22003-01-13 00:32:26 +00001593 switch (getClassB(Args[i].Ty)) {
Chris Lattner065faeb2002-12-28 20:24:02 +00001594 case cByte:
Chris Lattner2b10b082004-05-12 16:35:04 +00001595 if (Args[i].Val && isa<ConstantBool>(Args[i].Val)) {
1596 addRegOffset(BuildMI(BB, X86::MOV32mi, 5), X86::ESP, ArgOffset)
1597 .addImm(Args[i].Val == ConstantBool::True);
1598 break;
1599 }
1600 // FALL THROUGH
Chris Lattner21585222004-03-01 02:42:43 +00001601 case cShort:
1602 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1603 // Zero/Sign extend constant, then stuff into memory.
1604 ConstantInt *Val = cast<ConstantInt>(Args[i].Val);
1605 Val = cast<ConstantInt>(ConstantExpr::getCast(Val, Type::IntTy));
1606 addRegOffset(BuildMI(BB, X86::MOV32mi, 5), X86::ESP, ArgOffset)
1607 .addImm(Val->getRawValue() & 0xFFFFFFFF);
1608 } else {
1609 // Promote arg to 32 bits wide into a temporary register...
1610 ArgReg = makeAnotherReg(Type::UIntTy);
1611 promote32(ArgReg, Args[i]);
1612 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1613 X86::ESP, ArgOffset).addReg(ArgReg);
1614 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001615 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001616 case cInt:
Chris Lattner21585222004-03-01 02:42:43 +00001617 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1618 unsigned Val = cast<ConstantInt>(Args[i].Val)->getRawValue();
1619 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1620 X86::ESP, ArgOffset).addImm(Val);
Chris Lattnerb7cb9ff2004-05-13 15:26:48 +00001621 } else if (Args[i].Val && isa<ConstantPointerNull>(Args[i].Val)) {
1622 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1623 X86::ESP, ArgOffset).addImm(0);
Chris Lattner10902622005-04-21 19:11:03 +00001624 } else if (Args[i].Val && isa<GlobalValue>(Args[i].Val)) {
1625 addRegOffset(BuildMI(BB, X86::MOV32mi, 5), X86::ESP, ArgOffset)
1626 .addGlobalAddress(cast<GlobalValue>(Args[i].Val));
Chris Lattner21585222004-03-01 02:42:43 +00001627 } else {
1628 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1629 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1630 X86::ESP, ArgOffset).addReg(ArgReg);
1631 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001632 break;
Chris Lattner3e130a22003-01-13 00:32:26 +00001633 case cLong:
Chris Lattner92900a62004-04-06 03:23:00 +00001634 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1635 uint64_t Val = cast<ConstantInt>(Args[i].Val)->getRawValue();
1636 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1637 X86::ESP, ArgOffset).addImm(Val & ~0U);
1638 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1639 X86::ESP, ArgOffset+4).addImm(Val >> 32ULL);
1640 } else {
1641 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1642 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1643 X86::ESP, ArgOffset).addReg(ArgReg);
1644 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1645 X86::ESP, ArgOffset+4).addReg(ArgReg+1);
1646 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001647 ArgOffset += 4; // 8 byte entry, not 4.
1648 break;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001649
Chris Lattner065faeb2002-12-28 20:24:02 +00001650 case cFP:
Chris Lattner7ab65932005-01-08 05:45:24 +00001651 if (ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(Args[i].Val)) {
Chris Lattner8c926282005-01-08 06:59:50 +00001652 // Store constant FP values with integer instructions to avoid having
1653 // to load the constants from the constant pool then do a store.
Chris Lattner7ab65932005-01-08 05:45:24 +00001654 if (CFP->getType() == Type::FloatTy) {
1655 union {
1656 unsigned I;
1657 float F;
1658 } V;
1659 V.F = CFP->getValue();
1660 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1661 X86::ESP, ArgOffset).addImm(V.I);
1662 } else {
1663 union {
1664 uint64_t I;
1665 double F;
1666 } V;
1667 V.F = CFP->getValue();
1668 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1669 X86::ESP, ArgOffset).addImm((unsigned)V.I);
1670 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1671 X86::ESP, ArgOffset+4).addImm(unsigned(V.I >> 32));
1672 ArgOffset += 4; // 8 byte entry, not 4.
1673 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001674 } else {
Chris Lattner7ab65932005-01-08 05:45:24 +00001675 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1676 if (Args[i].Ty == Type::FloatTy) {
1677 addRegOffset(BuildMI(BB, X86::FST32m, 5),
1678 X86::ESP, ArgOffset).addReg(ArgReg);
1679 } else {
1680 assert(Args[i].Ty == Type::DoubleTy && "Unknown FP type!");
1681 addRegOffset(BuildMI(BB, X86::FST64m, 5),
1682 X86::ESP, ArgOffset).addReg(ArgReg);
1683 ArgOffset += 4; // 8 byte entry, not 4.
1684 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001685 }
1686 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001687
Chris Lattner3e130a22003-01-13 00:32:26 +00001688 default: assert(0 && "Unknown class!");
Chris Lattner065faeb2002-12-28 20:24:02 +00001689 }
1690 ArgOffset += 4;
Chris Lattner94af4142002-12-25 05:13:53 +00001691 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001692 } else {
Chris Lattneree352852004-02-29 07:22:16 +00001693 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(0);
Chris Lattner94af4142002-12-25 05:13:53 +00001694 }
Chris Lattner6e49a4b2002-12-13 14:13:27 +00001695
Chris Lattner3e130a22003-01-13 00:32:26 +00001696 BB->push_back(CallMI);
Misha Brukman0d2cf3a2002-12-04 19:22:53 +00001697
Chris Lattner3648c672005-05-13 21:44:04 +00001698 BuildMI(BB, X86::ADJCALLSTACKUP, 2).addImm(NumBytes).addImm(0);
Chris Lattnera3243642002-12-04 23:45:28 +00001699
1700 // If there is a return value, scavenge the result from the location the call
1701 // leaves it in...
1702 //
Chris Lattner3e130a22003-01-13 00:32:26 +00001703 if (Ret.Ty != Type::VoidTy) {
1704 unsigned DestClass = getClassB(Ret.Ty);
1705 switch (DestClass) {
Brian Gaeke20244b72002-12-12 15:33:40 +00001706 case cByte:
1707 case cShort:
1708 case cInt: {
1709 // Integral results are in %eax, or the appropriate portion
1710 // thereof.
1711 static const unsigned regRegMove[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001712 X86::MOV8rr, X86::MOV16rr, X86::MOV32rr
Brian Gaeke20244b72002-12-12 15:33:40 +00001713 };
1714 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX };
Chris Lattner3e130a22003-01-13 00:32:26 +00001715 BuildMI(BB, regRegMove[DestClass], 1, Ret.Reg).addReg(AReg[DestClass]);
Chris Lattner4fa1acc2002-12-04 23:50:28 +00001716 break;
Brian Gaeke20244b72002-12-12 15:33:40 +00001717 }
Chris Lattner94af4142002-12-25 05:13:53 +00001718 case cFP: // Floating-point return values live in %ST(0)
Chris Lattner3e130a22003-01-13 00:32:26 +00001719 BuildMI(BB, X86::FpGETRESULT, 1, Ret.Reg);
Brian Gaeke20244b72002-12-12 15:33:40 +00001720 break;
Chris Lattner3e130a22003-01-13 00:32:26 +00001721 case cLong: // Long values are left in EDX:EAX
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001722 BuildMI(BB, X86::MOV32rr, 1, Ret.Reg).addReg(X86::EAX);
1723 BuildMI(BB, X86::MOV32rr, 1, Ret.Reg+1).addReg(X86::EDX);
Chris Lattner3e130a22003-01-13 00:32:26 +00001724 break;
1725 default: assert(0 && "Unknown class!");
Chris Lattner4fa1acc2002-12-04 23:50:28 +00001726 }
Chris Lattnera3243642002-12-04 23:45:28 +00001727 }
Brian Gaekefa8d5712002-11-22 11:07:01 +00001728}
Chris Lattner2df035b2002-11-02 19:27:56 +00001729
Chris Lattner3e130a22003-01-13 00:32:26 +00001730
1731/// visitCallInst - Push args on stack and do a procedure call instruction.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001732void X86ISel::visitCallInst(CallInst &CI) {
Chris Lattner3e130a22003-01-13 00:32:26 +00001733 MachineInstr *TheCall;
1734 if (Function *F = CI.getCalledFunction()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001735 // Is it an intrinsic function call?
Brian Gaeked0fde302003-11-11 22:41:34 +00001736 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001737 visitIntrinsicCall(ID, CI); // Special intrinsics are not handled here
1738 return;
Chris Lattner3b6b6372005-04-28 21:48:42 +00001739 } else if (F->getName() == "fabs" || F->getName() == "fabsf") {
1740 if (CI.getNumOperands() == 2 && // Basic sanity checks.
1741 CI.getOperand(1)->getType()->isFloatingPoint() &&
1742 CI.getType() == CI.getOperand(1)->getType()) {
1743 unsigned op1Reg = getReg(CI.getOperand(1));
1744 unsigned DestReg = getReg(CI);
1745 BuildMI(BB, X86::FABS, 1, DestReg).addReg(op1Reg);
1746 return;
1747 }
Chris Lattner5434b422005-04-30 04:12:40 +00001748 } else if (F->getName() == "sin" && UnsafeFPMath || F->getName() == "sinf") {
1749 if (CI.getNumOperands() == 2 && // Basic sanity checks.
1750 CI.getOperand(1)->getType()->isFloatingPoint() &&
1751 CI.getType() == CI.getOperand(1)->getType()) {
1752 unsigned op1Reg = getReg(CI.getOperand(1));
1753 unsigned DestReg = getReg(CI);
1754 BuildMI(BB, X86::FSIN, 1, DestReg).addReg(op1Reg);
1755 return;
1756 }
1757 }
1758 else if (F->getName() == "cos" && UnsafeFPMath || F->getName() == "cosf") {
1759 if (CI.getNumOperands() == 2 && // Basic sanity checks.
1760 CI.getOperand(1)->getType()->isFloatingPoint() &&
1761 CI.getType() == CI.getOperand(1)->getType()) {
1762 unsigned op1Reg = getReg(CI.getOperand(1));
1763 unsigned DestReg = getReg(CI);
1764 BuildMI(BB, X86::FCOS, 1, DestReg).addReg(op1Reg);
1765 return;
1766 }
Chris Lattnereca195e2003-05-08 19:44:13 +00001767 }
1768
Chris Lattner3e130a22003-01-13 00:32:26 +00001769 // Emit a CALL instruction with PC-relative displacement.
1770 TheCall = BuildMI(X86::CALLpcrel32, 1).addGlobalAddress(F, true);
1771 } else { // Emit an indirect call...
1772 unsigned Reg = getReg(CI.getCalledValue());
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001773 TheCall = BuildMI(X86::CALL32r, 1).addReg(Reg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001774 }
1775
1776 std::vector<ValueRecord> Args;
1777 for (unsigned i = 1, e = CI.getNumOperands(); i != e; ++i)
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001778 Args.push_back(ValueRecord(CI.getOperand(i)));
Chris Lattner3e130a22003-01-13 00:32:26 +00001779
1780 unsigned DestReg = CI.getType() != Type::VoidTy ? getReg(CI) : 0;
1781 doCall(ValueRecord(DestReg, CI.getType()), TheCall, Args);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001782}
Chris Lattner3e130a22003-01-13 00:32:26 +00001783
Chris Lattner44827152003-12-28 09:47:19 +00001784/// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
1785/// function, lowering any calls to unknown intrinsic functions into the
1786/// equivalent LLVM code.
Misha Brukman538607f2004-03-01 23:53:11 +00001787///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001788void X86ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) {
Chris Lattner44827152003-12-28 09:47:19 +00001789 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1790 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
1791 if (CallInst *CI = dyn_cast<CallInst>(I++))
1792 if (Function *F = CI->getCalledFunction())
1793 switch (F->getIntrinsicID()) {
Chris Lattneraed386e2003-12-28 09:53:23 +00001794 case Intrinsic::not_intrinsic:
Chris Lattner317201d2004-03-13 00:24:00 +00001795 case Intrinsic::vastart:
1796 case Intrinsic::vacopy:
1797 case Intrinsic::vaend:
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001798 case Intrinsic::returnaddress:
1799 case Intrinsic::frameaddress:
Chris Lattner915e5e52004-02-12 17:53:22 +00001800 case Intrinsic::memcpy:
Chris Lattner2a0f2242004-02-14 04:46:05 +00001801 case Intrinsic::memset:
Chris Lattnerdc572442004-06-15 21:36:44 +00001802 case Intrinsic::isunordered:
Chris Lattner5434b422005-04-30 04:12:40 +00001803 case Intrinsic::sqrt:
John Criswell4ffff9e2004-04-08 20:31:47 +00001804 case Intrinsic::readport:
1805 case Intrinsic::writeport:
Chris Lattner44827152003-12-28 09:47:19 +00001806 // We directly implement these intrinsics
1807 break;
John Criswelle5a4c152004-04-13 22:13:14 +00001808 case Intrinsic::readio: {
1809 // On X86, memory operations are in-order. Lower this intrinsic
1810 // into a volatile load.
Chris Lattnerb4fe76c2004-06-11 04:31:10 +00001811 LoadInst * LI = new LoadInst(CI->getOperand(1), "", true, CI);
1812 CI->replaceAllUsesWith(LI);
1813 BB->getInstList().erase(CI);
John Criswelle5a4c152004-04-13 22:13:14 +00001814 break;
1815 }
1816 case Intrinsic::writeio: {
1817 // On X86, memory operations are in-order. Lower this intrinsic
1818 // into a volatile store.
Chris Lattnerb4fe76c2004-06-11 04:31:10 +00001819 StoreInst *LI = new StoreInst(CI->getOperand(1),
1820 CI->getOperand(2), true, CI);
1821 CI->replaceAllUsesWith(LI);
1822 BB->getInstList().erase(CI);
John Criswelle5a4c152004-04-13 22:13:14 +00001823 break;
1824 }
Chris Lattner44827152003-12-28 09:47:19 +00001825 default:
1826 // All other intrinsic calls we must lower.
1827 Instruction *Before = CI->getPrev();
Chris Lattnerf70e0c22003-12-28 21:23:38 +00001828 TM.getIntrinsicLowering().LowerIntrinsicCall(CI);
Chris Lattner44827152003-12-28 09:47:19 +00001829 if (Before) { // Move iterator to instruction after call
Chris Lattnerb4fe76c2004-06-11 04:31:10 +00001830 I = Before; ++I;
Chris Lattner44827152003-12-28 09:47:19 +00001831 } else {
1832 I = BB->begin();
1833 }
1834 }
Chris Lattner44827152003-12-28 09:47:19 +00001835}
1836
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001837void X86ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001838 unsigned TmpReg1, TmpReg2;
1839 switch (ID) {
Chris Lattner5634b9f2004-03-13 00:24:52 +00001840 case Intrinsic::vastart:
Chris Lattnereca195e2003-05-08 19:44:13 +00001841 // Get the address of the first vararg value...
Chris Lattner73815062003-10-18 05:56:40 +00001842 TmpReg1 = getReg(CI);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001843 addFrameReference(BuildMI(BB, X86::LEA32r, 5, TmpReg1), VarArgsFrameIndex);
Chris Lattnereca195e2003-05-08 19:44:13 +00001844 return;
1845
Chris Lattner5634b9f2004-03-13 00:24:52 +00001846 case Intrinsic::vacopy:
Chris Lattner73815062003-10-18 05:56:40 +00001847 TmpReg1 = getReg(CI);
1848 TmpReg2 = getReg(CI.getOperand(1));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001849 BuildMI(BB, X86::MOV32rr, 1, TmpReg1).addReg(TmpReg2);
Chris Lattnereca195e2003-05-08 19:44:13 +00001850 return;
Chris Lattner5634b9f2004-03-13 00:24:52 +00001851 case Intrinsic::vaend: return; // Noop on X86
Chris Lattnereca195e2003-05-08 19:44:13 +00001852
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001853 case Intrinsic::returnaddress:
1854 case Intrinsic::frameaddress:
1855 TmpReg1 = getReg(CI);
1856 if (cast<Constant>(CI.getOperand(1))->isNullValue()) {
Chris Lattner8cdbc352004-12-17 00:46:51 +00001857 if (ReturnAddressIndex == 0) {
Chris Lattner11cf7aa2004-12-17 00:07:46 +00001858 // Set up a frame object for the return address.
1859 ReturnAddressIndex = F->getFrameInfo()->CreateFixedObject(4, -4);
1860 }
1861
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001862 if (ID == Intrinsic::returnaddress) {
1863 // Just load the return address
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001864 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, TmpReg1),
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001865 ReturnAddressIndex);
1866 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001867 addFrameReference(BuildMI(BB, X86::LEA32r, 4, TmpReg1),
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001868 ReturnAddressIndex, -4);
1869 }
1870 } else {
1871 // Values other than zero are not implemented yet.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001872 BuildMI(BB, X86::MOV32ri, 1, TmpReg1).addImm(0);
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001873 }
1874 return;
1875
Chris Lattnerdc572442004-06-15 21:36:44 +00001876 case Intrinsic::isunordered:
1877 TmpReg1 = getReg(CI.getOperand(1));
1878 TmpReg2 = getReg(CI.getOperand(2));
1879 emitUCOMr(BB, BB->end(), TmpReg2, TmpReg1);
1880 TmpReg2 = getReg(CI);
1881 BuildMI(BB, X86::SETPr, 0, TmpReg2);
1882 return;
1883
Chris Lattner5434b422005-04-30 04:12:40 +00001884 case Intrinsic::sqrt:
1885 TmpReg1 = getReg(CI.getOperand(1));
1886 TmpReg2 = getReg(CI);
1887 BuildMI(BB, X86::FSQRT, 1, TmpReg2).addReg(TmpReg1);
1888 return;
1889
Chris Lattner915e5e52004-02-12 17:53:22 +00001890 case Intrinsic::memcpy: {
1891 assert(CI.getNumOperands() == 5 && "Illegal llvm.memcpy call!");
1892 unsigned Align = 1;
1893 if (ConstantInt *AlignC = dyn_cast<ConstantInt>(CI.getOperand(4))) {
1894 Align = AlignC->getRawValue();
1895 if (Align == 0) Align = 1;
1896 }
1897
1898 // Turn the byte code into # iterations
Chris Lattner915e5e52004-02-12 17:53:22 +00001899 unsigned CountReg;
Chris Lattner2a0f2242004-02-14 04:46:05 +00001900 unsigned Opcode;
Chris Lattner915e5e52004-02-12 17:53:22 +00001901 switch (Align & 3) {
1902 case 2: // WORD aligned
Chris Lattner07122832004-02-13 23:36:47 +00001903 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
1904 CountReg = getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/2));
1905 } else {
1906 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001907 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001908 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
Chris Lattner07122832004-02-13 23:36:47 +00001909 }
Chris Lattner2a0f2242004-02-14 04:46:05 +00001910 Opcode = X86::REP_MOVSW;
Chris Lattner915e5e52004-02-12 17:53:22 +00001911 break;
1912 case 0: // DWORD aligned
Chris Lattner07122832004-02-13 23:36:47 +00001913 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
1914 CountReg = getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/4));
1915 } else {
1916 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001917 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001918 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
Chris Lattner07122832004-02-13 23:36:47 +00001919 }
Chris Lattner2a0f2242004-02-14 04:46:05 +00001920 Opcode = X86::REP_MOVSD;
Chris Lattner915e5e52004-02-12 17:53:22 +00001921 break;
Chris Lattner8dd8d262004-02-26 01:20:02 +00001922 default: // BYTE aligned
Chris Lattner07122832004-02-13 23:36:47 +00001923 CountReg = getReg(CI.getOperand(3));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001924 Opcode = X86::REP_MOVSB;
Chris Lattner915e5e52004-02-12 17:53:22 +00001925 break;
1926 }
1927
1928 // No matter what the alignment is, we put the source in ESI, the
1929 // destination in EDI, and the count in ECX.
1930 TmpReg1 = getReg(CI.getOperand(1));
1931 TmpReg2 = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001932 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
1933 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
1934 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001935 BuildMI(BB, Opcode, 0);
1936 return;
1937 }
1938 case Intrinsic::memset: {
1939 assert(CI.getNumOperands() == 5 && "Illegal llvm.memset call!");
1940 unsigned Align = 1;
1941 if (ConstantInt *AlignC = dyn_cast<ConstantInt>(CI.getOperand(4))) {
1942 Align = AlignC->getRawValue();
1943 if (Align == 0) Align = 1;
Chris Lattner915e5e52004-02-12 17:53:22 +00001944 }
1945
Chris Lattner2a0f2242004-02-14 04:46:05 +00001946 // Turn the byte code into # iterations
Chris Lattner2a0f2242004-02-14 04:46:05 +00001947 unsigned CountReg;
1948 unsigned Opcode;
1949 if (ConstantInt *ValC = dyn_cast<ConstantInt>(CI.getOperand(2))) {
1950 unsigned Val = ValC->getRawValue() & 255;
1951
1952 // If the value is a constant, then we can potentially use larger copies.
1953 switch (Align & 3) {
1954 case 2: // WORD aligned
1955 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
Chris Lattner300d0ed2004-02-14 06:00:36 +00001956 CountReg =getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/2));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001957 } else {
1958 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001959 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001960 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001961 }
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001962 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001963 Opcode = X86::REP_STOSW;
1964 break;
1965 case 0: // DWORD aligned
1966 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
Chris Lattner300d0ed2004-02-14 06:00:36 +00001967 CountReg =getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/4));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001968 } else {
1969 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001970 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001971 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001972 }
1973 Val = (Val << 8) | Val;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001974 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001975 Opcode = X86::REP_STOSD;
1976 break;
Chris Lattner8dd8d262004-02-26 01:20:02 +00001977 default: // BYTE aligned
Chris Lattner2a0f2242004-02-14 04:46:05 +00001978 CountReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001979 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001980 Opcode = X86::REP_STOSB;
1981 break;
1982 }
1983 } else {
1984 // If it's not a constant value we are storing, just fall back. We could
1985 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
1986 unsigned ValReg = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001987 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001988 CountReg = getReg(CI.getOperand(3));
1989 Opcode = X86::REP_STOSB;
1990 }
1991
1992 // No matter what the alignment is, we put the source in ESI, the
1993 // destination in EDI, and the count in ECX.
1994 TmpReg1 = getReg(CI.getOperand(1));
1995 //TmpReg2 = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001996 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
1997 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001998 BuildMI(BB, Opcode, 0);
Chris Lattner915e5e52004-02-12 17:53:22 +00001999 return;
2000 }
2001
Chris Lattner87e18de2004-04-13 17:20:37 +00002002 case Intrinsic::readport: {
2003 // First, determine that the size of the operand falls within the acceptable
2004 // range for this architecture.
John Criswell4ffff9e2004-04-08 20:31:47 +00002005 //
Chris Lattner87e18de2004-04-13 17:20:37 +00002006 if (getClassB(CI.getOperand(1)->getType()) != cShort) {
John Criswellca6ea0f2004-04-08 22:39:13 +00002007 std::cerr << "llvm.readport: Address size is not 16 bits\n";
Chris Lattner87e18de2004-04-13 17:20:37 +00002008 exit(1);
John Criswellca6ea0f2004-04-08 22:39:13 +00002009 }
John Criswell4ffff9e2004-04-08 20:31:47 +00002010
John Criswell4ffff9e2004-04-08 20:31:47 +00002011 // Now, move the I/O port address into the DX register and use the IN
2012 // instruction to get the input data.
2013 //
Chris Lattner87e18de2004-04-13 17:20:37 +00002014 unsigned Class = getClass(CI.getCalledFunction()->getReturnType());
2015 unsigned DestReg = getReg(CI);
John Criswell4ffff9e2004-04-08 20:31:47 +00002016
Chris Lattner87e18de2004-04-13 17:20:37 +00002017 // If the port is a single-byte constant, use the immediate form.
2018 if (ConstantInt *C = dyn_cast<ConstantInt>(CI.getOperand(1)))
2019 if ((C->getRawValue() & 255) == C->getRawValue()) {
2020 switch (Class) {
2021 case cByte:
2022 BuildMI(BB, X86::IN8ri, 1).addImm((unsigned char)C->getRawValue());
2023 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AL);
2024 return;
2025 case cShort:
2026 BuildMI(BB, X86::IN16ri, 1).addImm((unsigned char)C->getRawValue());
2027 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AX);
2028 return;
2029 case cInt:
2030 BuildMI(BB, X86::IN32ri, 1).addImm((unsigned char)C->getRawValue());
2031 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::EAX);
2032 return;
2033 }
2034 }
2035
2036 unsigned Reg = getReg(CI.getOperand(1));
2037 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
2038 switch (Class) {
2039 case cByte:
2040 BuildMI(BB, X86::IN8rr, 0);
2041 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AL);
2042 break;
2043 case cShort:
2044 BuildMI(BB, X86::IN16rr, 0);
Chris Lattnercecd67b2005-05-09 21:06:04 +00002045 BuildMI(BB, X86::MOV16rr, 1, DestReg).addReg(X86::AX);
Chris Lattner87e18de2004-04-13 17:20:37 +00002046 break;
2047 case cInt:
2048 BuildMI(BB, X86::IN32rr, 0);
Chris Lattnercecd67b2005-05-09 21:06:04 +00002049 BuildMI(BB, X86::MOV32rr, 1, DestReg).addReg(X86::EAX);
Chris Lattner87e18de2004-04-13 17:20:37 +00002050 break;
2051 default:
2052 std::cerr << "Cannot do input on this data type";
John Criswellca6ea0f2004-04-08 22:39:13 +00002053 exit (1);
2054 }
John Criswell4ffff9e2004-04-08 20:31:47 +00002055 return;
Chris Lattner87e18de2004-04-13 17:20:37 +00002056 }
John Criswell4ffff9e2004-04-08 20:31:47 +00002057
Chris Lattner87e18de2004-04-13 17:20:37 +00002058 case Intrinsic::writeport: {
2059 // First, determine that the size of the operand falls within the
2060 // acceptable range for this architecture.
2061 if (getClass(CI.getOperand(2)->getType()) != cShort) {
2062 std::cerr << "llvm.writeport: Address size is not 16 bits\n";
2063 exit(1);
2064 }
2065
2066 unsigned Class = getClassB(CI.getOperand(1)->getType());
2067 unsigned ValReg = getReg(CI.getOperand(1));
2068 switch (Class) {
2069 case cByte:
2070 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
2071 break;
2072 case cShort:
2073 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(ValReg);
2074 break;
2075 case cInt:
2076 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(ValReg);
2077 break;
2078 default:
2079 std::cerr << "llvm.writeport: invalid data type for X86 target";
2080 exit(1);
2081 }
2082
2083
2084 // If the port is a single-byte constant, use the immediate form.
2085 if (ConstantInt *C = dyn_cast<ConstantInt>(CI.getOperand(2)))
2086 if ((C->getRawValue() & 255) == C->getRawValue()) {
2087 static const unsigned O[] = { X86::OUT8ir, X86::OUT16ir, X86::OUT32ir };
2088 BuildMI(BB, O[Class], 1).addImm((unsigned char)C->getRawValue());
2089 return;
2090 }
2091
2092 // Otherwise, move the I/O port address into the DX register and the value
2093 // to write into the AL/AX/EAX register.
2094 static const unsigned Opc[] = { X86::OUT8rr, X86::OUT16rr, X86::OUT32rr };
2095 unsigned Reg = getReg(CI.getOperand(2));
2096 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
2097 BuildMI(BB, Opc[Class], 0);
2098 return;
2099 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002100
Chris Lattner44827152003-12-28 09:47:19 +00002101 default: assert(0 && "Error: unknown intrinsics should have been lowered!");
Chris Lattnereca195e2003-05-08 19:44:13 +00002102 }
2103}
2104
Chris Lattner7dee5da2004-03-08 01:58:35 +00002105static bool isSafeToFoldLoadIntoInstruction(LoadInst &LI, Instruction &User) {
2106 if (LI.getParent() != User.getParent())
2107 return false;
2108 BasicBlock::iterator It = &LI;
2109 // Check all of the instructions between the load and the user. We should
2110 // really use alias analysis here, but for now we just do something simple.
2111 for (++It; It != BasicBlock::iterator(&User); ++It) {
2112 switch (It->getOpcode()) {
Chris Lattner85c84e72004-03-18 06:29:54 +00002113 case Instruction::Free:
Chris Lattner7dee5da2004-03-08 01:58:35 +00002114 case Instruction::Store:
2115 case Instruction::Call:
2116 case Instruction::Invoke:
2117 return false;
Chris Lattner133dbb12004-04-12 03:02:48 +00002118 case Instruction::Load:
2119 if (cast<LoadInst>(It)->isVolatile() && LI.isVolatile())
2120 return false;
2121 break;
Chris Lattner7dee5da2004-03-08 01:58:35 +00002122 }
2123 }
2124 return true;
2125}
2126
Chris Lattnerb515f6d2003-05-08 20:49:25 +00002127/// visitSimpleBinary - Implement simple binary operators for integral types...
2128/// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or, 4 for
2129/// Xor.
Misha Brukman538607f2004-03-01 23:53:11 +00002130///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002131void X86ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +00002132 unsigned DestReg = getReg(B);
2133 MachineBasicBlock::iterator MI = BB->end();
Chris Lattner721d2d42004-03-08 01:18:36 +00002134 Value *Op0 = B.getOperand(0), *Op1 = B.getOperand(1);
Chris Lattnerc81e6ba2004-05-10 15:15:55 +00002135 unsigned Class = getClassB(B.getType());
Chris Lattner721d2d42004-03-08 01:18:36 +00002136
Chris Lattnerde95c9e2004-10-17 06:10:40 +00002137 // If this is AND X, C, and it is only used by a setcc instruction, it will
2138 // be folded. There is no need to emit this instruction.
2139 if (B.hasOneUse() && OperatorClass == 2 && isa<ConstantInt>(Op1))
2140 if (Class == cByte || Class == cShort || Class == cInt) {
2141 Instruction *Use = cast<Instruction>(B.use_back());
2142 if (isa<SetCondInst>(Use) &&
2143 Use->getOperand(1) == Constant::getNullValue(B.getType())) {
2144 switch (getSetCCNumber(Use->getOpcode())) {
2145 case 0:
2146 case 1:
2147 return;
2148 default:
2149 if (B.getType()->isSigned()) return;
2150 }
2151 }
2152 }
2153
Chris Lattner7dee5da2004-03-08 01:58:35 +00002154 // Special case: op Reg, load [mem]
Chris Lattnerc81e6ba2004-05-10 15:15:55 +00002155 if (isa<LoadInst>(Op0) && !isa<LoadInst>(Op1) && Class != cLong &&
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002156 Op0->hasOneUse() &&
Chris Lattnerc81e6ba2004-05-10 15:15:55 +00002157 isSafeToFoldLoadIntoInstruction(*cast<LoadInst>(Op0), B))
Chris Lattner7dee5da2004-03-08 01:58:35 +00002158 if (!B.swapOperands())
2159 std::swap(Op0, Op1); // Make sure any loads are in the RHS.
2160
Chris Lattnerccd97962004-06-17 22:15:25 +00002161 if (isa<LoadInst>(Op1) && Class != cLong && Op1->hasOneUse() &&
Chris Lattner7dee5da2004-03-08 01:58:35 +00002162 isSafeToFoldLoadIntoInstruction(*cast<LoadInst>(Op1), B)) {
2163
Chris Lattner95157f72004-04-11 22:05:45 +00002164 unsigned Opcode;
2165 if (Class != cFP) {
2166 static const unsigned OpcodeTab[][3] = {
2167 // Arithmetic operators
2168 { X86::ADD8rm, X86::ADD16rm, X86::ADD32rm }, // ADD
2169 { X86::SUB8rm, X86::SUB16rm, X86::SUB32rm }, // SUB
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002170
Chris Lattner95157f72004-04-11 22:05:45 +00002171 // Bitwise operators
2172 { X86::AND8rm, X86::AND16rm, X86::AND32rm }, // AND
2173 { X86:: OR8rm, X86:: OR16rm, X86:: OR32rm }, // OR
2174 { X86::XOR8rm, X86::XOR16rm, X86::XOR32rm }, // XOR
2175 };
2176 Opcode = OpcodeTab[OperatorClass][Class];
2177 } else {
2178 static const unsigned OpcodeTab[][2] = {
2179 { X86::FADD32m, X86::FADD64m }, // ADD
2180 { X86::FSUB32m, X86::FSUB64m }, // SUB
2181 };
2182 const Type *Ty = Op0->getType();
2183 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
2184 Opcode = OpcodeTab[OperatorClass][Ty == Type::DoubleTy];
2185 }
Chris Lattner7dee5da2004-03-08 01:58:35 +00002186
Chris Lattner7dee5da2004-03-08 01:58:35 +00002187 unsigned Op0r = getReg(Op0);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002188 if (AllocaInst *AI =
2189 dyn_castFixedAlloca(cast<LoadInst>(Op1)->getOperand(0))) {
2190 unsigned FI = getFixedSizedAllocaFI(AI);
2191 addFrameReference(BuildMI(BB, Opcode, 5, DestReg).addReg(Op0r), FI);
2192
2193 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002194 X86AddressMode AM;
2195 getAddressingMode(cast<LoadInst>(Op1)->getOperand(0), AM);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002196
Reid Spencerfc989e12004-08-30 00:13:26 +00002197 addFullAddress(BuildMI(BB, Opcode, 5, DestReg).addReg(Op0r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002198 }
Chris Lattner7dee5da2004-03-08 01:58:35 +00002199 return;
2200 }
2201
Chris Lattner95157f72004-04-11 22:05:45 +00002202 // If this is a floating point subtract, check to see if we can fold the first
2203 // operand in.
2204 if (Class == cFP && OperatorClass == 1 &&
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002205 isa<LoadInst>(Op0) &&
Chris Lattner95157f72004-04-11 22:05:45 +00002206 isSafeToFoldLoadIntoInstruction(*cast<LoadInst>(Op0), B)) {
2207 const Type *Ty = Op0->getType();
2208 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
2209 unsigned Opcode = Ty == Type::FloatTy ? X86::FSUBR32m : X86::FSUBR64m;
2210
Chris Lattner95157f72004-04-11 22:05:45 +00002211 unsigned Op1r = getReg(Op1);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002212 if (AllocaInst *AI =
2213 dyn_castFixedAlloca(cast<LoadInst>(Op0)->getOperand(0))) {
2214 unsigned FI = getFixedSizedAllocaFI(AI);
2215 addFrameReference(BuildMI(BB, Opcode, 5, DestReg).addReg(Op1r), FI);
2216 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002217 X86AddressMode AM;
2218 getAddressingMode(cast<LoadInst>(Op0)->getOperand(0), AM);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002219
Reid Spencerfc989e12004-08-30 00:13:26 +00002220 addFullAddress(BuildMI(BB, Opcode, 5, DestReg).addReg(Op1r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002221 }
Chris Lattner95157f72004-04-11 22:05:45 +00002222 return;
2223 }
2224
Chris Lattner721d2d42004-03-08 01:18:36 +00002225 emitSimpleBinaryOperation(BB, MI, Op0, Op1, OperatorClass, DestReg);
Chris Lattnerb515f6d2003-05-08 20:49:25 +00002226}
Chris Lattner3e130a22003-01-13 00:32:26 +00002227
Chris Lattner6621ed92004-04-11 21:23:56 +00002228
2229/// emitBinaryFPOperation - This method handles emission of floating point
2230/// Add (0), Sub (1), Mul (2), and Div (3) operations.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002231void X86ISel::emitBinaryFPOperation(MachineBasicBlock *BB,
2232 MachineBasicBlock::iterator IP,
2233 Value *Op0, Value *Op1,
2234 unsigned OperatorClass, unsigned DestReg) {
Chris Lattner6621ed92004-04-11 21:23:56 +00002235 // Special case: op Reg, <const fp>
2236 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1))
2237 if (!Op1C->isExactlyValue(+0.0) && !Op1C->isExactlyValue(+1.0)) {
2238 // Create a constant pool entry for this constant.
2239 MachineConstantPool *CP = F->getConstantPool();
2240 unsigned CPI = CP->getConstantPoolIndex(Op1C);
2241 const Type *Ty = Op1->getType();
2242
2243 static const unsigned OpcodeTab[][4] = {
2244 { X86::FADD32m, X86::FSUB32m, X86::FMUL32m, X86::FDIV32m }, // Float
2245 { X86::FADD64m, X86::FSUB64m, X86::FMUL64m, X86::FDIV64m }, // Double
2246 };
2247
2248 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
2249 unsigned Opcode = OpcodeTab[Ty != Type::FloatTy][OperatorClass];
2250 unsigned Op0r = getReg(Op0, BB, IP);
2251 addConstantPoolReference(BuildMI(*BB, IP, Opcode, 5,
2252 DestReg).addReg(Op0r), CPI);
2253 return;
2254 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002255
Chris Lattner13c07fe2004-04-12 00:12:04 +00002256 // Special case: R1 = op <const fp>, R2
Chris Lattner6621ed92004-04-11 21:23:56 +00002257 if (ConstantFP *CFP = dyn_cast<ConstantFP>(Op0))
2258 if (CFP->isExactlyValue(-0.0) && OperatorClass == 1) {
2259 // -0.0 - X === -X
2260 unsigned op1Reg = getReg(Op1, BB, IP);
2261 BuildMI(*BB, IP, X86::FCHS, 1, DestReg).addReg(op1Reg);
2262 return;
2263 } else if (!CFP->isExactlyValue(+0.0) && !CFP->isExactlyValue(+1.0)) {
Chris Lattner13c07fe2004-04-12 00:12:04 +00002264 // R1 = op CST, R2 --> R1 = opr R2, CST
Chris Lattner6621ed92004-04-11 21:23:56 +00002265
2266 // Create a constant pool entry for this constant.
2267 MachineConstantPool *CP = F->getConstantPool();
2268 unsigned CPI = CP->getConstantPoolIndex(CFP);
2269 const Type *Ty = CFP->getType();
2270
2271 static const unsigned OpcodeTab[][4] = {
2272 { X86::FADD32m, X86::FSUBR32m, X86::FMUL32m, X86::FDIVR32m }, // Float
2273 { X86::FADD64m, X86::FSUBR64m, X86::FMUL64m, X86::FDIVR64m }, // Double
2274 };
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002275
Chris Lattner6621ed92004-04-11 21:23:56 +00002276 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2277 unsigned Opcode = OpcodeTab[Ty != Type::FloatTy][OperatorClass];
2278 unsigned Op1r = getReg(Op1, BB, IP);
2279 addConstantPoolReference(BuildMI(*BB, IP, Opcode, 5,
2280 DestReg).addReg(Op1r), CPI);
2281 return;
2282 }
2283
2284 // General case.
2285 static const unsigned OpcodeTab[4] = {
2286 X86::FpADD, X86::FpSUB, X86::FpMUL, X86::FpDIV
2287 };
2288
2289 unsigned Opcode = OpcodeTab[OperatorClass];
2290 unsigned Op0r = getReg(Op0, BB, IP);
2291 unsigned Op1r = getReg(Op1, BB, IP);
2292 BuildMI(*BB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r);
2293}
2294
Chris Lattnerb2acc512003-10-19 21:09:10 +00002295/// emitSimpleBinaryOperation - Implement simple binary operators for integral
2296/// types... OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for
2297/// Or, 4 for Xor.
Chris Lattner68aad932002-11-02 20:13:22 +00002298///
Chris Lattnerb515f6d2003-05-08 20:49:25 +00002299/// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
2300/// and constant expression support.
Chris Lattnerb2acc512003-10-19 21:09:10 +00002301///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002302void X86ISel::emitSimpleBinaryOperation(MachineBasicBlock *MBB,
2303 MachineBasicBlock::iterator IP,
2304 Value *Op0, Value *Op1,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002305 unsigned OperatorClass,
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002306 unsigned DestReg) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +00002307 unsigned Class = getClassB(Op0->getType());
Chris Lattnerb2acc512003-10-19 21:09:10 +00002308
Chris Lattner6621ed92004-04-11 21:23:56 +00002309 if (Class == cFP) {
2310 assert(OperatorClass < 2 && "No logical ops for FP!");
2311 emitBinaryFPOperation(MBB, IP, Op0, Op1, OperatorClass, DestReg);
2312 return;
2313 }
2314
Chris Lattner48b0c972004-04-11 20:26:20 +00002315 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0))
Chris Lattner667ea022004-06-18 00:50:37 +00002316 if (OperatorClass == 1) {
Chris Lattner48b0c972004-04-11 20:26:20 +00002317 static unsigned const NEGTab[] = {
2318 X86::NEG8r, X86::NEG16r, X86::NEG32r, 0, X86::NEG32r
2319 };
Chris Lattner667ea022004-06-18 00:50:37 +00002320
2321 // sub 0, X -> neg X
2322 if (CI->isNullValue()) {
2323 unsigned op1Reg = getReg(Op1, MBB, IP);
2324 BuildMI(*MBB, IP, NEGTab[Class], 1, DestReg).addReg(op1Reg);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002325
Chris Lattner667ea022004-06-18 00:50:37 +00002326 if (Class == cLong) {
2327 // We just emitted: Dl = neg Sl
2328 // Now emit : T = addc Sh, 0
2329 // : Dh = neg T
2330 unsigned T = makeAnotherReg(Type::IntTy);
2331 BuildMI(*MBB, IP, X86::ADC32ri, 2, T).addReg(op1Reg+1).addImm(0);
2332 BuildMI(*MBB, IP, X86::NEG32r, 1, DestReg+1).addReg(T);
2333 }
2334 return;
2335 } else if (Op1->hasOneUse() && Class != cLong) {
2336 // sub C, X -> tmp = neg X; DestReg = add tmp, C. This is better
2337 // than copying C into a temporary register, because of register
2338 // pressure (tmp and destreg can share a register.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002339 static unsigned const ADDRITab[] = {
Chris Lattner667ea022004-06-18 00:50:37 +00002340 X86::ADD8ri, X86::ADD16ri, X86::ADD32ri, 0, X86::ADD32ri
2341 };
2342 unsigned op1Reg = getReg(Op1, MBB, IP);
2343 unsigned Tmp = makeAnotherReg(Op0->getType());
2344 BuildMI(*MBB, IP, NEGTab[Class], 1, Tmp).addReg(op1Reg);
Chris Lattner30483732004-06-20 07:49:54 +00002345 BuildMI(*MBB, IP, ADDRITab[Class], 2,
2346 DestReg).addReg(Tmp).addImm(CI->getRawValue());
Chris Lattner667ea022004-06-18 00:50:37 +00002347 return;
Chris Lattnerb2acc512003-10-19 21:09:10 +00002348 }
Chris Lattner48b0c972004-04-11 20:26:20 +00002349 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002350
Chris Lattner48b0c972004-04-11 20:26:20 +00002351 // Special case: op Reg, <const int>
2352 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner721d2d42004-03-08 01:18:36 +00002353 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002354
Chris Lattner721d2d42004-03-08 01:18:36 +00002355 // xor X, -1 -> not X
2356 if (OperatorClass == 4 && Op1C->isAllOnesValue()) {
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002357 static unsigned const NOTTab[] = {
2358 X86::NOT8r, X86::NOT16r, X86::NOT32r, 0, X86::NOT32r
2359 };
Chris Lattner721d2d42004-03-08 01:18:36 +00002360 BuildMI(*MBB, IP, NOTTab[Class], 1, DestReg).addReg(Op0r);
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002361 if (Class == cLong) // Invert the top part too
2362 BuildMI(*MBB, IP, X86::NOT32r, 1, DestReg+1).addReg(Op0r+1);
Chris Lattner721d2d42004-03-08 01:18:36 +00002363 return;
2364 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002365
Chris Lattner721d2d42004-03-08 01:18:36 +00002366 // add X, -1 -> dec X
Chris Lattner06521672004-04-06 03:36:57 +00002367 if (OperatorClass == 0 && Op1C->isAllOnesValue() && Class != cLong) {
2368 // Note that we can't use dec for 64-bit decrements, because it does not
2369 // set the carry flag!
2370 static unsigned const DECTab[] = { X86::DEC8r, X86::DEC16r, X86::DEC32r };
Chris Lattner721d2d42004-03-08 01:18:36 +00002371 BuildMI(*MBB, IP, DECTab[Class], 1, DestReg).addReg(Op0r);
2372 return;
2373 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002374
Chris Lattner721d2d42004-03-08 01:18:36 +00002375 // add X, 1 -> inc X
Chris Lattner06521672004-04-06 03:36:57 +00002376 if (OperatorClass == 0 && Op1C->equalsInt(1) && Class != cLong) {
2377 // Note that we can't use inc for 64-bit increments, because it does not
2378 // set the carry flag!
2379 static unsigned const INCTab[] = { X86::INC8r, X86::INC16r, X86::INC32r };
Alkis Evlogimenosbee8a092004-04-02 18:11:32 +00002380 BuildMI(*MBB, IP, INCTab[Class], 1, DestReg).addReg(Op0r);
Chris Lattner721d2d42004-03-08 01:18:36 +00002381 return;
2382 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002383
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002384 static const unsigned OpcodeTab[][5] = {
Chris Lattner721d2d42004-03-08 01:18:36 +00002385 // Arithmetic operators
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002386 { X86::ADD8ri, X86::ADD16ri, X86::ADD32ri, 0, X86::ADD32ri }, // ADD
2387 { X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, X86::SUB32ri }, // SUB
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002388
Chris Lattner721d2d42004-03-08 01:18:36 +00002389 // Bitwise operators
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002390 { X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, X86::AND32ri }, // AND
2391 { X86:: OR8ri, X86:: OR16ri, X86:: OR32ri, 0, X86::OR32ri }, // OR
2392 { X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, X86::XOR32ri }, // XOR
Chris Lattner721d2d42004-03-08 01:18:36 +00002393 };
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002394
Chris Lattner721d2d42004-03-08 01:18:36 +00002395 unsigned Opcode = OpcodeTab[OperatorClass][Class];
Chris Lattner33f7fa32004-04-06 03:15:53 +00002396 unsigned Op1l = cast<ConstantInt>(Op1C)->getRawValue();
Chris Lattner721d2d42004-03-08 01:18:36 +00002397
Chris Lattner33f7fa32004-04-06 03:15:53 +00002398 if (Class != cLong) {
2399 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addImm(Op1l);
2400 return;
Chris Lattner6621ed92004-04-11 21:23:56 +00002401 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002402
Chris Lattner6621ed92004-04-11 21:23:56 +00002403 // If this is a long value and the high or low bits have a special
2404 // property, emit some special cases.
2405 unsigned Op1h = cast<ConstantInt>(Op1C)->getRawValue() >> 32LL;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002406
Chris Lattner6621ed92004-04-11 21:23:56 +00002407 // If the constant is zero in the low 32-bits, just copy the low part
2408 // across and apply the normal 32-bit operation to the high parts. There
2409 // will be no carry or borrow into the top.
2410 if (Op1l == 0) {
2411 if (OperatorClass != 2) // All but and...
2412 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg).addReg(Op0r);
2413 else
2414 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
2415 BuildMI(*MBB, IP, OpcodeTab[OperatorClass][cLong], 2, DestReg+1)
2416 .addReg(Op0r+1).addImm(Op1h);
Chris Lattner33f7fa32004-04-06 03:15:53 +00002417 return;
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002418 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002419
Chris Lattner6621ed92004-04-11 21:23:56 +00002420 // If this is a logical operation and the top 32-bits are zero, just
2421 // operate on the lower 32.
2422 if (Op1h == 0 && OperatorClass > 1) {
2423 BuildMI(*MBB, IP, OpcodeTab[OperatorClass][cLong], 2, DestReg)
2424 .addReg(Op0r).addImm(Op1l);
2425 if (OperatorClass != 2) // All but and
2426 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg+1).addReg(Op0r+1);
2427 else
2428 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
2429 return;
2430 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002431
Chris Lattner6621ed92004-04-11 21:23:56 +00002432 // TODO: We could handle lots of other special cases here, such as AND'ing
2433 // with 0xFFFFFFFF00000000 -> noop, etc.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002434
Chris Lattner6621ed92004-04-11 21:23:56 +00002435 // Otherwise, code generate the full operation with a constant.
2436 static const unsigned TopTab[] = {
2437 X86::ADC32ri, X86::SBB32ri, X86::AND32ri, X86::OR32ri, X86::XOR32ri
2438 };
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002439
Chris Lattner6621ed92004-04-11 21:23:56 +00002440 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addImm(Op1l);
2441 BuildMI(*MBB, IP, TopTab[OperatorClass], 2, DestReg+1)
2442 .addReg(Op0r+1).addImm(Op1h);
2443 return;
Chris Lattner721d2d42004-03-08 01:18:36 +00002444 }
2445
2446 // Finally, handle the general case now.
Chris Lattner7ba92302004-04-06 02:13:25 +00002447 static const unsigned OpcodeTab[][5] = {
Chris Lattner721d2d42004-03-08 01:18:36 +00002448 // Arithmetic operators
Chris Lattner6621ed92004-04-11 21:23:56 +00002449 { X86::ADD8rr, X86::ADD16rr, X86::ADD32rr, 0, X86::ADD32rr }, // ADD
2450 { X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, 0, X86::SUB32rr }, // SUB
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002451
Chris Lattnerb2acc512003-10-19 21:09:10 +00002452 // Bitwise operators
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002453 { X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, X86::AND32rr }, // AND
2454 { X86:: OR8rr, X86:: OR16rr, X86:: OR32rr, 0, X86:: OR32rr }, // OR
2455 { X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, X86::XOR32rr }, // XOR
Chris Lattnerb2acc512003-10-19 21:09:10 +00002456 };
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002457
Chris Lattnerb2acc512003-10-19 21:09:10 +00002458 unsigned Opcode = OpcodeTab[OperatorClass][Class];
Chris Lattner721d2d42004-03-08 01:18:36 +00002459 unsigned Op0r = getReg(Op0, MBB, IP);
2460 unsigned Op1r = getReg(Op1, MBB, IP);
2461 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002462
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002463 if (Class == cLong) { // Handle the upper 32 bits of long values...
Chris Lattner721d2d42004-03-08 01:18:36 +00002464 static const unsigned TopTab[] = {
2465 X86::ADC32rr, X86::SBB32rr, X86::AND32rr, X86::OR32rr, X86::XOR32rr
2466 };
2467 BuildMI(*MBB, IP, TopTab[OperatorClass], 2,
2468 DestReg+1).addReg(Op0r+1).addReg(Op1r+1);
2469 }
Chris Lattnere2954c82002-11-02 20:04:26 +00002470}
2471
Chris Lattner3e130a22003-01-13 00:32:26 +00002472/// doMultiply - Emit appropriate instructions to multiply together the
2473/// registers op0Reg and op1Reg, and put the result in DestReg. The type of the
2474/// result should be given as DestTy.
2475///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002476void X86ISel::doMultiply(MachineBasicBlock *MBB,
2477 MachineBasicBlock::iterator MBBI,
2478 unsigned DestReg, const Type *DestTy,
2479 unsigned op0Reg, unsigned op1Reg) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002480 unsigned Class = getClass(DestTy);
Chris Lattner94af4142002-12-25 05:13:53 +00002481 switch (Class) {
Chris Lattner0f1c4612003-06-21 17:16:58 +00002482 case cInt:
2483 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002484 BuildMI(*MBB, MBBI, Class == cInt ? X86::IMUL32rr:X86::IMUL16rr, 2, DestReg)
Chris Lattner0f1c4612003-06-21 17:16:58 +00002485 .addReg(op0Reg).addReg(op1Reg);
2486 return;
2487 case cByte:
2488 // Must use the MUL instruction, which forces use of AL...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002489 BuildMI(*MBB, MBBI, X86::MOV8rr, 1, X86::AL).addReg(op0Reg);
2490 BuildMI(*MBB, MBBI, X86::MUL8r, 1).addReg(op1Reg);
2491 BuildMI(*MBB, MBBI, X86::MOV8rr, 1, DestReg).addReg(X86::AL);
Chris Lattner0f1c4612003-06-21 17:16:58 +00002492 return;
Chris Lattner94af4142002-12-25 05:13:53 +00002493 default:
Chris Lattner3e130a22003-01-13 00:32:26 +00002494 case cLong: assert(0 && "doMultiply cannot operate on LONG values!");
Chris Lattner94af4142002-12-25 05:13:53 +00002495 }
Brian Gaeke20244b72002-12-12 15:33:40 +00002496}
2497
Chris Lattnerb2acc512003-10-19 21:09:10 +00002498// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
2499// returns zero when the input is not exactly a power of two.
2500static unsigned ExactLog2(unsigned Val) {
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002501 if (Val == 0 || (Val & (Val-1))) return 0;
Chris Lattnerb2acc512003-10-19 21:09:10 +00002502 unsigned Count = 0;
2503 while (Val != 1) {
Chris Lattnerb2acc512003-10-19 21:09:10 +00002504 Val >>= 1;
2505 ++Count;
2506 }
2507 return Count+1;
2508}
2509
Chris Lattner462fa822004-04-11 20:56:28 +00002510
2511/// doMultiplyConst - This function is specialized to efficiently codegen an 8,
2512/// 16, or 32-bit integer multiply by a constant.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002513void X86ISel::doMultiplyConst(MachineBasicBlock *MBB,
2514 MachineBasicBlock::iterator IP,
2515 unsigned DestReg, const Type *DestTy,
2516 unsigned op0Reg, unsigned ConstRHS) {
Chris Lattner6ab06d52004-04-06 04:55:43 +00002517 static const unsigned MOVrrTab[] = {X86::MOV8rr, X86::MOV16rr, X86::MOV32rr};
2518 static const unsigned MOVriTab[] = {X86::MOV8ri, X86::MOV16ri, X86::MOV32ri};
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002519 static const unsigned ADDrrTab[] = {X86::ADD8rr, X86::ADD16rr, X86::ADD32rr};
Chris Lattner596b97f2004-07-19 23:47:21 +00002520 static const unsigned NEGrTab[] = {X86::NEG8r , X86::NEG16r , X86::NEG32r };
Chris Lattner6ab06d52004-04-06 04:55:43 +00002521
Chris Lattnerb2acc512003-10-19 21:09:10 +00002522 unsigned Class = getClass(DestTy);
Chris Lattner596b97f2004-07-19 23:47:21 +00002523 unsigned TmpReg;
Chris Lattnerb2acc512003-10-19 21:09:10 +00002524
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002525 // Handle special cases here.
2526 switch (ConstRHS) {
Chris Lattner596b97f2004-07-19 23:47:21 +00002527 case -2:
2528 TmpReg = makeAnotherReg(DestTy);
2529 BuildMI(*MBB, IP, NEGrTab[Class], 1, TmpReg).addReg(op0Reg);
2530 BuildMI(*MBB, IP, ADDrrTab[Class], 1,DestReg).addReg(TmpReg).addReg(TmpReg);
2531 return;
2532 case -1:
2533 BuildMI(*MBB, IP, NEGrTab[Class], 1, DestReg).addReg(op0Reg);
2534 return;
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002535 case 0:
Chris Lattner6ab06d52004-04-06 04:55:43 +00002536 BuildMI(*MBB, IP, MOVriTab[Class], 1, DestReg).addImm(0);
2537 return;
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002538 case 1:
Chris Lattner6ab06d52004-04-06 04:55:43 +00002539 BuildMI(*MBB, IP, MOVrrTab[Class], 1, DestReg).addReg(op0Reg);
2540 return;
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002541 case 2:
2542 BuildMI(*MBB, IP, ADDrrTab[Class], 1,DestReg).addReg(op0Reg).addReg(op0Reg);
2543 return;
2544 case 3:
2545 case 5:
2546 case 9:
2547 if (Class == cInt) {
Reid Spencerfc989e12004-08-30 00:13:26 +00002548 X86AddressMode AM;
2549 AM.BaseType = X86AddressMode::RegBase;
2550 AM.Base.Reg = op0Reg;
2551 AM.Scale = ConstRHS-1;
2552 AM.IndexReg = op0Reg;
2553 AM.Disp = 0;
2554 addFullAddress(BuildMI(*MBB, IP, X86::LEA32r, 5, DestReg), AM);
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002555 return;
2556 }
Chris Lattner596b97f2004-07-19 23:47:21 +00002557 case -3:
2558 case -5:
2559 case -9:
2560 if (Class == cInt) {
2561 TmpReg = makeAnotherReg(DestTy);
Reid Spencerfc989e12004-08-30 00:13:26 +00002562 X86AddressMode AM;
2563 AM.BaseType = X86AddressMode::RegBase;
2564 AM.Base.Reg = op0Reg;
2565 AM.Scale = -ConstRHS-1;
2566 AM.IndexReg = op0Reg;
2567 AM.Disp = 0;
2568 addFullAddress(BuildMI(*MBB, IP, X86::LEA32r, 5, TmpReg), AM);
Chris Lattner596b97f2004-07-19 23:47:21 +00002569 BuildMI(*MBB, IP, NEGrTab[Class], 1, DestReg).addReg(TmpReg);
2570 return;
2571 }
Chris Lattner6ab06d52004-04-06 04:55:43 +00002572 }
2573
Chris Lattnerb2acc512003-10-19 21:09:10 +00002574 // If the element size is exactly a power of 2, use a shift to get it.
2575 if (unsigned Shift = ExactLog2(ConstRHS)) {
2576 switch (Class) {
2577 default: assert(0 && "Unknown class for this function!");
2578 case cByte:
Chris Lattner596b97f2004-07-19 23:47:21 +00002579 BuildMI(*MBB, IP, X86::SHL8ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002580 return;
2581 case cShort:
Chris Lattner596b97f2004-07-19 23:47:21 +00002582 BuildMI(*MBB, IP, X86::SHL16ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002583 return;
2584 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002585 BuildMI(*MBB, IP, X86::SHL32ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002586 return;
2587 }
2588 }
Chris Lattner596b97f2004-07-19 23:47:21 +00002589
2590 // If the element size is a negative power of 2, use a shift/neg to get it.
2591 if (unsigned Shift = ExactLog2(-ConstRHS)) {
2592 TmpReg = makeAnotherReg(DestTy);
2593 BuildMI(*MBB, IP, NEGrTab[Class], 1, TmpReg).addReg(op0Reg);
2594 switch (Class) {
2595 default: assert(0 && "Unknown class for this function!");
2596 case cByte:
2597 BuildMI(*MBB, IP, X86::SHL8ri,2, DestReg).addReg(TmpReg).addImm(Shift-1);
2598 return;
2599 case cShort:
2600 BuildMI(*MBB, IP, X86::SHL16ri,2, DestReg).addReg(TmpReg).addImm(Shift-1);
2601 return;
2602 case cInt:
2603 BuildMI(*MBB, IP, X86::SHL32ri,2, DestReg).addReg(TmpReg).addImm(Shift-1);
2604 return;
2605 }
2606 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002607
Chris Lattnerc01d1232003-10-20 03:42:58 +00002608 if (Class == cShort) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002609 BuildMI(*MBB, IP, X86::IMUL16rri,2,DestReg).addReg(op0Reg).addImm(ConstRHS);
Chris Lattnerc01d1232003-10-20 03:42:58 +00002610 return;
2611 } else if (Class == cInt) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002612 BuildMI(*MBB, IP, X86::IMUL32rri,2,DestReg).addReg(op0Reg).addImm(ConstRHS);
Chris Lattnerc01d1232003-10-20 03:42:58 +00002613 return;
2614 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002615
2616 // Most general case, emit a normal multiply...
Chris Lattner596b97f2004-07-19 23:47:21 +00002617 TmpReg = makeAnotherReg(DestTy);
Chris Lattneree352852004-02-29 07:22:16 +00002618 BuildMI(*MBB, IP, MOVriTab[Class], 1, TmpReg).addImm(ConstRHS);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002619
Chris Lattnerb2acc512003-10-19 21:09:10 +00002620 // Emit a MUL to multiply the register holding the index by
2621 // elementSize, putting the result in OffsetReg.
2622 doMultiply(MBB, IP, DestReg, DestTy, op0Reg, TmpReg);
2623}
2624
Chris Lattnerca9671d2002-11-02 20:28:58 +00002625/// visitMul - Multiplies are not simple binary operators because they must deal
2626/// with the EAX register explicitly.
2627///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002628void X86ISel::visitMul(BinaryOperator &I) {
Chris Lattner462fa822004-04-11 20:56:28 +00002629 unsigned ResultReg = getReg(I);
2630
Chris Lattner95157f72004-04-11 22:05:45 +00002631 Value *Op0 = I.getOperand(0);
2632 Value *Op1 = I.getOperand(1);
2633
2634 // Fold loads into floating point multiplies.
2635 if (getClass(Op0->getType()) == cFP) {
2636 if (isa<LoadInst>(Op0) && !isa<LoadInst>(Op1))
2637 if (!I.swapOperands())
2638 std::swap(Op0, Op1); // Make sure any loads are in the RHS.
2639 if (LoadInst *LI = dyn_cast<LoadInst>(Op1))
2640 if (isSafeToFoldLoadIntoInstruction(*LI, I)) {
2641 const Type *Ty = Op0->getType();
2642 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2643 unsigned Opcode = Ty == Type::FloatTy ? X86::FMUL32m : X86::FMUL64m;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002644
Chris Lattner95157f72004-04-11 22:05:45 +00002645 unsigned Op0r = getReg(Op0);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002646 if (AllocaInst *AI = dyn_castFixedAlloca(LI->getOperand(0))) {
2647 unsigned FI = getFixedSizedAllocaFI(AI);
2648 addFrameReference(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op0r), FI);
2649 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002650 X86AddressMode AM;
2651 getAddressingMode(LI->getOperand(0), AM);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002652
Reid Spencerfc989e12004-08-30 00:13:26 +00002653 addFullAddress(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op0r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002654 }
Chris Lattner95157f72004-04-11 22:05:45 +00002655 return;
2656 }
2657 }
2658
Chris Lattner462fa822004-04-11 20:56:28 +00002659 MachineBasicBlock::iterator IP = BB->end();
Chris Lattner95157f72004-04-11 22:05:45 +00002660 emitMultiply(BB, IP, Op0, Op1, ResultReg);
Chris Lattner462fa822004-04-11 20:56:28 +00002661}
2662
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002663void X86ISel::emitMultiply(MachineBasicBlock *MBB,
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002664 MachineBasicBlock::iterator IP,
2665 Value *Op0, Value *Op1, unsigned DestReg) {
Chris Lattner462fa822004-04-11 20:56:28 +00002666 MachineBasicBlock &BB = *MBB;
2667 TypeClass Class = getClass(Op0->getType());
Chris Lattner3e130a22003-01-13 00:32:26 +00002668
2669 // Simple scalar multiply?
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002670 unsigned Op0Reg = getReg(Op0, &BB, IP);
Chris Lattner462fa822004-04-11 20:56:28 +00002671 switch (Class) {
2672 case cByte:
2673 case cShort:
2674 case cInt:
2675 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner462fa822004-04-11 20:56:28 +00002676 unsigned Val = (unsigned)CI->getRawValue(); // Isn't a 64-bit constant
2677 doMultiplyConst(&BB, IP, DestReg, Op0->getType(), Op0Reg, Val);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002678 } else {
Chris Lattner462fa822004-04-11 20:56:28 +00002679 unsigned Op1Reg = getReg(Op1, &BB, IP);
2680 doMultiply(&BB, IP, DestReg, Op1->getType(), Op0Reg, Op1Reg);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002681 }
Chris Lattner462fa822004-04-11 20:56:28 +00002682 return;
2683 case cFP:
Chris Lattner6621ed92004-04-11 21:23:56 +00002684 emitBinaryFPOperation(MBB, IP, Op0, Op1, 2, DestReg);
2685 return;
Chris Lattner462fa822004-04-11 20:56:28 +00002686 case cLong:
2687 break;
2688 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002689
Chris Lattner462fa822004-04-11 20:56:28 +00002690 // Long value. We have to do things the hard way...
2691 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
2692 unsigned CLow = CI->getRawValue();
2693 unsigned CHi = CI->getRawValue() >> 32;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002694
Chris Lattner462fa822004-04-11 20:56:28 +00002695 if (CLow == 0) {
2696 // If the low part of the constant is all zeros, things are simple.
2697 BuildMI(BB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
2698 doMultiplyConst(&BB, IP, DestReg+1, Type::UIntTy, Op0Reg, CHi);
2699 return;
2700 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002701
Chris Lattner462fa822004-04-11 20:56:28 +00002702 // Multiply the two low parts... capturing carry into EDX
2703 unsigned OverflowReg = 0;
2704 if (CLow == 1) {
2705 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg).addReg(Op0Reg);
Chris Lattner028adc42004-04-06 04:29:36 +00002706 } else {
Chris Lattner462fa822004-04-11 20:56:28 +00002707 unsigned Op1RegL = makeAnotherReg(Type::UIntTy);
2708 OverflowReg = makeAnotherReg(Type::UIntTy);
2709 BuildMI(BB, IP, X86::MOV32ri, 1, Op1RegL).addImm(CLow);
2710 BuildMI(BB, IP, X86::MOV32rr, 1, X86::EAX).addReg(Op0Reg);
2711 BuildMI(BB, IP, X86::MUL32r, 1).addReg(Op1RegL); // AL*BL
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002712
Chris Lattner462fa822004-04-11 20:56:28 +00002713 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg).addReg(X86::EAX); // AL*BL
2714 BuildMI(BB, IP, X86::MOV32rr, 1,
2715 OverflowReg).addReg(X86::EDX); // AL*BL >> 32
2716 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002717
Chris Lattner462fa822004-04-11 20:56:28 +00002718 unsigned AHBLReg = makeAnotherReg(Type::UIntTy); // AH*BL
2719 doMultiplyConst(&BB, IP, AHBLReg, Type::UIntTy, Op0Reg+1, CLow);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002720
Chris Lattner462fa822004-04-11 20:56:28 +00002721 unsigned AHBLplusOverflowReg;
2722 if (OverflowReg) {
2723 AHBLplusOverflowReg = makeAnotherReg(Type::UIntTy);
2724 BuildMI(BB, IP, X86::ADD32rr, 2, // AH*BL+(AL*BL >> 32)
Chris Lattner028adc42004-04-06 04:29:36 +00002725 AHBLplusOverflowReg).addReg(AHBLReg).addReg(OverflowReg);
Chris Lattner462fa822004-04-11 20:56:28 +00002726 } else {
2727 AHBLplusOverflowReg = AHBLReg;
2728 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002729
Chris Lattner462fa822004-04-11 20:56:28 +00002730 if (CHi == 0) {
2731 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg+1).addReg(AHBLplusOverflowReg);
2732 } else {
Chris Lattner028adc42004-04-06 04:29:36 +00002733 unsigned ALBHReg = makeAnotherReg(Type::UIntTy); // AL*BH
Chris Lattner462fa822004-04-11 20:56:28 +00002734 doMultiplyConst(&BB, IP, ALBHReg, Type::UIntTy, Op0Reg, CHi);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002735
Chris Lattner462fa822004-04-11 20:56:28 +00002736 BuildMI(BB, IP, X86::ADD32rr, 2, // AL*BH + AH*BL + (AL*BL >> 32)
Chris Lattner028adc42004-04-06 04:29:36 +00002737 DestReg+1).addReg(AHBLplusOverflowReg).addReg(ALBHReg);
2738 }
Chris Lattner462fa822004-04-11 20:56:28 +00002739 return;
Chris Lattner3e130a22003-01-13 00:32:26 +00002740 }
Chris Lattner462fa822004-04-11 20:56:28 +00002741
2742 // General 64x64 multiply
2743
2744 unsigned Op1Reg = getReg(Op1, &BB, IP);
2745 // Multiply the two low parts... capturing carry into EDX
2746 BuildMI(BB, IP, X86::MOV32rr, 1, X86::EAX).addReg(Op0Reg);
2747 BuildMI(BB, IP, X86::MUL32r, 1).addReg(Op1Reg); // AL*BL
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002748
Chris Lattner462fa822004-04-11 20:56:28 +00002749 unsigned OverflowReg = makeAnotherReg(Type::UIntTy);
2750 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg).addReg(X86::EAX); // AL*BL
2751 BuildMI(BB, IP, X86::MOV32rr, 1,
2752 OverflowReg).addReg(X86::EDX); // AL*BL >> 32
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002753
Chris Lattner462fa822004-04-11 20:56:28 +00002754 unsigned AHBLReg = makeAnotherReg(Type::UIntTy); // AH*BL
2755 BuildMI(BB, IP, X86::IMUL32rr, 2,
2756 AHBLReg).addReg(Op0Reg+1).addReg(Op1Reg);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002757
Chris Lattner462fa822004-04-11 20:56:28 +00002758 unsigned AHBLplusOverflowReg = makeAnotherReg(Type::UIntTy);
2759 BuildMI(BB, IP, X86::ADD32rr, 2, // AH*BL+(AL*BL >> 32)
2760 AHBLplusOverflowReg).addReg(AHBLReg).addReg(OverflowReg);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002761
Chris Lattner462fa822004-04-11 20:56:28 +00002762 unsigned ALBHReg = makeAnotherReg(Type::UIntTy); // AL*BH
2763 BuildMI(BB, IP, X86::IMUL32rr, 2,
2764 ALBHReg).addReg(Op0Reg).addReg(Op1Reg+1);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002765
Chris Lattner462fa822004-04-11 20:56:28 +00002766 BuildMI(BB, IP, X86::ADD32rr, 2, // AL*BH + AH*BL + (AL*BL >> 32)
2767 DestReg+1).addReg(AHBLplusOverflowReg).addReg(ALBHReg);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002768}
Chris Lattnerca9671d2002-11-02 20:28:58 +00002769
Chris Lattner06925362002-11-17 21:56:38 +00002770
Chris Lattnerf01729e2002-11-02 20:54:46 +00002771/// visitDivRem - Handle division and remainder instructions... these
2772/// instruction both require the same instructions to be generated, they just
2773/// select the result from a different register. Note that both of these
2774/// instructions work differently for signed and unsigned operands.
2775///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002776void X86ISel::visitDivRem(BinaryOperator &I) {
Chris Lattnercadff442003-10-23 17:21:43 +00002777 unsigned ResultReg = getReg(I);
Chris Lattner95157f72004-04-11 22:05:45 +00002778 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2779
2780 // Fold loads into floating point divides.
2781 if (getClass(Op0->getType()) == cFP) {
2782 if (LoadInst *LI = dyn_cast<LoadInst>(Op1))
2783 if (isSafeToFoldLoadIntoInstruction(*LI, I)) {
2784 const Type *Ty = Op0->getType();
2785 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2786 unsigned Opcode = Ty == Type::FloatTy ? X86::FDIV32m : X86::FDIV64m;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002787
Chris Lattner95157f72004-04-11 22:05:45 +00002788 unsigned Op0r = getReg(Op0);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002789 if (AllocaInst *AI = dyn_castFixedAlloca(LI->getOperand(0))) {
2790 unsigned FI = getFixedSizedAllocaFI(AI);
2791 addFrameReference(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op0r), FI);
2792 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002793 X86AddressMode AM;
2794 getAddressingMode(LI->getOperand(0), AM);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002795
Reid Spencerfc989e12004-08-30 00:13:26 +00002796 addFullAddress(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op0r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002797 }
Chris Lattner95157f72004-04-11 22:05:45 +00002798 return;
2799 }
2800
2801 if (LoadInst *LI = dyn_cast<LoadInst>(Op0))
2802 if (isSafeToFoldLoadIntoInstruction(*LI, I)) {
2803 const Type *Ty = Op0->getType();
2804 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2805 unsigned Opcode = Ty == Type::FloatTy ? X86::FDIVR32m : X86::FDIVR64m;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002806
Chris Lattner95157f72004-04-11 22:05:45 +00002807 unsigned Op1r = getReg(Op1);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002808 if (AllocaInst *AI = dyn_castFixedAlloca(LI->getOperand(0))) {
2809 unsigned FI = getFixedSizedAllocaFI(AI);
2810 addFrameReference(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op1r), FI);
2811 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002812 X86AddressMode AM;
2813 getAddressingMode(LI->getOperand(0), AM);
2814 addFullAddress(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op1r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002815 }
Chris Lattner95157f72004-04-11 22:05:45 +00002816 return;
2817 }
2818 }
2819
Chris Lattner94af4142002-12-25 05:13:53 +00002820
Chris Lattnercadff442003-10-23 17:21:43 +00002821 MachineBasicBlock::iterator IP = BB->end();
Chris Lattner95157f72004-04-11 22:05:45 +00002822 emitDivRemOperation(BB, IP, Op0, Op1,
Chris Lattner462fa822004-04-11 20:56:28 +00002823 I.getOpcode() == Instruction::Div, ResultReg);
Chris Lattnercadff442003-10-23 17:21:43 +00002824}
2825
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002826void X86ISel::emitDivRemOperation(MachineBasicBlock *BB,
2827 MachineBasicBlock::iterator IP,
2828 Value *Op0, Value *Op1, bool isDiv,
2829 unsigned ResultReg) {
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002830 const Type *Ty = Op0->getType();
2831 unsigned Class = getClass(Ty);
Chris Lattner94af4142002-12-25 05:13:53 +00002832 switch (Class) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002833 case cFP: // Floating point divide
Chris Lattnercadff442003-10-23 17:21:43 +00002834 if (isDiv) {
Chris Lattner6621ed92004-04-11 21:23:56 +00002835 emitBinaryFPOperation(BB, IP, Op0, Op1, 3, ResultReg);
2836 return;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00002837 } else { // Floating point remainder...
Chris Lattner462fa822004-04-11 20:56:28 +00002838 unsigned Op0Reg = getReg(Op0, BB, IP);
2839 unsigned Op1Reg = getReg(Op1, BB, IP);
Chris Lattner3e130a22003-01-13 00:32:26 +00002840 MachineInstr *TheCall =
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002841 BuildMI(X86::CALLpcrel32, 1).addExternalSymbol("fmod", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00002842 std::vector<ValueRecord> Args;
Chris Lattnercadff442003-10-23 17:21:43 +00002843 Args.push_back(ValueRecord(Op0Reg, Type::DoubleTy));
2844 Args.push_back(ValueRecord(Op1Reg, Type::DoubleTy));
Chris Lattner3e130a22003-01-13 00:32:26 +00002845 doCall(ValueRecord(ResultReg, Type::DoubleTy), TheCall, Args);
2846 }
Chris Lattner94af4142002-12-25 05:13:53 +00002847 return;
Chris Lattner3e130a22003-01-13 00:32:26 +00002848 case cLong: {
2849 static const char *FnName[] =
2850 { "__moddi3", "__divdi3", "__umoddi3", "__udivdi3" };
Chris Lattner462fa822004-04-11 20:56:28 +00002851 unsigned Op0Reg = getReg(Op0, BB, IP);
2852 unsigned Op1Reg = getReg(Op1, BB, IP);
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002853 unsigned NameIdx = Ty->isUnsigned()*2 + isDiv;
Chris Lattner3e130a22003-01-13 00:32:26 +00002854 MachineInstr *TheCall =
2855 BuildMI(X86::CALLpcrel32, 1).addExternalSymbol(FnName[NameIdx], true);
2856
2857 std::vector<ValueRecord> Args;
Chris Lattnercadff442003-10-23 17:21:43 +00002858 Args.push_back(ValueRecord(Op0Reg, Type::LongTy));
2859 Args.push_back(ValueRecord(Op1Reg, Type::LongTy));
Chris Lattner3e130a22003-01-13 00:32:26 +00002860 doCall(ValueRecord(ResultReg, Type::LongTy), TheCall, Args);
2861 return;
2862 }
2863 case cByte: case cShort: case cInt:
Misha Brukmancf00c4a2003-10-10 17:57:28 +00002864 break; // Small integrals, handled below...
Chris Lattner3e130a22003-01-13 00:32:26 +00002865 default: assert(0 && "Unknown class!");
Chris Lattner94af4142002-12-25 05:13:53 +00002866 }
Chris Lattnerf01729e2002-11-02 20:54:46 +00002867
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002868 static const unsigned MovOpcode[]={ X86::MOV8rr, X86::MOV16rr, X86::MOV32rr };
Chris Lattner2483f672004-10-06 05:01:07 +00002869 static const unsigned NEGOpcode[]={ X86::NEG8r, X86::NEG16r, X86::NEG32r };
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002870 static const unsigned SAROpcode[]={ X86::SAR8ri, X86::SAR16ri, X86::SAR32ri };
2871 static const unsigned SHROpcode[]={ X86::SHR8ri, X86::SHR16ri, X86::SHR32ri };
2872 static const unsigned ADDOpcode[]={ X86::ADD8rr, X86::ADD16rr, X86::ADD32rr };
2873
2874 // Special case signed division by power of 2.
Chris Lattner2483f672004-10-06 05:01:07 +00002875 if (ConstantSInt *CI = dyn_cast<ConstantSInt>(Op1))
2876 if (isDiv) {
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002877 assert(Class != cLong && "This doesn't handle 64-bit divides!");
2878 int V = CI->getValue();
2879
2880 if (V == 1) { // X /s 1 => X
2881 unsigned Op0Reg = getReg(Op0, BB, IP);
2882 BuildMI(*BB, IP, MovOpcode[Class], 1, ResultReg).addReg(Op0Reg);
2883 return;
2884 }
2885
2886 if (V == -1) { // X /s -1 => -X
2887 unsigned Op0Reg = getReg(Op0, BB, IP);
2888 BuildMI(*BB, IP, NEGOpcode[Class], 1, ResultReg).addReg(Op0Reg);
2889 return;
2890 }
2891
Chris Lattner610f1e22004-10-06 04:02:39 +00002892 if (V == 2 || V == -2) { // X /s 2
2893 static const unsigned CMPOpcode[] = {
2894 X86::CMP8ri, X86::CMP16ri, X86::CMP32ri
2895 };
2896 static const unsigned SBBOpcode[] = {
2897 X86::SBB8ri, X86::SBB16ri, X86::SBB32ri
2898 };
2899 unsigned Op0Reg = getReg(Op0, BB, IP);
2900 unsigned SignBit = 1 << (CI->getType()->getPrimitiveSize()*8-1);
2901 BuildMI(*BB, IP, CMPOpcode[Class], 2).addReg(Op0Reg).addImm(SignBit);
2902
2903 unsigned TmpReg = makeAnotherReg(Op0->getType());
2904 BuildMI(*BB, IP, SBBOpcode[Class], 2, TmpReg).addReg(Op0Reg).addImm(-1);
2905
2906 unsigned TmpReg2 = V == 2 ? ResultReg : makeAnotherReg(Op0->getType());
2907 BuildMI(*BB, IP, SAROpcode[Class], 2, TmpReg2).addReg(TmpReg).addImm(1);
2908 if (V == -2) {
2909 BuildMI(*BB, IP, NEGOpcode[Class], 1, ResultReg).addReg(TmpReg2);
2910 }
2911 return;
2912 }
2913
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002914 bool isNeg = false;
2915 if (V < 0) { // Not a positive power of 2?
2916 V = -V;
2917 isNeg = true; // Maybe it's a negative power of 2.
2918 }
2919 if (unsigned Log = ExactLog2(V)) {
2920 --Log;
2921 unsigned Op0Reg = getReg(Op0, BB, IP);
2922 unsigned TmpReg = makeAnotherReg(Op0->getType());
Chris Lattner3ffdff62004-10-06 04:19:43 +00002923 BuildMI(*BB, IP, SAROpcode[Class], 2, TmpReg)
2924 .addReg(Op0Reg).addImm(Log-1);
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002925 unsigned TmpReg2 = makeAnotherReg(Op0->getType());
2926 BuildMI(*BB, IP, SHROpcode[Class], 2, TmpReg2)
Chris Lattnera96e5772005-05-13 21:48:20 +00002927 .addReg(TmpReg).addImm(CI->getType()->getPrimitiveSizeInBits()-Log);
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002928 unsigned TmpReg3 = makeAnotherReg(Op0->getType());
2929 BuildMI(*BB, IP, ADDOpcode[Class], 2, TmpReg3)
2930 .addReg(Op0Reg).addReg(TmpReg2);
2931
2932 unsigned TmpReg4 = isNeg ? makeAnotherReg(Op0->getType()) : ResultReg;
2933 BuildMI(*BB, IP, SAROpcode[Class], 2, TmpReg4)
Chris Lattner3ffdff62004-10-06 04:19:43 +00002934 .addReg(TmpReg3).addImm(Log);
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002935 if (isNeg)
2936 BuildMI(*BB, IP, NEGOpcode[Class], 1, ResultReg).addReg(TmpReg4);
2937 return;
2938 }
Chris Lattner2483f672004-10-06 05:01:07 +00002939 } else { // X % C
2940 assert(Class != cLong && "This doesn't handle 64-bit remainder!");
2941 int V = CI->getValue();
2942
2943 if (V == 2 || V == -2) { // X % 2, X % -2
Chris Lattner2483f672004-10-06 05:01:07 +00002944 static const unsigned SExtOpcode[] = { X86::CBW, X86::CWD, X86::CDQ };
2945 static const unsigned BaseReg[] = { X86::AL , X86::AX , X86::EAX };
2946 static const unsigned SExtReg[] = { X86::AH , X86::DX , X86::EDX };
2947 static const unsigned ANDOpcode[] = {
2948 X86::AND8ri, X86::AND16ri, X86::AND32ri
2949 };
2950 static const unsigned XOROpcode[] = {
2951 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr
2952 };
2953 static const unsigned SUBOpcode[] = {
2954 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr
2955 };
2956
2957 // Sign extend result into reg of -1 or 0.
2958 unsigned Op0Reg = getReg(Op0, BB, IP);
2959 BuildMI(*BB, IP, MovOpcode[Class], 1, BaseReg[Class]).addReg(Op0Reg);
2960 BuildMI(*BB, IP, SExtOpcode[Class], 0);
2961 unsigned TmpReg0 = makeAnotherReg(Op0->getType());
2962 BuildMI(*BB, IP, MovOpcode[Class], 1, TmpReg0).addReg(SExtReg[Class]);
2963
2964 unsigned TmpReg1 = makeAnotherReg(Op0->getType());
2965 BuildMI(*BB, IP, ANDOpcode[Class], 2, TmpReg1).addReg(Op0Reg).addImm(1);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002966
Chris Lattner2483f672004-10-06 05:01:07 +00002967 unsigned TmpReg2 = makeAnotherReg(Op0->getType());
2968 BuildMI(*BB, IP, XOROpcode[Class], 2,
2969 TmpReg2).addReg(TmpReg1).addReg(TmpReg0);
2970 BuildMI(*BB, IP, SUBOpcode[Class], 2,
2971 ResultReg).addReg(TmpReg2).addReg(TmpReg0);
2972 return;
2973 }
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002974 }
2975
2976 static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX };
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002977 static const unsigned ClrOpcode[]={ X86::MOV8ri, X86::MOV16ri, X86::MOV32ri };
Chris Lattnerf01729e2002-11-02 20:54:46 +00002978 static const unsigned ExtRegs[] ={ X86::AH , X86::DX , X86::EDX };
Chris Lattner5384b382005-01-05 16:30:14 +00002979 static const unsigned SExOpcode[]={ X86::CBW , X86::CWD , X86::CDQ };
Chris Lattnerf01729e2002-11-02 20:54:46 +00002980
2981 static const unsigned DivOpcode[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002982 { X86::DIV8r , X86::DIV16r , X86::DIV32r , 0 }, // Unsigned division
2983 { X86::IDIV8r, X86::IDIV16r, X86::IDIV32r, 0 }, // Signed division
Chris Lattnerf01729e2002-11-02 20:54:46 +00002984 };
2985
Chris Lattnerf01729e2002-11-02 20:54:46 +00002986 unsigned Reg = Regs[Class];
2987 unsigned ExtReg = ExtRegs[Class];
Chris Lattnerf01729e2002-11-02 20:54:46 +00002988
2989 // Put the first operand into one of the A registers...
Chris Lattner462fa822004-04-11 20:56:28 +00002990 unsigned Op0Reg = getReg(Op0, BB, IP);
2991 unsigned Op1Reg = getReg(Op1, BB, IP);
Chris Lattneree352852004-02-29 07:22:16 +00002992 BuildMI(*BB, IP, MovOpcode[Class], 1, Reg).addReg(Op0Reg);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002993
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002994 if (Ty->isSigned()) {
Chris Lattner5384b382005-01-05 16:30:14 +00002995 // Emit a sign extension instruction.
2996 BuildMI(*BB, IP, SExOpcode[Class], 0);
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002997
2998 // Emit the appropriate divide or remainder instruction...
2999 BuildMI(*BB, IP, DivOpcode[1][Class], 1).addReg(Op1Reg);
Chris Lattnerf01729e2002-11-02 20:54:46 +00003000 } else {
Alkis Evlogimenosf998a7e2004-01-12 07:22:45 +00003001 // If unsigned, emit a zeroing instruction... (reg = 0)
Chris Lattneree352852004-02-29 07:22:16 +00003002 BuildMI(*BB, IP, ClrOpcode[Class], 2, ExtReg).addImm(0);
Chris Lattnerf01729e2002-11-02 20:54:46 +00003003
Chris Lattnerc8af02c2004-05-04 19:33:58 +00003004 // Emit the appropriate divide or remainder instruction...
3005 BuildMI(*BB, IP, DivOpcode[0][Class], 1).addReg(Op1Reg);
3006 }
Chris Lattner06925362002-11-17 21:56:38 +00003007
Chris Lattnerf01729e2002-11-02 20:54:46 +00003008 // Figure out which register we want to pick the result out of...
Chris Lattnercadff442003-10-23 17:21:43 +00003009 unsigned DestReg = isDiv ? Reg : ExtReg;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003010
Chris Lattnerf01729e2002-11-02 20:54:46 +00003011 // Put the result into the destination register...
Chris Lattneree352852004-02-29 07:22:16 +00003012 BuildMI(*BB, IP, MovOpcode[Class], 1, ResultReg).addReg(DestReg);
Chris Lattnerca9671d2002-11-02 20:28:58 +00003013}
Chris Lattnere2954c82002-11-02 20:04:26 +00003014
Chris Lattner06925362002-11-17 21:56:38 +00003015
Brian Gaekea1719c92002-10-31 23:03:59 +00003016/// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here
3017/// for constant immediate shift values, and for constant immediate
3018/// shift values equal to 1. Even the general case is sort of special,
3019/// because the shift amount has to be in CL, not just any old register.
3020///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003021void X86ISel::visitShiftInst(ShiftInst &I) {
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00003022 MachineBasicBlock::iterator IP = BB->end ();
3023 emitShiftOperation (BB, IP, I.getOperand (0), I.getOperand (1),
3024 I.getOpcode () == Instruction::Shl, I.getType (),
3025 getReg (I));
3026}
3027
Chris Lattnerce7cafa2004-11-13 20:48:57 +00003028/// Emit code for a 'SHLD DestReg, Op0, Op1, Amt' operation, where Amt is a
3029/// constant.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003030void X86ISel::doSHLDConst(MachineBasicBlock *MBB,
Chris Lattnerce7cafa2004-11-13 20:48:57 +00003031 MachineBasicBlock::iterator IP,
3032 unsigned DestReg, unsigned Op0Reg, unsigned Op1Reg,
3033 unsigned Amt) {
3034 // SHLD is a very inefficient operation on every processor, try to do
3035 // somethign simpler for common values of 'Amt'.
3036 if (Amt == 0) {
3037 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg).addReg(Op0Reg);
3038 } else if (Amt == 1) {
3039 unsigned Tmp = makeAnotherReg(Type::UIntTy);
3040 BuildMI(*MBB, IP, X86::ADD32rr, 2, Tmp).addReg(Op1Reg).addReg(Op1Reg);
3041 BuildMI(*MBB, IP, X86::ADC32rr, 2, DestReg).addReg(Op0Reg).addReg(Op0Reg);
3042 } else if (Amt == 2 || Amt == 3) {
3043 // On the P4 and Athlon it is cheaper to replace shld ..., 2|3 with a
3044 // shift/lea pair. NOTE: This should not be done on the P6 family!
3045 unsigned Tmp = makeAnotherReg(Type::UIntTy);
3046 BuildMI(*MBB, IP, X86::SHR32ri, 2, Tmp).addReg(Op1Reg).addImm(32-Amt);
3047 X86AddressMode AM;
3048 AM.BaseType = X86AddressMode::RegBase;
3049 AM.Base.Reg = Tmp;
3050 AM.Scale = 1 << Amt;
3051 AM.IndexReg = Op0Reg;
3052 AM.Disp = 0;
3053 addFullAddress(BuildMI(*MBB, IP, X86::LEA32r, 4, DestReg), AM);
3054 } else {
3055 // NOTE: It is always cheaper on the P4 to emit SHLD as two shifts and an OR
3056 // than it is to emit a real SHLD.
3057
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003058 BuildMI(*MBB, IP, X86::SHLD32rri8, 3,
Chris Lattnerce7cafa2004-11-13 20:48:57 +00003059 DestReg).addReg(Op0Reg).addReg(Op1Reg).addImm(Amt);
3060 }
3061}
3062
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00003063/// emitShiftOperation - Common code shared between visitShiftInst and
3064/// constant expression support.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003065void X86ISel::emitShiftOperation(MachineBasicBlock *MBB,
3066 MachineBasicBlock::iterator IP,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003067 Value *Op, Value *ShiftAmount,
3068 bool isLeftShift, const Type *ResultTy,
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003069 unsigned DestReg) {
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00003070 unsigned SrcReg = getReg (Op, MBB, IP);
3071 bool isSigned = ResultTy->isSigned ();
3072 unsigned Class = getClass (ResultTy);
Chris Lattnerde95c9e2004-10-17 06:10:40 +00003073
Chris Lattnerce7cafa2004-11-13 20:48:57 +00003074 static const unsigned ConstantOperand[][3] = {
3075 { X86::SHR8ri, X86::SHR16ri, X86::SHR32ri }, // SHR
3076 { X86::SAR8ri, X86::SAR16ri, X86::SAR32ri }, // SAR
3077 { X86::SHL8ri, X86::SHL16ri, X86::SHL32ri }, // SHL
3078 { X86::SHL8ri, X86::SHL16ri, X86::SHL32ri }, // SAL = SHL
Chris Lattner3e130a22003-01-13 00:32:26 +00003079 };
Chris Lattnerb1761fc2002-11-02 01:15:18 +00003080
Chris Lattnerce7cafa2004-11-13 20:48:57 +00003081 static const unsigned NonConstantOperand[][3] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003082 { X86::SHR8rCL, X86::SHR16rCL, X86::SHR32rCL }, // SHR
3083 { X86::SAR8rCL, X86::SAR16rCL, X86::SAR32rCL }, // SAR
3084 { X86::SHL8rCL, X86::SHL16rCL, X86::SHL32rCL }, // SHL
3085 { X86::SHL8rCL, X86::SHL16rCL, X86::SHL32rCL }, // SAL = SHL
Chris Lattner3e130a22003-01-13 00:32:26 +00003086 };
Chris Lattner796df732002-11-02 00:44:25 +00003087
Chris Lattnerce7cafa2004-11-13 20:48:57 +00003088 // Longs, as usual, are handled specially.
Chris Lattner3e130a22003-01-13 00:32:26 +00003089 if (Class == cLong) {
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00003090 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00003091 unsigned Amount = CUI->getValue();
Chris Lattner62f5a942004-11-13 20:04:38 +00003092 if (Amount == 1 && isLeftShift) { // X << 1 == X+X
Chris Lattner44205ca2004-11-13 20:03:48 +00003093 BuildMI(*MBB, IP, X86::ADD32rr, 2,
3094 DestReg).addReg(SrcReg).addReg(SrcReg);
3095 BuildMI(*MBB, IP, X86::ADC32rr, 2,
3096 DestReg+1).addReg(SrcReg+1).addReg(SrcReg+1);
3097 } else if (Amount < 32) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003098 const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned];
3099 if (isLeftShift) {
Chris Lattnerce7cafa2004-11-13 20:48:57 +00003100 doSHLDConst(MBB, IP, DestReg+1, SrcReg+1, SrcReg, Amount);
Chris Lattneree352852004-02-29 07:22:16 +00003101 BuildMI(*MBB, IP, Opc[2], 2, DestReg).addReg(SrcReg).addImm(Amount);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003102 } else {
Chris Lattnerce7cafa2004-11-13 20:48:57 +00003103 BuildMI(*MBB, IP, X86::SHRD32rri8, 3,
3104 DestReg).addReg(SrcReg ).addReg(SrcReg+1).addImm(Amount);
Chris Lattneree352852004-02-29 07:22:16 +00003105 BuildMI(*MBB, IP, Opc[2],2,DestReg+1).addReg(SrcReg+1).addImm(Amount);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003106 }
Chris Lattner36c625d2004-11-15 23:16:34 +00003107 } else if (Amount == 32) {
3108 if (isLeftShift) {
3109 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg+1).addReg(SrcReg);
3110 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
3111 } else {
Chris Lattner39a83dc2004-11-16 18:40:52 +00003112 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg+1);
Chris Lattner36c625d2004-11-15 23:16:34 +00003113 if (!isSigned) {
3114 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
3115 } else {
3116 BuildMI(*MBB, IP, X86::SAR32ri, 2,
3117 DestReg+1).addReg(SrcReg).addImm(31);
3118 }
3119 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003120 } else { // Shifting more than 32 bits
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003121 Amount -= 32;
3122 if (isLeftShift) {
Chris Lattner36c625d2004-11-15 23:16:34 +00003123 BuildMI(*MBB, IP, X86::SHL32ri, 2,
3124 DestReg + 1).addReg(SrcReg).addImm(Amount);
Chris Lattner722070e2004-04-06 03:42:38 +00003125 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003126 } else {
Chris Lattner36c625d2004-11-15 23:16:34 +00003127 BuildMI(*MBB, IP, isSigned ? X86::SAR32ri : X86::SHR32ri, 2,
3128 DestReg).addReg(SrcReg+1).addImm(Amount);
Chris Lattner6d027f22005-04-06 20:59:35 +00003129 if (isSigned)
3130 BuildMI(*MBB, IP, X86::SAR32ri, 2,
3131 DestReg+1).addReg(SrcReg+1).addImm(31);
3132 else
3133 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003134 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003135 }
3136 } else {
Chris Lattner9171ef52003-06-01 01:56:54 +00003137 unsigned TmpReg = makeAnotherReg(Type::IntTy);
Chris Lattner9171ef52003-06-01 01:56:54 +00003138 if (!isLeftShift && isSigned) {
3139 // If this is a SHR of a Long, then we need to do funny sign extension
3140 // stuff. TmpReg gets the value to use as the high-part if we are
3141 // shifting more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003142 BuildMI(*MBB, IP, X86::SAR32ri, 2, TmpReg).addReg(SrcReg).addImm(31);
Chris Lattner9171ef52003-06-01 01:56:54 +00003143 } else {
3144 // Other shifts use a fixed zero value if the shift is more than 32
3145 // bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003146 BuildMI(*MBB, IP, X86::MOV32ri, 1, TmpReg).addImm(0);
Chris Lattner9171ef52003-06-01 01:56:54 +00003147 }
3148
3149 // Initialize CL with the shift amount...
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00003150 unsigned ShiftAmountReg = getReg(ShiftAmount, MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003151 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00003152
3153 unsigned TmpReg2 = makeAnotherReg(Type::IntTy);
3154 unsigned TmpReg3 = makeAnotherReg(Type::IntTy);
3155 if (isLeftShift) {
3156 // TmpReg2 = shld inHi, inLo
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003157 BuildMI(*MBB, IP, X86::SHLD32rrCL,2,TmpReg2).addReg(SrcReg+1)
Chris Lattneree352852004-02-29 07:22:16 +00003158 .addReg(SrcReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00003159 // TmpReg3 = shl inLo, CL
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003160 BuildMI(*MBB, IP, X86::SHL32rCL, 1, TmpReg3).addReg(SrcReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00003161
3162 // Set the flags to indicate whether the shift was by more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003163 BuildMI(*MBB, IP, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Chris Lattner9171ef52003-06-01 01:56:54 +00003164
3165 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003166 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00003167 DestReg+1).addReg(TmpReg2).addReg(TmpReg3);
3168 // DestLo = (>32) ? TmpReg : TmpReg3;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003169 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00003170 DestReg).addReg(TmpReg3).addReg(TmpReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00003171 } else {
3172 // TmpReg2 = shrd inLo, inHi
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003173 BuildMI(*MBB, IP, X86::SHRD32rrCL,2,TmpReg2).addReg(SrcReg)
Chris Lattneree352852004-02-29 07:22:16 +00003174 .addReg(SrcReg+1);
Chris Lattner9171ef52003-06-01 01:56:54 +00003175 // TmpReg3 = s[ah]r inHi, CL
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003176 BuildMI(*MBB, IP, isSigned ? X86::SAR32rCL : X86::SHR32rCL, 1, TmpReg3)
Chris Lattner9171ef52003-06-01 01:56:54 +00003177 .addReg(SrcReg+1);
3178
3179 // Set the flags to indicate whether the shift was by more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003180 BuildMI(*MBB, IP, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Chris Lattner9171ef52003-06-01 01:56:54 +00003181
3182 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003183 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00003184 DestReg).addReg(TmpReg2).addReg(TmpReg3);
3185
3186 // DestHi = (>32) ? TmpReg : TmpReg3;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003187 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00003188 DestReg+1).addReg(TmpReg3).addReg(TmpReg);
3189 }
Brian Gaekea1719c92002-10-31 23:03:59 +00003190 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003191 return;
3192 }
Chris Lattnere9913f22002-11-02 01:41:55 +00003193
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00003194 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00003195 // The shift amount is constant, guaranteed to be a ubyte. Get its value.
3196 assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?");
Chris Lattnerb1761fc2002-11-02 01:15:18 +00003197
Chris Lattner44205ca2004-11-13 20:03:48 +00003198 if (CUI->getValue() == 1 && isLeftShift) { // X << 1 -> X+X
3199 static const int AddOpC[] = { X86::ADD8rr, X86::ADD16rr, X86::ADD32rr };
3200 BuildMI(*MBB, IP, AddOpC[Class], 2,DestReg).addReg(SrcReg).addReg(SrcReg);
3201 } else {
3202 const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned];
3203 BuildMI(*MBB, IP, Opc[Class], 2,
3204 DestReg).addReg(SrcReg).addImm(CUI->getValue());
3205 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003206 } else { // The shift amount is non-constant.
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00003207 unsigned ShiftAmountReg = getReg (ShiftAmount, MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003208 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
Chris Lattnerb1761fc2002-11-02 01:15:18 +00003209
Chris Lattner3e130a22003-01-13 00:32:26 +00003210 const unsigned *Opc = NonConstantOperand[isLeftShift*2+isSigned];
Chris Lattneree352852004-02-29 07:22:16 +00003211 BuildMI(*MBB, IP, Opc[Class], 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003212 }
3213}
Chris Lattnerb1761fc2002-11-02 01:15:18 +00003214
Chris Lattner3e130a22003-01-13 00:32:26 +00003215
Chris Lattner6fc3c522002-11-17 21:11:55 +00003216/// visitLoadInst - Implement LLVM load instructions in terms of the x86 'mov'
Chris Lattnere8f0d922002-12-24 00:03:11 +00003217/// instruction. The load and store instructions are the only place where we
3218/// need to worry about the memory layout of the target machine.
Chris Lattner6fc3c522002-11-17 21:11:55 +00003219///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003220void X86ISel::visitLoadInst(LoadInst &I) {
Chris Lattner7dee5da2004-03-08 01:58:35 +00003221 // Check to see if this load instruction is going to be folded into a binary
3222 // instruction, like add. If so, we don't want to emit it. Wouldn't a real
3223 // pattern matching instruction selector be nice?
Chris Lattner95157f72004-04-11 22:05:45 +00003224 unsigned Class = getClassB(I.getType());
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003225 if (I.hasOneUse()) {
Chris Lattner7dee5da2004-03-08 01:58:35 +00003226 Instruction *User = cast<Instruction>(I.use_back());
3227 switch (User->getOpcode()) {
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003228 case Instruction::Cast:
3229 // If this is a cast from a signed-integer type to a floating point type,
3230 // fold the cast here.
John Criswell6b5bd582004-06-09 15:18:51 +00003231 if (getClassB(User->getType()) == cFP &&
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003232 (I.getType() == Type::ShortTy || I.getType() == Type::IntTy ||
3233 I.getType() == Type::LongTy)) {
3234 unsigned DestReg = getReg(User);
3235 static const unsigned Opcode[] = {
3236 0/*BYTE*/, X86::FILD16m, X86::FILD32m, 0/*FP*/, X86::FILD64m
3237 };
Chris Lattner9f1b5312004-05-13 15:12:43 +00003238
3239 if (AllocaInst *AI = dyn_castFixedAlloca(I.getOperand(0))) {
3240 unsigned FI = getFixedSizedAllocaFI(AI);
3241 addFrameReference(BuildMI(BB, Opcode[Class], 4, DestReg), FI);
3242 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00003243 X86AddressMode AM;
3244 getAddressingMode(I.getOperand(0), AM);
3245 addFullAddress(BuildMI(BB, Opcode[Class], 4, DestReg), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00003246 }
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003247 return;
3248 } else {
3249 User = 0;
3250 }
3251 break;
Chris Lattner13c07fe2004-04-12 00:12:04 +00003252
Chris Lattner7dee5da2004-03-08 01:58:35 +00003253 case Instruction::Add:
3254 case Instruction::Sub:
3255 case Instruction::And:
3256 case Instruction::Or:
3257 case Instruction::Xor:
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003258 if (Class == cLong) User = 0;
Chris Lattner7dee5da2004-03-08 01:58:35 +00003259 break;
Chris Lattner95157f72004-04-11 22:05:45 +00003260 case Instruction::Mul:
3261 case Instruction::Div:
Chris Lattner13c07fe2004-04-12 00:12:04 +00003262 if (Class != cFP) User = 0;
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003263 break; // Folding only implemented for floating point.
Chris Lattner95157f72004-04-11 22:05:45 +00003264 default: User = 0; break;
Chris Lattner7dee5da2004-03-08 01:58:35 +00003265 }
3266
3267 if (User) {
3268 // Okay, we found a user. If the load is the first operand and there is
3269 // no second operand load, reverse the operand ordering. Note that this
3270 // can fail for a subtract (ie, no change will be made).
Chris Lattner3dbb5042004-07-21 21:28:26 +00003271 bool Swapped = false;
Chris Lattner7dee5da2004-03-08 01:58:35 +00003272 if (!isa<LoadInst>(User->getOperand(1)))
Chris Lattner3dbb5042004-07-21 21:28:26 +00003273 Swapped = !cast<BinaryOperator>(User)->swapOperands();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003274
Chris Lattner7dee5da2004-03-08 01:58:35 +00003275 // Okay, now that everything is set up, if this load is used by the second
3276 // operand, and if there are no instructions that invalidate the load
3277 // before the binary operator, eliminate the load.
3278 if (User->getOperand(1) == &I &&
3279 isSafeToFoldLoadIntoInstruction(I, *User))
3280 return; // Eliminate the load!
Chris Lattner95157f72004-04-11 22:05:45 +00003281
3282 // If this is a floating point sub or div, we won't be able to swap the
3283 // operands, but we will still be able to eliminate the load.
3284 if (Class == cFP && User->getOperand(0) == &I &&
3285 !isa<LoadInst>(User->getOperand(1)) &&
3286 (User->getOpcode() == Instruction::Sub ||
3287 User->getOpcode() == Instruction::Div) &&
3288 isSafeToFoldLoadIntoInstruction(I, *User))
3289 return; // Eliminate the load!
Chris Lattner3dbb5042004-07-21 21:28:26 +00003290
3291 // If we swapped the operands to the instruction, but couldn't fold the
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003292 // load anyway, swap them back. We don't want to break add X, int
Chris Lattner3dbb5042004-07-21 21:28:26 +00003293 // folding.
3294 if (Swapped) cast<BinaryOperator>(User)->swapOperands();
Chris Lattner7dee5da2004-03-08 01:58:35 +00003295 }
3296 }
3297
Chris Lattner6ac1d712003-10-20 04:48:06 +00003298 static const unsigned Opcodes[] = {
Chris Lattner9f1b5312004-05-13 15:12:43 +00003299 X86::MOV8rm, X86::MOV16rm, X86::MOV32rm, X86::FLD32m, X86::MOV32rm
Chris Lattner3e130a22003-01-13 00:32:26 +00003300 };
Chris Lattner6ac1d712003-10-20 04:48:06 +00003301 unsigned Opcode = Opcodes[Class];
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003302 if (I.getType() == Type::DoubleTy) Opcode = X86::FLD64m;
Chris Lattner9f1b5312004-05-13 15:12:43 +00003303
3304 unsigned DestReg = getReg(I);
3305
3306 if (AllocaInst *AI = dyn_castFixedAlloca(I.getOperand(0))) {
3307 unsigned FI = getFixedSizedAllocaFI(AI);
3308 if (Class == cLong) {
3309 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, DestReg), FI);
3310 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, DestReg+1), FI, 4);
3311 } else {
3312 addFrameReference(BuildMI(BB, Opcode, 4, DestReg), FI);
3313 }
3314 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00003315 X86AddressMode AM;
3316 getAddressingMode(I.getOperand(0), AM);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003317
Chris Lattner9f1b5312004-05-13 15:12:43 +00003318 if (Class == cLong) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003319 addFullAddress(BuildMI(BB, X86::MOV32rm, 4, DestReg), AM);
3320 AM.Disp += 4;
3321 addFullAddress(BuildMI(BB, X86::MOV32rm, 4, DestReg+1), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00003322 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00003323 addFullAddress(BuildMI(BB, Opcode, 4, DestReg), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00003324 }
3325 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003326}
3327
Chris Lattner6fc3c522002-11-17 21:11:55 +00003328/// visitStoreInst - Implement LLVM store instructions in terms of the x86 'mov'
3329/// instruction.
3330///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003331void X86ISel::visitStoreInst(StoreInst &I) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003332 X86AddressMode AM;
3333 getAddressingMode(I.getOperand(1), AM);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003334
Chris Lattner6c09db22003-10-20 04:11:23 +00003335 const Type *ValTy = I.getOperand(0)->getType();
3336 unsigned Class = getClassB(ValTy);
Chris Lattner6ac1d712003-10-20 04:48:06 +00003337
Chris Lattner5a830962004-02-25 02:56:58 +00003338 if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(0))) {
3339 uint64_t Val = CI->getRawValue();
3340 if (Class == cLong) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003341 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(Val & ~0U);
3342 AM.Disp += 4;
3343 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(Val>>32);
Chris Lattner5a830962004-02-25 02:56:58 +00003344 } else {
3345 static const unsigned Opcodes[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003346 X86::MOV8mi, X86::MOV16mi, X86::MOV32mi
Chris Lattner5a830962004-02-25 02:56:58 +00003347 };
3348 unsigned Opcode = Opcodes[Class];
Reid Spencerfc989e12004-08-30 00:13:26 +00003349 addFullAddress(BuildMI(BB, Opcode, 5), AM).addImm(Val);
Chris Lattner5a830962004-02-25 02:56:58 +00003350 }
Chris Lattnerb7cb9ff2004-05-13 15:26:48 +00003351 } else if (isa<ConstantPointerNull>(I.getOperand(0))) {
Chris Lattner358a9022004-10-15 05:05:29 +00003352 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(0);
Chris Lattner10902622005-04-21 19:11:03 +00003353 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(I.getOperand(0))) {
3354 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addGlobalAddress(GV);
Chris Lattner5a830962004-02-25 02:56:58 +00003355 } else if (ConstantBool *CB = dyn_cast<ConstantBool>(I.getOperand(0))) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003356 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CB->getValue());
Chris Lattnere7a31c92004-05-07 21:18:15 +00003357 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0))) {
3358 // Store constant FP values with integer instructions to avoid having to
3359 // load the constants from the constant pool then do a store.
3360 if (CFP->getType() == Type::FloatTy) {
3361 union {
3362 unsigned I;
3363 float F;
3364 } V;
3365 V.F = CFP->getValue();
Reid Spencerfc989e12004-08-30 00:13:26 +00003366 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(V.I);
Chris Lattner5a830962004-02-25 02:56:58 +00003367 } else {
Chris Lattnere7a31c92004-05-07 21:18:15 +00003368 union {
3369 uint64_t I;
3370 double F;
3371 } V;
3372 V.F = CFP->getValue();
Reid Spencerfc989e12004-08-30 00:13:26 +00003373 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm((unsigned)V.I);
3374 AM.Disp += 4;
3375 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(
Chris Lattnere7a31c92004-05-07 21:18:15 +00003376 unsigned(V.I >> 32));
Chris Lattner5a830962004-02-25 02:56:58 +00003377 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003378
Chris Lattnere7a31c92004-05-07 21:18:15 +00003379 } else if (Class == cLong) {
3380 unsigned ValReg = getReg(I.getOperand(0));
Reid Spencerfc989e12004-08-30 00:13:26 +00003381 addFullAddress(BuildMI(BB, X86::MOV32mr, 5), AM).addReg(ValReg);
3382 AM.Disp += 4;
3383 addFullAddress(BuildMI(BB, X86::MOV32mr, 5), AM).addReg(ValReg+1);
Chris Lattnere7a31c92004-05-07 21:18:15 +00003384 } else {
Chris Lattner358a9022004-10-15 05:05:29 +00003385 // FIXME: stop emitting these two instructions:
3386 // movl $global,%eax
3387 // movl %eax,(%ebx)
3388 // when one instruction will suffice. That includes when the global
3389 // has an offset applied to it.
Chris Lattnere7a31c92004-05-07 21:18:15 +00003390 unsigned ValReg = getReg(I.getOperand(0));
3391 static const unsigned Opcodes[] = {
3392 X86::MOV8mr, X86::MOV16mr, X86::MOV32mr, X86::FST32m
3393 };
3394 unsigned Opcode = Opcodes[Class];
3395 if (ValTy == Type::DoubleTy) Opcode = X86::FST64m;
Chris Lattner9f1b5312004-05-13 15:12:43 +00003396
Reid Spencerfc989e12004-08-30 00:13:26 +00003397 addFullAddress(BuildMI(BB, Opcode, 1+4), AM).addReg(ValReg);
Chris Lattner94af4142002-12-25 05:13:53 +00003398 }
Chris Lattner6fc3c522002-11-17 21:11:55 +00003399}
3400
3401
Misha Brukman538607f2004-03-01 23:53:11 +00003402/// visitCastInst - Here we have various kinds of copying with or without sign
3403/// extension going on.
3404///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003405void X86ISel::visitCastInst(CastInst &CI) {
Chris Lattnerf5854472003-06-21 16:01:24 +00003406 Value *Op = CI.getOperand(0);
Chris Lattner427aeb42004-04-11 19:21:59 +00003407
Chris Lattner99382862004-04-12 00:23:04 +00003408 unsigned SrcClass = getClassB(Op->getType());
3409 unsigned DestClass = getClassB(CI.getType());
3410 // Noop casts are not emitted: getReg will return the source operand as the
3411 // register to use for any uses of the noop cast.
Chris Lattner8b486a12004-06-29 00:14:38 +00003412 if (DestClass == SrcClass) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003413 // The only detail in this plan is that casts from double -> float are
Chris Lattner8b486a12004-06-29 00:14:38 +00003414 // truncating operations that we have to codegen through memory (despite
3415 // the fact that the source/dest registers are the same class).
3416 if (CI.getType() != Type::FloatTy || Op->getType() != Type::DoubleTy)
3417 return;
3418 }
Chris Lattner427aeb42004-04-11 19:21:59 +00003419
Chris Lattnerf5854472003-06-21 16:01:24 +00003420 // If this is a cast from a 32-bit integer to a Long type, and the only uses
3421 // of the case are GEP instructions, then the cast does not need to be
3422 // generated explicitly, it will be folded into the GEP.
Chris Lattner99382862004-04-12 00:23:04 +00003423 if (DestClass == cLong && SrcClass == cInt) {
Chris Lattnerf5854472003-06-21 16:01:24 +00003424 bool AllUsesAreGEPs = true;
3425 for (Value::use_iterator I = CI.use_begin(), E = CI.use_end(); I != E; ++I)
3426 if (!isa<GetElementPtrInst>(*I)) {
3427 AllUsesAreGEPs = false;
3428 break;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003429 }
Chris Lattnerf5854472003-06-21 16:01:24 +00003430
3431 // No need to codegen this cast if all users are getelementptr instrs...
3432 if (AllUsesAreGEPs) return;
3433 }
3434
Chris Lattner99382862004-04-12 00:23:04 +00003435 // If this cast converts a load from a short,int, or long integer to a FP
3436 // value, we will have folded this cast away.
3437 if (DestClass == cFP && isa<LoadInst>(Op) && Op->hasOneUse() &&
3438 (Op->getType() == Type::ShortTy || Op->getType() == Type::IntTy ||
3439 Op->getType() == Type::LongTy))
3440 return;
3441
3442
Chris Lattner548f61d2003-04-23 17:22:12 +00003443 unsigned DestReg = getReg(CI);
3444 MachineBasicBlock::iterator MI = BB->end();
Chris Lattnerf5854472003-06-21 16:01:24 +00003445 emitCastOperation(BB, MI, Op, CI.getType(), DestReg);
Chris Lattner548f61d2003-04-23 17:22:12 +00003446}
3447
Misha Brukman538607f2004-03-01 23:53:11 +00003448/// emitCastOperation - Common code shared between visitCastInst and constant
3449/// expression cast support.
3450///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003451void X86ISel::emitCastOperation(MachineBasicBlock *BB,
3452 MachineBasicBlock::iterator IP,
3453 Value *Src, const Type *DestTy,
3454 unsigned DestReg) {
Chris Lattner3e130a22003-01-13 00:32:26 +00003455 const Type *SrcTy = Src->getType();
3456 unsigned SrcClass = getClassB(SrcTy);
Chris Lattner3e130a22003-01-13 00:32:26 +00003457 unsigned DestClass = getClassB(DestTy);
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003458 unsigned SrcReg = getReg(Src, BB, IP);
3459
Chris Lattner3e130a22003-01-13 00:32:26 +00003460 // Implement casts to bool by using compare on the operand followed by set if
3461 // not zero on the result.
3462 if (DestTy == Type::BoolTy) {
Chris Lattner20772542003-06-01 03:38:24 +00003463 switch (SrcClass) {
3464 case cByte:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003465 BuildMI(*BB, IP, X86::TEST8rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00003466 break;
3467 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003468 BuildMI(*BB, IP, X86::TEST16rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00003469 break;
3470 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003471 BuildMI(*BB, IP, X86::TEST32rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00003472 break;
3473 case cLong: {
3474 unsigned TmpReg = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003475 BuildMI(*BB, IP, X86::OR32rr, 2, TmpReg).addReg(SrcReg).addReg(SrcReg+1);
Chris Lattner20772542003-06-01 03:38:24 +00003476 break;
3477 }
3478 case cFP:
Chris Lattneree352852004-02-29 07:22:16 +00003479 BuildMI(*BB, IP, X86::FTST, 1).addReg(SrcReg);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003480 BuildMI(*BB, IP, X86::FNSTSW8r, 0);
Chris Lattneree352852004-02-29 07:22:16 +00003481 BuildMI(*BB, IP, X86::SAHF, 1);
Chris Lattner311ca2e2004-02-23 03:21:41 +00003482 break;
Chris Lattner20772542003-06-01 03:38:24 +00003483 }
3484
3485 // If the zero flag is not set, then the value is true, set the byte to
3486 // true.
Chris Lattneree352852004-02-29 07:22:16 +00003487 BuildMI(*BB, IP, X86::SETNEr, 1, DestReg);
Chris Lattner94af4142002-12-25 05:13:53 +00003488 return;
3489 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003490
3491 static const unsigned RegRegMove[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003492 X86::MOV8rr, X86::MOV16rr, X86::MOV32rr, X86::FpMOV, X86::MOV32rr
Chris Lattner3e130a22003-01-13 00:32:26 +00003493 };
3494
3495 // Implement casts between values of the same type class (as determined by
3496 // getClass) by using a register-to-register move.
3497 if (SrcClass == DestClass) {
3498 if (SrcClass <= cInt || (SrcClass == cFP && SrcTy == DestTy)) {
Chris Lattneree352852004-02-29 07:22:16 +00003499 BuildMI(*BB, IP, RegRegMove[SrcClass], 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003500 } else if (SrcClass == cFP) {
3501 if (SrcTy == Type::FloatTy) { // double -> float
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003502 assert(DestTy == Type::DoubleTy && "Unknown cFP member!");
Chris Lattneree352852004-02-29 07:22:16 +00003503 BuildMI(*BB, IP, X86::FpMOV, 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003504 } else { // float -> double
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003505 assert(SrcTy == Type::DoubleTy && DestTy == Type::FloatTy &&
3506 "Unknown cFP member!");
3507 // Truncate from double to float by storing to memory as short, then
3508 // reading it back.
3509 unsigned FltAlign = TM.getTargetData().getFloatAlignment();
Chris Lattner3e130a22003-01-13 00:32:26 +00003510 int FrameIdx = F->getFrameInfo()->CreateStackObject(4, FltAlign);
Chris Lattner5384b382005-01-05 16:30:14 +00003511 addFrameReference(BuildMI(*BB, IP, X86::FST32m, 5),
3512 FrameIdx).addReg(SrcReg);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003513 addFrameReference(BuildMI(*BB, IP, X86::FLD32m, 5, DestReg), FrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003514 }
3515 } else if (SrcClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003516 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg);
3517 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg+1).addReg(SrcReg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00003518 } else {
Chris Lattnerc53544a2003-05-12 20:16:58 +00003519 assert(0 && "Cannot handle this type of cast instruction!");
Chris Lattner548f61d2003-04-23 17:22:12 +00003520 abort();
Brian Gaeked474e9c2002-12-06 10:49:33 +00003521 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003522 return;
3523 }
3524
3525 // Handle cast of SMALLER int to LARGER int using a move with sign extension
3526 // or zero extension, depending on whether the source type was signed.
3527 if (SrcClass <= cInt && (DestClass <= cInt || DestClass == cLong) &&
3528 SrcClass < DestClass) {
3529 bool isLong = DestClass == cLong;
3530 if (isLong) DestClass = cInt;
3531
3532 static const unsigned Opc[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003533 { X86::MOVSX16rr8, X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOV32rr }, // s
3534 { X86::MOVZX16rr8, X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOV32rr } // u
Chris Lattner3e130a22003-01-13 00:32:26 +00003535 };
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003536
Chris Lattner96e3b422004-05-09 22:28:45 +00003537 bool isUnsigned = SrcTy->isUnsigned() || SrcTy == Type::BoolTy;
Chris Lattneree352852004-02-29 07:22:16 +00003538 BuildMI(*BB, IP, Opc[isUnsigned][SrcClass + DestClass - 1], 1,
Chris Lattner548f61d2003-04-23 17:22:12 +00003539 DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003540
3541 if (isLong) { // Handle upper 32 bits as appropriate...
3542 if (isUnsigned) // Zero out top bits...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003543 BuildMI(*BB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
Chris Lattner3e130a22003-01-13 00:32:26 +00003544 else // Sign extend bottom half...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003545 BuildMI(*BB, IP, X86::SAR32ri, 2, DestReg+1).addReg(DestReg).addImm(31);
Brian Gaeked474e9c2002-12-06 10:49:33 +00003546 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003547 return;
3548 }
3549
3550 // Special case long -> int ...
3551 if (SrcClass == cLong && DestClass == cInt) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003552 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003553 return;
3554 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003555
Chris Lattner3e130a22003-01-13 00:32:26 +00003556 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by a
3557 // move out of AX or AL.
3558 if ((SrcClass <= cInt || SrcClass == cLong) && DestClass <= cInt
3559 && SrcClass > DestClass) {
3560 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX, 0, X86::EAX };
Chris Lattneree352852004-02-29 07:22:16 +00003561 BuildMI(*BB, IP, RegRegMove[SrcClass], 1, AReg[SrcClass]).addReg(SrcReg);
3562 BuildMI(*BB, IP, RegRegMove[DestClass], 1, DestReg).addReg(AReg[DestClass]);
Chris Lattner3e130a22003-01-13 00:32:26 +00003563 return;
3564 }
3565
3566 // Handle casts from integer to floating point now...
3567 if (DestClass == cFP) {
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003568 // Promote the integer to a type supported by FLD. We do this because there
3569 // are no unsigned FLD instructions, so we must promote an unsigned value to
3570 // a larger signed value, then use FLD on the larger value.
3571 //
3572 const Type *PromoteType = 0;
Chris Lattner85aa7092004-04-10 18:32:01 +00003573 unsigned PromoteOpcode = 0;
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003574 unsigned RealDestReg = DestReg;
Chris Lattnerf70c22b2004-06-17 18:19:28 +00003575 switch (SrcTy->getTypeID()) {
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003576 case Type::BoolTyID:
3577 case Type::SByteTyID:
3578 // We don't have the facilities for directly loading byte sized data from
3579 // memory (even signed). Promote it to 16 bits.
3580 PromoteType = Type::ShortTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003581 PromoteOpcode = X86::MOVSX16rr8;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003582 break;
3583 case Type::UByteTyID:
3584 PromoteType = Type::ShortTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003585 PromoteOpcode = X86::MOVZX16rr8;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003586 break;
3587 case Type::UShortTyID:
3588 PromoteType = Type::IntTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003589 PromoteOpcode = X86::MOVZX32rr16;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003590 break;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003591 case Type::ULongTyID:
Chris Lattner56a31c62004-10-17 08:01:28 +00003592 case Type::UIntTyID:
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003593 // Don't fild into the read destination.
3594 DestReg = makeAnotherReg(Type::DoubleTy);
3595 break;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003596 default: // No promotion needed...
3597 break;
3598 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003599
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003600 if (PromoteType) {
3601 unsigned TmpReg = makeAnotherReg(PromoteType);
Chris Lattner7b92de1e2004-04-06 19:29:36 +00003602 BuildMI(*BB, IP, PromoteOpcode, 1, TmpReg).addReg(SrcReg);
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003603 SrcTy = PromoteType;
3604 SrcClass = getClass(PromoteType);
Chris Lattner3e130a22003-01-13 00:32:26 +00003605 SrcReg = TmpReg;
3606 }
3607
3608 // Spill the integer to memory and reload it from there...
Chris Lattnerb6bac512004-02-25 06:13:04 +00003609 int FrameIdx =
3610 F->getFrameInfo()->CreateStackObject(SrcTy, TM.getTargetData());
Chris Lattner3e130a22003-01-13 00:32:26 +00003611
3612 if (SrcClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003613 addFrameReference(BuildMI(*BB, IP, X86::MOV32mr, 5),
Chris Lattneree352852004-02-29 07:22:16 +00003614 FrameIdx).addReg(SrcReg);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003615 addFrameReference(BuildMI(*BB, IP, X86::MOV32mr, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003616 FrameIdx, 4).addReg(SrcReg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00003617 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003618 static const unsigned Op1[] = { X86::MOV8mr, X86::MOV16mr, X86::MOV32mr };
Chris Lattneree352852004-02-29 07:22:16 +00003619 addFrameReference(BuildMI(*BB, IP, Op1[SrcClass], 5),
3620 FrameIdx).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003621 }
3622
3623 static const unsigned Op2[] =
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003624 { 0/*byte*/, X86::FILD16m, X86::FILD32m, 0/*FP*/, X86::FILD64m };
Chris Lattneree352852004-02-29 07:22:16 +00003625 addFrameReference(BuildMI(*BB, IP, Op2[SrcClass], 5, DestReg), FrameIdx);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003626
Chris Lattner56a31c62004-10-17 08:01:28 +00003627 if (SrcTy == Type::UIntTy) {
3628 // If this is a cast from uint -> double, we need to be careful about if
3629 // the "sign" bit is set. If so, we don't want to make a negative number,
3630 // we want to make a positive number. Emit code to add an offset if the
3631 // sign bit is set.
3632
3633 // Compute whether the sign bit is set by shifting the reg right 31 bits.
3634 unsigned IsNeg = makeAnotherReg(Type::IntTy);
Chris Lattner6e7c47c2005-01-09 01:49:29 +00003635 BuildMI(*BB, IP, X86::SHR32ri, 2, IsNeg).addReg(SrcReg).addImm(31);
Chris Lattner56a31c62004-10-17 08:01:28 +00003636
3637 // Create a CP value that has the offset in one word and 0 in the other.
3638 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
3639 0x4f80000000000000ULL);
3640 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
Chris Lattner6e7c47c2005-01-09 01:49:29 +00003641 BuildMI(*BB, IP, X86::FADD32m, 5, RealDestReg).addReg(DestReg)
Chris Lattner56a31c62004-10-17 08:01:28 +00003642 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
3643
3644 } else if (SrcTy == Type::ULongTy) {
3645 // We need special handling for unsigned 64-bit integer sources. If the
3646 // input number has the "sign bit" set, then we loaded it incorrectly as a
3647 // negative 64-bit number. In this case, add an offset value.
3648
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003649 // Emit a test instruction to see if the dynamic input value was signed.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003650 BuildMI(*BB, IP, X86::TEST32rr, 2).addReg(SrcReg+1).addReg(SrcReg+1);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003651
Chris Lattnerb6bac512004-02-25 06:13:04 +00003652 // If the sign bit is set, get a pointer to an offset, otherwise get a
3653 // pointer to a zero.
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003654 MachineConstantPool *CP = F->getConstantPool();
3655 unsigned Zero = makeAnotherReg(Type::IntTy);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003656 Constant *Null = Constant::getNullValue(Type::UIntTy);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003657 addConstantPoolReference(BuildMI(*BB, IP, X86::LEA32r, 5, Zero),
Chris Lattnerb6bac512004-02-25 06:13:04 +00003658 CP->getConstantPoolIndex(Null));
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003659 unsigned Offset = makeAnotherReg(Type::IntTy);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003660 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003661
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003662 addConstantPoolReference(BuildMI(*BB, IP, X86::LEA32r, 5, Offset),
Chris Lattnerb6bac512004-02-25 06:13:04 +00003663 CP->getConstantPoolIndex(OffsetCst));
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003664 unsigned Addr = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003665 BuildMI(*BB, IP, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003666
3667 // Load the constant for an add. FIXME: this could make an 'fadd' that
3668 // reads directly from memory, but we don't support these yet.
3669 unsigned ConstReg = makeAnotherReg(Type::DoubleTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003670 addDirectMem(BuildMI(*BB, IP, X86::FLD32m, 4, ConstReg), Addr);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003671
Chris Lattneree352852004-02-29 07:22:16 +00003672 BuildMI(*BB, IP, X86::FpADD, 2, RealDestReg)
3673 .addReg(ConstReg).addReg(DestReg);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003674 }
3675
Chris Lattner3e130a22003-01-13 00:32:26 +00003676 return;
3677 }
3678
3679 // Handle casts from floating point to integer now...
3680 if (SrcClass == cFP) {
3681 // Change the floating point control register to use "round towards zero"
3682 // mode when truncating to an integer value.
3683 //
3684 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003685 addFrameReference(BuildMI(*BB, IP, X86::FNSTCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003686
3687 // Load the old value of the high byte of the control word...
3688 unsigned HighPartOfCW = makeAnotherReg(Type::UByteTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003689 addFrameReference(BuildMI(*BB, IP, X86::MOV8rm, 4, HighPartOfCW),
Chris Lattneree352852004-02-29 07:22:16 +00003690 CWFrameIdx, 1);
Chris Lattner3e130a22003-01-13 00:32:26 +00003691
3692 // Set the high part to be round to zero...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003693 addFrameReference(BuildMI(*BB, IP, X86::MOV8mi, 5),
Chris Lattneree352852004-02-29 07:22:16 +00003694 CWFrameIdx, 1).addImm(12);
Chris Lattner3e130a22003-01-13 00:32:26 +00003695
3696 // Reload the modified control word now...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003697 addFrameReference(BuildMI(*BB, IP, X86::FLDCW16m, 4), CWFrameIdx);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003698
Chris Lattner3e130a22003-01-13 00:32:26 +00003699 // Restore the memory image of control word to original value
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003700 addFrameReference(BuildMI(*BB, IP, X86::MOV8mr, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003701 CWFrameIdx, 1).addReg(HighPartOfCW);
Chris Lattner3e130a22003-01-13 00:32:26 +00003702
3703 // We don't have the facilities for directly storing byte sized data to
3704 // memory. Promote it to 16 bits. We also must promote unsigned values to
3705 // larger classes because we only have signed FP stores.
3706 unsigned StoreClass = DestClass;
3707 const Type *StoreTy = DestTy;
3708 if (StoreClass == cByte || DestTy->isUnsigned())
3709 switch (StoreClass) {
3710 case cByte: StoreTy = Type::ShortTy; StoreClass = cShort; break;
3711 case cShort: StoreTy = Type::IntTy; StoreClass = cInt; break;
3712 case cInt: StoreTy = Type::LongTy; StoreClass = cLong; break;
Brian Gaeked4615052003-07-18 20:23:43 +00003713 // The following treatment of cLong may not be perfectly right,
3714 // but it survives chains of casts of the form
3715 // double->ulong->double.
3716 case cLong: StoreTy = Type::LongTy; StoreClass = cLong; break;
Chris Lattner3e130a22003-01-13 00:32:26 +00003717 default: assert(0 && "Unknown store class!");
3718 }
3719
3720 // Spill the integer to memory and reload it from there...
3721 int FrameIdx =
3722 F->getFrameInfo()->CreateStackObject(StoreTy, TM.getTargetData());
3723
3724 static const unsigned Op1[] =
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003725 { 0, X86::FIST16m, X86::FIST32m, 0, X86::FISTP64m };
Chris Lattneree352852004-02-29 07:22:16 +00003726 addFrameReference(BuildMI(*BB, IP, Op1[StoreClass], 5),
3727 FrameIdx).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003728
3729 if (DestClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003730 addFrameReference(BuildMI(*BB, IP, X86::MOV32rm, 4, DestReg), FrameIdx);
3731 addFrameReference(BuildMI(*BB, IP, X86::MOV32rm, 4, DestReg+1),
Chris Lattneree352852004-02-29 07:22:16 +00003732 FrameIdx, 4);
Chris Lattner3e130a22003-01-13 00:32:26 +00003733 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003734 static const unsigned Op2[] = { X86::MOV8rm, X86::MOV16rm, X86::MOV32rm };
Chris Lattneree352852004-02-29 07:22:16 +00003735 addFrameReference(BuildMI(*BB, IP, Op2[DestClass], 4, DestReg), FrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003736 }
3737
3738 // Reload the original control word now...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003739 addFrameReference(BuildMI(*BB, IP, X86::FLDCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003740 return;
3741 }
3742
Brian Gaeked474e9c2002-12-06 10:49:33 +00003743 // Anything we haven't handled already, we can't (yet) handle at all.
Chris Lattnerc53544a2003-05-12 20:16:58 +00003744 assert(0 && "Unhandled cast instruction!");
Chris Lattner548f61d2003-04-23 17:22:12 +00003745 abort();
Brian Gaekefa8d5712002-11-22 11:07:01 +00003746}
Brian Gaekea1719c92002-10-31 23:03:59 +00003747
Chris Lattner73815062003-10-18 05:56:40 +00003748/// visitVANextInst - Implement the va_next instruction...
Chris Lattnereca195e2003-05-08 19:44:13 +00003749///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003750void X86ISel::visitVANextInst(VANextInst &I) {
Chris Lattner73815062003-10-18 05:56:40 +00003751 unsigned VAList = getReg(I.getOperand(0));
Chris Lattnereca195e2003-05-08 19:44:13 +00003752 unsigned DestReg = getReg(I);
3753
Chris Lattnereca195e2003-05-08 19:44:13 +00003754 unsigned Size;
Chris Lattnerf70c22b2004-06-17 18:19:28 +00003755 switch (I.getArgType()->getTypeID()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00003756 default:
3757 std::cerr << I;
Chris Lattner73815062003-10-18 05:56:40 +00003758 assert(0 && "Error: bad type for va_next instruction!");
Chris Lattnereca195e2003-05-08 19:44:13 +00003759 return;
3760 case Type::PointerTyID:
3761 case Type::UIntTyID:
3762 case Type::IntTyID:
3763 Size = 4;
Chris Lattnereca195e2003-05-08 19:44:13 +00003764 break;
3765 case Type::ULongTyID:
3766 case Type::LongTyID:
Chris Lattnereca195e2003-05-08 19:44:13 +00003767 case Type::DoubleTyID:
3768 Size = 8;
Chris Lattnereca195e2003-05-08 19:44:13 +00003769 break;
3770 }
3771
3772 // Increment the VAList pointer...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003773 BuildMI(BB, X86::ADD32ri, 2, DestReg).addReg(VAList).addImm(Size);
Chris Lattner73815062003-10-18 05:56:40 +00003774}
Chris Lattnereca195e2003-05-08 19:44:13 +00003775
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003776void X86ISel::visitVAArgInst(VAArgInst &I) {
Chris Lattner73815062003-10-18 05:56:40 +00003777 unsigned VAList = getReg(I.getOperand(0));
3778 unsigned DestReg = getReg(I);
3779
Chris Lattnerf70c22b2004-06-17 18:19:28 +00003780 switch (I.getType()->getTypeID()) {
Chris Lattner73815062003-10-18 05:56:40 +00003781 default:
3782 std::cerr << I;
3783 assert(0 && "Error: bad type for va_next instruction!");
3784 return;
3785 case Type::PointerTyID:
3786 case Type::UIntTyID:
3787 case Type::IntTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003788 addDirectMem(BuildMI(BB, X86::MOV32rm, 4, DestReg), VAList);
Chris Lattner73815062003-10-18 05:56:40 +00003789 break;
3790 case Type::ULongTyID:
3791 case Type::LongTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003792 addDirectMem(BuildMI(BB, X86::MOV32rm, 4, DestReg), VAList);
3793 addRegOffset(BuildMI(BB, X86::MOV32rm, 4, DestReg+1), VAList, 4);
Chris Lattner73815062003-10-18 05:56:40 +00003794 break;
3795 case Type::DoubleTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003796 addDirectMem(BuildMI(BB, X86::FLD64m, 4, DestReg), VAList);
Chris Lattner73815062003-10-18 05:56:40 +00003797 break;
3798 }
Chris Lattnereca195e2003-05-08 19:44:13 +00003799}
3800
Misha Brukman538607f2004-03-01 23:53:11 +00003801/// visitGetElementPtrInst - instruction-select GEP instructions
3802///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003803void X86ISel::visitGetElementPtrInst(GetElementPtrInst &I) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00003804 // If this GEP instruction will be folded into all of its users, we don't need
3805 // to explicitly calculate it!
Reid Spencerfc989e12004-08-30 00:13:26 +00003806 X86AddressMode AM;
3807 if (isGEPFoldable(0, I.getOperand(0), I.op_begin()+1, I.op_end(), AM)) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00003808 // Check all of the users of the instruction to see if they are loads and
3809 // stores.
3810 bool AllWillFold = true;
3811 for (Value::use_iterator UI = I.use_begin(), E = I.use_end(); UI != E; ++UI)
3812 if (cast<Instruction>(*UI)->getOpcode() != Instruction::Load)
3813 if (cast<Instruction>(*UI)->getOpcode() != Instruction::Store ||
3814 cast<Instruction>(*UI)->getOperand(0) == &I) {
3815 AllWillFold = false;
3816 break;
3817 }
3818
3819 // If the instruction is foldable, and will be folded into all users, don't
3820 // emit it!
3821 if (AllWillFold) return;
3822 }
3823
Chris Lattner3e130a22003-01-13 00:32:26 +00003824 unsigned outputReg = getReg(I);
Chris Lattner827832c2004-02-22 17:05:38 +00003825 emitGEPOperation(BB, BB->end(), I.getOperand(0),
Brian Gaeke68b1edc2002-12-16 04:23:29 +00003826 I.op_begin()+1, I.op_end(), outputReg);
Chris Lattnerc0812d82002-12-13 06:56:29 +00003827}
3828
Chris Lattner985fe3d2004-02-25 03:45:50 +00003829/// getGEPIndex - Inspect the getelementptr operands specified with GEPOps and
3830/// GEPTypes (the derived types being stepped through at each level). On return
3831/// from this function, if some indexes of the instruction are representable as
3832/// an X86 lea instruction, the machine operands are put into the Ops
3833/// instruction and the consumed indexes are poped from the GEPOps/GEPTypes
3834/// lists. Otherwise, GEPOps.size() is returned. If this returns a an
3835/// addressing mode that only partially consumes the input, the BaseReg input of
3836/// the addressing mode must be left free.
3837///
3838/// Note that there is one fewer entry in GEPTypes than there is in GEPOps.
3839///
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003840void X86ISel::getGEPIndex(MachineBasicBlock *MBB,
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003841 MachineBasicBlock::iterator IP,
3842 std::vector<Value*> &GEPOps,
3843 std::vector<const Type*> &GEPTypes,
3844 X86AddressMode &AM) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00003845 const TargetData &TD = TM.getTargetData();
3846
Chris Lattner985fe3d2004-02-25 03:45:50 +00003847 // Clear out the state we are working with...
Reid Spencerfc989e12004-08-30 00:13:26 +00003848 AM.BaseType = X86AddressMode::RegBase;
3849 AM.Base.Reg = 0; // No base register
3850 AM.Scale = 1; // Unit scale
3851 AM.IndexReg = 0; // No index register
3852 AM.Disp = 0; // No displacement
Chris Lattnerb6bac512004-02-25 06:13:04 +00003853
Chris Lattner985fe3d2004-02-25 03:45:50 +00003854 // While there are GEP indexes that can be folded into the current address,
3855 // keep processing them.
3856 while (!GEPTypes.empty()) {
3857 if (const StructType *StTy = dyn_cast<StructType>(GEPTypes.back())) {
3858 // It's a struct access. CUI is the index into the structure,
3859 // which names the field. This index must have unsigned type.
3860 const ConstantUInt *CUI = cast<ConstantUInt>(GEPOps.back());
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003861
Chris Lattner985fe3d2004-02-25 03:45:50 +00003862 // Use the TargetData structure to pick out what the layout of the
3863 // structure is in memory. Since the structure index must be constant, we
3864 // can get its value and use it to find the right byte offset from the
3865 // StructLayout class's list of structure member offsets.
Reid Spencerfc989e12004-08-30 00:13:26 +00003866 AM.Disp += TD.getStructLayout(StTy)->MemberOffsets[CUI->getValue()];
Chris Lattner985fe3d2004-02-25 03:45:50 +00003867 GEPOps.pop_back(); // Consume a GEP operand
3868 GEPTypes.pop_back();
3869 } else {
3870 // It's an array or pointer access: [ArraySize x ElementType].
3871 const SequentialType *SqTy = cast<SequentialType>(GEPTypes.back());
3872 Value *idx = GEPOps.back();
3873
3874 // idx is the index into the array. Unlike with structure
3875 // indices, we may not know its actual value at code-generation
3876 // time.
Chris Lattner985fe3d2004-02-25 03:45:50 +00003877
3878 // If idx is a constant, fold it into the offset.
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003879 unsigned TypeSize = TD.getTypeSize(SqTy->getElementType());
Chris Lattner985fe3d2004-02-25 03:45:50 +00003880 if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(idx)) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003881 AM.Disp += TypeSize*CSI->getValue();
Chris Lattner28977af2004-04-05 01:30:19 +00003882 } else if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(idx)) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003883 AM.Disp += TypeSize*CUI->getValue();
Chris Lattner985fe3d2004-02-25 03:45:50 +00003884 } else {
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003885 // If the index reg is already taken, we can't handle this index.
Reid Spencerfc989e12004-08-30 00:13:26 +00003886 if (AM.IndexReg) return;
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003887
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003888 // If this is a size that we can handle, then add the index as
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003889 switch (TypeSize) {
3890 case 1: case 2: case 4: case 8:
3891 // These are all acceptable scales on X86.
Reid Spencerfc989e12004-08-30 00:13:26 +00003892 AM.Scale = TypeSize;
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003893 break;
3894 default:
3895 // Otherwise, we can't handle this scale
3896 return;
3897 }
3898
3899 if (CastInst *CI = dyn_cast<CastInst>(idx))
3900 if (CI->getOperand(0)->getType() == Type::IntTy ||
3901 CI->getOperand(0)->getType() == Type::UIntTy)
3902 idx = CI->getOperand(0);
3903
Reid Spencerfc989e12004-08-30 00:13:26 +00003904 AM.IndexReg = MBB ? getReg(idx, MBB, IP) : 1;
Chris Lattner985fe3d2004-02-25 03:45:50 +00003905 }
3906
3907 GEPOps.pop_back(); // Consume a GEP operand
3908 GEPTypes.pop_back();
3909 }
3910 }
Chris Lattnerb6bac512004-02-25 06:13:04 +00003911
Chris Lattnerdf040972004-05-23 21:23:12 +00003912 // GEPTypes is empty, which means we have a single operand left. Set it as
3913 // the base register.
Chris Lattnerb6bac512004-02-25 06:13:04 +00003914 //
Reid Spencerfc989e12004-08-30 00:13:26 +00003915 assert(AM.Base.Reg == 0);
Chris Lattnerdf040972004-05-23 21:23:12 +00003916
Reid Spencerfc989e12004-08-30 00:13:26 +00003917 if (AllocaInst *AI = dyn_castFixedAlloca(GEPOps.back())) {
3918 AM.BaseType = X86AddressMode::FrameIndexBase;
3919 AM.Base.FrameIndex = getFixedSizedAllocaFI(AI);
Chris Lattnerdf040972004-05-23 21:23:12 +00003920 GEPOps.pop_back();
3921 return;
Reid Spencerfc989e12004-08-30 00:13:26 +00003922 }
3923
Chris Lattner358a9022004-10-15 05:05:29 +00003924 if (GlobalValue *GV = dyn_cast<GlobalValue>(GEPOps.back())) {
3925 AM.GV = GV;
3926 GEPOps.pop_back();
3927 return;
Chris Lattnerdf040972004-05-23 21:23:12 +00003928 }
Chris Lattnerdf040972004-05-23 21:23:12 +00003929
Reid Spencerfc989e12004-08-30 00:13:26 +00003930 AM.Base.Reg = MBB ? getReg(GEPOps[0], MBB, IP) : 1;
Chris Lattnerb6bac512004-02-25 06:13:04 +00003931 GEPOps.pop_back(); // Consume the last GEP operand
Chris Lattner985fe3d2004-02-25 03:45:50 +00003932}
3933
3934
Chris Lattnerb6bac512004-02-25 06:13:04 +00003935/// isGEPFoldable - Return true if the specified GEP can be completely
3936/// folded into the addressing mode of a load/store or lea instruction.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003937bool X86ISel::isGEPFoldable(MachineBasicBlock *MBB,
3938 Value *Src, User::op_iterator IdxBegin,
3939 User::op_iterator IdxEnd, X86AddressMode &AM) {
Chris Lattner7ca04092004-02-22 17:35:42 +00003940
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003941 std::vector<Value*> GEPOps;
3942 GEPOps.resize(IdxEnd-IdxBegin+1);
3943 GEPOps[0] = Src;
3944 std::copy(IdxBegin, IdxEnd, GEPOps.begin()+1);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003945
Chris Lattnerdf040972004-05-23 21:23:12 +00003946 std::vector<const Type*>
3947 GEPTypes(gep_type_begin(Src->getType(), IdxBegin, IdxEnd),
3948 gep_type_end(Src->getType(), IdxBegin, IdxEnd));
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003949
Chris Lattnerb6bac512004-02-25 06:13:04 +00003950 MachineBasicBlock::iterator IP;
3951 if (MBB) IP = MBB->end();
Reid Spencerfc989e12004-08-30 00:13:26 +00003952 getGEPIndex(MBB, IP, GEPOps, GEPTypes, AM);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003953
3954 // We can fold it away iff the getGEPIndex call eliminated all operands.
3955 return GEPOps.empty();
3956}
3957
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003958void X86ISel::emitGEPOperation(MachineBasicBlock *MBB,
3959 MachineBasicBlock::iterator IP,
3960 Value *Src, User::op_iterator IdxBegin,
3961 User::op_iterator IdxEnd, unsigned TargetReg) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00003962 const TargetData &TD = TM.getTargetData();
Chris Lattnerb6bac512004-02-25 06:13:04 +00003963
Chris Lattnerd2995df2004-07-15 00:58:53 +00003964 // If this is a getelementptr null, with all constant integer indices, just
3965 // replace it with TargetReg = 42.
3966 if (isa<ConstantPointerNull>(Src)) {
3967 User::op_iterator I = IdxBegin;
3968 for (; I != IdxEnd; ++I)
3969 if (!isa<ConstantInt>(*I))
3970 break;
3971 if (I == IdxEnd) { // All constant indices
3972 unsigned Offset = TD.getIndexedOffset(Src->getType(),
3973 std::vector<Value*>(IdxBegin, IdxEnd));
3974 BuildMI(*MBB, IP, X86::MOV32ri, 1, TargetReg).addImm(Offset);
3975 return;
3976 }
3977 }
3978
Chris Lattnerb6bac512004-02-25 06:13:04 +00003979 std::vector<Value*> GEPOps;
3980 GEPOps.resize(IdxEnd-IdxBegin+1);
3981 GEPOps[0] = Src;
3982 std::copy(IdxBegin, IdxEnd, GEPOps.begin()+1);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003983
Chris Lattnerb6bac512004-02-25 06:13:04 +00003984 std::vector<const Type*> GEPTypes;
3985 GEPTypes.assign(gep_type_begin(Src->getType(), IdxBegin, IdxEnd),
3986 gep_type_end(Src->getType(), IdxBegin, IdxEnd));
Chris Lattner985fe3d2004-02-25 03:45:50 +00003987
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003988 // Keep emitting instructions until we consume the entire GEP instruction.
3989 while (!GEPOps.empty()) {
3990 unsigned OldSize = GEPOps.size();
Reid Spencerfc989e12004-08-30 00:13:26 +00003991 X86AddressMode AM;
3992 getGEPIndex(MBB, IP, GEPOps, GEPTypes, AM);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003993
Chris Lattner985fe3d2004-02-25 03:45:50 +00003994 if (GEPOps.size() != OldSize) {
3995 // getGEPIndex consumed some of the input. Build an LEA instruction here.
Chris Lattnerb6bac512004-02-25 06:13:04 +00003996 unsigned NextTarget = 0;
3997 if (!GEPOps.empty()) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003998 assert(AM.Base.Reg == 0 &&
Chris Lattnerb6bac512004-02-25 06:13:04 +00003999 "getGEPIndex should have left the base register open for chaining!");
Reid Spencerfc989e12004-08-30 00:13:26 +00004000 NextTarget = AM.Base.Reg = makeAnotherReg(Type::UIntTy);
Chris Lattner985fe3d2004-02-25 03:45:50 +00004001 }
Chris Lattnerb6bac512004-02-25 06:13:04 +00004002
Reid Spencerfc989e12004-08-30 00:13:26 +00004003 if (AM.BaseType == X86AddressMode::RegBase &&
Chris Lattner358a9022004-10-15 05:05:29 +00004004 AM.IndexReg == 0 && AM.Disp == 0 && !AM.GV)
Reid Spencerfc989e12004-08-30 00:13:26 +00004005 BuildMI(*MBB, IP, X86::MOV32rr, 1, TargetReg).addReg(AM.Base.Reg);
Chris Lattner358a9022004-10-15 05:05:29 +00004006 else if (AM.BaseType == X86AddressMode::RegBase && AM.Base.Reg == 0 &&
4007 AM.IndexReg == 0 && AM.Disp == 0)
4008 BuildMI(*MBB, IP, X86::MOV32ri, 1, TargetReg).addGlobalAddress(AM.GV);
Chris Lattnerb6bac512004-02-25 06:13:04 +00004009 else
Reid Spencerfc989e12004-08-30 00:13:26 +00004010 addFullAddress(BuildMI(*MBB, IP, X86::LEA32r, 5, TargetReg), AM);
Chris Lattnerb6bac512004-02-25 06:13:04 +00004011 --IP;
4012 TargetReg = NextTarget;
Chris Lattner985fe3d2004-02-25 03:45:50 +00004013 } else if (GEPTypes.empty()) {
Chris Lattner3f1e8e72004-02-22 07:04:00 +00004014 // The getGEPIndex operation didn't want to build an LEA. Check to see if
4015 // all operands are consumed but the base pointer. If so, just load it
4016 // into the register.
Chris Lattner7ca04092004-02-22 17:35:42 +00004017 if (GlobalValue *GV = dyn_cast<GlobalValue>(GEPOps[0])) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00004018 BuildMI(*MBB, IP, X86::MOV32ri, 1, TargetReg).addGlobalAddress(GV);
Chris Lattner7ca04092004-02-22 17:35:42 +00004019 } else {
4020 unsigned BaseReg = getReg(GEPOps[0], MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00004021 BuildMI(*MBB, IP, X86::MOV32rr, 1, TargetReg).addReg(BaseReg);
Chris Lattner7ca04092004-02-22 17:35:42 +00004022 }
4023 break; // we are now done
Chris Lattnerb6bac512004-02-25 06:13:04 +00004024
Chris Lattner3f1e8e72004-02-22 07:04:00 +00004025 } else {
Brian Gaeke20244b72002-12-12 15:33:40 +00004026 // It's an array or pointer access: [ArraySize x ElementType].
Chris Lattner3f1e8e72004-02-22 07:04:00 +00004027 const SequentialType *SqTy = cast<SequentialType>(GEPTypes.back());
4028 Value *idx = GEPOps.back();
4029 GEPOps.pop_back(); // Consume a GEP operand
4030 GEPTypes.pop_back();
Chris Lattner8a307e82002-12-16 19:32:50 +00004031
Chris Lattner28977af2004-04-05 01:30:19 +00004032 // Many GEP instructions use a [cast (int/uint) to LongTy] as their
Chris Lattnerf5854472003-06-21 16:01:24 +00004033 // operand on X86. Handle this case directly now...
4034 if (CastInst *CI = dyn_cast<CastInst>(idx))
4035 if (CI->getOperand(0)->getType() == Type::IntTy ||
4036 CI->getOperand(0)->getType() == Type::UIntTy)
4037 idx = CI->getOperand(0);
4038
Chris Lattner3e130a22003-01-13 00:32:26 +00004039 // We want to add BaseReg to(idxReg * sizeof ElementType). First, we
Chris Lattner8a307e82002-12-16 19:32:50 +00004040 // must find the size of the pointed-to type (Not coincidentally, the next
4041 // type is the type of the elements in the array).
Chris Lattner3f1e8e72004-02-22 07:04:00 +00004042 const Type *ElTy = SqTy->getElementType();
4043 unsigned elementSize = TD.getTypeSize(ElTy);
Chris Lattner8a307e82002-12-16 19:32:50 +00004044
4045 // If idxReg is a constant, we don't need to perform the multiply!
Chris Lattner28977af2004-04-05 01:30:19 +00004046 if (ConstantInt *CSI = dyn_cast<ConstantInt>(idx)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00004047 if (!CSI->isNullValue()) {
Chris Lattner28977af2004-04-05 01:30:19 +00004048 unsigned Offset = elementSize*CSI->getRawValue();
Chris Lattner3f1e8e72004-02-22 07:04:00 +00004049 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00004050 BuildMI(*MBB, IP, X86::ADD32ri, 2, TargetReg)
Chris Lattneree352852004-02-29 07:22:16 +00004051 .addReg(Reg).addImm(Offset);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00004052 --IP; // Insert the next instruction before this one.
4053 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00004054 }
4055 } else if (elementSize == 1) {
4056 // If the element size is 1, we don't have to multiply, just add
4057 unsigned idxReg = getReg(idx, MBB, IP);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00004058 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00004059 BuildMI(*MBB, IP, X86::ADD32rr, 2,TargetReg).addReg(Reg).addReg(idxReg);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00004060 --IP; // Insert the next instruction before this one.
4061 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00004062 } else {
4063 unsigned idxReg = getReg(idx, MBB, IP);
4064 unsigned OffsetReg = makeAnotherReg(Type::UIntTy);
Chris Lattnerb2acc512003-10-19 21:09:10 +00004065
Chris Lattner3f1e8e72004-02-22 07:04:00 +00004066 // Make sure we can back the iterator up to point to the first
4067 // instruction emitted.
4068 MachineBasicBlock::iterator BeforeIt = IP;
4069 if (IP == MBB->begin())
4070 BeforeIt = MBB->end();
4071 else
4072 --BeforeIt;
Chris Lattnerb2acc512003-10-19 21:09:10 +00004073 doMultiplyConst(MBB, IP, OffsetReg, Type::IntTy, idxReg, elementSize);
4074
Chris Lattner8a307e82002-12-16 19:32:50 +00004075 // Emit an ADD to add OffsetReg to the basePtr.
Chris Lattner3f1e8e72004-02-22 07:04:00 +00004076 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00004077 BuildMI(*MBB, IP, X86::ADD32rr, 2, TargetReg)
Chris Lattneree352852004-02-29 07:22:16 +00004078 .addReg(Reg).addReg(OffsetReg);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00004079
4080 // Step to the first instruction of the multiply.
4081 if (BeforeIt == MBB->end())
4082 IP = MBB->begin();
4083 else
4084 IP = ++BeforeIt;
4085
4086 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00004087 }
Brian Gaeke20244b72002-12-12 15:33:40 +00004088 }
Brian Gaeke20244b72002-12-12 15:33:40 +00004089 }
Brian Gaeke20244b72002-12-12 15:33:40 +00004090}
4091
Chris Lattner065faeb2002-12-28 20:24:02 +00004092/// visitAllocaInst - If this is a fixed size alloca, allocate space from the
4093/// frame manager, otherwise do it the hard way.
4094///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00004095void X86ISel::visitAllocaInst(AllocaInst &I) {
Chris Lattner9f1b5312004-05-13 15:12:43 +00004096 // If this is a fixed size alloca in the entry block for the function, we
4097 // statically stack allocate the space, so we don't need to do anything here.
4098 //
Chris Lattnercb2fd552004-05-13 07:40:27 +00004099 if (dyn_castFixedAlloca(&I)) return;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004100
Brian Gaekee48ec012002-12-13 06:46:31 +00004101 // Find the data size of the alloca inst's getAllocatedType.
Chris Lattner065faeb2002-12-28 20:24:02 +00004102 const Type *Ty = I.getAllocatedType();
4103 unsigned TySize = TM.getTargetData().getTypeSize(Ty);
4104
Chris Lattner065faeb2002-12-28 20:24:02 +00004105 // Create a register to hold the temporary result of multiplying the type size
4106 // constant by the variable amount.
4107 unsigned TotalSizeReg = makeAnotherReg(Type::UIntTy);
4108 unsigned SrcReg1 = getReg(I.getArraySize());
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004109
Chris Lattner065faeb2002-12-28 20:24:02 +00004110 // TotalSizeReg = mul <numelements>, <TypeSize>
4111 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattnerb2acc512003-10-19 21:09:10 +00004112 doMultiplyConst(BB, MBBI, TotalSizeReg, Type::UIntTy, SrcReg1, TySize);
Chris Lattner065faeb2002-12-28 20:24:02 +00004113
4114 // AddedSize = add <TotalSizeReg>, 15
4115 unsigned AddedSizeReg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00004116 BuildMI(BB, X86::ADD32ri, 2, AddedSizeReg).addReg(TotalSizeReg).addImm(15);
Chris Lattner065faeb2002-12-28 20:24:02 +00004117
4118 // AlignedSize = and <AddedSize>, ~15
4119 unsigned AlignedSize = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00004120 BuildMI(BB, X86::AND32ri, 2, AlignedSize).addReg(AddedSizeReg).addImm(~15);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004121
Brian Gaekee48ec012002-12-13 06:46:31 +00004122 // Subtract size from stack pointer, thereby allocating some space.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00004123 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(AlignedSize);
Chris Lattner065faeb2002-12-28 20:24:02 +00004124
Brian Gaekee48ec012002-12-13 06:46:31 +00004125 // Put a pointer to the space into the result register, by copying
4126 // the stack pointer.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00004127 BuildMI(BB, X86::MOV32rr, 1, getReg(I)).addReg(X86::ESP);
Chris Lattner065faeb2002-12-28 20:24:02 +00004128
Misha Brukman48196b32003-05-03 02:18:17 +00004129 // Inform the Frame Information that we have just allocated a variable-sized
Chris Lattner065faeb2002-12-28 20:24:02 +00004130 // object.
4131 F->getFrameInfo()->CreateVariableSizedObject();
Brian Gaeke20244b72002-12-12 15:33:40 +00004132}
Chris Lattner3e130a22003-01-13 00:32:26 +00004133
4134/// visitMallocInst - Malloc instructions are code generated into direct calls
4135/// to the library malloc.
4136///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00004137void X86ISel::visitMallocInst(MallocInst &I) {
Chris Lattner3e130a22003-01-13 00:32:26 +00004138 unsigned AllocSize = TM.getTargetData().getTypeSize(I.getAllocatedType());
4139 unsigned Arg;
4140
4141 if (ConstantUInt *C = dyn_cast<ConstantUInt>(I.getOperand(0))) {
4142 Arg = getReg(ConstantUInt::get(Type::UIntTy, C->getValue() * AllocSize));
4143 } else {
4144 Arg = makeAnotherReg(Type::UIntTy);
Chris Lattnerb2acc512003-10-19 21:09:10 +00004145 unsigned Op0Reg = getReg(I.getOperand(0));
Chris Lattner3e130a22003-01-13 00:32:26 +00004146 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattnerb2acc512003-10-19 21:09:10 +00004147 doMultiplyConst(BB, MBBI, Arg, Type::UIntTy, Op0Reg, AllocSize);
Chris Lattner3e130a22003-01-13 00:32:26 +00004148 }
4149
4150 std::vector<ValueRecord> Args;
4151 Args.push_back(ValueRecord(Arg, Type::UIntTy));
4152 MachineInstr *TheCall = BuildMI(X86::CALLpcrel32,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00004153 1).addExternalSymbol("malloc", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00004154 doCall(ValueRecord(getReg(I), I.getType()), TheCall, Args);
4155}
4156
4157
4158/// visitFreeInst - Free instructions are code gen'd to call the free libc
4159/// function.
4160///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00004161void X86ISel::visitFreeInst(FreeInst &I) {
Chris Lattner3e130a22003-01-13 00:32:26 +00004162 std::vector<ValueRecord> Args;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00004163 Args.push_back(ValueRecord(I.getOperand(0)));
Chris Lattner3e130a22003-01-13 00:32:26 +00004164 MachineInstr *TheCall = BuildMI(X86::CALLpcrel32,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00004165 1).addExternalSymbol("free", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00004166 doCall(ValueRecord(0, Type::VoidTy), TheCall, Args);
4167}
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004168
Chris Lattnerd281de22003-07-26 23:49:58 +00004169/// createX86SimpleInstructionSelector - This pass converts an LLVM function
Chris Lattnerb4f68ed2002-10-29 22:37:54 +00004170/// into a machine code representation is a very simple peep-hole fashion. The
Chris Lattner72614082002-10-25 22:55:53 +00004171/// generated code sucks but the implementation is nice and simple.
4172///
Chris Lattnerf70e0c22003-12-28 21:23:38 +00004173FunctionPass *llvm::createX86SimpleInstructionSelector(TargetMachine &TM) {
Misha Brukmaneae1bf12004-09-21 18:21:21 +00004174 return new X86ISel(TM);
Chris Lattner72614082002-10-25 22:55:53 +00004175}