blob: 926492e31f5284bf1138702be37725817ce1257b [file] [log] [blame]
Misha Brukman91b5ca82004-07-26 18:45:48 +00001//===-- X86ISelSimple.cpp - A simple instruction selector for x86 ---------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner72614082002-10-25 22:55:53 +00009//
Chris Lattner3e130a22003-01-13 00:32:26 +000010// This file defines a simple peephole instruction selector for the x86 target
Chris Lattner72614082002-10-25 22:55:53 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "X86.h"
Chris Lattner6fc3c522002-11-17 21:11:55 +000015#include "X86InstrBuilder.h"
Misha Brukmanc8893fc2003-10-23 16:22:08 +000016#include "X86InstrInfo.h"
17#include "llvm/Constants.h"
18#include "llvm/DerivedTypes.h"
Chris Lattner72614082002-10-25 22:55:53 +000019#include "llvm/Function.h"
Chris Lattner67580ed2003-05-13 20:21:19 +000020#include "llvm/Instructions.h"
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 Lattner3f1e8e72004-02-22 07:04:00 +000029#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattner67580ed2003-05-13 20:21:19 +000030#include "llvm/Support/InstVisitor.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000031#include "llvm/ADT/Statistic.h"
Chris Lattner44827152003-12-28 09:47:19 +000032using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000033
Chris Lattner986618e2004-02-22 19:47:26 +000034namespace {
35 Statistic<>
36 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
Chris Lattner427aeb42004-04-11 19:21:59 +000037
38 /// TypeClass - Used by the X86 backend to group LLVM types by their basic X86
39 /// Representation.
40 ///
41 enum TypeClass {
42 cByte, cShort, cInt, cFP, cLong
43 };
44}
45
46/// getClass - Turn a primitive type into a "class" number which is based on the
47/// size of the type, and whether or not it is floating point.
48///
49static inline TypeClass getClass(const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000050 switch (Ty->getTypeID()) {
Chris Lattner427aeb42004-04-11 19:21:59 +000051 case Type::SByteTyID:
52 case Type::UByteTyID: return cByte; // Byte operands are class #0
53 case Type::ShortTyID:
54 case Type::UShortTyID: return cShort; // Short operands are class #1
55 case Type::IntTyID:
56 case Type::UIntTyID:
57 case Type::PointerTyID: return cInt; // Int's and pointers are class #2
58
59 case Type::FloatTyID:
60 case Type::DoubleTyID: return cFP; // Floating Point is #3
61
62 case Type::LongTyID:
63 case Type::ULongTyID: return cLong; // Longs are class #4
64 default:
65 assert(0 && "Invalid type to getClass!");
66 return cByte; // not reached
67 }
68}
69
70// getClassB - Just like getClass, but treat boolean values as bytes.
71static inline TypeClass getClassB(const Type *Ty) {
72 if (Ty == Type::BoolTy) return cByte;
73 return getClass(Ty);
Chris Lattner986618e2004-02-22 19:47:26 +000074}
Chris Lattnercf93cdd2004-01-30 22:13:44 +000075
Chris Lattner72614082002-10-25 22:55:53 +000076namespace {
Misha Brukmaneae1bf12004-09-21 18:21:21 +000077 struct X86ISel : public FunctionPass, InstVisitor<X86ISel> {
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000078 TargetMachine &TM;
Chris Lattnereca195e2003-05-08 19:44:13 +000079 MachineFunction *F; // The function we are compiling into
80 MachineBasicBlock *BB; // The current MBB we are compiling
81 int VarArgsFrameIndex; // FrameIndex for start of varargs area
Chris Lattner0e5b79c2004-02-15 01:04:03 +000082 int ReturnAddressIndex; // FrameIndex for the return address
Chris Lattner72614082002-10-25 22:55:53 +000083
Chris Lattner72614082002-10-25 22:55:53 +000084 std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs
85
Chris Lattner333b2fa2002-12-13 10:09:43 +000086 // MBBMap - Mapping between LLVM BB -> Machine BB
87 std::map<const BasicBlock*, MachineBasicBlock*> MBBMap;
88
Chris Lattnercb2fd552004-05-13 07:40:27 +000089 // AllocaMap - Mapping from fixed sized alloca instructions to the
90 // FrameIndex for the alloca.
91 std::map<AllocaInst*, unsigned> AllocaMap;
92
Misha Brukmaneae1bf12004-09-21 18:21:21 +000093 X86ISel(TargetMachine &tm) : TM(tm), F(0), BB(0) {}
Chris Lattner72614082002-10-25 22:55:53 +000094
95 /// runOnFunction - Top level implementation of instruction selection for
96 /// the entire function.
97 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000098 bool runOnFunction(Function &Fn) {
Chris Lattner44827152003-12-28 09:47:19 +000099 // First pass over the function, lower any unknown intrinsic functions
100 // with the IntrinsicLowering class.
101 LowerUnknownIntrinsicFunctionCalls(Fn);
102
Chris Lattner36b36032002-10-29 23:40:58 +0000103 F = &MachineFunction::construct(&Fn, TM);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000104
Chris Lattner065faeb2002-12-28 20:24:02 +0000105 // Create all of the machine basic blocks for the function...
Chris Lattner333b2fa2002-12-13 10:09:43 +0000106 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
107 F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I));
108
Chris Lattner14aa7fe2002-12-16 22:54:46 +0000109 BB = &F->front();
Chris Lattnerdbd73722003-05-06 21:32:22 +0000110
Chris Lattner0e5b79c2004-02-15 01:04:03 +0000111 // Set up a frame object for the return address. This is used by the
112 // llvm.returnaddress & llvm.frameaddress intrinisics.
113 ReturnAddressIndex = F->getFrameInfo()->CreateFixedObject(4, -4);
114
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 Lattner333b2fa2002-12-13 10:09:43 +0000118 // Instruction select everything except PHI nodes
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000119 visit(Fn);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000120
121 // Select the PHI nodes
122 SelectPHINodes();
123
Chris Lattner986618e2004-02-22 19:47:26 +0000124 // Insert the FP_REG_KILL instructions into blocks that need them.
125 InsertFPRegKills();
126
Chris Lattner72614082002-10-25 22:55:53 +0000127 RegMap.clear();
Chris Lattner333b2fa2002-12-13 10:09:43 +0000128 MBBMap.clear();
Chris Lattnercb2fd552004-05-13 07:40:27 +0000129 AllocaMap.clear();
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000130 F = 0;
Chris Lattner2a865b02003-07-26 23:05:37 +0000131 // We always build a machine code representation for the function
132 return true;
Chris Lattner72614082002-10-25 22:55:53 +0000133 }
134
Chris Lattnerf0eb7be2002-12-15 21:13:40 +0000135 virtual const char *getPassName() const {
136 return "X86 Simple Instruction Selection";
137 }
138
Chris Lattner72614082002-10-25 22:55:53 +0000139 /// visitBasicBlock - This method is called when we are visiting a new basic
Chris Lattner33f53b52002-10-29 20:48:56 +0000140 /// block. This simply creates a new MachineBasicBlock to emit code into
141 /// and adds it to the current MachineFunction. Subsequent visit* for
142 /// instructions will be invoked for all instructions in the basic block.
Chris Lattner72614082002-10-25 22:55:53 +0000143 ///
144 void visitBasicBlock(BasicBlock &LLVM_BB) {
Chris Lattner333b2fa2002-12-13 10:09:43 +0000145 BB = MBBMap[&LLVM_BB];
Chris Lattner72614082002-10-25 22:55:53 +0000146 }
147
Chris Lattner44827152003-12-28 09:47:19 +0000148 /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
149 /// function, lowering any calls to unknown intrinsic functions into the
150 /// equivalent LLVM code.
Misha Brukman538607f2004-03-01 23:53:11 +0000151 ///
Chris Lattner44827152003-12-28 09:47:19 +0000152 void LowerUnknownIntrinsicFunctionCalls(Function &F);
153
Chris Lattner065faeb2002-12-28 20:24:02 +0000154 /// LoadArgumentsToVirtualRegs - Load all of the arguments to this function
155 /// from the stack into virtual registers.
156 ///
157 void LoadArgumentsToVirtualRegs(Function &F);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000158
159 /// SelectPHINodes - Insert machine code to generate phis. This is tricky
160 /// because we have to generate our sources into the source basic blocks,
161 /// not the current one.
162 ///
163 void SelectPHINodes();
164
Chris Lattner986618e2004-02-22 19:47:26 +0000165 /// InsertFPRegKills - Insert FP_REG_KILL instructions into basic blocks
166 /// that need them. This only occurs due to the floating point stackifier
167 /// not being aggressive enough to handle arbitrary global stackification.
168 ///
169 void InsertFPRegKills();
170
Chris Lattner72614082002-10-25 22:55:53 +0000171 // Visitation methods for various instructions. These methods simply emit
172 // fixed X86 code for each instruction.
173 //
Brian Gaekefa8d5712002-11-22 11:07:01 +0000174
175 // Control flow operators
Chris Lattner72614082002-10-25 22:55:53 +0000176 void visitReturnInst(ReturnInst &RI);
Chris Lattner2df035b2002-11-02 19:27:56 +0000177 void visitBranchInst(BranchInst &BI);
Chris Lattner30483b02004-10-16 18:13:05 +0000178 void visitUnreachableInst(UnreachableInst &UI) {}
Chris Lattner3e130a22003-01-13 00:32:26 +0000179
180 struct ValueRecord {
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000181 Value *Val;
Chris Lattner3e130a22003-01-13 00:32:26 +0000182 unsigned Reg;
183 const Type *Ty;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000184 ValueRecord(unsigned R, const Type *T) : Val(0), Reg(R), Ty(T) {}
185 ValueRecord(Value *V) : Val(V), Reg(0), Ty(V->getType()) {}
Chris Lattner3e130a22003-01-13 00:32:26 +0000186 };
187 void doCall(const ValueRecord &Ret, MachineInstr *CallMI,
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000188 const std::vector<ValueRecord> &Args);
Brian Gaekefa8d5712002-11-22 11:07:01 +0000189 void visitCallInst(CallInst &I);
Brian Gaeked0fde302003-11-11 22:41:34 +0000190 void visitIntrinsicCall(Intrinsic::ID ID, CallInst &I);
Chris Lattnere2954c82002-11-02 20:04:26 +0000191
192 // Arithmetic operators
Chris Lattnerf01729e2002-11-02 20:54:46 +0000193 void visitSimpleBinary(BinaryOperator &B, unsigned OpcodeClass);
Chris Lattner68aad932002-11-02 20:13:22 +0000194 void visitAdd(BinaryOperator &B) { visitSimpleBinary(B, 0); }
195 void visitSub(BinaryOperator &B) { visitSimpleBinary(B, 1); }
Chris Lattnerca9671d2002-11-02 20:28:58 +0000196 void visitMul(BinaryOperator &B);
Chris Lattnere2954c82002-11-02 20:04:26 +0000197
Chris Lattnerf01729e2002-11-02 20:54:46 +0000198 void visitDiv(BinaryOperator &B) { visitDivRem(B); }
199 void visitRem(BinaryOperator &B) { visitDivRem(B); }
200 void visitDivRem(BinaryOperator &B);
201
Chris Lattnere2954c82002-11-02 20:04:26 +0000202 // Bitwise operators
Chris Lattner68aad932002-11-02 20:13:22 +0000203 void visitAnd(BinaryOperator &B) { visitSimpleBinary(B, 2); }
204 void visitOr (BinaryOperator &B) { visitSimpleBinary(B, 3); }
205 void visitXor(BinaryOperator &B) { visitSimpleBinary(B, 4); }
Chris Lattnere2954c82002-11-02 20:04:26 +0000206
Chris Lattner6d40c192003-01-16 16:43:00 +0000207 // Comparison operators...
208 void visitSetCondInst(SetCondInst &I);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000209 unsigned EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
210 MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000211 MachineBasicBlock::iterator MBBI);
Chris Lattner12d96a02004-03-30 21:22:00 +0000212 void visitSelectInst(SelectInst &SI);
213
Chris Lattnerb2acc512003-10-19 21:09:10 +0000214
Chris Lattner6fc3c522002-11-17 21:11:55 +0000215 // Memory Instructions
216 void visitLoadInst(LoadInst &I);
217 void visitStoreInst(StoreInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000218 void visitGetElementPtrInst(GetElementPtrInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000219 void visitAllocaInst(AllocaInst &I);
Chris Lattner3e130a22003-01-13 00:32:26 +0000220 void visitMallocInst(MallocInst &I);
221 void visitFreeInst(FreeInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000222
Chris Lattnere2954c82002-11-02 20:04:26 +0000223 // Other operators
Brian Gaekea1719c92002-10-31 23:03:59 +0000224 void visitShiftInst(ShiftInst &I);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000225 void visitPHINode(PHINode &I) {} // PHI nodes handled by second pass
Brian Gaekefa8d5712002-11-22 11:07:01 +0000226 void visitCastInst(CastInst &I);
Chris Lattner73815062003-10-18 05:56:40 +0000227 void visitVANextInst(VANextInst &I);
228 void visitVAArgInst(VAArgInst &I);
Chris Lattner72614082002-10-25 22:55:53 +0000229
230 void visitInstruction(Instruction &I) {
231 std::cerr << "Cannot instruction select: " << I;
232 abort();
233 }
234
Brian Gaeke95780cc2002-12-13 07:56:18 +0000235 /// promote32 - Make a value 32-bits wide, and put it somewhere.
Chris Lattner3e130a22003-01-13 00:32:26 +0000236 ///
237 void promote32(unsigned targetReg, const ValueRecord &VR);
238
Chris Lattner721d2d42004-03-08 01:18:36 +0000239 /// getAddressingMode - Get the addressing mode to use to address the
240 /// specified value. The returned value should be used with addFullAddress.
Reid Spencerfc989e12004-08-30 00:13:26 +0000241 void getAddressingMode(Value *Addr, X86AddressMode &AM);
Chris Lattner721d2d42004-03-08 01:18:36 +0000242
243
244 /// getGEPIndex - This is used to fold GEP instructions into X86 addressing
245 /// expressions.
Chris Lattnerb6bac512004-02-25 06:13:04 +0000246 void getGEPIndex(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
247 std::vector<Value*> &GEPOps,
Reid Spencerfc989e12004-08-30 00:13:26 +0000248 std::vector<const Type*> &GEPTypes,
249 X86AddressMode &AM);
Chris Lattnerb6bac512004-02-25 06:13:04 +0000250
251 /// isGEPFoldable - Return true if the specified GEP can be completely
252 /// folded into the addressing mode of a load/store or lea instruction.
253 bool isGEPFoldable(MachineBasicBlock *MBB,
254 Value *Src, User::op_iterator IdxBegin,
Reid Spencerfc989e12004-08-30 00:13:26 +0000255 User::op_iterator IdxEnd, X86AddressMode &AM);
Chris Lattnerb6bac512004-02-25 06:13:04 +0000256
Chris Lattner3e130a22003-01-13 00:32:26 +0000257 /// emitGEPOperation - Common code shared between visitGetElementPtrInst and
258 /// constant expression GEP support.
259 ///
Chris Lattner827832c2004-02-22 17:05:38 +0000260 void emitGEPOperation(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
Chris Lattner333b2fa2002-12-13 10:09:43 +0000261 Value *Src, User::op_iterator IdxBegin,
Chris Lattnerc0812d82002-12-13 06:56:29 +0000262 User::op_iterator IdxEnd, unsigned TargetReg);
263
Chris Lattner548f61d2003-04-23 17:22:12 +0000264 /// emitCastOperation - Common code shared between visitCastInst and
265 /// constant expression cast support.
Misha Brukman538607f2004-03-01 23:53:11 +0000266 ///
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000267 void emitCastOperation(MachineBasicBlock *BB,MachineBasicBlock::iterator IP,
Chris Lattner548f61d2003-04-23 17:22:12 +0000268 Value *Src, const Type *DestTy, unsigned TargetReg);
269
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000270 /// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
271 /// and constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000272 ///
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000273 void emitSimpleBinaryOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000274 MachineBasicBlock::iterator IP,
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000275 Value *Op0, Value *Op1,
276 unsigned OperatorClass, unsigned TargetReg);
277
Chris Lattner6621ed92004-04-11 21:23:56 +0000278 /// emitBinaryFPOperation - This method handles emission of floating point
279 /// Add (0), Sub (1), Mul (2), and Div (3) operations.
280 void emitBinaryFPOperation(MachineBasicBlock *BB,
281 MachineBasicBlock::iterator IP,
282 Value *Op0, Value *Op1,
283 unsigned OperatorClass, unsigned TargetReg);
284
Chris Lattner462fa822004-04-11 20:56:28 +0000285 void emitMultiply(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
286 Value *Op0, Value *Op1, unsigned TargetReg);
287
288 void doMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI,
289 unsigned DestReg, const Type *DestTy,
290 unsigned Op0Reg, unsigned Op1Reg);
291 void doMultiplyConst(MachineBasicBlock *MBB,
292 MachineBasicBlock::iterator MBBI,
293 unsigned DestReg, const Type *DestTy,
294 unsigned Op0Reg, unsigned Op1Val);
295
Chris Lattnercadff442003-10-23 17:21:43 +0000296 void emitDivRemOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000297 MachineBasicBlock::iterator IP,
Chris Lattner462fa822004-04-11 20:56:28 +0000298 Value *Op0, Value *Op1, bool isDiv,
299 unsigned TargetReg);
Chris Lattnercadff442003-10-23 17:21:43 +0000300
Chris Lattner58c41fe2003-08-24 19:19:47 +0000301 /// emitSetCCOperation - Common code shared between visitSetCondInst and
302 /// constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000303 ///
Chris Lattner58c41fe2003-08-24 19:19:47 +0000304 void emitSetCCOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000305 MachineBasicBlock::iterator IP,
Chris Lattner58c41fe2003-08-24 19:19:47 +0000306 Value *Op0, Value *Op1, unsigned Opcode,
307 unsigned TargetReg);
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000308
309 /// emitShiftOperation - Common code shared between visitShiftInst and
310 /// constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000311 ///
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000312 void emitShiftOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000313 MachineBasicBlock::iterator IP,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000314 Value *Op, Value *ShiftAmount, bool isLeftShift,
315 const Type *ResultTy, unsigned DestReg);
316
Chris Lattner12d96a02004-03-30 21:22:00 +0000317 /// emitSelectOperation - Common code shared between visitSelectInst and the
318 /// constant expression support.
319 void emitSelectOperation(MachineBasicBlock *MBB,
320 MachineBasicBlock::iterator IP,
321 Value *Cond, Value *TrueVal, Value *FalseVal,
322 unsigned DestReg);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000323
Chris Lattnerc5291f52002-10-27 21:16:59 +0000324 /// copyConstantToRegister - Output the instructions required to put the
325 /// specified constant into the specified register.
326 ///
Chris Lattner8a307e82002-12-16 19:32:50 +0000327 void copyConstantToRegister(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000328 MachineBasicBlock::iterator MBBI,
Chris Lattner8a307e82002-12-16 19:32:50 +0000329 Constant *C, unsigned Reg);
Chris Lattnerc5291f52002-10-27 21:16:59 +0000330
Chris Lattner01cdb1b2004-06-11 05:33:49 +0000331 void emitUCOMr(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI,
332 unsigned LHS, unsigned RHS);
333
Chris Lattner3e130a22003-01-13 00:32:26 +0000334 /// makeAnotherReg - This method returns the next register number we haven't
335 /// yet used.
336 ///
337 /// Long values are handled somewhat specially. They are always allocated
338 /// as pairs of 32 bit integer values. The register number returned is the
339 /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits
340 /// of the long value.
341 ///
Chris Lattnerc0812d82002-12-13 06:56:29 +0000342 unsigned makeAnotherReg(const Type *Ty) {
Chris Lattner7db1fa92003-07-30 05:33:48 +0000343 assert(dynamic_cast<const X86RegisterInfo*>(TM.getRegisterInfo()) &&
344 "Current target doesn't have X86 reg info??");
345 const X86RegisterInfo *MRI =
346 static_cast<const X86RegisterInfo*>(TM.getRegisterInfo());
Chris Lattner3e130a22003-01-13 00:32:26 +0000347 if (Ty == Type::LongTy || Ty == Type::ULongTy) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000348 const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy);
349 // Create the lower part
350 F->getSSARegMap()->createVirtualRegister(RC);
351 // Create the upper part.
352 return F->getSSARegMap()->createVirtualRegister(RC)-1;
Chris Lattner3e130a22003-01-13 00:32:26 +0000353 }
354
Chris Lattnerc0812d82002-12-13 06:56:29 +0000355 // Add the mapping of regnumber => reg class to MachineFunction
Chris Lattner7db1fa92003-07-30 05:33:48 +0000356 const TargetRegisterClass *RC = MRI->getRegClassForType(Ty);
Chris Lattner3e130a22003-01-13 00:32:26 +0000357 return F->getSSARegMap()->createVirtualRegister(RC);
Brian Gaeke20244b72002-12-12 15:33:40 +0000358 }
359
Chris Lattnercb2fd552004-05-13 07:40:27 +0000360 /// getReg - This method turns an LLVM value into a register number.
Chris Lattner72614082002-10-25 22:55:53 +0000361 ///
362 unsigned getReg(Value &V) { return getReg(&V); } // Allow references
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000363 unsigned getReg(Value *V) {
364 // Just append to the end of the current bb.
365 MachineBasicBlock::iterator It = BB->end();
366 return getReg(V, BB, It);
367 }
Brian Gaeke71794c02002-12-13 11:22:48 +0000368 unsigned getReg(Value *V, MachineBasicBlock *MBB,
Chris Lattnercb2fd552004-05-13 07:40:27 +0000369 MachineBasicBlock::iterator IPt);
Chris Lattner427aeb42004-04-11 19:21:59 +0000370
Chris Lattnercb2fd552004-05-13 07:40:27 +0000371 /// getFixedSizedAllocaFI - Return the frame index for a fixed sized alloca
372 /// that is to be statically allocated with the initial stack frame
373 /// adjustment.
374 unsigned getFixedSizedAllocaFI(AllocaInst *AI);
Chris Lattner72614082002-10-25 22:55:53 +0000375 };
376}
377
Chris Lattnercb2fd552004-05-13 07:40:27 +0000378/// dyn_castFixedAlloca - If the specified value is a fixed size alloca
379/// instruction in the entry block, return it. Otherwise, return a null
380/// pointer.
381static AllocaInst *dyn_castFixedAlloca(Value *V) {
382 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
383 BasicBlock *BB = AI->getParent();
384 if (isa<ConstantUInt>(AI->getArraySize()) && BB ==&BB->getParent()->front())
385 return AI;
386 }
387 return 0;
388}
389
390/// getReg - This method turns an LLVM value into a register number.
391///
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000392unsigned X86ISel::getReg(Value *V, MachineBasicBlock *MBB,
393 MachineBasicBlock::iterator IPt) {
Chris Lattnercb2fd552004-05-13 07:40:27 +0000394 // If this operand is a constant, emit the code to copy the constant into
395 // the register here...
Chris Lattnercb2fd552004-05-13 07:40:27 +0000396 if (Constant *C = dyn_cast<Constant>(V)) {
397 unsigned Reg = makeAnotherReg(V->getType());
398 copyConstantToRegister(MBB, IPt, C, Reg);
399 return Reg;
Chris Lattnercb2fd552004-05-13 07:40:27 +0000400 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
Chris Lattner8b486a12004-06-29 00:14:38 +0000401 // Do not emit noop casts at all, unless it's a double -> float cast.
402 if (getClassB(CI->getType()) == getClassB(CI->getOperand(0)->getType()) &&
403 (CI->getType() != Type::FloatTy ||
404 CI->getOperand(0)->getType() != Type::DoubleTy))
Chris Lattnercb2fd552004-05-13 07:40:27 +0000405 return getReg(CI->getOperand(0), MBB, IPt);
406 } else if (AllocaInst *AI = dyn_castFixedAlloca(V)) {
407 // If the alloca address couldn't be folded into the instruction addressing,
408 // emit an explicit LEA as appropriate.
409 unsigned Reg = makeAnotherReg(V->getType());
410 unsigned FI = getFixedSizedAllocaFI(AI);
411 addFrameReference(BuildMI(*MBB, IPt, X86::LEA32r, 4, Reg), FI);
412 return Reg;
413 }
414
415 unsigned &Reg = RegMap[V];
416 if (Reg == 0) {
417 Reg = makeAnotherReg(V->getType());
418 RegMap[V] = Reg;
419 }
420
421 return Reg;
422}
423
424/// getFixedSizedAllocaFI - Return the frame index for a fixed sized alloca
425/// that is to be statically allocated with the initial stack frame
426/// adjustment.
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000427unsigned X86ISel::getFixedSizedAllocaFI(AllocaInst *AI) {
Chris Lattnercb2fd552004-05-13 07:40:27 +0000428 // Already computed this?
429 std::map<AllocaInst*, unsigned>::iterator I = AllocaMap.lower_bound(AI);
430 if (I != AllocaMap.end() && I->first == AI) return I->second;
431
432 const Type *Ty = AI->getAllocatedType();
433 ConstantUInt *CUI = cast<ConstantUInt>(AI->getArraySize());
434 unsigned TySize = TM.getTargetData().getTypeSize(Ty);
435 TySize *= CUI->getValue(); // Get total allocated size...
436 unsigned Alignment = TM.getTargetData().getTypeAlignment(Ty);
437
438 // Create a new stack object using the frame manager...
439 int FrameIdx = F->getFrameInfo()->CreateStackObject(TySize, Alignment);
440 AllocaMap.insert(I, std::make_pair(AI, FrameIdx));
441 return FrameIdx;
442}
443
444
Chris Lattnerc5291f52002-10-27 21:16:59 +0000445/// copyConstantToRegister - Output the instructions required to put the
446/// specified constant into the specified register.
447///
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000448void X86ISel::copyConstantToRegister(MachineBasicBlock *MBB,
449 MachineBasicBlock::iterator IP,
450 Constant *C, unsigned R) {
Chris Lattner30483b02004-10-16 18:13:05 +0000451 if (isa<UndefValue>(C)) {
452 switch (getClassB(C->getType())) {
453 case cFP:
454 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
455 BuildMI(*MBB, IP, X86::FLD0, 0, R);
456 return;
457 case cLong:
458 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, R+1);
459 // FALL THROUGH
460 default:
461 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, R);
462 return;
463 }
464 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000465 unsigned Class = 0;
466 switch (CE->getOpcode()) {
467 case Instruction::GetElementPtr:
Brian Gaeke68b1edc2002-12-16 04:23:29 +0000468 emitGEPOperation(MBB, IP, CE->getOperand(0),
Chris Lattner333b2fa2002-12-13 10:09:43 +0000469 CE->op_begin()+1, CE->op_end(), R);
Chris Lattnerc0812d82002-12-13 06:56:29 +0000470 return;
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000471 case Instruction::Cast:
Chris Lattner548f61d2003-04-23 17:22:12 +0000472 emitCastOperation(MBB, IP, CE->getOperand(0), CE->getType(), R);
Chris Lattner4b12cde2003-04-21 21:33:44 +0000473 return;
Chris Lattnerc0812d82002-12-13 06:56:29 +0000474
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000475 case Instruction::Xor: ++Class; // FALL THROUGH
476 case Instruction::Or: ++Class; // FALL THROUGH
477 case Instruction::And: ++Class; // FALL THROUGH
478 case Instruction::Sub: ++Class; // FALL THROUGH
479 case Instruction::Add:
480 emitSimpleBinaryOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
481 Class, R);
482 return;
483
Chris Lattner462fa822004-04-11 20:56:28 +0000484 case Instruction::Mul:
485 emitMultiply(MBB, IP, CE->getOperand(0), CE->getOperand(1), R);
Chris Lattnercadff442003-10-23 17:21:43 +0000486 return;
Chris Lattner462fa822004-04-11 20:56:28 +0000487
Chris Lattnercadff442003-10-23 17:21:43 +0000488 case Instruction::Div:
Chris Lattner462fa822004-04-11 20:56:28 +0000489 case Instruction::Rem:
490 emitDivRemOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
491 CE->getOpcode() == Instruction::Div, R);
Chris Lattnercadff442003-10-23 17:21:43 +0000492 return;
Chris Lattnercadff442003-10-23 17:21:43 +0000493
Chris Lattner58c41fe2003-08-24 19:19:47 +0000494 case Instruction::SetNE:
495 case Instruction::SetEQ:
496 case Instruction::SetLT:
497 case Instruction::SetGT:
498 case Instruction::SetLE:
499 case Instruction::SetGE:
500 emitSetCCOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
501 CE->getOpcode(), R);
502 return;
503
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000504 case Instruction::Shl:
505 case Instruction::Shr:
506 emitShiftOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000507 CE->getOpcode() == Instruction::Shl, CE->getType(), R);
508 return;
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000509
Chris Lattner12d96a02004-03-30 21:22:00 +0000510 case Instruction::Select:
511 emitSelectOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
512 CE->getOperand(2), R);
513 return;
514
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000515 default:
Chris Lattner76e2df22004-07-15 02:14:30 +0000516 std::cerr << "Offending expr: " << *C << "\n";
Chris Lattnerb2acc512003-10-19 21:09:10 +0000517 assert(0 && "Constant expression not yet handled!\n");
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000518 }
Brian Gaeke20244b72002-12-12 15:33:40 +0000519 }
Chris Lattnerc5291f52002-10-27 21:16:59 +0000520
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000521 if (C->getType()->isIntegral()) {
Chris Lattner6b993cc2002-12-15 08:02:15 +0000522 unsigned Class = getClassB(C->getType());
Chris Lattner3e130a22003-01-13 00:32:26 +0000523
524 if (Class == cLong) {
525 // Copy the value into the register pair.
Chris Lattnerc07736a2003-07-23 15:22:26 +0000526 uint64_t Val = cast<ConstantInt>(C)->getRawValue();
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000527 BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addImm(Val & 0xFFFFFFFF);
528 BuildMI(*MBB, IP, X86::MOV32ri, 1, R+1).addImm(Val >> 32);
Chris Lattner3e130a22003-01-13 00:32:26 +0000529 return;
530 }
531
Chris Lattner94af4142002-12-25 05:13:53 +0000532 assert(Class <= cInt && "Type not handled yet!");
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000533
534 static const unsigned IntegralOpcodeTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000535 X86::MOV8ri, X86::MOV16ri, X86::MOV32ri
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000536 };
537
Chris Lattner6b993cc2002-12-15 08:02:15 +0000538 if (C->getType() == Type::BoolTy) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000539 BuildMI(*MBB, IP, X86::MOV8ri, 1, R).addImm(C == ConstantBool::True);
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000540 } else {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000541 ConstantInt *CI = cast<ConstantInt>(C);
Chris Lattneree352852004-02-29 07:22:16 +0000542 BuildMI(*MBB, IP, IntegralOpcodeTab[Class],1,R).addImm(CI->getRawValue());
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000543 }
Chris Lattner94af4142002-12-25 05:13:53 +0000544 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
Chris Lattneraf703622004-02-02 18:56:30 +0000545 if (CFP->isExactlyValue(+0.0))
Chris Lattneree352852004-02-29 07:22:16 +0000546 BuildMI(*MBB, IP, X86::FLD0, 0, R);
Chris Lattneraf703622004-02-02 18:56:30 +0000547 else if (CFP->isExactlyValue(+1.0))
Chris Lattneree352852004-02-29 07:22:16 +0000548 BuildMI(*MBB, IP, X86::FLD1, 0, R);
Chris Lattner94af4142002-12-25 05:13:53 +0000549 else {
Chris Lattner3e130a22003-01-13 00:32:26 +0000550 // Otherwise we need to spill the constant to memory...
551 MachineConstantPool *CP = F->getConstantPool();
552 unsigned CPI = CP->getConstantPoolIndex(CFP);
Chris Lattner6c09db22003-10-20 04:11:23 +0000553 const Type *Ty = CFP->getType();
554
555 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000556 unsigned LoadOpcode = Ty == Type::FloatTy ? X86::FLD32m : X86::FLD64m;
Chris Lattneree352852004-02-29 07:22:16 +0000557 addConstantPoolReference(BuildMI(*MBB, IP, LoadOpcode, 4, R), CPI);
Chris Lattner94af4142002-12-25 05:13:53 +0000558 }
559
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000560 } else if (isa<ConstantPointerNull>(C)) {
Brian Gaeke20244b72002-12-12 15:33:40 +0000561 // Copy zero (null pointer) to the register.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000562 BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addImm(0);
Reid Spencer8863f182004-07-18 00:38:32 +0000563 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) {
564 BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addGlobalAddress(GV);
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000565 } else {
Chris Lattner76e2df22004-07-15 02:14:30 +0000566 std::cerr << "Offending constant: " << *C << "\n";
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000567 assert(0 && "Type not handled yet!");
Chris Lattnerc5291f52002-10-27 21:16:59 +0000568 }
569}
570
Chris Lattner065faeb2002-12-28 20:24:02 +0000571/// LoadArgumentsToVirtualRegs - Load all of the arguments to this function from
572/// the stack into virtual registers.
573///
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000574void X86ISel::LoadArgumentsToVirtualRegs(Function &Fn) {
Chris Lattner065faeb2002-12-28 20:24:02 +0000575 // Emit instructions to load the arguments... On entry to a function on the
576 // X86, the stack frame looks like this:
577 //
578 // [ESP] -- return address
Chris Lattner3e130a22003-01-13 00:32:26 +0000579 // [ESP + 4] -- first argument (leftmost lexically)
580 // [ESP + 8] -- second argument, if first argument is four bytes in size
Chris Lattner065faeb2002-12-28 20:24:02 +0000581 // ...
582 //
Chris Lattnerf158da22003-01-16 02:20:12 +0000583 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattneraa09b752002-12-28 21:08:28 +0000584 MachineFrameInfo *MFI = F->getFrameInfo();
Chris Lattner065faeb2002-12-28 20:24:02 +0000585
586 for (Function::aiterator I = Fn.abegin(), E = Fn.aend(); I != E; ++I) {
Chris Lattner427aeb42004-04-11 19:21:59 +0000587 bool ArgLive = !I->use_empty();
588 unsigned Reg = ArgLive ? getReg(*I) : 0;
Chris Lattner065faeb2002-12-28 20:24:02 +0000589 int FI; // Frame object index
Chris Lattner427aeb42004-04-11 19:21:59 +0000590
Chris Lattner065faeb2002-12-28 20:24:02 +0000591 switch (getClassB(I->getType())) {
592 case cByte:
Chris Lattner427aeb42004-04-11 19:21:59 +0000593 if (ArgLive) {
594 FI = MFI->CreateFixedObject(1, ArgOffset);
595 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Reg), FI);
596 }
Chris Lattner065faeb2002-12-28 20:24:02 +0000597 break;
598 case cShort:
Chris Lattner427aeb42004-04-11 19:21:59 +0000599 if (ArgLive) {
600 FI = MFI->CreateFixedObject(2, ArgOffset);
601 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Reg), FI);
602 }
Chris Lattner065faeb2002-12-28 20:24:02 +0000603 break;
604 case cInt:
Chris Lattner427aeb42004-04-11 19:21:59 +0000605 if (ArgLive) {
606 FI = MFI->CreateFixedObject(4, ArgOffset);
607 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Reg), FI);
608 }
Chris Lattner065faeb2002-12-28 20:24:02 +0000609 break;
Chris Lattner3e130a22003-01-13 00:32:26 +0000610 case cLong:
Chris Lattner427aeb42004-04-11 19:21:59 +0000611 if (ArgLive) {
612 FI = MFI->CreateFixedObject(8, ArgOffset);
613 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Reg), FI);
614 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Reg+1), FI, 4);
615 }
Chris Lattner3e130a22003-01-13 00:32:26 +0000616 ArgOffset += 4; // longs require 4 additional bytes
617 break;
Chris Lattner065faeb2002-12-28 20:24:02 +0000618 case cFP:
Chris Lattner427aeb42004-04-11 19:21:59 +0000619 if (ArgLive) {
620 unsigned Opcode;
621 if (I->getType() == Type::FloatTy) {
622 Opcode = X86::FLD32m;
623 FI = MFI->CreateFixedObject(4, ArgOffset);
624 } else {
625 Opcode = X86::FLD64m;
626 FI = MFI->CreateFixedObject(8, ArgOffset);
627 }
628 addFrameReference(BuildMI(BB, Opcode, 4, Reg), FI);
Chris Lattner065faeb2002-12-28 20:24:02 +0000629 }
Chris Lattner427aeb42004-04-11 19:21:59 +0000630 if (I->getType() == Type::DoubleTy)
631 ArgOffset += 4; // doubles require 4 additional bytes
Chris Lattner065faeb2002-12-28 20:24:02 +0000632 break;
633 default:
634 assert(0 && "Unhandled argument type!");
635 }
Chris Lattner3e130a22003-01-13 00:32:26 +0000636 ArgOffset += 4; // Each argument takes at least 4 bytes on the stack...
Chris Lattner065faeb2002-12-28 20:24:02 +0000637 }
Chris Lattnereca195e2003-05-08 19:44:13 +0000638
639 // If the function takes variable number of arguments, add a frame offset for
640 // the start of the first vararg value... this is used to expand
641 // llvm.va_start.
642 if (Fn.getFunctionType()->isVarArg())
643 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner065faeb2002-12-28 20:24:02 +0000644}
645
646
Chris Lattner333b2fa2002-12-13 10:09:43 +0000647/// SelectPHINodes - Insert machine code to generate phis. This is tricky
648/// because we have to generate our sources into the source basic blocks, not
649/// the current one.
650///
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000651void X86ISel::SelectPHINodes() {
Chris Lattnerd029cd22004-06-02 05:55:25 +0000652 const TargetInstrInfo &TII = *TM.getInstrInfo();
Chris Lattner333b2fa2002-12-13 10:09:43 +0000653 const Function &LF = *F->getFunction(); // The LLVM function...
654 for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) {
655 const BasicBlock *BB = I;
Chris Lattner168aa902004-02-29 07:10:16 +0000656 MachineBasicBlock &MBB = *MBBMap[I];
Chris Lattner333b2fa2002-12-13 10:09:43 +0000657
658 // Loop over all of the PHI nodes in the LLVM basic block...
Chris Lattner168aa902004-02-29 07:10:16 +0000659 MachineBasicBlock::iterator PHIInsertPoint = MBB.begin();
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000660 for (BasicBlock::const_iterator I = BB->begin(); isa<PHINode>(I); ++I) {
661 PHINode *PN = const_cast<PHINode*>(dyn_cast<PHINode>(I));
Chris Lattner3e130a22003-01-13 00:32:26 +0000662
Chris Lattner333b2fa2002-12-13 10:09:43 +0000663 // Create a new machine instr PHI node, and insert it.
Chris Lattner3e130a22003-01-13 00:32:26 +0000664 unsigned PHIReg = getReg(*PN);
Chris Lattner168aa902004-02-29 07:10:16 +0000665 MachineInstr *PhiMI = BuildMI(MBB, PHIInsertPoint,
666 X86::PHI, PN->getNumOperands(), PHIReg);
Chris Lattner3e130a22003-01-13 00:32:26 +0000667
668 MachineInstr *LongPhiMI = 0;
Chris Lattner168aa902004-02-29 07:10:16 +0000669 if (PN->getType() == Type::LongTy || PN->getType() == Type::ULongTy)
670 LongPhiMI = BuildMI(MBB, PHIInsertPoint,
671 X86::PHI, PN->getNumOperands(), PHIReg+1);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000672
Chris Lattnera6e73f12003-05-12 14:22:21 +0000673 // PHIValues - Map of blocks to incoming virtual registers. We use this
674 // so that we only initialize one incoming value for a particular block,
675 // even if the block has multiple entries in the PHI node.
676 //
677 std::map<MachineBasicBlock*, unsigned> PHIValues;
678
Chris Lattner333b2fa2002-12-13 10:09:43 +0000679 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
680 MachineBasicBlock *PredMBB = MBBMap[PN->getIncomingBlock(i)];
Chris Lattnera6e73f12003-05-12 14:22:21 +0000681 unsigned ValReg;
682 std::map<MachineBasicBlock*, unsigned>::iterator EntryIt =
683 PHIValues.lower_bound(PredMBB);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000684
Chris Lattnera6e73f12003-05-12 14:22:21 +0000685 if (EntryIt != PHIValues.end() && EntryIt->first == PredMBB) {
686 // We already inserted an initialization of the register for this
687 // predecessor. Recycle it.
688 ValReg = EntryIt->second;
689
690 } else {
Chris Lattnera81fc682003-10-19 00:26:11 +0000691 // Get the incoming value into a virtual register.
Chris Lattnera6e73f12003-05-12 14:22:21 +0000692 //
Chris Lattnera81fc682003-10-19 00:26:11 +0000693 Value *Val = PN->getIncomingValue(i);
694
695 // If this is a constant or GlobalValue, we may have to insert code
696 // into the basic block to compute it into a virtual register.
Reid Spencer8863f182004-07-18 00:38:32 +0000697 if ((isa<Constant>(Val) && !isa<ConstantExpr>(Val))) {
Chris Lattnercb2fd552004-05-13 07:40:27 +0000698 // Simple constants get emitted at the end of the basic block,
699 // before any terminator instructions. We "know" that the code to
700 // move a constant into a register will never clobber any flags.
701 ValReg = getReg(Val, PredMBB, PredMBB->getFirstTerminator());
Chris Lattnera81fc682003-10-19 00:26:11 +0000702 } else {
Chris Lattnercb2fd552004-05-13 07:40:27 +0000703 // Because we don't want to clobber any values which might be in
704 // physical registers with the computation of this constant (which
705 // might be arbitrarily complex if it is a constant expression),
706 // just insert the computation at the top of the basic block.
707 MachineBasicBlock::iterator PI = PredMBB->begin();
708
709 // Skip over any PHI nodes though!
710 while (PI != PredMBB->end() && PI->getOpcode() == X86::PHI)
711 ++PI;
712
713 ValReg = getReg(Val, PredMBB, PI);
Chris Lattnera81fc682003-10-19 00:26:11 +0000714 }
Chris Lattnera6e73f12003-05-12 14:22:21 +0000715
716 // Remember that we inserted a value for this PHI for this predecessor
717 PHIValues.insert(EntryIt, std::make_pair(PredMBB, ValReg));
718 }
719
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000720 PhiMI->addRegOperand(ValReg);
Chris Lattner3e130a22003-01-13 00:32:26 +0000721 PhiMI->addMachineBasicBlockOperand(PredMBB);
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000722 if (LongPhiMI) {
723 LongPhiMI->addRegOperand(ValReg+1);
724 LongPhiMI->addMachineBasicBlockOperand(PredMBB);
725 }
Chris Lattner333b2fa2002-12-13 10:09:43 +0000726 }
Chris Lattner168aa902004-02-29 07:10:16 +0000727
728 // Now that we emitted all of the incoming values for the PHI node, make
729 // sure to reposition the InsertPoint after the PHI that we just added.
730 // This is needed because we might have inserted a constant into this
731 // block, right after the PHI's which is before the old insert point!
732 PHIInsertPoint = LongPhiMI ? LongPhiMI : PhiMI;
733 ++PHIInsertPoint;
Chris Lattner333b2fa2002-12-13 10:09:43 +0000734 }
735 }
736}
737
Chris Lattner986618e2004-02-22 19:47:26 +0000738/// RequiresFPRegKill - The floating point stackifier pass cannot insert
739/// compensation code on critical edges. As such, it requires that we kill all
740/// FP registers on the exit from any blocks that either ARE critical edges, or
741/// branch to a block that has incoming critical edges.
742///
743/// Note that this kill instruction will eventually be eliminated when
744/// restrictions in the stackifier are relaxed.
745///
Brian Gaeke1afe7732004-04-28 04:45:55 +0000746static bool RequiresFPRegKill(const MachineBasicBlock *MBB) {
Chris Lattner986618e2004-02-22 19:47:26 +0000747#if 0
Brian Gaeke1afe7732004-04-28 04:45:55 +0000748 const BasicBlock *BB = MBB->getBasicBlock ();
Chris Lattner986618e2004-02-22 19:47:26 +0000749 for (succ_const_iterator SI = succ_begin(BB), E = succ_end(BB); SI!=E; ++SI) {
750 const BasicBlock *Succ = *SI;
751 pred_const_iterator PI = pred_begin(Succ), PE = pred_end(Succ);
752 ++PI; // Block have at least one predecessory
753 if (PI != PE) { // If it has exactly one, this isn't crit edge
754 // If this block has more than one predecessor, check all of the
755 // predecessors to see if they have multiple successors. If so, then the
756 // block we are analyzing needs an FPRegKill.
757 for (PI = pred_begin(Succ); PI != PE; ++PI) {
758 const BasicBlock *Pred = *PI;
759 succ_const_iterator SI2 = succ_begin(Pred);
760 ++SI2; // There must be at least one successor of this block.
761 if (SI2 != succ_end(Pred))
762 return true; // Yes, we must insert the kill on this edge.
763 }
764 }
765 }
766 // If we got this far, there is no need to insert the kill instruction.
767 return false;
768#else
769 return true;
770#endif
771}
772
773// InsertFPRegKills - Insert FP_REG_KILL instructions into basic blocks that
774// need them. This only occurs due to the floating point stackifier not being
775// aggressive enough to handle arbitrary global stackification.
776//
777// Currently we insert an FP_REG_KILL instruction into each block that uses or
778// defines a floating point virtual register.
779//
780// When the global register allocators (like linear scan) finally update live
781// variable analysis, we can keep floating point values in registers across
782// portions of the CFG that do not involve critical edges. This will be a big
783// win, but we are waiting on the global allocators before we can do this.
784//
785// With a bit of work, the floating point stackifier pass can be enhanced to
786// break critical edges as needed (to make a place to put compensation code),
787// but this will require some infrastructure improvements as well.
788//
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000789void X86ISel::InsertFPRegKills() {
Chris Lattner986618e2004-02-22 19:47:26 +0000790 SSARegMap &RegMap = *F->getSSARegMap();
Chris Lattner986618e2004-02-22 19:47:26 +0000791
792 for (MachineFunction::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
Chris Lattner986618e2004-02-22 19:47:26 +0000793 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000794 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
795 MachineOperand& MO = I->getOperand(i);
796 if (MO.isRegister() && MO.getReg()) {
797 unsigned Reg = MO.getReg();
Chris Lattner986618e2004-02-22 19:47:26 +0000798 if (MRegisterInfo::isVirtualRegister(Reg))
Chris Lattner65cf42d2004-02-23 07:29:45 +0000799 if (RegMap.getRegClass(Reg)->getSize() == 10)
800 goto UsesFPReg;
Chris Lattner986618e2004-02-22 19:47:26 +0000801 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000802 }
Chris Lattner65cf42d2004-02-23 07:29:45 +0000803 // If we haven't found an FP register use or def in this basic block, check
804 // to see if any of our successors has an FP PHI node, which will cause a
805 // copy to be inserted into this block.
Brian Gaeke235aa5e2004-04-28 04:34:16 +0000806 for (MachineBasicBlock::const_succ_iterator SI = BB->succ_begin(),
807 SE = BB->succ_end(); SI != SE; ++SI) {
808 MachineBasicBlock *SBB = *SI;
Chris Lattnerfbc39d52004-02-23 07:42:19 +0000809 for (MachineBasicBlock::iterator I = SBB->begin();
810 I != SBB->end() && I->getOpcode() == X86::PHI; ++I) {
811 if (RegMap.getRegClass(I->getOperand(0).getReg())->getSize() == 10)
812 goto UsesFPReg;
Chris Lattner986618e2004-02-22 19:47:26 +0000813 }
Chris Lattnerfbc39d52004-02-23 07:42:19 +0000814 }
Chris Lattner65cf42d2004-02-23 07:29:45 +0000815 continue;
816 UsesFPReg:
817 // Okay, this block uses an FP register. If the block has successors (ie,
818 // it's not an unwind/return), insert the FP_REG_KILL instruction.
Brian Gaeke1afe7732004-04-28 04:45:55 +0000819 if (BB->succ_size () && RequiresFPRegKill(BB)) {
Chris Lattneree352852004-02-29 07:22:16 +0000820 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
Chris Lattner65cf42d2004-02-23 07:29:45 +0000821 ++NumFPKill;
Chris Lattner986618e2004-02-22 19:47:26 +0000822 }
823 }
824}
825
826
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000827void X86ISel::getAddressingMode(Value *Addr, X86AddressMode &AM) {
Reid Spencerfc989e12004-08-30 00:13:26 +0000828 AM.BaseType = X86AddressMode::RegBase;
829 AM.Base.Reg = 0; AM.Scale = 1; AM.IndexReg = 0; AM.Disp = 0;
Chris Lattner9f1b5312004-05-13 15:12:43 +0000830 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Addr)) {
831 if (isGEPFoldable(BB, GEP->getOperand(0), GEP->op_begin()+1, GEP->op_end(),
Reid Spencerfc989e12004-08-30 00:13:26 +0000832 AM))
Chris Lattner9f1b5312004-05-13 15:12:43 +0000833 return;
834 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Addr)) {
835 if (CE->getOpcode() == Instruction::GetElementPtr)
836 if (isGEPFoldable(BB, CE->getOperand(0), CE->op_begin()+1, CE->op_end(),
Reid Spencerfc989e12004-08-30 00:13:26 +0000837 AM))
Chris Lattner9f1b5312004-05-13 15:12:43 +0000838 return;
Reid Spencerfc989e12004-08-30 00:13:26 +0000839 } else if (AllocaInst *AI = dyn_castFixedAlloca(Addr)) {
840 AM.BaseType = X86AddressMode::FrameIndexBase;
841 AM.Base.FrameIndex = getFixedSizedAllocaFI(AI);
842 return;
Chris Lattner358a9022004-10-15 05:05:29 +0000843 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(Addr)) {
844 AM.GV = GV;
845 return;
Chris Lattner9f1b5312004-05-13 15:12:43 +0000846 }
847
848 // If it's not foldable, reset addr mode.
Reid Spencerfc989e12004-08-30 00:13:26 +0000849 AM.BaseType = X86AddressMode::RegBase;
850 AM.Base.Reg = getReg(Addr);
851 AM.Scale = 1; AM.IndexReg = 0; AM.Disp = 0;
Chris Lattner9f1b5312004-05-13 15:12:43 +0000852}
853
Chris Lattner307ecba2004-03-30 22:39:09 +0000854// canFoldSetCCIntoBranchOrSelect - Return the setcc instruction if we can fold
855// it into the conditional branch or select instruction which is the only user
856// of the cc instruction. This is the case if the conditional branch is the
Chris Lattnera6f9fe62004-06-18 00:29:22 +0000857// only user of the setcc. We also don't handle long arguments below, so we
858// reject them here as well.
Chris Lattner6d40c192003-01-16 16:43:00 +0000859//
Chris Lattner307ecba2004-03-30 22:39:09 +0000860static SetCondInst *canFoldSetCCIntoBranchOrSelect(Value *V) {
Chris Lattner6d40c192003-01-16 16:43:00 +0000861 if (SetCondInst *SCI = dyn_cast<SetCondInst>(V))
Chris Lattner307ecba2004-03-30 22:39:09 +0000862 if (SCI->hasOneUse()) {
863 Instruction *User = cast<Instruction>(SCI->use_back());
864 if ((isa<BranchInst>(User) || isa<SelectInst>(User)) &&
Chris Lattner48c937e2004-04-06 17:34:50 +0000865 (getClassB(SCI->getOperand(0)->getType()) != cLong ||
866 SCI->getOpcode() == Instruction::SetEQ ||
Chris Lattnerd04cd552004-10-08 16:34:13 +0000867 SCI->getOpcode() == Instruction::SetNE) &&
Chris Lattnerb0f4e382004-10-08 22:24:31 +0000868 (isa<BranchInst>(User) || User->getOperand(0) == V))
Chris Lattner6d40c192003-01-16 16:43:00 +0000869 return SCI;
870 }
871 return 0;
872}
Chris Lattner333b2fa2002-12-13 10:09:43 +0000873
Chris Lattner6d40c192003-01-16 16:43:00 +0000874// Return a fixed numbering for setcc instructions which does not depend on the
875// order of the opcodes.
876//
877static unsigned getSetCCNumber(unsigned Opcode) {
878 switch(Opcode) {
879 default: assert(0 && "Unknown setcc instruction!");
880 case Instruction::SetEQ: return 0;
881 case Instruction::SetNE: return 1;
882 case Instruction::SetLT: return 2;
Chris Lattner55f6fab2003-01-16 18:07:23 +0000883 case Instruction::SetGE: return 3;
884 case Instruction::SetGT: return 4;
885 case Instruction::SetLE: return 5;
Chris Lattner6d40c192003-01-16 16:43:00 +0000886 }
887}
Chris Lattner06925362002-11-17 21:56:38 +0000888
Chris Lattner6d40c192003-01-16 16:43:00 +0000889// LLVM -> X86 signed X86 unsigned
890// ----- ---------- ------------
891// seteq -> sete sete
892// setne -> setne setne
893// setlt -> setl setb
Chris Lattner55f6fab2003-01-16 18:07:23 +0000894// setge -> setge setae
Chris Lattner6d40c192003-01-16 16:43:00 +0000895// setgt -> setg seta
896// setle -> setle setbe
Chris Lattnerb2acc512003-10-19 21:09:10 +0000897// ----
898// sets // Used by comparison with 0 optimization
899// setns
900static const unsigned SetCCOpcodeTab[2][8] = {
901 { X86::SETEr, X86::SETNEr, X86::SETBr, X86::SETAEr, X86::SETAr, X86::SETBEr,
902 0, 0 },
903 { X86::SETEr, X86::SETNEr, X86::SETLr, X86::SETGEr, X86::SETGr, X86::SETLEr,
904 X86::SETSr, X86::SETNSr },
Chris Lattner6d40c192003-01-16 16:43:00 +0000905};
906
Chris Lattner01cdb1b2004-06-11 05:33:49 +0000907/// emitUCOMr - In the future when we support processors before the P6, this
908/// wraps the logic for emitting an FUCOMr vs FUCOMIr.
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000909void X86ISel::emitUCOMr(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
910 unsigned LHS, unsigned RHS) {
Chris Lattner01cdb1b2004-06-11 05:33:49 +0000911 if (0) { // for processors prior to the P6
912 BuildMI(*MBB, IP, X86::FUCOMr, 2).addReg(LHS).addReg(RHS);
913 BuildMI(*MBB, IP, X86::FNSTSW8r, 0);
914 BuildMI(*MBB, IP, X86::SAHF, 1);
915 } else {
916 BuildMI(*MBB, IP, X86::FUCOMIr, 2).addReg(LHS).addReg(RHS);
917 }
918}
919
Chris Lattnerb2acc512003-10-19 21:09:10 +0000920// EmitComparison - This function emits a comparison of the two operands,
921// returning the extended setcc code to use.
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000922unsigned X86ISel::EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
923 MachineBasicBlock *MBB,
924 MachineBasicBlock::iterator IP) {
Brian Gaeke1749d632002-11-07 17:59:21 +0000925 // The arguments are already supposed to be of the same type.
Chris Lattner6d40c192003-01-16 16:43:00 +0000926 const Type *CompTy = Op0->getType();
Chris Lattner3e130a22003-01-13 00:32:26 +0000927 unsigned Class = getClassB(CompTy);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000928 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattner333864d2003-06-05 19:30:30 +0000929
930 // Special case handling of: cmp R, i
Chris Lattner260195d2004-05-07 19:55:55 +0000931 if (isa<ConstantPointerNull>(Op1)) {
932 if (OpNum < 2) // seteq/setne -> test
933 BuildMI(*MBB, IP, X86::TEST32rr, 2).addReg(Op0r).addReg(Op0r);
934 else
935 BuildMI(*MBB, IP, X86::CMP32ri, 2).addReg(Op0r).addImm(0);
936 return OpNum;
937
938 } else if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
Chris Lattnere80e6372004-04-06 16:02:27 +0000939 if (Class == cByte || Class == cShort || Class == cInt) {
940 unsigned Op1v = CI->getRawValue();
Chris Lattnerc07736a2003-07-23 15:22:26 +0000941
Chris Lattner333864d2003-06-05 19:30:30 +0000942 // Mask off any upper bits of the constant, if there are any...
943 Op1v &= (1ULL << (8 << Class)) - 1;
944
Chris Lattnerb2acc512003-10-19 21:09:10 +0000945 // If this is a comparison against zero, emit more efficient code. We
946 // can't handle unsigned comparisons against zero unless they are == or
947 // !=. These should have been strength reduced already anyway.
948 if (Op1v == 0 && (CompTy->isSigned() || OpNum < 2)) {
949 static const unsigned TESTTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000950 X86::TEST8rr, X86::TEST16rr, X86::TEST32rr
Chris Lattnerb2acc512003-10-19 21:09:10 +0000951 };
Chris Lattneree352852004-02-29 07:22:16 +0000952 BuildMI(*MBB, IP, TESTTab[Class], 2).addReg(Op0r).addReg(Op0r);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000953
954 if (OpNum == 2) return 6; // Map jl -> js
955 if (OpNum == 3) return 7; // Map jg -> jns
956 return OpNum;
Chris Lattner333864d2003-06-05 19:30:30 +0000957 }
Chris Lattnerb2acc512003-10-19 21:09:10 +0000958
959 static const unsigned CMPTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000960 X86::CMP8ri, X86::CMP16ri, X86::CMP32ri
Chris Lattnerb2acc512003-10-19 21:09:10 +0000961 };
962
Chris Lattneree352852004-02-29 07:22:16 +0000963 BuildMI(*MBB, IP, CMPTab[Class], 2).addReg(Op0r).addImm(Op1v);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000964 return OpNum;
Chris Lattnere80e6372004-04-06 16:02:27 +0000965 } else {
966 assert(Class == cLong && "Unknown integer class!");
967 unsigned LowCst = CI->getRawValue();
968 unsigned HiCst = CI->getRawValue() >> 32;
969 if (OpNum < 2) { // seteq, setne
970 unsigned LoTmp = Op0r;
971 if (LowCst != 0) {
972 LoTmp = makeAnotherReg(Type::IntTy);
973 BuildMI(*MBB, IP, X86::XOR32ri, 2, LoTmp).addReg(Op0r).addImm(LowCst);
974 }
975 unsigned HiTmp = Op0r+1;
976 if (HiCst != 0) {
977 HiTmp = makeAnotherReg(Type::IntTy);
Chris Lattner48c937e2004-04-06 17:34:50 +0000978 BuildMI(*MBB, IP, X86::XOR32ri, 2,HiTmp).addReg(Op0r+1).addImm(HiCst);
Chris Lattnere80e6372004-04-06 16:02:27 +0000979 }
980 unsigned FinalTmp = makeAnotherReg(Type::IntTy);
981 BuildMI(*MBB, IP, X86::OR32rr, 2, FinalTmp).addReg(LoTmp).addReg(HiTmp);
982 return OpNum;
Chris Lattner48c937e2004-04-06 17:34:50 +0000983 } else {
984 // Emit a sequence of code which compares the high and low parts once
985 // each, then uses a conditional move to handle the overflow case. For
986 // example, a setlt for long would generate code like this:
987 //
Chris Lattner9984fd02004-05-09 23:16:33 +0000988 // AL = lo(op1) < lo(op2) // Always unsigned comparison
989 // BL = hi(op1) < hi(op2) // Signedness depends on operands
990 // dest = hi(op1) == hi(op2) ? BL : AL;
Chris Lattner48c937e2004-04-06 17:34:50 +0000991 //
992
993 // FIXME: This would be much better if we had hierarchical register
994 // classes! Until then, hardcode registers so that we can deal with
995 // their aliases (because we don't have conditional byte moves).
996 //
997 BuildMI(*MBB, IP, X86::CMP32ri, 2).addReg(Op0r).addImm(LowCst);
998 BuildMI(*MBB, IP, SetCCOpcodeTab[0][OpNum], 0, X86::AL);
999 BuildMI(*MBB, IP, X86::CMP32ri, 2).addReg(Op0r+1).addImm(HiCst);
1000 BuildMI(*MBB, IP, SetCCOpcodeTab[CompTy->isSigned()][OpNum], 0,X86::BL);
1001 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::BH);
1002 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::AH);
1003 BuildMI(*MBB, IP, X86::CMOVE16rr, 2, X86::BX).addReg(X86::BX)
1004 .addReg(X86::AX);
1005 // NOTE: visitSetCondInst knows that the value is dumped into the BL
1006 // register at this point for long values...
1007 return OpNum;
Chris Lattnere80e6372004-04-06 16:02:27 +00001008 }
Chris Lattner333864d2003-06-05 19:30:30 +00001009 }
Chris Lattnere80e6372004-04-06 16:02:27 +00001010 }
Chris Lattner333864d2003-06-05 19:30:30 +00001011
Chris Lattner9f08a922004-02-03 18:54:04 +00001012 // Special case handling of comparison against +/- 0.0
1013 if (ConstantFP *CFP = dyn_cast<ConstantFP>(Op1))
1014 if (CFP->isExactlyValue(+0.0) || CFP->isExactlyValue(-0.0)) {
Chris Lattneree352852004-02-29 07:22:16 +00001015 BuildMI(*MBB, IP, X86::FTST, 1).addReg(Op0r);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001016 BuildMI(*MBB, IP, X86::FNSTSW8r, 0);
Chris Lattneree352852004-02-29 07:22:16 +00001017 BuildMI(*MBB, IP, X86::SAHF, 1);
Chris Lattner9f08a922004-02-03 18:54:04 +00001018 return OpNum;
1019 }
1020
Chris Lattner58c41fe2003-08-24 19:19:47 +00001021 unsigned Op1r = getReg(Op1, MBB, IP);
Chris Lattner3e130a22003-01-13 00:32:26 +00001022 switch (Class) {
1023 default: assert(0 && "Unknown type class!");
1024 // Emit: cmp <var1>, <var2> (do the comparison). We can
1025 // compare 8-bit with 8-bit, 16-bit with 16-bit, 32-bit with
1026 // 32-bit.
1027 case cByte:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001028 BuildMI(*MBB, IP, X86::CMP8rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +00001029 break;
1030 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001031 BuildMI(*MBB, IP, X86::CMP16rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +00001032 break;
1033 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001034 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +00001035 break;
1036 case cFP:
Chris Lattner01cdb1b2004-06-11 05:33:49 +00001037 emitUCOMr(MBB, IP, Op0r, Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +00001038 break;
1039
1040 case cLong:
1041 if (OpNum < 2) { // seteq, setne
1042 unsigned LoTmp = makeAnotherReg(Type::IntTy);
1043 unsigned HiTmp = makeAnotherReg(Type::IntTy);
1044 unsigned FinalTmp = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001045 BuildMI(*MBB, IP, X86::XOR32rr, 2, LoTmp).addReg(Op0r).addReg(Op1r);
1046 BuildMI(*MBB, IP, X86::XOR32rr, 2, HiTmp).addReg(Op0r+1).addReg(Op1r+1);
1047 BuildMI(*MBB, IP, X86::OR32rr, 2, FinalTmp).addReg(LoTmp).addReg(HiTmp);
Chris Lattner3e130a22003-01-13 00:32:26 +00001048 break; // Allow the sete or setne to be generated from flags set by OR
1049 } else {
1050 // Emit a sequence of code which compares the high and low parts once
1051 // each, then uses a conditional move to handle the overflow case. For
1052 // example, a setlt for long would generate code like this:
1053 //
1054 // AL = lo(op1) < lo(op2) // Signedness depends on operands
1055 // BL = hi(op1) < hi(op2) // Always unsigned comparison
Chris Lattner9984fd02004-05-09 23:16:33 +00001056 // dest = hi(op1) == hi(op2) ? BL : AL;
Chris Lattner3e130a22003-01-13 00:32:26 +00001057 //
1058
Chris Lattner6d40c192003-01-16 16:43:00 +00001059 // FIXME: This would be much better if we had hierarchical register
Chris Lattner3e130a22003-01-13 00:32:26 +00001060 // classes! Until then, hardcode registers so that we can deal with their
1061 // aliases (because we don't have conditional byte moves).
1062 //
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001063 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattneree352852004-02-29 07:22:16 +00001064 BuildMI(*MBB, IP, SetCCOpcodeTab[0][OpNum], 0, X86::AL);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001065 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r+1).addReg(Op1r+1);
Chris Lattneree352852004-02-29 07:22:16 +00001066 BuildMI(*MBB, IP, SetCCOpcodeTab[CompTy->isSigned()][OpNum], 0, X86::BL);
1067 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::BH);
1068 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::AH);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001069 BuildMI(*MBB, IP, X86::CMOVE16rr, 2, X86::BX).addReg(X86::BX)
Chris Lattneree352852004-02-29 07:22:16 +00001070 .addReg(X86::AX);
Chris Lattner6d40c192003-01-16 16:43:00 +00001071 // NOTE: visitSetCondInst knows that the value is dumped into the BL
1072 // register at this point for long values...
Chris Lattnerb2acc512003-10-19 21:09:10 +00001073 return OpNum;
Chris Lattner3e130a22003-01-13 00:32:26 +00001074 }
1075 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001076 return OpNum;
Chris Lattner6d40c192003-01-16 16:43:00 +00001077}
Chris Lattner3e130a22003-01-13 00:32:26 +00001078
Chris Lattner6d40c192003-01-16 16:43:00 +00001079/// SetCC instructions - Here we just emit boilerplate code to set a byte-sized
1080/// register, then move it to wherever the result should be.
1081///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001082void X86ISel::visitSetCondInst(SetCondInst &I) {
Chris Lattner307ecba2004-03-30 22:39:09 +00001083 if (canFoldSetCCIntoBranchOrSelect(&I))
1084 return; // Fold this into a branch or select.
Chris Lattner6d40c192003-01-16 16:43:00 +00001085
Chris Lattner6d40c192003-01-16 16:43:00 +00001086 unsigned DestReg = getReg(I);
Chris Lattner58c41fe2003-08-24 19:19:47 +00001087 MachineBasicBlock::iterator MII = BB->end();
1088 emitSetCCOperation(BB, MII, I.getOperand(0), I.getOperand(1), I.getOpcode(),
1089 DestReg);
1090}
Chris Lattner6d40c192003-01-16 16:43:00 +00001091
Chris Lattner58c41fe2003-08-24 19:19:47 +00001092/// emitSetCCOperation - Common code shared between visitSetCondInst and
1093/// constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +00001094///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001095void X86ISel::emitSetCCOperation(MachineBasicBlock *MBB,
1096 MachineBasicBlock::iterator IP,
1097 Value *Op0, Value *Op1, unsigned Opcode,
1098 unsigned TargetReg) {
Chris Lattner58c41fe2003-08-24 19:19:47 +00001099 unsigned OpNum = getSetCCNumber(Opcode);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001100 OpNum = EmitComparison(OpNum, Op0, Op1, MBB, IP);
Chris Lattner58c41fe2003-08-24 19:19:47 +00001101
Chris Lattnerb2acc512003-10-19 21:09:10 +00001102 const Type *CompTy = Op0->getType();
1103 unsigned CompClass = getClassB(CompTy);
1104 bool isSigned = CompTy->isSigned() && CompClass != cFP;
1105
1106 if (CompClass != cLong || OpNum < 2) {
Chris Lattner6d40c192003-01-16 16:43:00 +00001107 // Handle normal comparisons with a setcc instruction...
Chris Lattneree352852004-02-29 07:22:16 +00001108 BuildMI(*MBB, IP, SetCCOpcodeTab[isSigned][OpNum], 0, TargetReg);
Chris Lattner6d40c192003-01-16 16:43:00 +00001109 } else {
1110 // Handle long comparisons by copying the value which is already in BL into
1111 // the register we want...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001112 BuildMI(*MBB, IP, X86::MOV8rr, 1, TargetReg).addReg(X86::BL);
Chris Lattner6d40c192003-01-16 16:43:00 +00001113 }
Brian Gaeke1749d632002-11-07 17:59:21 +00001114}
Chris Lattner51b49a92002-11-02 19:45:49 +00001115
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001116void X86ISel::visitSelectInst(SelectInst &SI) {
Chris Lattner12d96a02004-03-30 21:22:00 +00001117 unsigned DestReg = getReg(SI);
1118 MachineBasicBlock::iterator MII = BB->end();
1119 emitSelectOperation(BB, MII, SI.getCondition(), SI.getTrueValue(),
1120 SI.getFalseValue(), DestReg);
1121}
1122
1123/// emitSelect - Common code shared between visitSelectInst and the constant
1124/// expression support.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001125void X86ISel::emitSelectOperation(MachineBasicBlock *MBB,
1126 MachineBasicBlock::iterator IP,
1127 Value *Cond, Value *TrueVal, Value *FalseVal,
1128 unsigned DestReg) {
Chris Lattner12d96a02004-03-30 21:22:00 +00001129 unsigned SelectClass = getClassB(TrueVal->getType());
1130
1131 // We don't support 8-bit conditional moves. If we have incoming constants,
1132 // transform them into 16-bit constants to avoid having a run-time conversion.
1133 if (SelectClass == cByte) {
1134 if (Constant *T = dyn_cast<Constant>(TrueVal))
1135 TrueVal = ConstantExpr::getCast(T, Type::ShortTy);
1136 if (Constant *F = dyn_cast<Constant>(FalseVal))
1137 FalseVal = ConstantExpr::getCast(F, Type::ShortTy);
1138 }
1139
Chris Lattner82c5a992004-04-13 21:56:09 +00001140 unsigned TrueReg = getReg(TrueVal, MBB, IP);
1141 unsigned FalseReg = getReg(FalseVal, MBB, IP);
1142 if (TrueReg == FalseReg) {
1143 static const unsigned Opcode[] = {
1144 X86::MOV8rr, X86::MOV16rr, X86::MOV32rr, X86::FpMOV, X86::MOV32rr
1145 };
1146 BuildMI(*MBB, IP, Opcode[SelectClass], 1, DestReg).addReg(TrueReg);
1147 if (SelectClass == cLong)
1148 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg+1).addReg(TrueReg+1);
1149 return;
1150 }
1151
Chris Lattner307ecba2004-03-30 22:39:09 +00001152 unsigned Opcode;
1153 if (SetCondInst *SCI = canFoldSetCCIntoBranchOrSelect(Cond)) {
1154 // We successfully folded the setcc into the select instruction.
1155
1156 unsigned OpNum = getSetCCNumber(SCI->getOpcode());
1157 OpNum = EmitComparison(OpNum, SCI->getOperand(0), SCI->getOperand(1), MBB,
1158 IP);
1159
1160 const Type *CompTy = SCI->getOperand(0)->getType();
1161 bool isSigned = CompTy->isSigned() && getClassB(CompTy) != cFP;
1162
1163 // LLVM -> X86 signed X86 unsigned
1164 // ----- ---------- ------------
1165 // seteq -> cmovNE cmovNE
1166 // setne -> cmovE cmovE
1167 // setlt -> cmovGE cmovAE
1168 // setge -> cmovL cmovB
1169 // setgt -> cmovLE cmovBE
1170 // setle -> cmovG cmovA
1171 // ----
1172 // cmovNS // Used by comparison with 0 optimization
1173 // cmovS
1174
1175 switch (SelectClass) {
Chris Lattner352eb482004-03-31 22:03:35 +00001176 default: assert(0 && "Unknown value class!");
1177 case cFP: {
1178 // Annoyingly, we don't have a full set of floating point conditional
1179 // moves. :(
1180 static const unsigned OpcodeTab[2][8] = {
1181 { X86::FCMOVNE, X86::FCMOVE, X86::FCMOVAE, X86::FCMOVB,
1182 X86::FCMOVBE, X86::FCMOVA, 0, 0 },
1183 { X86::FCMOVNE, X86::FCMOVE, 0, 0, 0, 0, 0, 0 },
1184 };
1185 Opcode = OpcodeTab[isSigned][OpNum];
1186
1187 // If opcode == 0, we hit a case that we don't support. Output a setcc
1188 // and compare the result against zero.
1189 if (Opcode == 0) {
1190 unsigned CompClass = getClassB(CompTy);
1191 unsigned CondReg;
1192 if (CompClass != cLong || OpNum < 2) {
1193 CondReg = makeAnotherReg(Type::BoolTy);
1194 // Handle normal comparisons with a setcc instruction...
1195 BuildMI(*MBB, IP, SetCCOpcodeTab[isSigned][OpNum], 0, CondReg);
1196 } else {
1197 // Long comparisons end up in the BL register.
1198 CondReg = X86::BL;
1199 }
1200
Chris Lattner68626c22004-03-31 22:22:36 +00001201 BuildMI(*MBB, IP, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
Chris Lattner352eb482004-03-31 22:03:35 +00001202 Opcode = X86::FCMOVE;
1203 }
1204 break;
1205 }
Chris Lattner307ecba2004-03-30 22:39:09 +00001206 case cByte:
1207 case cShort: {
1208 static const unsigned OpcodeTab[2][8] = {
1209 { X86::CMOVNE16rr, X86::CMOVE16rr, X86::CMOVAE16rr, X86::CMOVB16rr,
1210 X86::CMOVBE16rr, X86::CMOVA16rr, 0, 0 },
1211 { X86::CMOVNE16rr, X86::CMOVE16rr, X86::CMOVGE16rr, X86::CMOVL16rr,
1212 X86::CMOVLE16rr, X86::CMOVG16rr, X86::CMOVNS16rr, X86::CMOVS16rr },
1213 };
1214 Opcode = OpcodeTab[isSigned][OpNum];
1215 break;
1216 }
1217 case cInt:
1218 case cLong: {
1219 static const unsigned OpcodeTab[2][8] = {
1220 { X86::CMOVNE32rr, X86::CMOVE32rr, X86::CMOVAE32rr, X86::CMOVB32rr,
1221 X86::CMOVBE32rr, X86::CMOVA32rr, 0, 0 },
1222 { X86::CMOVNE32rr, X86::CMOVE32rr, X86::CMOVGE32rr, X86::CMOVL32rr,
1223 X86::CMOVLE32rr, X86::CMOVG32rr, X86::CMOVNS32rr, X86::CMOVS32rr },
1224 };
1225 Opcode = OpcodeTab[isSigned][OpNum];
1226 break;
1227 }
1228 }
1229 } else {
1230 // Get the value being branched on, and use it to set the condition codes.
1231 unsigned CondReg = getReg(Cond, MBB, IP);
Chris Lattner68626c22004-03-31 22:22:36 +00001232 BuildMI(*MBB, IP, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
Chris Lattner307ecba2004-03-30 22:39:09 +00001233 switch (SelectClass) {
Chris Lattner352eb482004-03-31 22:03:35 +00001234 default: assert(0 && "Unknown value class!");
1235 case cFP: Opcode = X86::FCMOVE; break;
Chris Lattner307ecba2004-03-30 22:39:09 +00001236 case cByte:
Chris Lattner352eb482004-03-31 22:03:35 +00001237 case cShort: Opcode = X86::CMOVE16rr; break;
Chris Lattner307ecba2004-03-30 22:39:09 +00001238 case cInt:
Chris Lattner352eb482004-03-31 22:03:35 +00001239 case cLong: Opcode = X86::CMOVE32rr; break;
Chris Lattner307ecba2004-03-30 22:39:09 +00001240 }
1241 }
Chris Lattner12d96a02004-03-30 21:22:00 +00001242
Chris Lattner12d96a02004-03-30 21:22:00 +00001243 unsigned RealDestReg = DestReg;
Chris Lattner12d96a02004-03-30 21:22:00 +00001244
Chris Lattner12d96a02004-03-30 21:22:00 +00001245
1246 // Annoyingly enough, X86 doesn't HAVE 8-bit conditional moves. Because of
1247 // this, we have to promote the incoming values to 16 bits, perform a 16-bit
1248 // cmove, then truncate the result.
1249 if (SelectClass == cByte) {
1250 DestReg = makeAnotherReg(Type::ShortTy);
1251 if (getClassB(TrueVal->getType()) == cByte) {
1252 // Promote the true value, by storing it into AL, and reading from AX.
1253 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::AL).addReg(TrueReg);
1254 BuildMI(*MBB, IP, X86::MOV8ri, 1, X86::AH).addImm(0);
1255 TrueReg = makeAnotherReg(Type::ShortTy);
1256 BuildMI(*MBB, IP, X86::MOV16rr, 1, TrueReg).addReg(X86::AX);
1257 }
1258 if (getClassB(FalseVal->getType()) == cByte) {
1259 // Promote the true value, by storing it into CL, and reading from CX.
1260 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(FalseReg);
1261 BuildMI(*MBB, IP, X86::MOV8ri, 1, X86::CH).addImm(0);
1262 FalseReg = makeAnotherReg(Type::ShortTy);
1263 BuildMI(*MBB, IP, X86::MOV16rr, 1, FalseReg).addReg(X86::CX);
1264 }
1265 }
1266
1267 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(TrueReg).addReg(FalseReg);
1268
1269 switch (SelectClass) {
1270 case cByte:
1271 // We did the computation with 16-bit registers. Truncate back to our
1272 // result by copying into AX then copying out AL.
1273 BuildMI(*MBB, IP, X86::MOV16rr, 1, X86::AX).addReg(DestReg);
1274 BuildMI(*MBB, IP, X86::MOV8rr, 1, RealDestReg).addReg(X86::AL);
1275 break;
1276 case cLong:
1277 // Move the upper half of the value as well.
1278 BuildMI(*MBB, IP, Opcode, 2,DestReg+1).addReg(TrueReg+1).addReg(FalseReg+1);
1279 break;
1280 }
1281}
Chris Lattner58c41fe2003-08-24 19:19:47 +00001282
1283
1284
Brian Gaekec2505982002-11-30 11:57:28 +00001285/// promote32 - Emit instructions to turn a narrow operand into a 32-bit-wide
1286/// operand, in the specified target register.
Misha Brukman538607f2004-03-01 23:53:11 +00001287///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001288void X86ISel::promote32(unsigned targetReg, const ValueRecord &VR) {
Chris Lattner9984fd02004-05-09 23:16:33 +00001289 bool isUnsigned = VR.Ty->isUnsigned() || VR.Ty == Type::BoolTy;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001290
Chris Lattner29bf0622004-04-06 01:21:00 +00001291 Value *Val = VR.Val;
1292 const Type *Ty = VR.Ty;
Chris Lattner502e36c2004-04-06 01:25:33 +00001293 if (Val) {
Chris Lattner29bf0622004-04-06 01:21:00 +00001294 if (Constant *C = dyn_cast<Constant>(Val)) {
1295 Val = ConstantExpr::getCast(C, Type::IntTy);
1296 Ty = Type::IntTy;
1297 }
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001298
Chris Lattner502e36c2004-04-06 01:25:33 +00001299 // If this is a simple constant, just emit a MOVri directly to avoid the
1300 // copy.
1301 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
1302 int TheVal = CI->getRawValue() & 0xFFFFFFFF;
Chris Lattner2b10b082004-05-12 16:35:04 +00001303 BuildMI(BB, X86::MOV32ri, 1, targetReg).addImm(TheVal);
Chris Lattner502e36c2004-04-06 01:25:33 +00001304 return;
1305 }
1306 }
1307
Chris Lattner29bf0622004-04-06 01:21:00 +00001308 // Make sure we have the register number for this value...
1309 unsigned Reg = Val ? getReg(Val) : VR.Reg;
1310
1311 switch (getClassB(Ty)) {
Chris Lattner94af4142002-12-25 05:13:53 +00001312 case cByte:
1313 // Extend value into target register (8->32)
1314 if (isUnsigned)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001315 BuildMI(BB, X86::MOVZX32rr8, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001316 else
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001317 BuildMI(BB, X86::MOVSX32rr8, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001318 break;
1319 case cShort:
1320 // Extend value into target register (16->32)
1321 if (isUnsigned)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001322 BuildMI(BB, X86::MOVZX32rr16, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001323 else
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001324 BuildMI(BB, X86::MOVSX32rr16, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001325 break;
1326 case cInt:
1327 // Move value into target register (32->32)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001328 BuildMI(BB, X86::MOV32rr, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001329 break;
1330 default:
1331 assert(0 && "Unpromotable operand class in promote32");
1332 }
Brian Gaekec2505982002-11-30 11:57:28 +00001333}
Chris Lattnerc5291f52002-10-27 21:16:59 +00001334
Chris Lattner72614082002-10-25 22:55:53 +00001335/// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such,
1336/// we have the following possibilities:
1337///
1338/// ret void: No return value, simply emit a 'ret' instruction
1339/// ret sbyte, ubyte : Extend value into EAX and return
1340/// ret short, ushort: Extend value into EAX and return
1341/// ret int, uint : Move value into EAX and return
1342/// ret pointer : Move value into EAX and return
Chris Lattner06925362002-11-17 21:56:38 +00001343/// ret long, ulong : Move value into EAX/EDX and return
1344/// ret float/double : Top of FP stack
Chris Lattner72614082002-10-25 22:55:53 +00001345///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001346void X86ISel::visitReturnInst(ReturnInst &I) {
Chris Lattner94af4142002-12-25 05:13:53 +00001347 if (I.getNumOperands() == 0) {
1348 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
1349 return;
1350 }
1351
1352 Value *RetVal = I.getOperand(0);
Chris Lattner3e130a22003-01-13 00:32:26 +00001353 switch (getClassB(RetVal->getType())) {
Chris Lattner94af4142002-12-25 05:13:53 +00001354 case cByte: // integral return values: extend or move into EAX and return
1355 case cShort:
1356 case cInt:
Chris Lattner29bf0622004-04-06 01:21:00 +00001357 promote32(X86::EAX, ValueRecord(RetVal));
Chris Lattnerdbd73722003-05-06 21:32:22 +00001358 // Declare that EAX is live on exit
Chris Lattnerc2489032003-05-07 19:21:28 +00001359 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
Chris Lattner94af4142002-12-25 05:13:53 +00001360 break;
Chris Lattner29bf0622004-04-06 01:21:00 +00001361 case cFP: { // Floats & Doubles: Return in ST(0)
1362 unsigned RetReg = getReg(RetVal);
Chris Lattner3e130a22003-01-13 00:32:26 +00001363 BuildMI(BB, X86::FpSETRESULT, 1).addReg(RetReg);
Chris Lattnerdbd73722003-05-06 21:32:22 +00001364 // Declare that top-of-stack is live on exit
Chris Lattnerc2489032003-05-07 19:21:28 +00001365 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
Chris Lattner94af4142002-12-25 05:13:53 +00001366 break;
Chris Lattner29bf0622004-04-06 01:21:00 +00001367 }
1368 case cLong: {
1369 unsigned RetReg = getReg(RetVal);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001370 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(RetReg);
1371 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RetReg+1);
Chris Lattnerdbd73722003-05-06 21:32:22 +00001372 // Declare that EAX & EDX are live on exit
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001373 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
1374 .addReg(X86::ESP);
Chris Lattner3e130a22003-01-13 00:32:26 +00001375 break;
Chris Lattner29bf0622004-04-06 01:21:00 +00001376 }
Chris Lattner94af4142002-12-25 05:13:53 +00001377 default:
Chris Lattner3e130a22003-01-13 00:32:26 +00001378 visitInstruction(I);
Chris Lattner94af4142002-12-25 05:13:53 +00001379 }
Chris Lattner43189d12002-11-17 20:07:45 +00001380 // Emit a 'ret' instruction
Chris Lattner94af4142002-12-25 05:13:53 +00001381 BuildMI(BB, X86::RET, 0);
Chris Lattner72614082002-10-25 22:55:53 +00001382}
1383
Chris Lattner55f6fab2003-01-16 18:07:23 +00001384// getBlockAfter - Return the basic block which occurs lexically after the
1385// specified one.
1386static inline BasicBlock *getBlockAfter(BasicBlock *BB) {
1387 Function::iterator I = BB; ++I; // Get iterator to next block
1388 return I != BB->getParent()->end() ? &*I : 0;
1389}
1390
Chris Lattner51b49a92002-11-02 19:45:49 +00001391/// visitBranchInst - Handle conditional and unconditional branches here. Note
1392/// that since code layout is frozen at this point, that if we are trying to
1393/// jump to a block that is the immediate successor of the current block, we can
Chris Lattner6d40c192003-01-16 16:43:00 +00001394/// just make a fall-through (but we don't currently).
Chris Lattner51b49a92002-11-02 19:45:49 +00001395///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001396void X86ISel::visitBranchInst(BranchInst &BI) {
Brian Gaekeea9ca672004-04-28 04:19:37 +00001397 // Update machine-CFG edges
1398 BB->addSuccessor (MBBMap[BI.getSuccessor(0)]);
1399 if (BI.isConditional())
1400 BB->addSuccessor (MBBMap[BI.getSuccessor(1)]);
1401
Chris Lattner55f6fab2003-01-16 18:07:23 +00001402 BasicBlock *NextBB = getBlockAfter(BI.getParent()); // BB after current one
1403
1404 if (!BI.isConditional()) { // Unconditional branch?
Chris Lattnercf93cdd2004-01-30 22:13:44 +00001405 if (BI.getSuccessor(0) != NextBB)
Brian Gaeke9f088e42004-05-14 06:54:56 +00001406 BuildMI(BB, X86::JMP, 1).addMBB(MBBMap[BI.getSuccessor(0)]);
Chris Lattner6d40c192003-01-16 16:43:00 +00001407 return;
1408 }
1409
1410 // See if we can fold the setcc into the branch itself...
Chris Lattner307ecba2004-03-30 22:39:09 +00001411 SetCondInst *SCI = canFoldSetCCIntoBranchOrSelect(BI.getCondition());
Chris Lattner6d40c192003-01-16 16:43:00 +00001412 if (SCI == 0) {
1413 // Nope, cannot fold setcc into this branch. Emit a branch on a condition
1414 // computed some other way...
Chris Lattner065faeb2002-12-28 20:24:02 +00001415 unsigned condReg = getReg(BI.getCondition());
Chris Lattner68626c22004-03-31 22:22:36 +00001416 BuildMI(BB, X86::TEST8rr, 2).addReg(condReg).addReg(condReg);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001417 if (BI.getSuccessor(1) == NextBB) {
1418 if (BI.getSuccessor(0) != NextBB)
Brian Gaeke9f088e42004-05-14 06:54:56 +00001419 BuildMI(BB, X86::JNE, 1).addMBB(MBBMap[BI.getSuccessor(0)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001420 } else {
Brian Gaeke9f088e42004-05-14 06:54:56 +00001421 BuildMI(BB, X86::JE, 1).addMBB(MBBMap[BI.getSuccessor(1)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001422
1423 if (BI.getSuccessor(0) != NextBB)
Brian Gaeke9f088e42004-05-14 06:54:56 +00001424 BuildMI(BB, X86::JMP, 1).addMBB(MBBMap[BI.getSuccessor(0)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001425 }
Chris Lattner6d40c192003-01-16 16:43:00 +00001426 return;
Chris Lattner94af4142002-12-25 05:13:53 +00001427 }
Chris Lattner6d40c192003-01-16 16:43:00 +00001428
1429 unsigned OpNum = getSetCCNumber(SCI->getOpcode());
Chris Lattner58c41fe2003-08-24 19:19:47 +00001430 MachineBasicBlock::iterator MII = BB->end();
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001431 OpNum = EmitComparison(OpNum, SCI->getOperand(0), SCI->getOperand(1), BB,MII);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001432
1433 const Type *CompTy = SCI->getOperand(0)->getType();
1434 bool isSigned = CompTy->isSigned() && getClassB(CompTy) != cFP;
Chris Lattner6d40c192003-01-16 16:43:00 +00001435
Chris Lattnerb2acc512003-10-19 21:09:10 +00001436
Chris Lattner6d40c192003-01-16 16:43:00 +00001437 // LLVM -> X86 signed X86 unsigned
1438 // ----- ---------- ------------
1439 // seteq -> je je
1440 // setne -> jne jne
1441 // setlt -> jl jb
Chris Lattner55f6fab2003-01-16 18:07:23 +00001442 // setge -> jge jae
Chris Lattner6d40c192003-01-16 16:43:00 +00001443 // setgt -> jg ja
1444 // setle -> jle jbe
Chris Lattnerb2acc512003-10-19 21:09:10 +00001445 // ----
1446 // js // Used by comparison with 0 optimization
1447 // jns
1448
1449 static const unsigned OpcodeTab[2][8] = {
1450 { X86::JE, X86::JNE, X86::JB, X86::JAE, X86::JA, X86::JBE, 0, 0 },
1451 { X86::JE, X86::JNE, X86::JL, X86::JGE, X86::JG, X86::JLE,
1452 X86::JS, X86::JNS },
Chris Lattner6d40c192003-01-16 16:43:00 +00001453 };
1454
Chris Lattner55f6fab2003-01-16 18:07:23 +00001455 if (BI.getSuccessor(0) != NextBB) {
Brian Gaeke9f088e42004-05-14 06:54:56 +00001456 BuildMI(BB, OpcodeTab[isSigned][OpNum], 1)
1457 .addMBB(MBBMap[BI.getSuccessor(0)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001458 if (BI.getSuccessor(1) != NextBB)
Brian Gaeke9f088e42004-05-14 06:54:56 +00001459 BuildMI(BB, X86::JMP, 1).addMBB(MBBMap[BI.getSuccessor(1)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001460 } else {
1461 // Change to the inverse condition...
1462 if (BI.getSuccessor(1) != NextBB) {
1463 OpNum ^= 1;
Brian Gaeke9f088e42004-05-14 06:54:56 +00001464 BuildMI(BB, OpcodeTab[isSigned][OpNum], 1)
1465 .addMBB(MBBMap[BI.getSuccessor(1)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001466 }
1467 }
Chris Lattner2df035b2002-11-02 19:27:56 +00001468}
1469
Chris Lattner3e130a22003-01-13 00:32:26 +00001470
1471/// doCall - This emits an abstract call instruction, setting up the arguments
1472/// and the return value as appropriate. For the actual function call itself,
1473/// it inserts the specified CallMI instruction into the stream.
1474///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001475void X86ISel::doCall(const ValueRecord &Ret, MachineInstr *CallMI,
1476 const std::vector<ValueRecord> &Args) {
Chris Lattner065faeb2002-12-28 20:24:02 +00001477 // Count how many bytes are to be pushed on the stack...
1478 unsigned NumBytes = 0;
Misha Brukman0d2cf3a2002-12-04 19:22:53 +00001479
Chris Lattner3e130a22003-01-13 00:32:26 +00001480 if (!Args.empty()) {
1481 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1482 switch (getClassB(Args[i].Ty)) {
Chris Lattner065faeb2002-12-28 20:24:02 +00001483 case cByte: case cShort: case cInt:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001484 NumBytes += 4; break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001485 case cLong:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001486 NumBytes += 8; break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001487 case cFP:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001488 NumBytes += Args[i].Ty == Type::FloatTy ? 4 : 8;
1489 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001490 default: assert(0 && "Unknown class!");
1491 }
1492
1493 // Adjust the stack pointer for the new arguments...
Chris Lattneree352852004-02-29 07:22:16 +00001494 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(NumBytes);
Chris Lattner065faeb2002-12-28 20:24:02 +00001495
1496 // Arguments go on the stack in reverse order, as specified by the ABI.
1497 unsigned ArgOffset = 0;
Chris Lattner3e130a22003-01-13 00:32:26 +00001498 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattnerce6096f2004-03-01 02:34:08 +00001499 unsigned ArgReg;
Chris Lattner3e130a22003-01-13 00:32:26 +00001500 switch (getClassB(Args[i].Ty)) {
Chris Lattner065faeb2002-12-28 20:24:02 +00001501 case cByte:
Chris Lattner2b10b082004-05-12 16:35:04 +00001502 if (Args[i].Val && isa<ConstantBool>(Args[i].Val)) {
1503 addRegOffset(BuildMI(BB, X86::MOV32mi, 5), X86::ESP, ArgOffset)
1504 .addImm(Args[i].Val == ConstantBool::True);
1505 break;
1506 }
1507 // FALL THROUGH
Chris Lattner21585222004-03-01 02:42:43 +00001508 case cShort:
1509 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1510 // Zero/Sign extend constant, then stuff into memory.
1511 ConstantInt *Val = cast<ConstantInt>(Args[i].Val);
1512 Val = cast<ConstantInt>(ConstantExpr::getCast(Val, Type::IntTy));
1513 addRegOffset(BuildMI(BB, X86::MOV32mi, 5), X86::ESP, ArgOffset)
1514 .addImm(Val->getRawValue() & 0xFFFFFFFF);
1515 } else {
1516 // Promote arg to 32 bits wide into a temporary register...
1517 ArgReg = makeAnotherReg(Type::UIntTy);
1518 promote32(ArgReg, Args[i]);
1519 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1520 X86::ESP, ArgOffset).addReg(ArgReg);
1521 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001522 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001523 case cInt:
Chris Lattner21585222004-03-01 02:42:43 +00001524 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1525 unsigned Val = cast<ConstantInt>(Args[i].Val)->getRawValue();
1526 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1527 X86::ESP, ArgOffset).addImm(Val);
Chris Lattnerb7cb9ff2004-05-13 15:26:48 +00001528 } else if (Args[i].Val && isa<ConstantPointerNull>(Args[i].Val)) {
1529 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1530 X86::ESP, ArgOffset).addImm(0);
Chris Lattner21585222004-03-01 02:42:43 +00001531 } else {
1532 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1533 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1534 X86::ESP, ArgOffset).addReg(ArgReg);
1535 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001536 break;
Chris Lattner3e130a22003-01-13 00:32:26 +00001537 case cLong:
Chris Lattner92900a62004-04-06 03:23:00 +00001538 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1539 uint64_t Val = cast<ConstantInt>(Args[i].Val)->getRawValue();
1540 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1541 X86::ESP, ArgOffset).addImm(Val & ~0U);
1542 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1543 X86::ESP, ArgOffset+4).addImm(Val >> 32ULL);
1544 } else {
1545 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1546 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1547 X86::ESP, ArgOffset).addReg(ArgReg);
1548 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1549 X86::ESP, ArgOffset+4).addReg(ArgReg+1);
1550 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001551 ArgOffset += 4; // 8 byte entry, not 4.
1552 break;
1553
Chris Lattner065faeb2002-12-28 20:24:02 +00001554 case cFP:
Chris Lattnerce6096f2004-03-01 02:34:08 +00001555 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001556 if (Args[i].Ty == Type::FloatTy) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001557 addRegOffset(BuildMI(BB, X86::FST32m, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001558 X86::ESP, ArgOffset).addReg(ArgReg);
1559 } else {
1560 assert(Args[i].Ty == Type::DoubleTy && "Unknown FP type!");
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001561 addRegOffset(BuildMI(BB, X86::FST64m, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001562 X86::ESP, ArgOffset).addReg(ArgReg);
1563 ArgOffset += 4; // 8 byte entry, not 4.
1564 }
1565 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001566
Chris Lattner3e130a22003-01-13 00:32:26 +00001567 default: assert(0 && "Unknown class!");
Chris Lattner065faeb2002-12-28 20:24:02 +00001568 }
1569 ArgOffset += 4;
Chris Lattner94af4142002-12-25 05:13:53 +00001570 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001571 } else {
Chris Lattneree352852004-02-29 07:22:16 +00001572 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(0);
Chris Lattner94af4142002-12-25 05:13:53 +00001573 }
Chris Lattner6e49a4b2002-12-13 14:13:27 +00001574
Chris Lattner3e130a22003-01-13 00:32:26 +00001575 BB->push_back(CallMI);
Misha Brukman0d2cf3a2002-12-04 19:22:53 +00001576
Chris Lattneree352852004-02-29 07:22:16 +00001577 BuildMI(BB, X86::ADJCALLSTACKUP, 1).addImm(NumBytes);
Chris Lattnera3243642002-12-04 23:45:28 +00001578
1579 // If there is a return value, scavenge the result from the location the call
1580 // leaves it in...
1581 //
Chris Lattner3e130a22003-01-13 00:32:26 +00001582 if (Ret.Ty != Type::VoidTy) {
1583 unsigned DestClass = getClassB(Ret.Ty);
1584 switch (DestClass) {
Brian Gaeke20244b72002-12-12 15:33:40 +00001585 case cByte:
1586 case cShort:
1587 case cInt: {
1588 // Integral results are in %eax, or the appropriate portion
1589 // thereof.
1590 static const unsigned regRegMove[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001591 X86::MOV8rr, X86::MOV16rr, X86::MOV32rr
Brian Gaeke20244b72002-12-12 15:33:40 +00001592 };
1593 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX };
Chris Lattner3e130a22003-01-13 00:32:26 +00001594 BuildMI(BB, regRegMove[DestClass], 1, Ret.Reg).addReg(AReg[DestClass]);
Chris Lattner4fa1acc2002-12-04 23:50:28 +00001595 break;
Brian Gaeke20244b72002-12-12 15:33:40 +00001596 }
Chris Lattner94af4142002-12-25 05:13:53 +00001597 case cFP: // Floating-point return values live in %ST(0)
Chris Lattner3e130a22003-01-13 00:32:26 +00001598 BuildMI(BB, X86::FpGETRESULT, 1, Ret.Reg);
Brian Gaeke20244b72002-12-12 15:33:40 +00001599 break;
Chris Lattner3e130a22003-01-13 00:32:26 +00001600 case cLong: // Long values are left in EDX:EAX
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001601 BuildMI(BB, X86::MOV32rr, 1, Ret.Reg).addReg(X86::EAX);
1602 BuildMI(BB, X86::MOV32rr, 1, Ret.Reg+1).addReg(X86::EDX);
Chris Lattner3e130a22003-01-13 00:32:26 +00001603 break;
1604 default: assert(0 && "Unknown class!");
Chris Lattner4fa1acc2002-12-04 23:50:28 +00001605 }
Chris Lattnera3243642002-12-04 23:45:28 +00001606 }
Brian Gaekefa8d5712002-11-22 11:07:01 +00001607}
Chris Lattner2df035b2002-11-02 19:27:56 +00001608
Chris Lattner3e130a22003-01-13 00:32:26 +00001609
1610/// visitCallInst - Push args on stack and do a procedure call instruction.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001611void X86ISel::visitCallInst(CallInst &CI) {
Chris Lattner3e130a22003-01-13 00:32:26 +00001612 MachineInstr *TheCall;
1613 if (Function *F = CI.getCalledFunction()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001614 // Is it an intrinsic function call?
Brian Gaeked0fde302003-11-11 22:41:34 +00001615 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001616 visitIntrinsicCall(ID, CI); // Special intrinsics are not handled here
1617 return;
1618 }
1619
Chris Lattner3e130a22003-01-13 00:32:26 +00001620 // Emit a CALL instruction with PC-relative displacement.
1621 TheCall = BuildMI(X86::CALLpcrel32, 1).addGlobalAddress(F, true);
1622 } else { // Emit an indirect call...
1623 unsigned Reg = getReg(CI.getCalledValue());
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001624 TheCall = BuildMI(X86::CALL32r, 1).addReg(Reg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001625 }
1626
1627 std::vector<ValueRecord> Args;
1628 for (unsigned i = 1, e = CI.getNumOperands(); i != e; ++i)
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001629 Args.push_back(ValueRecord(CI.getOperand(i)));
Chris Lattner3e130a22003-01-13 00:32:26 +00001630
1631 unsigned DestReg = CI.getType() != Type::VoidTy ? getReg(CI) : 0;
1632 doCall(ValueRecord(DestReg, CI.getType()), TheCall, Args);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001633}
Chris Lattner3e130a22003-01-13 00:32:26 +00001634
Chris Lattner44827152003-12-28 09:47:19 +00001635/// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
1636/// function, lowering any calls to unknown intrinsic functions into the
1637/// equivalent LLVM code.
Misha Brukman538607f2004-03-01 23:53:11 +00001638///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001639void X86ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) {
Chris Lattner44827152003-12-28 09:47:19 +00001640 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1641 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
1642 if (CallInst *CI = dyn_cast<CallInst>(I++))
1643 if (Function *F = CI->getCalledFunction())
1644 switch (F->getIntrinsicID()) {
Chris Lattneraed386e2003-12-28 09:53:23 +00001645 case Intrinsic::not_intrinsic:
Chris Lattner317201d2004-03-13 00:24:00 +00001646 case Intrinsic::vastart:
1647 case Intrinsic::vacopy:
1648 case Intrinsic::vaend:
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001649 case Intrinsic::returnaddress:
1650 case Intrinsic::frameaddress:
Chris Lattner915e5e52004-02-12 17:53:22 +00001651 case Intrinsic::memcpy:
Chris Lattner2a0f2242004-02-14 04:46:05 +00001652 case Intrinsic::memset:
Chris Lattnerdc572442004-06-15 21:36:44 +00001653 case Intrinsic::isunordered:
John Criswell4ffff9e2004-04-08 20:31:47 +00001654 case Intrinsic::readport:
1655 case Intrinsic::writeport:
Chris Lattner44827152003-12-28 09:47:19 +00001656 // We directly implement these intrinsics
1657 break;
John Criswelle5a4c152004-04-13 22:13:14 +00001658 case Intrinsic::readio: {
1659 // On X86, memory operations are in-order. Lower this intrinsic
1660 // into a volatile load.
1661 Instruction *Before = CI->getPrev();
Chris Lattnerb4fe76c2004-06-11 04:31:10 +00001662 LoadInst * LI = new LoadInst(CI->getOperand(1), "", true, CI);
1663 CI->replaceAllUsesWith(LI);
1664 BB->getInstList().erase(CI);
John Criswelle5a4c152004-04-13 22:13:14 +00001665 break;
1666 }
1667 case Intrinsic::writeio: {
1668 // On X86, memory operations are in-order. Lower this intrinsic
1669 // into a volatile store.
1670 Instruction *Before = CI->getPrev();
Chris Lattnerb4fe76c2004-06-11 04:31:10 +00001671 StoreInst *LI = new StoreInst(CI->getOperand(1),
1672 CI->getOperand(2), true, CI);
1673 CI->replaceAllUsesWith(LI);
1674 BB->getInstList().erase(CI);
John Criswelle5a4c152004-04-13 22:13:14 +00001675 break;
1676 }
Chris Lattner44827152003-12-28 09:47:19 +00001677 default:
1678 // All other intrinsic calls we must lower.
1679 Instruction *Before = CI->getPrev();
Chris Lattnerf70e0c22003-12-28 21:23:38 +00001680 TM.getIntrinsicLowering().LowerIntrinsicCall(CI);
Chris Lattner44827152003-12-28 09:47:19 +00001681 if (Before) { // Move iterator to instruction after call
Chris Lattnerb4fe76c2004-06-11 04:31:10 +00001682 I = Before; ++I;
Chris Lattner44827152003-12-28 09:47:19 +00001683 } else {
1684 I = BB->begin();
1685 }
1686 }
Chris Lattner44827152003-12-28 09:47:19 +00001687}
1688
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001689void X86ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001690 unsigned TmpReg1, TmpReg2;
1691 switch (ID) {
Chris Lattner5634b9f2004-03-13 00:24:52 +00001692 case Intrinsic::vastart:
Chris Lattnereca195e2003-05-08 19:44:13 +00001693 // Get the address of the first vararg value...
Chris Lattner73815062003-10-18 05:56:40 +00001694 TmpReg1 = getReg(CI);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001695 addFrameReference(BuildMI(BB, X86::LEA32r, 5, TmpReg1), VarArgsFrameIndex);
Chris Lattnereca195e2003-05-08 19:44:13 +00001696 return;
1697
Chris Lattner5634b9f2004-03-13 00:24:52 +00001698 case Intrinsic::vacopy:
Chris Lattner73815062003-10-18 05:56:40 +00001699 TmpReg1 = getReg(CI);
1700 TmpReg2 = getReg(CI.getOperand(1));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001701 BuildMI(BB, X86::MOV32rr, 1, TmpReg1).addReg(TmpReg2);
Chris Lattnereca195e2003-05-08 19:44:13 +00001702 return;
Chris Lattner5634b9f2004-03-13 00:24:52 +00001703 case Intrinsic::vaend: return; // Noop on X86
Chris Lattnereca195e2003-05-08 19:44:13 +00001704
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001705 case Intrinsic::returnaddress:
1706 case Intrinsic::frameaddress:
1707 TmpReg1 = getReg(CI);
1708 if (cast<Constant>(CI.getOperand(1))->isNullValue()) {
1709 if (ID == Intrinsic::returnaddress) {
1710 // Just load the return address
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001711 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, TmpReg1),
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001712 ReturnAddressIndex);
1713 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001714 addFrameReference(BuildMI(BB, X86::LEA32r, 4, TmpReg1),
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001715 ReturnAddressIndex, -4);
1716 }
1717 } else {
1718 // Values other than zero are not implemented yet.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001719 BuildMI(BB, X86::MOV32ri, 1, TmpReg1).addImm(0);
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001720 }
1721 return;
1722
Chris Lattnerdc572442004-06-15 21:36:44 +00001723 case Intrinsic::isunordered:
1724 TmpReg1 = getReg(CI.getOperand(1));
1725 TmpReg2 = getReg(CI.getOperand(2));
1726 emitUCOMr(BB, BB->end(), TmpReg2, TmpReg1);
1727 TmpReg2 = getReg(CI);
1728 BuildMI(BB, X86::SETPr, 0, TmpReg2);
1729 return;
1730
Chris Lattner915e5e52004-02-12 17:53:22 +00001731 case Intrinsic::memcpy: {
1732 assert(CI.getNumOperands() == 5 && "Illegal llvm.memcpy call!");
1733 unsigned Align = 1;
1734 if (ConstantInt *AlignC = dyn_cast<ConstantInt>(CI.getOperand(4))) {
1735 Align = AlignC->getRawValue();
1736 if (Align == 0) Align = 1;
1737 }
1738
1739 // Turn the byte code into # iterations
Chris Lattner915e5e52004-02-12 17:53:22 +00001740 unsigned CountReg;
Chris Lattner2a0f2242004-02-14 04:46:05 +00001741 unsigned Opcode;
Chris Lattner915e5e52004-02-12 17:53:22 +00001742 switch (Align & 3) {
1743 case 2: // WORD aligned
Chris Lattner07122832004-02-13 23:36:47 +00001744 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
1745 CountReg = getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/2));
1746 } else {
1747 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001748 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001749 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
Chris Lattner07122832004-02-13 23:36:47 +00001750 }
Chris Lattner2a0f2242004-02-14 04:46:05 +00001751 Opcode = X86::REP_MOVSW;
Chris Lattner915e5e52004-02-12 17:53:22 +00001752 break;
1753 case 0: // DWORD aligned
Chris Lattner07122832004-02-13 23:36:47 +00001754 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
1755 CountReg = getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/4));
1756 } else {
1757 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001758 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001759 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
Chris Lattner07122832004-02-13 23:36:47 +00001760 }
Chris Lattner2a0f2242004-02-14 04:46:05 +00001761 Opcode = X86::REP_MOVSD;
Chris Lattner915e5e52004-02-12 17:53:22 +00001762 break;
Chris Lattner8dd8d262004-02-26 01:20:02 +00001763 default: // BYTE aligned
Chris Lattner07122832004-02-13 23:36:47 +00001764 CountReg = getReg(CI.getOperand(3));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001765 Opcode = X86::REP_MOVSB;
Chris Lattner915e5e52004-02-12 17:53:22 +00001766 break;
1767 }
1768
1769 // No matter what the alignment is, we put the source in ESI, the
1770 // destination in EDI, and the count in ECX.
1771 TmpReg1 = getReg(CI.getOperand(1));
1772 TmpReg2 = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001773 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
1774 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
1775 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001776 BuildMI(BB, Opcode, 0);
1777 return;
1778 }
1779 case Intrinsic::memset: {
1780 assert(CI.getNumOperands() == 5 && "Illegal llvm.memset call!");
1781 unsigned Align = 1;
1782 if (ConstantInt *AlignC = dyn_cast<ConstantInt>(CI.getOperand(4))) {
1783 Align = AlignC->getRawValue();
1784 if (Align == 0) Align = 1;
Chris Lattner915e5e52004-02-12 17:53:22 +00001785 }
1786
Chris Lattner2a0f2242004-02-14 04:46:05 +00001787 // Turn the byte code into # iterations
Chris Lattner2a0f2242004-02-14 04:46:05 +00001788 unsigned CountReg;
1789 unsigned Opcode;
1790 if (ConstantInt *ValC = dyn_cast<ConstantInt>(CI.getOperand(2))) {
1791 unsigned Val = ValC->getRawValue() & 255;
1792
1793 // If the value is a constant, then we can potentially use larger copies.
1794 switch (Align & 3) {
1795 case 2: // WORD aligned
1796 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
Chris Lattner300d0ed2004-02-14 06:00:36 +00001797 CountReg =getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/2));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001798 } else {
1799 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001800 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001801 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001802 }
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001803 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001804 Opcode = X86::REP_STOSW;
1805 break;
1806 case 0: // DWORD aligned
1807 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
Chris Lattner300d0ed2004-02-14 06:00:36 +00001808 CountReg =getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/4));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001809 } else {
1810 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001811 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001812 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001813 }
1814 Val = (Val << 8) | Val;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001815 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001816 Opcode = X86::REP_STOSD;
1817 break;
Chris Lattner8dd8d262004-02-26 01:20:02 +00001818 default: // BYTE aligned
Chris Lattner2a0f2242004-02-14 04:46:05 +00001819 CountReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001820 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001821 Opcode = X86::REP_STOSB;
1822 break;
1823 }
1824 } else {
1825 // If it's not a constant value we are storing, just fall back. We could
1826 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
1827 unsigned ValReg = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001828 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001829 CountReg = getReg(CI.getOperand(3));
1830 Opcode = X86::REP_STOSB;
1831 }
1832
1833 // No matter what the alignment is, we put the source in ESI, the
1834 // destination in EDI, and the count in ECX.
1835 TmpReg1 = getReg(CI.getOperand(1));
1836 //TmpReg2 = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001837 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
1838 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001839 BuildMI(BB, Opcode, 0);
Chris Lattner915e5e52004-02-12 17:53:22 +00001840 return;
1841 }
1842
Chris Lattner87e18de2004-04-13 17:20:37 +00001843 case Intrinsic::readport: {
1844 // First, determine that the size of the operand falls within the acceptable
1845 // range for this architecture.
John Criswell4ffff9e2004-04-08 20:31:47 +00001846 //
Chris Lattner87e18de2004-04-13 17:20:37 +00001847 if (getClassB(CI.getOperand(1)->getType()) != cShort) {
John Criswellca6ea0f2004-04-08 22:39:13 +00001848 std::cerr << "llvm.readport: Address size is not 16 bits\n";
Chris Lattner87e18de2004-04-13 17:20:37 +00001849 exit(1);
John Criswellca6ea0f2004-04-08 22:39:13 +00001850 }
John Criswell4ffff9e2004-04-08 20:31:47 +00001851
John Criswell4ffff9e2004-04-08 20:31:47 +00001852 // Now, move the I/O port address into the DX register and use the IN
1853 // instruction to get the input data.
1854 //
Chris Lattner87e18de2004-04-13 17:20:37 +00001855 unsigned Class = getClass(CI.getCalledFunction()->getReturnType());
1856 unsigned DestReg = getReg(CI);
John Criswell4ffff9e2004-04-08 20:31:47 +00001857
Chris Lattner87e18de2004-04-13 17:20:37 +00001858 // If the port is a single-byte constant, use the immediate form.
1859 if (ConstantInt *C = dyn_cast<ConstantInt>(CI.getOperand(1)))
1860 if ((C->getRawValue() & 255) == C->getRawValue()) {
1861 switch (Class) {
1862 case cByte:
1863 BuildMI(BB, X86::IN8ri, 1).addImm((unsigned char)C->getRawValue());
1864 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AL);
1865 return;
1866 case cShort:
1867 BuildMI(BB, X86::IN16ri, 1).addImm((unsigned char)C->getRawValue());
1868 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AX);
1869 return;
1870 case cInt:
1871 BuildMI(BB, X86::IN32ri, 1).addImm((unsigned char)C->getRawValue());
1872 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::EAX);
1873 return;
1874 }
1875 }
1876
1877 unsigned Reg = getReg(CI.getOperand(1));
1878 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
1879 switch (Class) {
1880 case cByte:
1881 BuildMI(BB, X86::IN8rr, 0);
1882 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AL);
1883 break;
1884 case cShort:
1885 BuildMI(BB, X86::IN16rr, 0);
1886 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AX);
1887 break;
1888 case cInt:
1889 BuildMI(BB, X86::IN32rr, 0);
1890 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::EAX);
1891 break;
1892 default:
1893 std::cerr << "Cannot do input on this data type";
John Criswellca6ea0f2004-04-08 22:39:13 +00001894 exit (1);
1895 }
John Criswell4ffff9e2004-04-08 20:31:47 +00001896 return;
Chris Lattner87e18de2004-04-13 17:20:37 +00001897 }
John Criswell4ffff9e2004-04-08 20:31:47 +00001898
Chris Lattner87e18de2004-04-13 17:20:37 +00001899 case Intrinsic::writeport: {
1900 // First, determine that the size of the operand falls within the
1901 // acceptable range for this architecture.
1902 if (getClass(CI.getOperand(2)->getType()) != cShort) {
1903 std::cerr << "llvm.writeport: Address size is not 16 bits\n";
1904 exit(1);
1905 }
1906
1907 unsigned Class = getClassB(CI.getOperand(1)->getType());
1908 unsigned ValReg = getReg(CI.getOperand(1));
1909 switch (Class) {
1910 case cByte:
1911 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
1912 break;
1913 case cShort:
1914 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(ValReg);
1915 break;
1916 case cInt:
1917 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(ValReg);
1918 break;
1919 default:
1920 std::cerr << "llvm.writeport: invalid data type for X86 target";
1921 exit(1);
1922 }
1923
1924
1925 // If the port is a single-byte constant, use the immediate form.
1926 if (ConstantInt *C = dyn_cast<ConstantInt>(CI.getOperand(2)))
1927 if ((C->getRawValue() & 255) == C->getRawValue()) {
1928 static const unsigned O[] = { X86::OUT8ir, X86::OUT16ir, X86::OUT32ir };
1929 BuildMI(BB, O[Class], 1).addImm((unsigned char)C->getRawValue());
1930 return;
1931 }
1932
1933 // Otherwise, move the I/O port address into the DX register and the value
1934 // to write into the AL/AX/EAX register.
1935 static const unsigned Opc[] = { X86::OUT8rr, X86::OUT16rr, X86::OUT32rr };
1936 unsigned Reg = getReg(CI.getOperand(2));
1937 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
1938 BuildMI(BB, Opc[Class], 0);
1939 return;
1940 }
1941
Chris Lattner44827152003-12-28 09:47:19 +00001942 default: assert(0 && "Error: unknown intrinsics should have been lowered!");
Chris Lattnereca195e2003-05-08 19:44:13 +00001943 }
1944}
1945
Chris Lattner7dee5da2004-03-08 01:58:35 +00001946static bool isSafeToFoldLoadIntoInstruction(LoadInst &LI, Instruction &User) {
1947 if (LI.getParent() != User.getParent())
1948 return false;
1949 BasicBlock::iterator It = &LI;
1950 // Check all of the instructions between the load and the user. We should
1951 // really use alias analysis here, but for now we just do something simple.
1952 for (++It; It != BasicBlock::iterator(&User); ++It) {
1953 switch (It->getOpcode()) {
Chris Lattner85c84e72004-03-18 06:29:54 +00001954 case Instruction::Free:
Chris Lattner7dee5da2004-03-08 01:58:35 +00001955 case Instruction::Store:
1956 case Instruction::Call:
1957 case Instruction::Invoke:
1958 return false;
Chris Lattner133dbb12004-04-12 03:02:48 +00001959 case Instruction::Load:
1960 if (cast<LoadInst>(It)->isVolatile() && LI.isVolatile())
1961 return false;
1962 break;
Chris Lattner7dee5da2004-03-08 01:58:35 +00001963 }
1964 }
1965 return true;
1966}
1967
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001968/// visitSimpleBinary - Implement simple binary operators for integral types...
1969/// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or, 4 for
1970/// Xor.
Misha Brukman538607f2004-03-01 23:53:11 +00001971///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001972void X86ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001973 unsigned DestReg = getReg(B);
1974 MachineBasicBlock::iterator MI = BB->end();
Chris Lattner721d2d42004-03-08 01:18:36 +00001975 Value *Op0 = B.getOperand(0), *Op1 = B.getOperand(1);
Chris Lattnerc81e6ba2004-05-10 15:15:55 +00001976 unsigned Class = getClassB(B.getType());
Chris Lattner721d2d42004-03-08 01:18:36 +00001977
Chris Lattner7dee5da2004-03-08 01:58:35 +00001978 // Special case: op Reg, load [mem]
Chris Lattnerc81e6ba2004-05-10 15:15:55 +00001979 if (isa<LoadInst>(Op0) && !isa<LoadInst>(Op1) && Class != cLong &&
Chris Lattnerccd97962004-06-17 22:15:25 +00001980 Op0->hasOneUse() &&
Chris Lattnerc81e6ba2004-05-10 15:15:55 +00001981 isSafeToFoldLoadIntoInstruction(*cast<LoadInst>(Op0), B))
Chris Lattner7dee5da2004-03-08 01:58:35 +00001982 if (!B.swapOperands())
1983 std::swap(Op0, Op1); // Make sure any loads are in the RHS.
1984
Chris Lattnerccd97962004-06-17 22:15:25 +00001985 if (isa<LoadInst>(Op1) && Class != cLong && Op1->hasOneUse() &&
Chris Lattner7dee5da2004-03-08 01:58:35 +00001986 isSafeToFoldLoadIntoInstruction(*cast<LoadInst>(Op1), B)) {
1987
Chris Lattner95157f72004-04-11 22:05:45 +00001988 unsigned Opcode;
1989 if (Class != cFP) {
1990 static const unsigned OpcodeTab[][3] = {
1991 // Arithmetic operators
1992 { X86::ADD8rm, X86::ADD16rm, X86::ADD32rm }, // ADD
1993 { X86::SUB8rm, X86::SUB16rm, X86::SUB32rm }, // SUB
1994
1995 // Bitwise operators
1996 { X86::AND8rm, X86::AND16rm, X86::AND32rm }, // AND
1997 { X86:: OR8rm, X86:: OR16rm, X86:: OR32rm }, // OR
1998 { X86::XOR8rm, X86::XOR16rm, X86::XOR32rm }, // XOR
1999 };
2000 Opcode = OpcodeTab[OperatorClass][Class];
2001 } else {
2002 static const unsigned OpcodeTab[][2] = {
2003 { X86::FADD32m, X86::FADD64m }, // ADD
2004 { X86::FSUB32m, X86::FSUB64m }, // SUB
2005 };
2006 const Type *Ty = Op0->getType();
2007 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
2008 Opcode = OpcodeTab[OperatorClass][Ty == Type::DoubleTy];
2009 }
Chris Lattner7dee5da2004-03-08 01:58:35 +00002010
Chris Lattner7dee5da2004-03-08 01:58:35 +00002011 unsigned Op0r = getReg(Op0);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002012 if (AllocaInst *AI =
2013 dyn_castFixedAlloca(cast<LoadInst>(Op1)->getOperand(0))) {
2014 unsigned FI = getFixedSizedAllocaFI(AI);
2015 addFrameReference(BuildMI(BB, Opcode, 5, DestReg).addReg(Op0r), FI);
2016
2017 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002018 X86AddressMode AM;
2019 getAddressingMode(cast<LoadInst>(Op1)->getOperand(0), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002020
Reid Spencerfc989e12004-08-30 00:13:26 +00002021 addFullAddress(BuildMI(BB, Opcode, 5, DestReg).addReg(Op0r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002022 }
Chris Lattner7dee5da2004-03-08 01:58:35 +00002023 return;
2024 }
2025
Chris Lattner95157f72004-04-11 22:05:45 +00002026 // If this is a floating point subtract, check to see if we can fold the first
2027 // operand in.
2028 if (Class == cFP && OperatorClass == 1 &&
2029 isa<LoadInst>(Op0) &&
2030 isSafeToFoldLoadIntoInstruction(*cast<LoadInst>(Op0), B)) {
2031 const Type *Ty = Op0->getType();
2032 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
2033 unsigned Opcode = Ty == Type::FloatTy ? X86::FSUBR32m : X86::FSUBR64m;
2034
Chris Lattner95157f72004-04-11 22:05:45 +00002035 unsigned Op1r = getReg(Op1);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002036 if (AllocaInst *AI =
2037 dyn_castFixedAlloca(cast<LoadInst>(Op0)->getOperand(0))) {
2038 unsigned FI = getFixedSizedAllocaFI(AI);
2039 addFrameReference(BuildMI(BB, Opcode, 5, DestReg).addReg(Op1r), FI);
2040 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002041 X86AddressMode AM;
2042 getAddressingMode(cast<LoadInst>(Op0)->getOperand(0), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002043
Reid Spencerfc989e12004-08-30 00:13:26 +00002044 addFullAddress(BuildMI(BB, Opcode, 5, DestReg).addReg(Op1r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002045 }
Chris Lattner95157f72004-04-11 22:05:45 +00002046 return;
2047 }
2048
Chris Lattner721d2d42004-03-08 01:18:36 +00002049 emitSimpleBinaryOperation(BB, MI, Op0, Op1, OperatorClass, DestReg);
Chris Lattnerb515f6d2003-05-08 20:49:25 +00002050}
Chris Lattner3e130a22003-01-13 00:32:26 +00002051
Chris Lattner6621ed92004-04-11 21:23:56 +00002052
2053/// emitBinaryFPOperation - This method handles emission of floating point
2054/// Add (0), Sub (1), Mul (2), and Div (3) operations.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002055void X86ISel::emitBinaryFPOperation(MachineBasicBlock *BB,
2056 MachineBasicBlock::iterator IP,
2057 Value *Op0, Value *Op1,
2058 unsigned OperatorClass, unsigned DestReg) {
Chris Lattner6621ed92004-04-11 21:23:56 +00002059 // Special case: op Reg, <const fp>
2060 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1))
2061 if (!Op1C->isExactlyValue(+0.0) && !Op1C->isExactlyValue(+1.0)) {
2062 // Create a constant pool entry for this constant.
2063 MachineConstantPool *CP = F->getConstantPool();
2064 unsigned CPI = CP->getConstantPoolIndex(Op1C);
2065 const Type *Ty = Op1->getType();
2066
2067 static const unsigned OpcodeTab[][4] = {
2068 { X86::FADD32m, X86::FSUB32m, X86::FMUL32m, X86::FDIV32m }, // Float
2069 { X86::FADD64m, X86::FSUB64m, X86::FMUL64m, X86::FDIV64m }, // Double
2070 };
2071
2072 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
2073 unsigned Opcode = OpcodeTab[Ty != Type::FloatTy][OperatorClass];
2074 unsigned Op0r = getReg(Op0, BB, IP);
2075 addConstantPoolReference(BuildMI(*BB, IP, Opcode, 5,
2076 DestReg).addReg(Op0r), CPI);
2077 return;
2078 }
2079
Chris Lattner13c07fe2004-04-12 00:12:04 +00002080 // Special case: R1 = op <const fp>, R2
Chris Lattner6621ed92004-04-11 21:23:56 +00002081 if (ConstantFP *CFP = dyn_cast<ConstantFP>(Op0))
2082 if (CFP->isExactlyValue(-0.0) && OperatorClass == 1) {
2083 // -0.0 - X === -X
2084 unsigned op1Reg = getReg(Op1, BB, IP);
2085 BuildMI(*BB, IP, X86::FCHS, 1, DestReg).addReg(op1Reg);
2086 return;
2087 } else if (!CFP->isExactlyValue(+0.0) && !CFP->isExactlyValue(+1.0)) {
Chris Lattner13c07fe2004-04-12 00:12:04 +00002088 // R1 = op CST, R2 --> R1 = opr R2, CST
Chris Lattner6621ed92004-04-11 21:23:56 +00002089
2090 // Create a constant pool entry for this constant.
2091 MachineConstantPool *CP = F->getConstantPool();
2092 unsigned CPI = CP->getConstantPoolIndex(CFP);
2093 const Type *Ty = CFP->getType();
2094
2095 static const unsigned OpcodeTab[][4] = {
2096 { X86::FADD32m, X86::FSUBR32m, X86::FMUL32m, X86::FDIVR32m }, // Float
2097 { X86::FADD64m, X86::FSUBR64m, X86::FMUL64m, X86::FDIVR64m }, // Double
2098 };
2099
2100 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2101 unsigned Opcode = OpcodeTab[Ty != Type::FloatTy][OperatorClass];
2102 unsigned Op1r = getReg(Op1, BB, IP);
2103 addConstantPoolReference(BuildMI(*BB, IP, Opcode, 5,
2104 DestReg).addReg(Op1r), CPI);
2105 return;
2106 }
2107
2108 // General case.
2109 static const unsigned OpcodeTab[4] = {
2110 X86::FpADD, X86::FpSUB, X86::FpMUL, X86::FpDIV
2111 };
2112
2113 unsigned Opcode = OpcodeTab[OperatorClass];
2114 unsigned Op0r = getReg(Op0, BB, IP);
2115 unsigned Op1r = getReg(Op1, BB, IP);
2116 BuildMI(*BB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r);
2117}
2118
Chris Lattnerb2acc512003-10-19 21:09:10 +00002119/// emitSimpleBinaryOperation - Implement simple binary operators for integral
2120/// types... OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for
2121/// Or, 4 for Xor.
Chris Lattner68aad932002-11-02 20:13:22 +00002122///
Chris Lattnerb515f6d2003-05-08 20:49:25 +00002123/// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
2124/// and constant expression support.
Chris Lattnerb2acc512003-10-19 21:09:10 +00002125///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002126void X86ISel::emitSimpleBinaryOperation(MachineBasicBlock *MBB,
2127 MachineBasicBlock::iterator IP,
2128 Value *Op0, Value *Op1,
2129 unsigned OperatorClass,
2130 unsigned DestReg) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +00002131 unsigned Class = getClassB(Op0->getType());
Chris Lattnerb2acc512003-10-19 21:09:10 +00002132
Chris Lattner6621ed92004-04-11 21:23:56 +00002133 if (Class == cFP) {
2134 assert(OperatorClass < 2 && "No logical ops for FP!");
2135 emitBinaryFPOperation(MBB, IP, Op0, Op1, OperatorClass, DestReg);
2136 return;
2137 }
2138
Chris Lattner48b0c972004-04-11 20:26:20 +00002139 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0))
Chris Lattner667ea022004-06-18 00:50:37 +00002140 if (OperatorClass == 1) {
Chris Lattner48b0c972004-04-11 20:26:20 +00002141 static unsigned const NEGTab[] = {
2142 X86::NEG8r, X86::NEG16r, X86::NEG32r, 0, X86::NEG32r
2143 };
Chris Lattner667ea022004-06-18 00:50:37 +00002144
2145 // sub 0, X -> neg X
2146 if (CI->isNullValue()) {
2147 unsigned op1Reg = getReg(Op1, MBB, IP);
2148 BuildMI(*MBB, IP, NEGTab[Class], 1, DestReg).addReg(op1Reg);
Chris Lattner48b0c972004-04-11 20:26:20 +00002149
Chris Lattner667ea022004-06-18 00:50:37 +00002150 if (Class == cLong) {
2151 // We just emitted: Dl = neg Sl
2152 // Now emit : T = addc Sh, 0
2153 // : Dh = neg T
2154 unsigned T = makeAnotherReg(Type::IntTy);
2155 BuildMI(*MBB, IP, X86::ADC32ri, 2, T).addReg(op1Reg+1).addImm(0);
2156 BuildMI(*MBB, IP, X86::NEG32r, 1, DestReg+1).addReg(T);
2157 }
2158 return;
2159 } else if (Op1->hasOneUse() && Class != cLong) {
2160 // sub C, X -> tmp = neg X; DestReg = add tmp, C. This is better
2161 // than copying C into a temporary register, because of register
2162 // pressure (tmp and destreg can share a register.
2163 static unsigned const ADDRITab[] = {
2164 X86::ADD8ri, X86::ADD16ri, X86::ADD32ri, 0, X86::ADD32ri
2165 };
2166 unsigned op1Reg = getReg(Op1, MBB, IP);
2167 unsigned Tmp = makeAnotherReg(Op0->getType());
2168 BuildMI(*MBB, IP, NEGTab[Class], 1, Tmp).addReg(op1Reg);
Chris Lattner30483732004-06-20 07:49:54 +00002169 BuildMI(*MBB, IP, ADDRITab[Class], 2,
2170 DestReg).addReg(Tmp).addImm(CI->getRawValue());
Chris Lattner667ea022004-06-18 00:50:37 +00002171 return;
Chris Lattnerb2acc512003-10-19 21:09:10 +00002172 }
Chris Lattner48b0c972004-04-11 20:26:20 +00002173 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002174
Chris Lattner48b0c972004-04-11 20:26:20 +00002175 // Special case: op Reg, <const int>
2176 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner721d2d42004-03-08 01:18:36 +00002177 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002178
Chris Lattner721d2d42004-03-08 01:18:36 +00002179 // xor X, -1 -> not X
2180 if (OperatorClass == 4 && Op1C->isAllOnesValue()) {
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002181 static unsigned const NOTTab[] = {
2182 X86::NOT8r, X86::NOT16r, X86::NOT32r, 0, X86::NOT32r
2183 };
Chris Lattner721d2d42004-03-08 01:18:36 +00002184 BuildMI(*MBB, IP, NOTTab[Class], 1, DestReg).addReg(Op0r);
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002185 if (Class == cLong) // Invert the top part too
2186 BuildMI(*MBB, IP, X86::NOT32r, 1, DestReg+1).addReg(Op0r+1);
Chris Lattner721d2d42004-03-08 01:18:36 +00002187 return;
2188 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002189
Chris Lattner721d2d42004-03-08 01:18:36 +00002190 // add X, -1 -> dec X
Chris Lattner06521672004-04-06 03:36:57 +00002191 if (OperatorClass == 0 && Op1C->isAllOnesValue() && Class != cLong) {
2192 // Note that we can't use dec for 64-bit decrements, because it does not
2193 // set the carry flag!
2194 static unsigned const DECTab[] = { X86::DEC8r, X86::DEC16r, X86::DEC32r };
Chris Lattner721d2d42004-03-08 01:18:36 +00002195 BuildMI(*MBB, IP, DECTab[Class], 1, DestReg).addReg(Op0r);
2196 return;
2197 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002198
Chris Lattner721d2d42004-03-08 01:18:36 +00002199 // add X, 1 -> inc X
Chris Lattner06521672004-04-06 03:36:57 +00002200 if (OperatorClass == 0 && Op1C->equalsInt(1) && Class != cLong) {
2201 // Note that we can't use inc for 64-bit increments, because it does not
2202 // set the carry flag!
2203 static unsigned const INCTab[] = { X86::INC8r, X86::INC16r, X86::INC32r };
Alkis Evlogimenosbee8a092004-04-02 18:11:32 +00002204 BuildMI(*MBB, IP, INCTab[Class], 1, DestReg).addReg(Op0r);
Chris Lattner721d2d42004-03-08 01:18:36 +00002205 return;
2206 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002207
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002208 static const unsigned OpcodeTab[][5] = {
Chris Lattner721d2d42004-03-08 01:18:36 +00002209 // Arithmetic operators
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002210 { X86::ADD8ri, X86::ADD16ri, X86::ADD32ri, 0, X86::ADD32ri }, // ADD
2211 { X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, X86::SUB32ri }, // SUB
Chris Lattnerb2acc512003-10-19 21:09:10 +00002212
Chris Lattner721d2d42004-03-08 01:18:36 +00002213 // Bitwise operators
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002214 { X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, X86::AND32ri }, // AND
2215 { X86:: OR8ri, X86:: OR16ri, X86:: OR32ri, 0, X86::OR32ri }, // OR
2216 { X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, X86::XOR32ri }, // XOR
Chris Lattner721d2d42004-03-08 01:18:36 +00002217 };
2218
Chris Lattner721d2d42004-03-08 01:18:36 +00002219 unsigned Opcode = OpcodeTab[OperatorClass][Class];
Chris Lattner33f7fa32004-04-06 03:15:53 +00002220 unsigned Op1l = cast<ConstantInt>(Op1C)->getRawValue();
Chris Lattner721d2d42004-03-08 01:18:36 +00002221
Chris Lattner33f7fa32004-04-06 03:15:53 +00002222 if (Class != cLong) {
2223 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addImm(Op1l);
2224 return;
Chris Lattner6621ed92004-04-11 21:23:56 +00002225 }
2226
2227 // If this is a long value and the high or low bits have a special
2228 // property, emit some special cases.
2229 unsigned Op1h = cast<ConstantInt>(Op1C)->getRawValue() >> 32LL;
2230
2231 // If the constant is zero in the low 32-bits, just copy the low part
2232 // across and apply the normal 32-bit operation to the high parts. There
2233 // will be no carry or borrow into the top.
2234 if (Op1l == 0) {
2235 if (OperatorClass != 2) // All but and...
2236 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg).addReg(Op0r);
2237 else
2238 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
2239 BuildMI(*MBB, IP, OpcodeTab[OperatorClass][cLong], 2, DestReg+1)
2240 .addReg(Op0r+1).addImm(Op1h);
Chris Lattner33f7fa32004-04-06 03:15:53 +00002241 return;
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002242 }
Chris Lattner6621ed92004-04-11 21:23:56 +00002243
2244 // If this is a logical operation and the top 32-bits are zero, just
2245 // operate on the lower 32.
2246 if (Op1h == 0 && OperatorClass > 1) {
2247 BuildMI(*MBB, IP, OpcodeTab[OperatorClass][cLong], 2, DestReg)
2248 .addReg(Op0r).addImm(Op1l);
2249 if (OperatorClass != 2) // All but and
2250 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg+1).addReg(Op0r+1);
2251 else
2252 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
2253 return;
2254 }
2255
2256 // TODO: We could handle lots of other special cases here, such as AND'ing
2257 // with 0xFFFFFFFF00000000 -> noop, etc.
2258
2259 // Otherwise, code generate the full operation with a constant.
2260 static const unsigned TopTab[] = {
2261 X86::ADC32ri, X86::SBB32ri, X86::AND32ri, X86::OR32ri, X86::XOR32ri
2262 };
2263
2264 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addImm(Op1l);
2265 BuildMI(*MBB, IP, TopTab[OperatorClass], 2, DestReg+1)
2266 .addReg(Op0r+1).addImm(Op1h);
2267 return;
Chris Lattner721d2d42004-03-08 01:18:36 +00002268 }
2269
2270 // Finally, handle the general case now.
Chris Lattner7ba92302004-04-06 02:13:25 +00002271 static const unsigned OpcodeTab[][5] = {
Chris Lattner721d2d42004-03-08 01:18:36 +00002272 // Arithmetic operators
Chris Lattner6621ed92004-04-11 21:23:56 +00002273 { X86::ADD8rr, X86::ADD16rr, X86::ADD32rr, 0, X86::ADD32rr }, // ADD
2274 { X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, 0, X86::SUB32rr }, // SUB
Chris Lattner721d2d42004-03-08 01:18:36 +00002275
Chris Lattnerb2acc512003-10-19 21:09:10 +00002276 // Bitwise operators
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002277 { X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, X86::AND32rr }, // AND
2278 { X86:: OR8rr, X86:: OR16rr, X86:: OR32rr, 0, X86:: OR32rr }, // OR
2279 { X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, X86::XOR32rr }, // XOR
Chris Lattnerb2acc512003-10-19 21:09:10 +00002280 };
Chris Lattner721d2d42004-03-08 01:18:36 +00002281
Chris Lattnerb2acc512003-10-19 21:09:10 +00002282 unsigned Opcode = OpcodeTab[OperatorClass][Class];
Chris Lattner721d2d42004-03-08 01:18:36 +00002283 unsigned Op0r = getReg(Op0, MBB, IP);
2284 unsigned Op1r = getReg(Op1, MBB, IP);
2285 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r);
2286
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002287 if (Class == cLong) { // Handle the upper 32 bits of long values...
Chris Lattner721d2d42004-03-08 01:18:36 +00002288 static const unsigned TopTab[] = {
2289 X86::ADC32rr, X86::SBB32rr, X86::AND32rr, X86::OR32rr, X86::XOR32rr
2290 };
2291 BuildMI(*MBB, IP, TopTab[OperatorClass], 2,
2292 DestReg+1).addReg(Op0r+1).addReg(Op1r+1);
2293 }
Chris Lattnere2954c82002-11-02 20:04:26 +00002294}
2295
Chris Lattner3e130a22003-01-13 00:32:26 +00002296/// doMultiply - Emit appropriate instructions to multiply together the
2297/// registers op0Reg and op1Reg, and put the result in DestReg. The type of the
2298/// result should be given as DestTy.
2299///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002300void X86ISel::doMultiply(MachineBasicBlock *MBB,
2301 MachineBasicBlock::iterator MBBI,
2302 unsigned DestReg, const Type *DestTy,
2303 unsigned op0Reg, unsigned op1Reg) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002304 unsigned Class = getClass(DestTy);
Chris Lattner94af4142002-12-25 05:13:53 +00002305 switch (Class) {
Chris Lattner0f1c4612003-06-21 17:16:58 +00002306 case cInt:
2307 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002308 BuildMI(*MBB, MBBI, Class == cInt ? X86::IMUL32rr:X86::IMUL16rr, 2, DestReg)
Chris Lattner0f1c4612003-06-21 17:16:58 +00002309 .addReg(op0Reg).addReg(op1Reg);
2310 return;
2311 case cByte:
2312 // Must use the MUL instruction, which forces use of AL...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002313 BuildMI(*MBB, MBBI, X86::MOV8rr, 1, X86::AL).addReg(op0Reg);
2314 BuildMI(*MBB, MBBI, X86::MUL8r, 1).addReg(op1Reg);
2315 BuildMI(*MBB, MBBI, X86::MOV8rr, 1, DestReg).addReg(X86::AL);
Chris Lattner0f1c4612003-06-21 17:16:58 +00002316 return;
Chris Lattner94af4142002-12-25 05:13:53 +00002317 default:
Chris Lattner3e130a22003-01-13 00:32:26 +00002318 case cLong: assert(0 && "doMultiply cannot operate on LONG values!");
Chris Lattner94af4142002-12-25 05:13:53 +00002319 }
Brian Gaeke20244b72002-12-12 15:33:40 +00002320}
2321
Chris Lattnerb2acc512003-10-19 21:09:10 +00002322// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
2323// returns zero when the input is not exactly a power of two.
2324static unsigned ExactLog2(unsigned Val) {
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002325 if (Val == 0 || (Val & (Val-1))) return 0;
Chris Lattnerb2acc512003-10-19 21:09:10 +00002326 unsigned Count = 0;
2327 while (Val != 1) {
Chris Lattnerb2acc512003-10-19 21:09:10 +00002328 Val >>= 1;
2329 ++Count;
2330 }
2331 return Count+1;
2332}
2333
Chris Lattner462fa822004-04-11 20:56:28 +00002334
2335/// doMultiplyConst - This function is specialized to efficiently codegen an 8,
2336/// 16, or 32-bit integer multiply by a constant.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002337void X86ISel::doMultiplyConst(MachineBasicBlock *MBB,
2338 MachineBasicBlock::iterator IP,
2339 unsigned DestReg, const Type *DestTy,
2340 unsigned op0Reg, unsigned ConstRHS) {
Chris Lattner6ab06d52004-04-06 04:55:43 +00002341 static const unsigned MOVrrTab[] = {X86::MOV8rr, X86::MOV16rr, X86::MOV32rr};
2342 static const unsigned MOVriTab[] = {X86::MOV8ri, X86::MOV16ri, X86::MOV32ri};
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002343 static const unsigned ADDrrTab[] = {X86::ADD8rr, X86::ADD16rr, X86::ADD32rr};
Chris Lattner596b97f2004-07-19 23:47:21 +00002344 static const unsigned NEGrTab[] = {X86::NEG8r , X86::NEG16r , X86::NEG32r };
Chris Lattner6ab06d52004-04-06 04:55:43 +00002345
Chris Lattnerb2acc512003-10-19 21:09:10 +00002346 unsigned Class = getClass(DestTy);
Chris Lattner596b97f2004-07-19 23:47:21 +00002347 unsigned TmpReg;
Chris Lattnerb2acc512003-10-19 21:09:10 +00002348
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002349 // Handle special cases here.
2350 switch (ConstRHS) {
Chris Lattner596b97f2004-07-19 23:47:21 +00002351 case -2:
2352 TmpReg = makeAnotherReg(DestTy);
2353 BuildMI(*MBB, IP, NEGrTab[Class], 1, TmpReg).addReg(op0Reg);
2354 BuildMI(*MBB, IP, ADDrrTab[Class], 1,DestReg).addReg(TmpReg).addReg(TmpReg);
2355 return;
2356 case -1:
2357 BuildMI(*MBB, IP, NEGrTab[Class], 1, DestReg).addReg(op0Reg);
2358 return;
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002359 case 0:
Chris Lattner6ab06d52004-04-06 04:55:43 +00002360 BuildMI(*MBB, IP, MOVriTab[Class], 1, DestReg).addImm(0);
2361 return;
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002362 case 1:
Chris Lattner6ab06d52004-04-06 04:55:43 +00002363 BuildMI(*MBB, IP, MOVrrTab[Class], 1, DestReg).addReg(op0Reg);
2364 return;
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002365 case 2:
2366 BuildMI(*MBB, IP, ADDrrTab[Class], 1,DestReg).addReg(op0Reg).addReg(op0Reg);
2367 return;
2368 case 3:
2369 case 5:
2370 case 9:
2371 if (Class == cInt) {
Reid Spencerfc989e12004-08-30 00:13:26 +00002372 X86AddressMode AM;
2373 AM.BaseType = X86AddressMode::RegBase;
2374 AM.Base.Reg = op0Reg;
2375 AM.Scale = ConstRHS-1;
2376 AM.IndexReg = op0Reg;
2377 AM.Disp = 0;
2378 addFullAddress(BuildMI(*MBB, IP, X86::LEA32r, 5, DestReg), AM);
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002379 return;
2380 }
Chris Lattner596b97f2004-07-19 23:47:21 +00002381 case -3:
2382 case -5:
2383 case -9:
2384 if (Class == cInt) {
2385 TmpReg = makeAnotherReg(DestTy);
Reid Spencerfc989e12004-08-30 00:13:26 +00002386 X86AddressMode AM;
2387 AM.BaseType = X86AddressMode::RegBase;
2388 AM.Base.Reg = op0Reg;
2389 AM.Scale = -ConstRHS-1;
2390 AM.IndexReg = op0Reg;
2391 AM.Disp = 0;
2392 addFullAddress(BuildMI(*MBB, IP, X86::LEA32r, 5, TmpReg), AM);
Chris Lattner596b97f2004-07-19 23:47:21 +00002393 BuildMI(*MBB, IP, NEGrTab[Class], 1, DestReg).addReg(TmpReg);
2394 return;
2395 }
Chris Lattner6ab06d52004-04-06 04:55:43 +00002396 }
2397
Chris Lattnerb2acc512003-10-19 21:09:10 +00002398 // If the element size is exactly a power of 2, use a shift to get it.
2399 if (unsigned Shift = ExactLog2(ConstRHS)) {
2400 switch (Class) {
2401 default: assert(0 && "Unknown class for this function!");
2402 case cByte:
Chris Lattner596b97f2004-07-19 23:47:21 +00002403 BuildMI(*MBB, IP, X86::SHL8ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002404 return;
2405 case cShort:
Chris Lattner596b97f2004-07-19 23:47:21 +00002406 BuildMI(*MBB, IP, X86::SHL16ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002407 return;
2408 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002409 BuildMI(*MBB, IP, X86::SHL32ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002410 return;
2411 }
2412 }
Chris Lattner596b97f2004-07-19 23:47:21 +00002413
2414 // If the element size is a negative power of 2, use a shift/neg to get it.
2415 if (unsigned Shift = ExactLog2(-ConstRHS)) {
2416 TmpReg = makeAnotherReg(DestTy);
2417 BuildMI(*MBB, IP, NEGrTab[Class], 1, TmpReg).addReg(op0Reg);
2418 switch (Class) {
2419 default: assert(0 && "Unknown class for this function!");
2420 case cByte:
2421 BuildMI(*MBB, IP, X86::SHL8ri,2, DestReg).addReg(TmpReg).addImm(Shift-1);
2422 return;
2423 case cShort:
2424 BuildMI(*MBB, IP, X86::SHL16ri,2, DestReg).addReg(TmpReg).addImm(Shift-1);
2425 return;
2426 case cInt:
2427 BuildMI(*MBB, IP, X86::SHL32ri,2, DestReg).addReg(TmpReg).addImm(Shift-1);
2428 return;
2429 }
2430 }
Chris Lattnerc01d1232003-10-20 03:42:58 +00002431
2432 if (Class == cShort) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002433 BuildMI(*MBB, IP, X86::IMUL16rri,2,DestReg).addReg(op0Reg).addImm(ConstRHS);
Chris Lattnerc01d1232003-10-20 03:42:58 +00002434 return;
2435 } else if (Class == cInt) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002436 BuildMI(*MBB, IP, X86::IMUL32rri,2,DestReg).addReg(op0Reg).addImm(ConstRHS);
Chris Lattnerc01d1232003-10-20 03:42:58 +00002437 return;
2438 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002439
2440 // Most general case, emit a normal multiply...
Chris Lattner596b97f2004-07-19 23:47:21 +00002441 TmpReg = makeAnotherReg(DestTy);
Chris Lattneree352852004-02-29 07:22:16 +00002442 BuildMI(*MBB, IP, MOVriTab[Class], 1, TmpReg).addImm(ConstRHS);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002443
2444 // Emit a MUL to multiply the register holding the index by
2445 // elementSize, putting the result in OffsetReg.
2446 doMultiply(MBB, IP, DestReg, DestTy, op0Reg, TmpReg);
2447}
2448
Chris Lattnerca9671d2002-11-02 20:28:58 +00002449/// visitMul - Multiplies are not simple binary operators because they must deal
2450/// with the EAX register explicitly.
2451///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002452void X86ISel::visitMul(BinaryOperator &I) {
Chris Lattner462fa822004-04-11 20:56:28 +00002453 unsigned ResultReg = getReg(I);
2454
Chris Lattner95157f72004-04-11 22:05:45 +00002455 Value *Op0 = I.getOperand(0);
2456 Value *Op1 = I.getOperand(1);
2457
2458 // Fold loads into floating point multiplies.
2459 if (getClass(Op0->getType()) == cFP) {
2460 if (isa<LoadInst>(Op0) && !isa<LoadInst>(Op1))
2461 if (!I.swapOperands())
2462 std::swap(Op0, Op1); // Make sure any loads are in the RHS.
2463 if (LoadInst *LI = dyn_cast<LoadInst>(Op1))
2464 if (isSafeToFoldLoadIntoInstruction(*LI, I)) {
2465 const Type *Ty = Op0->getType();
2466 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2467 unsigned Opcode = Ty == Type::FloatTy ? X86::FMUL32m : X86::FMUL64m;
2468
Chris Lattner95157f72004-04-11 22:05:45 +00002469 unsigned Op0r = getReg(Op0);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002470 if (AllocaInst *AI = dyn_castFixedAlloca(LI->getOperand(0))) {
2471 unsigned FI = getFixedSizedAllocaFI(AI);
2472 addFrameReference(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op0r), FI);
2473 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002474 X86AddressMode AM;
2475 getAddressingMode(LI->getOperand(0), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002476
Reid Spencerfc989e12004-08-30 00:13:26 +00002477 addFullAddress(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op0r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002478 }
Chris Lattner95157f72004-04-11 22:05:45 +00002479 return;
2480 }
2481 }
2482
Chris Lattner462fa822004-04-11 20:56:28 +00002483 MachineBasicBlock::iterator IP = BB->end();
Chris Lattner95157f72004-04-11 22:05:45 +00002484 emitMultiply(BB, IP, Op0, Op1, ResultReg);
Chris Lattner462fa822004-04-11 20:56:28 +00002485}
2486
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002487void X86ISel::emitMultiply(MachineBasicBlock *MBB,
2488 MachineBasicBlock::iterator IP,
2489 Value *Op0, Value *Op1, unsigned DestReg) {
Chris Lattner462fa822004-04-11 20:56:28 +00002490 MachineBasicBlock &BB = *MBB;
2491 TypeClass Class = getClass(Op0->getType());
Chris Lattner3e130a22003-01-13 00:32:26 +00002492
2493 // Simple scalar multiply?
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002494 unsigned Op0Reg = getReg(Op0, &BB, IP);
Chris Lattner462fa822004-04-11 20:56:28 +00002495 switch (Class) {
2496 case cByte:
2497 case cShort:
2498 case cInt:
2499 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner462fa822004-04-11 20:56:28 +00002500 unsigned Val = (unsigned)CI->getRawValue(); // Isn't a 64-bit constant
2501 doMultiplyConst(&BB, IP, DestReg, Op0->getType(), Op0Reg, Val);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002502 } else {
Chris Lattner462fa822004-04-11 20:56:28 +00002503 unsigned Op1Reg = getReg(Op1, &BB, IP);
2504 doMultiply(&BB, IP, DestReg, Op1->getType(), Op0Reg, Op1Reg);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002505 }
Chris Lattner462fa822004-04-11 20:56:28 +00002506 return;
2507 case cFP:
Chris Lattner6621ed92004-04-11 21:23:56 +00002508 emitBinaryFPOperation(MBB, IP, Op0, Op1, 2, DestReg);
2509 return;
Chris Lattner462fa822004-04-11 20:56:28 +00002510 case cLong:
2511 break;
2512 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002513
Chris Lattner462fa822004-04-11 20:56:28 +00002514 // Long value. We have to do things the hard way...
2515 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
2516 unsigned CLow = CI->getRawValue();
2517 unsigned CHi = CI->getRawValue() >> 32;
2518
2519 if (CLow == 0) {
2520 // If the low part of the constant is all zeros, things are simple.
2521 BuildMI(BB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
2522 doMultiplyConst(&BB, IP, DestReg+1, Type::UIntTy, Op0Reg, CHi);
2523 return;
2524 }
2525
2526 // Multiply the two low parts... capturing carry into EDX
2527 unsigned OverflowReg = 0;
2528 if (CLow == 1) {
2529 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg).addReg(Op0Reg);
Chris Lattner028adc42004-04-06 04:29:36 +00002530 } else {
Chris Lattner462fa822004-04-11 20:56:28 +00002531 unsigned Op1RegL = makeAnotherReg(Type::UIntTy);
2532 OverflowReg = makeAnotherReg(Type::UIntTy);
2533 BuildMI(BB, IP, X86::MOV32ri, 1, Op1RegL).addImm(CLow);
2534 BuildMI(BB, IP, X86::MOV32rr, 1, X86::EAX).addReg(Op0Reg);
2535 BuildMI(BB, IP, X86::MUL32r, 1).addReg(Op1RegL); // AL*BL
Chris Lattner028adc42004-04-06 04:29:36 +00002536
Chris Lattner462fa822004-04-11 20:56:28 +00002537 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg).addReg(X86::EAX); // AL*BL
2538 BuildMI(BB, IP, X86::MOV32rr, 1,
2539 OverflowReg).addReg(X86::EDX); // AL*BL >> 32
2540 }
2541
2542 unsigned AHBLReg = makeAnotherReg(Type::UIntTy); // AH*BL
2543 doMultiplyConst(&BB, IP, AHBLReg, Type::UIntTy, Op0Reg+1, CLow);
2544
2545 unsigned AHBLplusOverflowReg;
2546 if (OverflowReg) {
2547 AHBLplusOverflowReg = makeAnotherReg(Type::UIntTy);
2548 BuildMI(BB, IP, X86::ADD32rr, 2, // AH*BL+(AL*BL >> 32)
Chris Lattner028adc42004-04-06 04:29:36 +00002549 AHBLplusOverflowReg).addReg(AHBLReg).addReg(OverflowReg);
Chris Lattner462fa822004-04-11 20:56:28 +00002550 } else {
2551 AHBLplusOverflowReg = AHBLReg;
2552 }
2553
2554 if (CHi == 0) {
2555 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg+1).addReg(AHBLplusOverflowReg);
2556 } else {
Chris Lattner028adc42004-04-06 04:29:36 +00002557 unsigned ALBHReg = makeAnotherReg(Type::UIntTy); // AL*BH
Chris Lattner462fa822004-04-11 20:56:28 +00002558 doMultiplyConst(&BB, IP, ALBHReg, Type::UIntTy, Op0Reg, CHi);
Chris Lattner028adc42004-04-06 04:29:36 +00002559
Chris Lattner462fa822004-04-11 20:56:28 +00002560 BuildMI(BB, IP, X86::ADD32rr, 2, // AL*BH + AH*BL + (AL*BL >> 32)
Chris Lattner028adc42004-04-06 04:29:36 +00002561 DestReg+1).addReg(AHBLplusOverflowReg).addReg(ALBHReg);
2562 }
Chris Lattner462fa822004-04-11 20:56:28 +00002563 return;
Chris Lattner3e130a22003-01-13 00:32:26 +00002564 }
Chris Lattner462fa822004-04-11 20:56:28 +00002565
2566 // General 64x64 multiply
2567
2568 unsigned Op1Reg = getReg(Op1, &BB, IP);
2569 // Multiply the two low parts... capturing carry into EDX
2570 BuildMI(BB, IP, X86::MOV32rr, 1, X86::EAX).addReg(Op0Reg);
2571 BuildMI(BB, IP, X86::MUL32r, 1).addReg(Op1Reg); // AL*BL
2572
2573 unsigned OverflowReg = makeAnotherReg(Type::UIntTy);
2574 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg).addReg(X86::EAX); // AL*BL
2575 BuildMI(BB, IP, X86::MOV32rr, 1,
2576 OverflowReg).addReg(X86::EDX); // AL*BL >> 32
2577
2578 unsigned AHBLReg = makeAnotherReg(Type::UIntTy); // AH*BL
2579 BuildMI(BB, IP, X86::IMUL32rr, 2,
2580 AHBLReg).addReg(Op0Reg+1).addReg(Op1Reg);
2581
2582 unsigned AHBLplusOverflowReg = makeAnotherReg(Type::UIntTy);
2583 BuildMI(BB, IP, X86::ADD32rr, 2, // AH*BL+(AL*BL >> 32)
2584 AHBLplusOverflowReg).addReg(AHBLReg).addReg(OverflowReg);
2585
2586 unsigned ALBHReg = makeAnotherReg(Type::UIntTy); // AL*BH
2587 BuildMI(BB, IP, X86::IMUL32rr, 2,
2588 ALBHReg).addReg(Op0Reg).addReg(Op1Reg+1);
2589
2590 BuildMI(BB, IP, X86::ADD32rr, 2, // AL*BH + AH*BL + (AL*BL >> 32)
2591 DestReg+1).addReg(AHBLplusOverflowReg).addReg(ALBHReg);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002592}
Chris Lattnerca9671d2002-11-02 20:28:58 +00002593
Chris Lattner06925362002-11-17 21:56:38 +00002594
Chris Lattnerf01729e2002-11-02 20:54:46 +00002595/// visitDivRem - Handle division and remainder instructions... these
2596/// instruction both require the same instructions to be generated, they just
2597/// select the result from a different register. Note that both of these
2598/// instructions work differently for signed and unsigned operands.
2599///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002600void X86ISel::visitDivRem(BinaryOperator &I) {
Chris Lattnercadff442003-10-23 17:21:43 +00002601 unsigned ResultReg = getReg(I);
Chris Lattner95157f72004-04-11 22:05:45 +00002602 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2603
2604 // Fold loads into floating point divides.
2605 if (getClass(Op0->getType()) == cFP) {
2606 if (LoadInst *LI = dyn_cast<LoadInst>(Op1))
2607 if (isSafeToFoldLoadIntoInstruction(*LI, I)) {
2608 const Type *Ty = Op0->getType();
2609 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2610 unsigned Opcode = Ty == Type::FloatTy ? X86::FDIV32m : X86::FDIV64m;
2611
Chris Lattner95157f72004-04-11 22:05:45 +00002612 unsigned Op0r = getReg(Op0);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002613 if (AllocaInst *AI = dyn_castFixedAlloca(LI->getOperand(0))) {
2614 unsigned FI = getFixedSizedAllocaFI(AI);
2615 addFrameReference(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op0r), FI);
2616 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002617 X86AddressMode AM;
2618 getAddressingMode(LI->getOperand(0), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002619
Reid Spencerfc989e12004-08-30 00:13:26 +00002620 addFullAddress(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op0r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002621 }
Chris Lattner95157f72004-04-11 22:05:45 +00002622 return;
2623 }
2624
2625 if (LoadInst *LI = dyn_cast<LoadInst>(Op0))
2626 if (isSafeToFoldLoadIntoInstruction(*LI, I)) {
2627 const Type *Ty = Op0->getType();
2628 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2629 unsigned Opcode = Ty == Type::FloatTy ? X86::FDIVR32m : X86::FDIVR64m;
2630
Chris Lattner95157f72004-04-11 22:05:45 +00002631 unsigned Op1r = getReg(Op1);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002632 if (AllocaInst *AI = dyn_castFixedAlloca(LI->getOperand(0))) {
2633 unsigned FI = getFixedSizedAllocaFI(AI);
2634 addFrameReference(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op1r), FI);
2635 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002636 X86AddressMode AM;
2637 getAddressingMode(LI->getOperand(0), AM);
2638 addFullAddress(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op1r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002639 }
Chris Lattner95157f72004-04-11 22:05:45 +00002640 return;
2641 }
2642 }
2643
Chris Lattner94af4142002-12-25 05:13:53 +00002644
Chris Lattnercadff442003-10-23 17:21:43 +00002645 MachineBasicBlock::iterator IP = BB->end();
Chris Lattner95157f72004-04-11 22:05:45 +00002646 emitDivRemOperation(BB, IP, Op0, Op1,
Chris Lattner462fa822004-04-11 20:56:28 +00002647 I.getOpcode() == Instruction::Div, ResultReg);
Chris Lattnercadff442003-10-23 17:21:43 +00002648}
2649
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002650void X86ISel::emitDivRemOperation(MachineBasicBlock *BB,
2651 MachineBasicBlock::iterator IP,
2652 Value *Op0, Value *Op1, bool isDiv,
2653 unsigned ResultReg) {
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002654 const Type *Ty = Op0->getType();
2655 unsigned Class = getClass(Ty);
Chris Lattner94af4142002-12-25 05:13:53 +00002656 switch (Class) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002657 case cFP: // Floating point divide
Chris Lattnercadff442003-10-23 17:21:43 +00002658 if (isDiv) {
Chris Lattner6621ed92004-04-11 21:23:56 +00002659 emitBinaryFPOperation(BB, IP, Op0, Op1, 3, ResultReg);
2660 return;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00002661 } else { // Floating point remainder...
Chris Lattner462fa822004-04-11 20:56:28 +00002662 unsigned Op0Reg = getReg(Op0, BB, IP);
2663 unsigned Op1Reg = getReg(Op1, BB, IP);
Chris Lattner3e130a22003-01-13 00:32:26 +00002664 MachineInstr *TheCall =
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002665 BuildMI(X86::CALLpcrel32, 1).addExternalSymbol("fmod", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00002666 std::vector<ValueRecord> Args;
Chris Lattnercadff442003-10-23 17:21:43 +00002667 Args.push_back(ValueRecord(Op0Reg, Type::DoubleTy));
2668 Args.push_back(ValueRecord(Op1Reg, Type::DoubleTy));
Chris Lattner3e130a22003-01-13 00:32:26 +00002669 doCall(ValueRecord(ResultReg, Type::DoubleTy), TheCall, Args);
2670 }
Chris Lattner94af4142002-12-25 05:13:53 +00002671 return;
Chris Lattner3e130a22003-01-13 00:32:26 +00002672 case cLong: {
2673 static const char *FnName[] =
2674 { "__moddi3", "__divdi3", "__umoddi3", "__udivdi3" };
Chris Lattner462fa822004-04-11 20:56:28 +00002675 unsigned Op0Reg = getReg(Op0, BB, IP);
2676 unsigned Op1Reg = getReg(Op1, BB, IP);
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002677 unsigned NameIdx = Ty->isUnsigned()*2 + isDiv;
Chris Lattner3e130a22003-01-13 00:32:26 +00002678 MachineInstr *TheCall =
2679 BuildMI(X86::CALLpcrel32, 1).addExternalSymbol(FnName[NameIdx], true);
2680
2681 std::vector<ValueRecord> Args;
Chris Lattnercadff442003-10-23 17:21:43 +00002682 Args.push_back(ValueRecord(Op0Reg, Type::LongTy));
2683 Args.push_back(ValueRecord(Op1Reg, Type::LongTy));
Chris Lattner3e130a22003-01-13 00:32:26 +00002684 doCall(ValueRecord(ResultReg, Type::LongTy), TheCall, Args);
2685 return;
2686 }
2687 case cByte: case cShort: case cInt:
Misha Brukmancf00c4a2003-10-10 17:57:28 +00002688 break; // Small integrals, handled below...
Chris Lattner3e130a22003-01-13 00:32:26 +00002689 default: assert(0 && "Unknown class!");
Chris Lattner94af4142002-12-25 05:13:53 +00002690 }
Chris Lattnerf01729e2002-11-02 20:54:46 +00002691
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002692 static const unsigned MovOpcode[]={ X86::MOV8rr, X86::MOV16rr, X86::MOV32rr };
Chris Lattner2483f672004-10-06 05:01:07 +00002693 static const unsigned NEGOpcode[]={ X86::NEG8r, X86::NEG16r, X86::NEG32r };
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002694 static const unsigned SAROpcode[]={ X86::SAR8ri, X86::SAR16ri, X86::SAR32ri };
2695 static const unsigned SHROpcode[]={ X86::SHR8ri, X86::SHR16ri, X86::SHR32ri };
2696 static const unsigned ADDOpcode[]={ X86::ADD8rr, X86::ADD16rr, X86::ADD32rr };
2697
2698 // Special case signed division by power of 2.
Chris Lattner2483f672004-10-06 05:01:07 +00002699 if (ConstantSInt *CI = dyn_cast<ConstantSInt>(Op1))
2700 if (isDiv) {
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002701 assert(Class != cLong && "This doesn't handle 64-bit divides!");
2702 int V = CI->getValue();
2703
2704 if (V == 1) { // X /s 1 => X
2705 unsigned Op0Reg = getReg(Op0, BB, IP);
2706 BuildMI(*BB, IP, MovOpcode[Class], 1, ResultReg).addReg(Op0Reg);
2707 return;
2708 }
2709
2710 if (V == -1) { // X /s -1 => -X
2711 unsigned Op0Reg = getReg(Op0, BB, IP);
2712 BuildMI(*BB, IP, NEGOpcode[Class], 1, ResultReg).addReg(Op0Reg);
2713 return;
2714 }
2715
Chris Lattner610f1e22004-10-06 04:02:39 +00002716 if (V == 2 || V == -2) { // X /s 2
2717 static const unsigned CMPOpcode[] = {
2718 X86::CMP8ri, X86::CMP16ri, X86::CMP32ri
2719 };
2720 static const unsigned SBBOpcode[] = {
2721 X86::SBB8ri, X86::SBB16ri, X86::SBB32ri
2722 };
2723 unsigned Op0Reg = getReg(Op0, BB, IP);
2724 unsigned SignBit = 1 << (CI->getType()->getPrimitiveSize()*8-1);
2725 BuildMI(*BB, IP, CMPOpcode[Class], 2).addReg(Op0Reg).addImm(SignBit);
2726
2727 unsigned TmpReg = makeAnotherReg(Op0->getType());
2728 BuildMI(*BB, IP, SBBOpcode[Class], 2, TmpReg).addReg(Op0Reg).addImm(-1);
2729
2730 unsigned TmpReg2 = V == 2 ? ResultReg : makeAnotherReg(Op0->getType());
2731 BuildMI(*BB, IP, SAROpcode[Class], 2, TmpReg2).addReg(TmpReg).addImm(1);
2732 if (V == -2) {
2733 BuildMI(*BB, IP, NEGOpcode[Class], 1, ResultReg).addReg(TmpReg2);
2734 }
2735 return;
2736 }
2737
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002738 bool isNeg = false;
2739 if (V < 0) { // Not a positive power of 2?
2740 V = -V;
2741 isNeg = true; // Maybe it's a negative power of 2.
2742 }
2743 if (unsigned Log = ExactLog2(V)) {
2744 --Log;
2745 unsigned Op0Reg = getReg(Op0, BB, IP);
2746 unsigned TmpReg = makeAnotherReg(Op0->getType());
Chris Lattner3ffdff62004-10-06 04:19:43 +00002747 BuildMI(*BB, IP, SAROpcode[Class], 2, TmpReg)
2748 .addReg(Op0Reg).addImm(Log-1);
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002749 unsigned TmpReg2 = makeAnotherReg(Op0->getType());
2750 BuildMI(*BB, IP, SHROpcode[Class], 2, TmpReg2)
2751 .addReg(TmpReg).addImm(32-Log);
2752 unsigned TmpReg3 = makeAnotherReg(Op0->getType());
2753 BuildMI(*BB, IP, ADDOpcode[Class], 2, TmpReg3)
2754 .addReg(Op0Reg).addReg(TmpReg2);
2755
2756 unsigned TmpReg4 = isNeg ? makeAnotherReg(Op0->getType()) : ResultReg;
2757 BuildMI(*BB, IP, SAROpcode[Class], 2, TmpReg4)
Chris Lattner3ffdff62004-10-06 04:19:43 +00002758 .addReg(TmpReg3).addImm(Log);
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002759 if (isNeg)
2760 BuildMI(*BB, IP, NEGOpcode[Class], 1, ResultReg).addReg(TmpReg4);
2761 return;
2762 }
Chris Lattner2483f672004-10-06 05:01:07 +00002763 } else { // X % C
2764 assert(Class != cLong && "This doesn't handle 64-bit remainder!");
2765 int V = CI->getValue();
2766
2767 if (V == 2 || V == -2) { // X % 2, X % -2
Chris Lattner2483f672004-10-06 05:01:07 +00002768 static const unsigned SExtOpcode[] = { X86::CBW, X86::CWD, X86::CDQ };
2769 static const unsigned BaseReg[] = { X86::AL , X86::AX , X86::EAX };
2770 static const unsigned SExtReg[] = { X86::AH , X86::DX , X86::EDX };
2771 static const unsigned ANDOpcode[] = {
2772 X86::AND8ri, X86::AND16ri, X86::AND32ri
2773 };
2774 static const unsigned XOROpcode[] = {
2775 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr
2776 };
2777 static const unsigned SUBOpcode[] = {
2778 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr
2779 };
2780
2781 // Sign extend result into reg of -1 or 0.
2782 unsigned Op0Reg = getReg(Op0, BB, IP);
2783 BuildMI(*BB, IP, MovOpcode[Class], 1, BaseReg[Class]).addReg(Op0Reg);
2784 BuildMI(*BB, IP, SExtOpcode[Class], 0);
2785 unsigned TmpReg0 = makeAnotherReg(Op0->getType());
2786 BuildMI(*BB, IP, MovOpcode[Class], 1, TmpReg0).addReg(SExtReg[Class]);
2787
2788 unsigned TmpReg1 = makeAnotherReg(Op0->getType());
2789 BuildMI(*BB, IP, ANDOpcode[Class], 2, TmpReg1).addReg(Op0Reg).addImm(1);
2790
2791 unsigned TmpReg2 = makeAnotherReg(Op0->getType());
2792 BuildMI(*BB, IP, XOROpcode[Class], 2,
2793 TmpReg2).addReg(TmpReg1).addReg(TmpReg0);
2794 BuildMI(*BB, IP, SUBOpcode[Class], 2,
2795 ResultReg).addReg(TmpReg2).addReg(TmpReg0);
2796 return;
2797 }
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002798 }
2799
2800 static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX };
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002801 static const unsigned ClrOpcode[]={ X86::MOV8ri, X86::MOV16ri, X86::MOV32ri };
Chris Lattnerf01729e2002-11-02 20:54:46 +00002802 static const unsigned ExtRegs[] ={ X86::AH , X86::DX , X86::EDX };
2803
2804 static const unsigned DivOpcode[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002805 { X86::DIV8r , X86::DIV16r , X86::DIV32r , 0 }, // Unsigned division
2806 { X86::IDIV8r, X86::IDIV16r, X86::IDIV32r, 0 }, // Signed division
Chris Lattnerf01729e2002-11-02 20:54:46 +00002807 };
2808
Chris Lattnerf01729e2002-11-02 20:54:46 +00002809 unsigned Reg = Regs[Class];
2810 unsigned ExtReg = ExtRegs[Class];
Chris Lattnerf01729e2002-11-02 20:54:46 +00002811
2812 // Put the first operand into one of the A registers...
Chris Lattner462fa822004-04-11 20:56:28 +00002813 unsigned Op0Reg = getReg(Op0, BB, IP);
2814 unsigned Op1Reg = getReg(Op1, BB, IP);
Chris Lattneree352852004-02-29 07:22:16 +00002815 BuildMI(*BB, IP, MovOpcode[Class], 1, Reg).addReg(Op0Reg);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002816
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002817 if (Ty->isSigned()) {
Chris Lattnerf01729e2002-11-02 20:54:46 +00002818 // Emit a sign extension instruction...
Chris Lattner462fa822004-04-11 20:56:28 +00002819 unsigned ShiftResult = makeAnotherReg(Op0->getType());
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002820 BuildMI(*BB, IP, SAROpcode[Class], 2,ShiftResult).addReg(Op0Reg).addImm(31);
Chris Lattneree352852004-02-29 07:22:16 +00002821 BuildMI(*BB, IP, MovOpcode[Class], 1, ExtReg).addReg(ShiftResult);
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002822
2823 // Emit the appropriate divide or remainder instruction...
2824 BuildMI(*BB, IP, DivOpcode[1][Class], 1).addReg(Op1Reg);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002825 } else {
Alkis Evlogimenosf998a7e2004-01-12 07:22:45 +00002826 // If unsigned, emit a zeroing instruction... (reg = 0)
Chris Lattneree352852004-02-29 07:22:16 +00002827 BuildMI(*BB, IP, ClrOpcode[Class], 2, ExtReg).addImm(0);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002828
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002829 // Emit the appropriate divide or remainder instruction...
2830 BuildMI(*BB, IP, DivOpcode[0][Class], 1).addReg(Op1Reg);
2831 }
Chris Lattner06925362002-11-17 21:56:38 +00002832
Chris Lattnerf01729e2002-11-02 20:54:46 +00002833 // Figure out which register we want to pick the result out of...
Chris Lattnercadff442003-10-23 17:21:43 +00002834 unsigned DestReg = isDiv ? Reg : ExtReg;
Chris Lattnerf01729e2002-11-02 20:54:46 +00002835
Chris Lattnerf01729e2002-11-02 20:54:46 +00002836 // Put the result into the destination register...
Chris Lattneree352852004-02-29 07:22:16 +00002837 BuildMI(*BB, IP, MovOpcode[Class], 1, ResultReg).addReg(DestReg);
Chris Lattnerca9671d2002-11-02 20:28:58 +00002838}
Chris Lattnere2954c82002-11-02 20:04:26 +00002839
Chris Lattner06925362002-11-17 21:56:38 +00002840
Brian Gaekea1719c92002-10-31 23:03:59 +00002841/// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here
2842/// for constant immediate shift values, and for constant immediate
2843/// shift values equal to 1. Even the general case is sort of special,
2844/// because the shift amount has to be in CL, not just any old register.
2845///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002846void X86ISel::visitShiftInst(ShiftInst &I) {
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002847 MachineBasicBlock::iterator IP = BB->end ();
2848 emitShiftOperation (BB, IP, I.getOperand (0), I.getOperand (1),
2849 I.getOpcode () == Instruction::Shl, I.getType (),
2850 getReg (I));
2851}
2852
2853/// emitShiftOperation - Common code shared between visitShiftInst and
2854/// constant expression support.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002855void X86ISel::emitShiftOperation(MachineBasicBlock *MBB,
2856 MachineBasicBlock::iterator IP,
2857 Value *Op, Value *ShiftAmount,
2858 bool isLeftShift, const Type *ResultTy,
2859 unsigned DestReg) {
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002860 unsigned SrcReg = getReg (Op, MBB, IP);
2861 bool isSigned = ResultTy->isSigned ();
2862 unsigned Class = getClass (ResultTy);
Chris Lattner3e130a22003-01-13 00:32:26 +00002863
2864 static const unsigned ConstantOperand[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002865 { X86::SHR8ri, X86::SHR16ri, X86::SHR32ri, X86::SHRD32rri8 }, // SHR
2866 { X86::SAR8ri, X86::SAR16ri, X86::SAR32ri, X86::SHRD32rri8 }, // SAR
2867 { X86::SHL8ri, X86::SHL16ri, X86::SHL32ri, X86::SHLD32rri8 }, // SHL
2868 { X86::SHL8ri, X86::SHL16ri, X86::SHL32ri, X86::SHLD32rri8 }, // SAL = SHL
Chris Lattner3e130a22003-01-13 00:32:26 +00002869 };
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002870
Chris Lattner3e130a22003-01-13 00:32:26 +00002871 static const unsigned NonConstantOperand[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002872 { X86::SHR8rCL, X86::SHR16rCL, X86::SHR32rCL }, // SHR
2873 { X86::SAR8rCL, X86::SAR16rCL, X86::SAR32rCL }, // SAR
2874 { X86::SHL8rCL, X86::SHL16rCL, X86::SHL32rCL }, // SHL
2875 { X86::SHL8rCL, X86::SHL16rCL, X86::SHL32rCL }, // SAL = SHL
Chris Lattner3e130a22003-01-13 00:32:26 +00002876 };
Chris Lattner796df732002-11-02 00:44:25 +00002877
Chris Lattner3e130a22003-01-13 00:32:26 +00002878 // Longs, as usual, are handled specially...
2879 if (Class == cLong) {
2880 // If we have a constant shift, we can generate much more efficient code
2881 // than otherwise...
2882 //
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002883 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002884 unsigned Amount = CUI->getValue();
2885 if (Amount < 32) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002886 const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned];
2887 if (isLeftShift) {
Chris Lattneree352852004-02-29 07:22:16 +00002888 BuildMI(*MBB, IP, Opc[3], 3,
2889 DestReg+1).addReg(SrcReg+1).addReg(SrcReg).addImm(Amount);
2890 BuildMI(*MBB, IP, Opc[2], 2, DestReg).addReg(SrcReg).addImm(Amount);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002891 } else {
Chris Lattneree352852004-02-29 07:22:16 +00002892 BuildMI(*MBB, IP, Opc[3], 3,
2893 DestReg).addReg(SrcReg ).addReg(SrcReg+1).addImm(Amount);
2894 BuildMI(*MBB, IP, Opc[2],2,DestReg+1).addReg(SrcReg+1).addImm(Amount);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002895 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002896 } else { // Shifting more than 32 bits
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002897 Amount -= 32;
2898 if (isLeftShift) {
Chris Lattner722070e2004-04-06 03:42:38 +00002899 if (Amount != 0) {
2900 BuildMI(*MBB, IP, X86::SHL32ri, 2,
2901 DestReg + 1).addReg(SrcReg).addImm(Amount);
2902 } else {
2903 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg+1).addReg(SrcReg);
2904 }
2905 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002906 } else {
Chris Lattner722070e2004-04-06 03:42:38 +00002907 if (Amount != 0) {
2908 BuildMI(*MBB, IP, isSigned ? X86::SAR32ri : X86::SHR32ri, 2,
2909 DestReg).addReg(SrcReg+1).addImm(Amount);
2910 } else {
2911 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg+1);
2912 }
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002913 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002914 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002915 }
2916 } else {
Chris Lattner9171ef52003-06-01 01:56:54 +00002917 unsigned TmpReg = makeAnotherReg(Type::IntTy);
2918
2919 if (!isLeftShift && isSigned) {
2920 // If this is a SHR of a Long, then we need to do funny sign extension
2921 // stuff. TmpReg gets the value to use as the high-part if we are
2922 // shifting more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002923 BuildMI(*MBB, IP, X86::SAR32ri, 2, TmpReg).addReg(SrcReg).addImm(31);
Chris Lattner9171ef52003-06-01 01:56:54 +00002924 } else {
2925 // Other shifts use a fixed zero value if the shift is more than 32
2926 // bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002927 BuildMI(*MBB, IP, X86::MOV32ri, 1, TmpReg).addImm(0);
Chris Lattner9171ef52003-06-01 01:56:54 +00002928 }
2929
2930 // Initialize CL with the shift amount...
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002931 unsigned ShiftAmountReg = getReg(ShiftAmount, MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002932 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002933
2934 unsigned TmpReg2 = makeAnotherReg(Type::IntTy);
2935 unsigned TmpReg3 = makeAnotherReg(Type::IntTy);
2936 if (isLeftShift) {
2937 // TmpReg2 = shld inHi, inLo
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002938 BuildMI(*MBB, IP, X86::SHLD32rrCL,2,TmpReg2).addReg(SrcReg+1)
Chris Lattneree352852004-02-29 07:22:16 +00002939 .addReg(SrcReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002940 // TmpReg3 = shl inLo, CL
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002941 BuildMI(*MBB, IP, X86::SHL32rCL, 1, TmpReg3).addReg(SrcReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002942
2943 // Set the flags to indicate whether the shift was by more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002944 BuildMI(*MBB, IP, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Chris Lattner9171ef52003-06-01 01:56:54 +00002945
2946 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002947 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00002948 DestReg+1).addReg(TmpReg2).addReg(TmpReg3);
2949 // DestLo = (>32) ? TmpReg : TmpReg3;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002950 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002951 DestReg).addReg(TmpReg3).addReg(TmpReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002952 } else {
2953 // TmpReg2 = shrd inLo, inHi
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002954 BuildMI(*MBB, IP, X86::SHRD32rrCL,2,TmpReg2).addReg(SrcReg)
Chris Lattneree352852004-02-29 07:22:16 +00002955 .addReg(SrcReg+1);
Chris Lattner9171ef52003-06-01 01:56:54 +00002956 // TmpReg3 = s[ah]r inHi, CL
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002957 BuildMI(*MBB, IP, isSigned ? X86::SAR32rCL : X86::SHR32rCL, 1, TmpReg3)
Chris Lattner9171ef52003-06-01 01:56:54 +00002958 .addReg(SrcReg+1);
2959
2960 // Set the flags to indicate whether the shift was by more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002961 BuildMI(*MBB, IP, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Chris Lattner9171ef52003-06-01 01:56:54 +00002962
2963 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002964 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00002965 DestReg).addReg(TmpReg2).addReg(TmpReg3);
2966
2967 // DestHi = (>32) ? TmpReg : TmpReg3;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002968 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00002969 DestReg+1).addReg(TmpReg3).addReg(TmpReg);
2970 }
Brian Gaekea1719c92002-10-31 23:03:59 +00002971 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002972 return;
2973 }
Chris Lattnere9913f22002-11-02 01:41:55 +00002974
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002975 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002976 // The shift amount is constant, guaranteed to be a ubyte. Get its value.
2977 assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?");
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002978
Chris Lattner3e130a22003-01-13 00:32:26 +00002979 const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned];
Chris Lattneree352852004-02-29 07:22:16 +00002980 BuildMI(*MBB, IP, Opc[Class], 2,
2981 DestReg).addReg(SrcReg).addImm(CUI->getValue());
Chris Lattner3e130a22003-01-13 00:32:26 +00002982 } else { // The shift amount is non-constant.
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002983 unsigned ShiftAmountReg = getReg (ShiftAmount, MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002984 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002985
Chris Lattner3e130a22003-01-13 00:32:26 +00002986 const unsigned *Opc = NonConstantOperand[isLeftShift*2+isSigned];
Chris Lattneree352852004-02-29 07:22:16 +00002987 BuildMI(*MBB, IP, Opc[Class], 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002988 }
2989}
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002990
Chris Lattner3e130a22003-01-13 00:32:26 +00002991
Chris Lattner6fc3c522002-11-17 21:11:55 +00002992/// visitLoadInst - Implement LLVM load instructions in terms of the x86 'mov'
Chris Lattnere8f0d922002-12-24 00:03:11 +00002993/// instruction. The load and store instructions are the only place where we
2994/// need to worry about the memory layout of the target machine.
Chris Lattner6fc3c522002-11-17 21:11:55 +00002995///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002996void X86ISel::visitLoadInst(LoadInst &I) {
Chris Lattner7dee5da2004-03-08 01:58:35 +00002997 // Check to see if this load instruction is going to be folded into a binary
2998 // instruction, like add. If so, we don't want to emit it. Wouldn't a real
2999 // pattern matching instruction selector be nice?
Chris Lattner95157f72004-04-11 22:05:45 +00003000 unsigned Class = getClassB(I.getType());
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003001 if (I.hasOneUse()) {
Chris Lattner7dee5da2004-03-08 01:58:35 +00003002 Instruction *User = cast<Instruction>(I.use_back());
3003 switch (User->getOpcode()) {
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003004 case Instruction::Cast:
3005 // If this is a cast from a signed-integer type to a floating point type,
3006 // fold the cast here.
John Criswell6b5bd582004-06-09 15:18:51 +00003007 if (getClassB(User->getType()) == cFP &&
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003008 (I.getType() == Type::ShortTy || I.getType() == Type::IntTy ||
3009 I.getType() == Type::LongTy)) {
3010 unsigned DestReg = getReg(User);
3011 static const unsigned Opcode[] = {
3012 0/*BYTE*/, X86::FILD16m, X86::FILD32m, 0/*FP*/, X86::FILD64m
3013 };
Chris Lattner9f1b5312004-05-13 15:12:43 +00003014
3015 if (AllocaInst *AI = dyn_castFixedAlloca(I.getOperand(0))) {
3016 unsigned FI = getFixedSizedAllocaFI(AI);
3017 addFrameReference(BuildMI(BB, Opcode[Class], 4, DestReg), FI);
3018 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00003019 X86AddressMode AM;
3020 getAddressingMode(I.getOperand(0), AM);
3021 addFullAddress(BuildMI(BB, Opcode[Class], 4, DestReg), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00003022 }
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003023 return;
3024 } else {
3025 User = 0;
3026 }
3027 break;
Chris Lattner13c07fe2004-04-12 00:12:04 +00003028
Chris Lattner7dee5da2004-03-08 01:58:35 +00003029 case Instruction::Add:
3030 case Instruction::Sub:
3031 case Instruction::And:
3032 case Instruction::Or:
3033 case Instruction::Xor:
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003034 if (Class == cLong) User = 0;
Chris Lattner7dee5da2004-03-08 01:58:35 +00003035 break;
Chris Lattner95157f72004-04-11 22:05:45 +00003036 case Instruction::Mul:
3037 case Instruction::Div:
Chris Lattner13c07fe2004-04-12 00:12:04 +00003038 if (Class != cFP) User = 0;
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003039 break; // Folding only implemented for floating point.
Chris Lattner95157f72004-04-11 22:05:45 +00003040 default: User = 0; break;
Chris Lattner7dee5da2004-03-08 01:58:35 +00003041 }
3042
3043 if (User) {
3044 // Okay, we found a user. If the load is the first operand and there is
3045 // no second operand load, reverse the operand ordering. Note that this
3046 // can fail for a subtract (ie, no change will be made).
Chris Lattner3dbb5042004-07-21 21:28:26 +00003047 bool Swapped = false;
Chris Lattner7dee5da2004-03-08 01:58:35 +00003048 if (!isa<LoadInst>(User->getOperand(1)))
Chris Lattner3dbb5042004-07-21 21:28:26 +00003049 Swapped = !cast<BinaryOperator>(User)->swapOperands();
Chris Lattner7dee5da2004-03-08 01:58:35 +00003050
3051 // Okay, now that everything is set up, if this load is used by the second
3052 // operand, and if there are no instructions that invalidate the load
3053 // before the binary operator, eliminate the load.
3054 if (User->getOperand(1) == &I &&
3055 isSafeToFoldLoadIntoInstruction(I, *User))
3056 return; // Eliminate the load!
Chris Lattner95157f72004-04-11 22:05:45 +00003057
3058 // If this is a floating point sub or div, we won't be able to swap the
3059 // operands, but we will still be able to eliminate the load.
3060 if (Class == cFP && User->getOperand(0) == &I &&
3061 !isa<LoadInst>(User->getOperand(1)) &&
3062 (User->getOpcode() == Instruction::Sub ||
3063 User->getOpcode() == Instruction::Div) &&
3064 isSafeToFoldLoadIntoInstruction(I, *User))
3065 return; // Eliminate the load!
Chris Lattner3dbb5042004-07-21 21:28:26 +00003066
3067 // If we swapped the operands to the instruction, but couldn't fold the
3068 // load anyway, swap them back. We don't want to break add X, int
3069 // folding.
3070 if (Swapped) cast<BinaryOperator>(User)->swapOperands();
Chris Lattner7dee5da2004-03-08 01:58:35 +00003071 }
3072 }
3073
Chris Lattner6ac1d712003-10-20 04:48:06 +00003074 static const unsigned Opcodes[] = {
Chris Lattner9f1b5312004-05-13 15:12:43 +00003075 X86::MOV8rm, X86::MOV16rm, X86::MOV32rm, X86::FLD32m, X86::MOV32rm
Chris Lattner3e130a22003-01-13 00:32:26 +00003076 };
Chris Lattner6ac1d712003-10-20 04:48:06 +00003077 unsigned Opcode = Opcodes[Class];
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003078 if (I.getType() == Type::DoubleTy) Opcode = X86::FLD64m;
Chris Lattner9f1b5312004-05-13 15:12:43 +00003079
3080 unsigned DestReg = getReg(I);
3081
3082 if (AllocaInst *AI = dyn_castFixedAlloca(I.getOperand(0))) {
3083 unsigned FI = getFixedSizedAllocaFI(AI);
3084 if (Class == cLong) {
3085 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, DestReg), FI);
3086 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, DestReg+1), FI, 4);
3087 } else {
3088 addFrameReference(BuildMI(BB, Opcode, 4, DestReg), FI);
3089 }
3090 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00003091 X86AddressMode AM;
3092 getAddressingMode(I.getOperand(0), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00003093
3094 if (Class == cLong) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003095 addFullAddress(BuildMI(BB, X86::MOV32rm, 4, DestReg), AM);
3096 AM.Disp += 4;
3097 addFullAddress(BuildMI(BB, X86::MOV32rm, 4, DestReg+1), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00003098 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00003099 addFullAddress(BuildMI(BB, Opcode, 4, DestReg), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00003100 }
3101 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003102}
3103
Chris Lattner6fc3c522002-11-17 21:11:55 +00003104/// visitStoreInst - Implement LLVM store instructions in terms of the x86 'mov'
3105/// instruction.
3106///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003107void X86ISel::visitStoreInst(StoreInst &I) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003108 X86AddressMode AM;
3109 getAddressingMode(I.getOperand(1), AM);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003110
Chris Lattner6c09db22003-10-20 04:11:23 +00003111 const Type *ValTy = I.getOperand(0)->getType();
3112 unsigned Class = getClassB(ValTy);
Chris Lattner6ac1d712003-10-20 04:48:06 +00003113
Chris Lattner5a830962004-02-25 02:56:58 +00003114 if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(0))) {
3115 uint64_t Val = CI->getRawValue();
3116 if (Class == cLong) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003117 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(Val & ~0U);
3118 AM.Disp += 4;
3119 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(Val>>32);
Chris Lattner5a830962004-02-25 02:56:58 +00003120 } else {
3121 static const unsigned Opcodes[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003122 X86::MOV8mi, X86::MOV16mi, X86::MOV32mi
Chris Lattner5a830962004-02-25 02:56:58 +00003123 };
3124 unsigned Opcode = Opcodes[Class];
Reid Spencerfc989e12004-08-30 00:13:26 +00003125 addFullAddress(BuildMI(BB, Opcode, 5), AM).addImm(Val);
Chris Lattner5a830962004-02-25 02:56:58 +00003126 }
Chris Lattnerb7cb9ff2004-05-13 15:26:48 +00003127 } else if (isa<ConstantPointerNull>(I.getOperand(0))) {
Chris Lattner358a9022004-10-15 05:05:29 +00003128 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(0);
Chris Lattner5a830962004-02-25 02:56:58 +00003129 } else if (ConstantBool *CB = dyn_cast<ConstantBool>(I.getOperand(0))) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003130 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CB->getValue());
Chris Lattnere7a31c92004-05-07 21:18:15 +00003131 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0))) {
3132 // Store constant FP values with integer instructions to avoid having to
3133 // load the constants from the constant pool then do a store.
3134 if (CFP->getType() == Type::FloatTy) {
3135 union {
3136 unsigned I;
3137 float F;
3138 } V;
3139 V.F = CFP->getValue();
Reid Spencerfc989e12004-08-30 00:13:26 +00003140 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(V.I);
Chris Lattner5a830962004-02-25 02:56:58 +00003141 } else {
Chris Lattnere7a31c92004-05-07 21:18:15 +00003142 union {
3143 uint64_t I;
3144 double F;
3145 } V;
3146 V.F = CFP->getValue();
Reid Spencerfc989e12004-08-30 00:13:26 +00003147 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm((unsigned)V.I);
3148 AM.Disp += 4;
3149 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(
Chris Lattnere7a31c92004-05-07 21:18:15 +00003150 unsigned(V.I >> 32));
Chris Lattner5a830962004-02-25 02:56:58 +00003151 }
Chris Lattnere7a31c92004-05-07 21:18:15 +00003152
3153 } else if (Class == cLong) {
3154 unsigned ValReg = getReg(I.getOperand(0));
Reid Spencerfc989e12004-08-30 00:13:26 +00003155 addFullAddress(BuildMI(BB, X86::MOV32mr, 5), AM).addReg(ValReg);
3156 AM.Disp += 4;
3157 addFullAddress(BuildMI(BB, X86::MOV32mr, 5), AM).addReg(ValReg+1);
Chris Lattnere7a31c92004-05-07 21:18:15 +00003158 } else {
Chris Lattner358a9022004-10-15 05:05:29 +00003159 // FIXME: stop emitting these two instructions:
3160 // movl $global,%eax
3161 // movl %eax,(%ebx)
3162 // when one instruction will suffice. That includes when the global
3163 // has an offset applied to it.
Chris Lattnere7a31c92004-05-07 21:18:15 +00003164 unsigned ValReg = getReg(I.getOperand(0));
3165 static const unsigned Opcodes[] = {
3166 X86::MOV8mr, X86::MOV16mr, X86::MOV32mr, X86::FST32m
3167 };
3168 unsigned Opcode = Opcodes[Class];
3169 if (ValTy == Type::DoubleTy) Opcode = X86::FST64m;
Chris Lattner9f1b5312004-05-13 15:12:43 +00003170
Reid Spencerfc989e12004-08-30 00:13:26 +00003171 addFullAddress(BuildMI(BB, Opcode, 1+4), AM).addReg(ValReg);
Chris Lattner94af4142002-12-25 05:13:53 +00003172 }
Chris Lattner6fc3c522002-11-17 21:11:55 +00003173}
3174
3175
Misha Brukman538607f2004-03-01 23:53:11 +00003176/// visitCastInst - Here we have various kinds of copying with or without sign
3177/// extension going on.
3178///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003179void X86ISel::visitCastInst(CastInst &CI) {
Chris Lattnerf5854472003-06-21 16:01:24 +00003180 Value *Op = CI.getOperand(0);
Chris Lattner427aeb42004-04-11 19:21:59 +00003181
Chris Lattner99382862004-04-12 00:23:04 +00003182 unsigned SrcClass = getClassB(Op->getType());
3183 unsigned DestClass = getClassB(CI.getType());
3184 // Noop casts are not emitted: getReg will return the source operand as the
3185 // register to use for any uses of the noop cast.
Chris Lattner8b486a12004-06-29 00:14:38 +00003186 if (DestClass == SrcClass) {
3187 // The only detail in this plan is that casts from double -> float are
3188 // truncating operations that we have to codegen through memory (despite
3189 // the fact that the source/dest registers are the same class).
3190 if (CI.getType() != Type::FloatTy || Op->getType() != Type::DoubleTy)
3191 return;
3192 }
Chris Lattner427aeb42004-04-11 19:21:59 +00003193
Chris Lattnerf5854472003-06-21 16:01:24 +00003194 // If this is a cast from a 32-bit integer to a Long type, and the only uses
3195 // of the case are GEP instructions, then the cast does not need to be
3196 // generated explicitly, it will be folded into the GEP.
Chris Lattner99382862004-04-12 00:23:04 +00003197 if (DestClass == cLong && SrcClass == cInt) {
Chris Lattnerf5854472003-06-21 16:01:24 +00003198 bool AllUsesAreGEPs = true;
3199 for (Value::use_iterator I = CI.use_begin(), E = CI.use_end(); I != E; ++I)
3200 if (!isa<GetElementPtrInst>(*I)) {
3201 AllUsesAreGEPs = false;
3202 break;
3203 }
3204
3205 // No need to codegen this cast if all users are getelementptr instrs...
3206 if (AllUsesAreGEPs) return;
3207 }
3208
Chris Lattner99382862004-04-12 00:23:04 +00003209 // If this cast converts a load from a short,int, or long integer to a FP
3210 // value, we will have folded this cast away.
3211 if (DestClass == cFP && isa<LoadInst>(Op) && Op->hasOneUse() &&
3212 (Op->getType() == Type::ShortTy || Op->getType() == Type::IntTy ||
3213 Op->getType() == Type::LongTy))
3214 return;
3215
3216
Chris Lattner548f61d2003-04-23 17:22:12 +00003217 unsigned DestReg = getReg(CI);
3218 MachineBasicBlock::iterator MI = BB->end();
Chris Lattnerf5854472003-06-21 16:01:24 +00003219 emitCastOperation(BB, MI, Op, CI.getType(), DestReg);
Chris Lattner548f61d2003-04-23 17:22:12 +00003220}
3221
Misha Brukman538607f2004-03-01 23:53:11 +00003222/// emitCastOperation - Common code shared between visitCastInst and constant
3223/// expression cast support.
3224///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003225void X86ISel::emitCastOperation(MachineBasicBlock *BB,
3226 MachineBasicBlock::iterator IP,
3227 Value *Src, const Type *DestTy,
3228 unsigned DestReg) {
Chris Lattner3e130a22003-01-13 00:32:26 +00003229 const Type *SrcTy = Src->getType();
3230 unsigned SrcClass = getClassB(SrcTy);
Chris Lattner3e130a22003-01-13 00:32:26 +00003231 unsigned DestClass = getClassB(DestTy);
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003232 unsigned SrcReg = getReg(Src, BB, IP);
3233
Chris Lattner3e130a22003-01-13 00:32:26 +00003234 // Implement casts to bool by using compare on the operand followed by set if
3235 // not zero on the result.
3236 if (DestTy == Type::BoolTy) {
Chris Lattner20772542003-06-01 03:38:24 +00003237 switch (SrcClass) {
3238 case cByte:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003239 BuildMI(*BB, IP, X86::TEST8rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00003240 break;
3241 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003242 BuildMI(*BB, IP, X86::TEST16rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00003243 break;
3244 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003245 BuildMI(*BB, IP, X86::TEST32rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00003246 break;
3247 case cLong: {
3248 unsigned TmpReg = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003249 BuildMI(*BB, IP, X86::OR32rr, 2, TmpReg).addReg(SrcReg).addReg(SrcReg+1);
Chris Lattner20772542003-06-01 03:38:24 +00003250 break;
3251 }
3252 case cFP:
Chris Lattneree352852004-02-29 07:22:16 +00003253 BuildMI(*BB, IP, X86::FTST, 1).addReg(SrcReg);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003254 BuildMI(*BB, IP, X86::FNSTSW8r, 0);
Chris Lattneree352852004-02-29 07:22:16 +00003255 BuildMI(*BB, IP, X86::SAHF, 1);
Chris Lattner311ca2e2004-02-23 03:21:41 +00003256 break;
Chris Lattner20772542003-06-01 03:38:24 +00003257 }
3258
3259 // If the zero flag is not set, then the value is true, set the byte to
3260 // true.
Chris Lattneree352852004-02-29 07:22:16 +00003261 BuildMI(*BB, IP, X86::SETNEr, 1, DestReg);
Chris Lattner94af4142002-12-25 05:13:53 +00003262 return;
3263 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003264
3265 static const unsigned RegRegMove[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003266 X86::MOV8rr, X86::MOV16rr, X86::MOV32rr, X86::FpMOV, X86::MOV32rr
Chris Lattner3e130a22003-01-13 00:32:26 +00003267 };
3268
3269 // Implement casts between values of the same type class (as determined by
3270 // getClass) by using a register-to-register move.
3271 if (SrcClass == DestClass) {
3272 if (SrcClass <= cInt || (SrcClass == cFP && SrcTy == DestTy)) {
Chris Lattneree352852004-02-29 07:22:16 +00003273 BuildMI(*BB, IP, RegRegMove[SrcClass], 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003274 } else if (SrcClass == cFP) {
3275 if (SrcTy == Type::FloatTy) { // double -> float
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003276 assert(DestTy == Type::DoubleTy && "Unknown cFP member!");
Chris Lattneree352852004-02-29 07:22:16 +00003277 BuildMI(*BB, IP, X86::FpMOV, 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003278 } else { // float -> double
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003279 assert(SrcTy == Type::DoubleTy && DestTy == Type::FloatTy &&
3280 "Unknown cFP member!");
3281 // Truncate from double to float by storing to memory as short, then
3282 // reading it back.
3283 unsigned FltAlign = TM.getTargetData().getFloatAlignment();
Chris Lattner3e130a22003-01-13 00:32:26 +00003284 int FrameIdx = F->getFrameInfo()->CreateStackObject(4, FltAlign);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003285 addFrameReference(BuildMI(*BB, IP, X86::FST32m, 5), FrameIdx).addReg(SrcReg);
3286 addFrameReference(BuildMI(*BB, IP, X86::FLD32m, 5, DestReg), FrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003287 }
3288 } else if (SrcClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003289 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg);
3290 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg+1).addReg(SrcReg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00003291 } else {
Chris Lattnerc53544a2003-05-12 20:16:58 +00003292 assert(0 && "Cannot handle this type of cast instruction!");
Chris Lattner548f61d2003-04-23 17:22:12 +00003293 abort();
Brian Gaeked474e9c2002-12-06 10:49:33 +00003294 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003295 return;
3296 }
3297
3298 // Handle cast of SMALLER int to LARGER int using a move with sign extension
3299 // or zero extension, depending on whether the source type was signed.
3300 if (SrcClass <= cInt && (DestClass <= cInt || DestClass == cLong) &&
3301 SrcClass < DestClass) {
3302 bool isLong = DestClass == cLong;
3303 if (isLong) DestClass = cInt;
3304
3305 static const unsigned Opc[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003306 { X86::MOVSX16rr8, X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOV32rr }, // s
3307 { X86::MOVZX16rr8, X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOV32rr } // u
Chris Lattner3e130a22003-01-13 00:32:26 +00003308 };
3309
Chris Lattner96e3b422004-05-09 22:28:45 +00003310 bool isUnsigned = SrcTy->isUnsigned() || SrcTy == Type::BoolTy;
Chris Lattneree352852004-02-29 07:22:16 +00003311 BuildMI(*BB, IP, Opc[isUnsigned][SrcClass + DestClass - 1], 1,
Chris Lattner548f61d2003-04-23 17:22:12 +00003312 DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003313
3314 if (isLong) { // Handle upper 32 bits as appropriate...
3315 if (isUnsigned) // Zero out top bits...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003316 BuildMI(*BB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
Chris Lattner3e130a22003-01-13 00:32:26 +00003317 else // Sign extend bottom half...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003318 BuildMI(*BB, IP, X86::SAR32ri, 2, DestReg+1).addReg(DestReg).addImm(31);
Brian Gaeked474e9c2002-12-06 10:49:33 +00003319 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003320 return;
3321 }
3322
3323 // Special case long -> int ...
3324 if (SrcClass == cLong && DestClass == cInt) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003325 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003326 return;
3327 }
3328
3329 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by a
3330 // move out of AX or AL.
3331 if ((SrcClass <= cInt || SrcClass == cLong) && DestClass <= cInt
3332 && SrcClass > DestClass) {
3333 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX, 0, X86::EAX };
Chris Lattneree352852004-02-29 07:22:16 +00003334 BuildMI(*BB, IP, RegRegMove[SrcClass], 1, AReg[SrcClass]).addReg(SrcReg);
3335 BuildMI(*BB, IP, RegRegMove[DestClass], 1, DestReg).addReg(AReg[DestClass]);
Chris Lattner3e130a22003-01-13 00:32:26 +00003336 return;
3337 }
3338
3339 // Handle casts from integer to floating point now...
3340 if (DestClass == cFP) {
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003341 // Promote the integer to a type supported by FLD. We do this because there
3342 // are no unsigned FLD instructions, so we must promote an unsigned value to
3343 // a larger signed value, then use FLD on the larger value.
3344 //
3345 const Type *PromoteType = 0;
Chris Lattner85aa7092004-04-10 18:32:01 +00003346 unsigned PromoteOpcode = 0;
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003347 unsigned RealDestReg = DestReg;
Chris Lattnerf70c22b2004-06-17 18:19:28 +00003348 switch (SrcTy->getTypeID()) {
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003349 case Type::BoolTyID:
3350 case Type::SByteTyID:
3351 // We don't have the facilities for directly loading byte sized data from
3352 // memory (even signed). Promote it to 16 bits.
3353 PromoteType = Type::ShortTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003354 PromoteOpcode = X86::MOVSX16rr8;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003355 break;
3356 case Type::UByteTyID:
3357 PromoteType = Type::ShortTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003358 PromoteOpcode = X86::MOVZX16rr8;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003359 break;
3360 case Type::UShortTyID:
3361 PromoteType = Type::IntTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003362 PromoteOpcode = X86::MOVZX32rr16;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003363 break;
3364 case Type::UIntTyID: {
3365 // Make a 64 bit temporary... and zero out the top of it...
3366 unsigned TmpReg = makeAnotherReg(Type::LongTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003367 BuildMI(*BB, IP, X86::MOV32rr, 1, TmpReg).addReg(SrcReg);
3368 BuildMI(*BB, IP, X86::MOV32ri, 1, TmpReg+1).addImm(0);
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003369 SrcTy = Type::LongTy;
3370 SrcClass = cLong;
3371 SrcReg = TmpReg;
3372 break;
3373 }
3374 case Type::ULongTyID:
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003375 // Don't fild into the read destination.
3376 DestReg = makeAnotherReg(Type::DoubleTy);
3377 break;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003378 default: // No promotion needed...
3379 break;
3380 }
3381
3382 if (PromoteType) {
3383 unsigned TmpReg = makeAnotherReg(PromoteType);
Chris Lattner7b92de1e2004-04-06 19:29:36 +00003384 BuildMI(*BB, IP, PromoteOpcode, 1, TmpReg).addReg(SrcReg);
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003385 SrcTy = PromoteType;
3386 SrcClass = getClass(PromoteType);
Chris Lattner3e130a22003-01-13 00:32:26 +00003387 SrcReg = TmpReg;
3388 }
3389
3390 // Spill the integer to memory and reload it from there...
Chris Lattnerb6bac512004-02-25 06:13:04 +00003391 int FrameIdx =
3392 F->getFrameInfo()->CreateStackObject(SrcTy, TM.getTargetData());
Chris Lattner3e130a22003-01-13 00:32:26 +00003393
3394 if (SrcClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003395 addFrameReference(BuildMI(*BB, IP, X86::MOV32mr, 5),
Chris Lattneree352852004-02-29 07:22:16 +00003396 FrameIdx).addReg(SrcReg);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003397 addFrameReference(BuildMI(*BB, IP, X86::MOV32mr, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003398 FrameIdx, 4).addReg(SrcReg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00003399 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003400 static const unsigned Op1[] = { X86::MOV8mr, X86::MOV16mr, X86::MOV32mr };
Chris Lattneree352852004-02-29 07:22:16 +00003401 addFrameReference(BuildMI(*BB, IP, Op1[SrcClass], 5),
3402 FrameIdx).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003403 }
3404
3405 static const unsigned Op2[] =
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003406 { 0/*byte*/, X86::FILD16m, X86::FILD32m, 0/*FP*/, X86::FILD64m };
Chris Lattneree352852004-02-29 07:22:16 +00003407 addFrameReference(BuildMI(*BB, IP, Op2[SrcClass], 5, DestReg), FrameIdx);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003408
3409 // We need special handling for unsigned 64-bit integer sources. If the
3410 // input number has the "sign bit" set, then we loaded it incorrectly as a
3411 // negative 64-bit number. In this case, add an offset value.
3412 if (SrcTy == Type::ULongTy) {
3413 // Emit a test instruction to see if the dynamic input value was signed.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003414 BuildMI(*BB, IP, X86::TEST32rr, 2).addReg(SrcReg+1).addReg(SrcReg+1);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003415
Chris Lattnerb6bac512004-02-25 06:13:04 +00003416 // If the sign bit is set, get a pointer to an offset, otherwise get a
3417 // pointer to a zero.
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003418 MachineConstantPool *CP = F->getConstantPool();
3419 unsigned Zero = makeAnotherReg(Type::IntTy);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003420 Constant *Null = Constant::getNullValue(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003421 addConstantPoolReference(BuildMI(*BB, IP, X86::LEA32r, 5, Zero),
Chris Lattnerb6bac512004-02-25 06:13:04 +00003422 CP->getConstantPoolIndex(Null));
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003423 unsigned Offset = makeAnotherReg(Type::IntTy);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003424 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
3425
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003426 addConstantPoolReference(BuildMI(*BB, IP, X86::LEA32r, 5, Offset),
Chris Lattnerb6bac512004-02-25 06:13:04 +00003427 CP->getConstantPoolIndex(OffsetCst));
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003428 unsigned Addr = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003429 BuildMI(*BB, IP, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003430
3431 // Load the constant for an add. FIXME: this could make an 'fadd' that
3432 // reads directly from memory, but we don't support these yet.
3433 unsigned ConstReg = makeAnotherReg(Type::DoubleTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003434 addDirectMem(BuildMI(*BB, IP, X86::FLD32m, 4, ConstReg), Addr);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003435
Chris Lattneree352852004-02-29 07:22:16 +00003436 BuildMI(*BB, IP, X86::FpADD, 2, RealDestReg)
3437 .addReg(ConstReg).addReg(DestReg);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003438 }
3439
Chris Lattner3e130a22003-01-13 00:32:26 +00003440 return;
3441 }
3442
3443 // Handle casts from floating point to integer now...
3444 if (SrcClass == cFP) {
3445 // Change the floating point control register to use "round towards zero"
3446 // mode when truncating to an integer value.
3447 //
3448 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003449 addFrameReference(BuildMI(*BB, IP, X86::FNSTCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003450
3451 // Load the old value of the high byte of the control word...
3452 unsigned HighPartOfCW = makeAnotherReg(Type::UByteTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003453 addFrameReference(BuildMI(*BB, IP, X86::MOV8rm, 4, HighPartOfCW),
Chris Lattneree352852004-02-29 07:22:16 +00003454 CWFrameIdx, 1);
Chris Lattner3e130a22003-01-13 00:32:26 +00003455
3456 // Set the high part to be round to zero...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003457 addFrameReference(BuildMI(*BB, IP, X86::MOV8mi, 5),
Chris Lattneree352852004-02-29 07:22:16 +00003458 CWFrameIdx, 1).addImm(12);
Chris Lattner3e130a22003-01-13 00:32:26 +00003459
3460 // Reload the modified control word now...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003461 addFrameReference(BuildMI(*BB, IP, X86::FLDCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003462
3463 // Restore the memory image of control word to original value
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003464 addFrameReference(BuildMI(*BB, IP, X86::MOV8mr, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003465 CWFrameIdx, 1).addReg(HighPartOfCW);
Chris Lattner3e130a22003-01-13 00:32:26 +00003466
3467 // We don't have the facilities for directly storing byte sized data to
3468 // memory. Promote it to 16 bits. We also must promote unsigned values to
3469 // larger classes because we only have signed FP stores.
3470 unsigned StoreClass = DestClass;
3471 const Type *StoreTy = DestTy;
3472 if (StoreClass == cByte || DestTy->isUnsigned())
3473 switch (StoreClass) {
3474 case cByte: StoreTy = Type::ShortTy; StoreClass = cShort; break;
3475 case cShort: StoreTy = Type::IntTy; StoreClass = cInt; break;
3476 case cInt: StoreTy = Type::LongTy; StoreClass = cLong; break;
Brian Gaeked4615052003-07-18 20:23:43 +00003477 // The following treatment of cLong may not be perfectly right,
3478 // but it survives chains of casts of the form
3479 // double->ulong->double.
3480 case cLong: StoreTy = Type::LongTy; StoreClass = cLong; break;
Chris Lattner3e130a22003-01-13 00:32:26 +00003481 default: assert(0 && "Unknown store class!");
3482 }
3483
3484 // Spill the integer to memory and reload it from there...
3485 int FrameIdx =
3486 F->getFrameInfo()->CreateStackObject(StoreTy, TM.getTargetData());
3487
3488 static const unsigned Op1[] =
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003489 { 0, X86::FIST16m, X86::FIST32m, 0, X86::FISTP64m };
Chris Lattneree352852004-02-29 07:22:16 +00003490 addFrameReference(BuildMI(*BB, IP, Op1[StoreClass], 5),
3491 FrameIdx).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003492
3493 if (DestClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003494 addFrameReference(BuildMI(*BB, IP, X86::MOV32rm, 4, DestReg), FrameIdx);
3495 addFrameReference(BuildMI(*BB, IP, X86::MOV32rm, 4, DestReg+1),
Chris Lattneree352852004-02-29 07:22:16 +00003496 FrameIdx, 4);
Chris Lattner3e130a22003-01-13 00:32:26 +00003497 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003498 static const unsigned Op2[] = { X86::MOV8rm, X86::MOV16rm, X86::MOV32rm };
Chris Lattneree352852004-02-29 07:22:16 +00003499 addFrameReference(BuildMI(*BB, IP, Op2[DestClass], 4, DestReg), FrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003500 }
3501
3502 // Reload the original control word now...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003503 addFrameReference(BuildMI(*BB, IP, X86::FLDCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003504 return;
3505 }
3506
Brian Gaeked474e9c2002-12-06 10:49:33 +00003507 // Anything we haven't handled already, we can't (yet) handle at all.
Chris Lattnerc53544a2003-05-12 20:16:58 +00003508 assert(0 && "Unhandled cast instruction!");
Chris Lattner548f61d2003-04-23 17:22:12 +00003509 abort();
Brian Gaekefa8d5712002-11-22 11:07:01 +00003510}
Brian Gaekea1719c92002-10-31 23:03:59 +00003511
Chris Lattner73815062003-10-18 05:56:40 +00003512/// visitVANextInst - Implement the va_next instruction...
Chris Lattnereca195e2003-05-08 19:44:13 +00003513///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003514void X86ISel::visitVANextInst(VANextInst &I) {
Chris Lattner73815062003-10-18 05:56:40 +00003515 unsigned VAList = getReg(I.getOperand(0));
Chris Lattnereca195e2003-05-08 19:44:13 +00003516 unsigned DestReg = getReg(I);
3517
Chris Lattnereca195e2003-05-08 19:44:13 +00003518 unsigned Size;
Chris Lattnerf70c22b2004-06-17 18:19:28 +00003519 switch (I.getArgType()->getTypeID()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00003520 default:
3521 std::cerr << I;
Chris Lattner73815062003-10-18 05:56:40 +00003522 assert(0 && "Error: bad type for va_next instruction!");
Chris Lattnereca195e2003-05-08 19:44:13 +00003523 return;
3524 case Type::PointerTyID:
3525 case Type::UIntTyID:
3526 case Type::IntTyID:
3527 Size = 4;
Chris Lattnereca195e2003-05-08 19:44:13 +00003528 break;
3529 case Type::ULongTyID:
3530 case Type::LongTyID:
Chris Lattnereca195e2003-05-08 19:44:13 +00003531 case Type::DoubleTyID:
3532 Size = 8;
Chris Lattnereca195e2003-05-08 19:44:13 +00003533 break;
3534 }
3535
3536 // Increment the VAList pointer...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003537 BuildMI(BB, X86::ADD32ri, 2, DestReg).addReg(VAList).addImm(Size);
Chris Lattner73815062003-10-18 05:56:40 +00003538}
Chris Lattnereca195e2003-05-08 19:44:13 +00003539
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003540void X86ISel::visitVAArgInst(VAArgInst &I) {
Chris Lattner73815062003-10-18 05:56:40 +00003541 unsigned VAList = getReg(I.getOperand(0));
3542 unsigned DestReg = getReg(I);
3543
Chris Lattnerf70c22b2004-06-17 18:19:28 +00003544 switch (I.getType()->getTypeID()) {
Chris Lattner73815062003-10-18 05:56:40 +00003545 default:
3546 std::cerr << I;
3547 assert(0 && "Error: bad type for va_next instruction!");
3548 return;
3549 case Type::PointerTyID:
3550 case Type::UIntTyID:
3551 case Type::IntTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003552 addDirectMem(BuildMI(BB, X86::MOV32rm, 4, DestReg), VAList);
Chris Lattner73815062003-10-18 05:56:40 +00003553 break;
3554 case Type::ULongTyID:
3555 case Type::LongTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003556 addDirectMem(BuildMI(BB, X86::MOV32rm, 4, DestReg), VAList);
3557 addRegOffset(BuildMI(BB, X86::MOV32rm, 4, DestReg+1), VAList, 4);
Chris Lattner73815062003-10-18 05:56:40 +00003558 break;
3559 case Type::DoubleTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003560 addDirectMem(BuildMI(BB, X86::FLD64m, 4, DestReg), VAList);
Chris Lattner73815062003-10-18 05:56:40 +00003561 break;
3562 }
Chris Lattnereca195e2003-05-08 19:44:13 +00003563}
3564
Misha Brukman538607f2004-03-01 23:53:11 +00003565/// visitGetElementPtrInst - instruction-select GEP instructions
3566///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003567void X86ISel::visitGetElementPtrInst(GetElementPtrInst &I) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00003568 // If this GEP instruction will be folded into all of its users, we don't need
3569 // to explicitly calculate it!
Reid Spencerfc989e12004-08-30 00:13:26 +00003570 X86AddressMode AM;
3571 if (isGEPFoldable(0, I.getOperand(0), I.op_begin()+1, I.op_end(), AM)) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00003572 // Check all of the users of the instruction to see if they are loads and
3573 // stores.
3574 bool AllWillFold = true;
3575 for (Value::use_iterator UI = I.use_begin(), E = I.use_end(); UI != E; ++UI)
3576 if (cast<Instruction>(*UI)->getOpcode() != Instruction::Load)
3577 if (cast<Instruction>(*UI)->getOpcode() != Instruction::Store ||
3578 cast<Instruction>(*UI)->getOperand(0) == &I) {
3579 AllWillFold = false;
3580 break;
3581 }
3582
3583 // If the instruction is foldable, and will be folded into all users, don't
3584 // emit it!
3585 if (AllWillFold) return;
3586 }
3587
Chris Lattner3e130a22003-01-13 00:32:26 +00003588 unsigned outputReg = getReg(I);
Chris Lattner827832c2004-02-22 17:05:38 +00003589 emitGEPOperation(BB, BB->end(), I.getOperand(0),
Brian Gaeke68b1edc2002-12-16 04:23:29 +00003590 I.op_begin()+1, I.op_end(), outputReg);
Chris Lattnerc0812d82002-12-13 06:56:29 +00003591}
3592
Chris Lattner985fe3d2004-02-25 03:45:50 +00003593/// getGEPIndex - Inspect the getelementptr operands specified with GEPOps and
3594/// GEPTypes (the derived types being stepped through at each level). On return
3595/// from this function, if some indexes of the instruction are representable as
3596/// an X86 lea instruction, the machine operands are put into the Ops
3597/// instruction and the consumed indexes are poped from the GEPOps/GEPTypes
3598/// lists. Otherwise, GEPOps.size() is returned. If this returns a an
3599/// addressing mode that only partially consumes the input, the BaseReg input of
3600/// the addressing mode must be left free.
3601///
3602/// Note that there is one fewer entry in GEPTypes than there is in GEPOps.
3603///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003604void X86ISel::getGEPIndex(MachineBasicBlock *MBB,
3605 MachineBasicBlock::iterator IP,
3606 std::vector<Value*> &GEPOps,
3607 std::vector<const Type*> &GEPTypes,
3608 X86AddressMode &AM) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00003609 const TargetData &TD = TM.getTargetData();
3610
Chris Lattner985fe3d2004-02-25 03:45:50 +00003611 // Clear out the state we are working with...
Reid Spencerfc989e12004-08-30 00:13:26 +00003612 AM.BaseType = X86AddressMode::RegBase;
3613 AM.Base.Reg = 0; // No base register
3614 AM.Scale = 1; // Unit scale
3615 AM.IndexReg = 0; // No index register
3616 AM.Disp = 0; // No displacement
Chris Lattnerb6bac512004-02-25 06:13:04 +00003617
Chris Lattner985fe3d2004-02-25 03:45:50 +00003618 // While there are GEP indexes that can be folded into the current address,
3619 // keep processing them.
3620 while (!GEPTypes.empty()) {
3621 if (const StructType *StTy = dyn_cast<StructType>(GEPTypes.back())) {
3622 // It's a struct access. CUI is the index into the structure,
3623 // which names the field. This index must have unsigned type.
3624 const ConstantUInt *CUI = cast<ConstantUInt>(GEPOps.back());
3625
3626 // Use the TargetData structure to pick out what the layout of the
3627 // structure is in memory. Since the structure index must be constant, we
3628 // can get its value and use it to find the right byte offset from the
3629 // StructLayout class's list of structure member offsets.
Reid Spencerfc989e12004-08-30 00:13:26 +00003630 AM.Disp += TD.getStructLayout(StTy)->MemberOffsets[CUI->getValue()];
Chris Lattner985fe3d2004-02-25 03:45:50 +00003631 GEPOps.pop_back(); // Consume a GEP operand
3632 GEPTypes.pop_back();
3633 } else {
3634 // It's an array or pointer access: [ArraySize x ElementType].
3635 const SequentialType *SqTy = cast<SequentialType>(GEPTypes.back());
3636 Value *idx = GEPOps.back();
3637
3638 // idx is the index into the array. Unlike with structure
3639 // indices, we may not know its actual value at code-generation
3640 // time.
Chris Lattner985fe3d2004-02-25 03:45:50 +00003641
3642 // If idx is a constant, fold it into the offset.
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003643 unsigned TypeSize = TD.getTypeSize(SqTy->getElementType());
Chris Lattner985fe3d2004-02-25 03:45:50 +00003644 if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(idx)) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003645 AM.Disp += TypeSize*CSI->getValue();
Chris Lattner28977af2004-04-05 01:30:19 +00003646 } else if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(idx)) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003647 AM.Disp += TypeSize*CUI->getValue();
Chris Lattner985fe3d2004-02-25 03:45:50 +00003648 } else {
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003649 // If the index reg is already taken, we can't handle this index.
Reid Spencerfc989e12004-08-30 00:13:26 +00003650 if (AM.IndexReg) return;
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003651
3652 // If this is a size that we can handle, then add the index as
3653 switch (TypeSize) {
3654 case 1: case 2: case 4: case 8:
3655 // These are all acceptable scales on X86.
Reid Spencerfc989e12004-08-30 00:13:26 +00003656 AM.Scale = TypeSize;
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003657 break;
3658 default:
3659 // Otherwise, we can't handle this scale
3660 return;
3661 }
3662
3663 if (CastInst *CI = dyn_cast<CastInst>(idx))
3664 if (CI->getOperand(0)->getType() == Type::IntTy ||
3665 CI->getOperand(0)->getType() == Type::UIntTy)
3666 idx = CI->getOperand(0);
3667
Reid Spencerfc989e12004-08-30 00:13:26 +00003668 AM.IndexReg = MBB ? getReg(idx, MBB, IP) : 1;
Chris Lattner985fe3d2004-02-25 03:45:50 +00003669 }
3670
3671 GEPOps.pop_back(); // Consume a GEP operand
3672 GEPTypes.pop_back();
3673 }
3674 }
Chris Lattnerb6bac512004-02-25 06:13:04 +00003675
Chris Lattnerdf040972004-05-23 21:23:12 +00003676 // GEPTypes is empty, which means we have a single operand left. Set it as
3677 // the base register.
Chris Lattnerb6bac512004-02-25 06:13:04 +00003678 //
Reid Spencerfc989e12004-08-30 00:13:26 +00003679 assert(AM.Base.Reg == 0);
Chris Lattnerdf040972004-05-23 21:23:12 +00003680
Reid Spencerfc989e12004-08-30 00:13:26 +00003681 if (AllocaInst *AI = dyn_castFixedAlloca(GEPOps.back())) {
3682 AM.BaseType = X86AddressMode::FrameIndexBase;
3683 AM.Base.FrameIndex = getFixedSizedAllocaFI(AI);
Chris Lattnerdf040972004-05-23 21:23:12 +00003684 GEPOps.pop_back();
3685 return;
Reid Spencerfc989e12004-08-30 00:13:26 +00003686 }
3687
Chris Lattner358a9022004-10-15 05:05:29 +00003688 if (GlobalValue *GV = dyn_cast<GlobalValue>(GEPOps.back())) {
3689 AM.GV = GV;
3690 GEPOps.pop_back();
3691 return;
Chris Lattnerdf040972004-05-23 21:23:12 +00003692 }
Chris Lattnerdf040972004-05-23 21:23:12 +00003693
Reid Spencerfc989e12004-08-30 00:13:26 +00003694 AM.Base.Reg = MBB ? getReg(GEPOps[0], MBB, IP) : 1;
Chris Lattnerb6bac512004-02-25 06:13:04 +00003695 GEPOps.pop_back(); // Consume the last GEP operand
Chris Lattner985fe3d2004-02-25 03:45:50 +00003696}
3697
3698
Chris Lattnerb6bac512004-02-25 06:13:04 +00003699/// isGEPFoldable - Return true if the specified GEP can be completely
3700/// folded into the addressing mode of a load/store or lea instruction.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003701bool X86ISel::isGEPFoldable(MachineBasicBlock *MBB,
3702 Value *Src, User::op_iterator IdxBegin,
3703 User::op_iterator IdxEnd, X86AddressMode &AM) {
Chris Lattner7ca04092004-02-22 17:35:42 +00003704
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003705 std::vector<Value*> GEPOps;
3706 GEPOps.resize(IdxEnd-IdxBegin+1);
3707 GEPOps[0] = Src;
3708 std::copy(IdxBegin, IdxEnd, GEPOps.begin()+1);
3709
Chris Lattnerdf040972004-05-23 21:23:12 +00003710 std::vector<const Type*>
3711 GEPTypes(gep_type_begin(Src->getType(), IdxBegin, IdxEnd),
3712 gep_type_end(Src->getType(), IdxBegin, IdxEnd));
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003713
Chris Lattnerb6bac512004-02-25 06:13:04 +00003714 MachineBasicBlock::iterator IP;
3715 if (MBB) IP = MBB->end();
Reid Spencerfc989e12004-08-30 00:13:26 +00003716 getGEPIndex(MBB, IP, GEPOps, GEPTypes, AM);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003717
3718 // We can fold it away iff the getGEPIndex call eliminated all operands.
3719 return GEPOps.empty();
3720}
3721
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003722void X86ISel::emitGEPOperation(MachineBasicBlock *MBB,
3723 MachineBasicBlock::iterator IP,
3724 Value *Src, User::op_iterator IdxBegin,
3725 User::op_iterator IdxEnd, unsigned TargetReg) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00003726 const TargetData &TD = TM.getTargetData();
Chris Lattnerb6bac512004-02-25 06:13:04 +00003727
Chris Lattnerd2995df2004-07-15 00:58:53 +00003728 // If this is a getelementptr null, with all constant integer indices, just
3729 // replace it with TargetReg = 42.
3730 if (isa<ConstantPointerNull>(Src)) {
3731 User::op_iterator I = IdxBegin;
3732 for (; I != IdxEnd; ++I)
3733 if (!isa<ConstantInt>(*I))
3734 break;
3735 if (I == IdxEnd) { // All constant indices
3736 unsigned Offset = TD.getIndexedOffset(Src->getType(),
3737 std::vector<Value*>(IdxBegin, IdxEnd));
3738 BuildMI(*MBB, IP, X86::MOV32ri, 1, TargetReg).addImm(Offset);
3739 return;
3740 }
3741 }
3742
Chris Lattnerb6bac512004-02-25 06:13:04 +00003743 std::vector<Value*> GEPOps;
3744 GEPOps.resize(IdxEnd-IdxBegin+1);
3745 GEPOps[0] = Src;
3746 std::copy(IdxBegin, IdxEnd, GEPOps.begin()+1);
3747
3748 std::vector<const Type*> GEPTypes;
3749 GEPTypes.assign(gep_type_begin(Src->getType(), IdxBegin, IdxEnd),
3750 gep_type_end(Src->getType(), IdxBegin, IdxEnd));
Chris Lattner985fe3d2004-02-25 03:45:50 +00003751
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003752 // Keep emitting instructions until we consume the entire GEP instruction.
3753 while (!GEPOps.empty()) {
3754 unsigned OldSize = GEPOps.size();
Reid Spencerfc989e12004-08-30 00:13:26 +00003755 X86AddressMode AM;
3756 getGEPIndex(MBB, IP, GEPOps, GEPTypes, AM);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003757
Chris Lattner985fe3d2004-02-25 03:45:50 +00003758 if (GEPOps.size() != OldSize) {
3759 // getGEPIndex consumed some of the input. Build an LEA instruction here.
Chris Lattnerb6bac512004-02-25 06:13:04 +00003760 unsigned NextTarget = 0;
3761 if (!GEPOps.empty()) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003762 assert(AM.Base.Reg == 0 &&
Chris Lattnerb6bac512004-02-25 06:13:04 +00003763 "getGEPIndex should have left the base register open for chaining!");
Reid Spencerfc989e12004-08-30 00:13:26 +00003764 NextTarget = AM.Base.Reg = makeAnotherReg(Type::UIntTy);
Chris Lattner985fe3d2004-02-25 03:45:50 +00003765 }
Chris Lattnerb6bac512004-02-25 06:13:04 +00003766
Reid Spencerfc989e12004-08-30 00:13:26 +00003767 if (AM.BaseType == X86AddressMode::RegBase &&
Chris Lattner358a9022004-10-15 05:05:29 +00003768 AM.IndexReg == 0 && AM.Disp == 0 && !AM.GV)
Reid Spencerfc989e12004-08-30 00:13:26 +00003769 BuildMI(*MBB, IP, X86::MOV32rr, 1, TargetReg).addReg(AM.Base.Reg);
Chris Lattner358a9022004-10-15 05:05:29 +00003770 else if (AM.BaseType == X86AddressMode::RegBase && AM.Base.Reg == 0 &&
3771 AM.IndexReg == 0 && AM.Disp == 0)
3772 BuildMI(*MBB, IP, X86::MOV32ri, 1, TargetReg).addGlobalAddress(AM.GV);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003773 else
Reid Spencerfc989e12004-08-30 00:13:26 +00003774 addFullAddress(BuildMI(*MBB, IP, X86::LEA32r, 5, TargetReg), AM);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003775 --IP;
3776 TargetReg = NextTarget;
Chris Lattner985fe3d2004-02-25 03:45:50 +00003777 } else if (GEPTypes.empty()) {
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003778 // The getGEPIndex operation didn't want to build an LEA. Check to see if
3779 // all operands are consumed but the base pointer. If so, just load it
3780 // into the register.
Chris Lattner7ca04092004-02-22 17:35:42 +00003781 if (GlobalValue *GV = dyn_cast<GlobalValue>(GEPOps[0])) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003782 BuildMI(*MBB, IP, X86::MOV32ri, 1, TargetReg).addGlobalAddress(GV);
Chris Lattner7ca04092004-02-22 17:35:42 +00003783 } else {
3784 unsigned BaseReg = getReg(GEPOps[0], MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003785 BuildMI(*MBB, IP, X86::MOV32rr, 1, TargetReg).addReg(BaseReg);
Chris Lattner7ca04092004-02-22 17:35:42 +00003786 }
3787 break; // we are now done
Chris Lattnerb6bac512004-02-25 06:13:04 +00003788
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003789 } else {
Brian Gaeke20244b72002-12-12 15:33:40 +00003790 // It's an array or pointer access: [ArraySize x ElementType].
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003791 const SequentialType *SqTy = cast<SequentialType>(GEPTypes.back());
3792 Value *idx = GEPOps.back();
3793 GEPOps.pop_back(); // Consume a GEP operand
3794 GEPTypes.pop_back();
Chris Lattner8a307e82002-12-16 19:32:50 +00003795
Chris Lattner28977af2004-04-05 01:30:19 +00003796 // Many GEP instructions use a [cast (int/uint) to LongTy] as their
Chris Lattnerf5854472003-06-21 16:01:24 +00003797 // operand on X86. Handle this case directly now...
3798 if (CastInst *CI = dyn_cast<CastInst>(idx))
3799 if (CI->getOperand(0)->getType() == Type::IntTy ||
3800 CI->getOperand(0)->getType() == Type::UIntTy)
3801 idx = CI->getOperand(0);
3802
Chris Lattner3e130a22003-01-13 00:32:26 +00003803 // We want to add BaseReg to(idxReg * sizeof ElementType). First, we
Chris Lattner8a307e82002-12-16 19:32:50 +00003804 // must find the size of the pointed-to type (Not coincidentally, the next
3805 // type is the type of the elements in the array).
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003806 const Type *ElTy = SqTy->getElementType();
3807 unsigned elementSize = TD.getTypeSize(ElTy);
Chris Lattner8a307e82002-12-16 19:32:50 +00003808
3809 // If idxReg is a constant, we don't need to perform the multiply!
Chris Lattner28977af2004-04-05 01:30:19 +00003810 if (ConstantInt *CSI = dyn_cast<ConstantInt>(idx)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00003811 if (!CSI->isNullValue()) {
Chris Lattner28977af2004-04-05 01:30:19 +00003812 unsigned Offset = elementSize*CSI->getRawValue();
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003813 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003814 BuildMI(*MBB, IP, X86::ADD32ri, 2, TargetReg)
Chris Lattneree352852004-02-29 07:22:16 +00003815 .addReg(Reg).addImm(Offset);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003816 --IP; // Insert the next instruction before this one.
3817 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00003818 }
3819 } else if (elementSize == 1) {
3820 // If the element size is 1, we don't have to multiply, just add
3821 unsigned idxReg = getReg(idx, MBB, IP);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003822 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003823 BuildMI(*MBB, IP, X86::ADD32rr, 2,TargetReg).addReg(Reg).addReg(idxReg);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003824 --IP; // Insert the next instruction before this one.
3825 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00003826 } else {
3827 unsigned idxReg = getReg(idx, MBB, IP);
3828 unsigned OffsetReg = makeAnotherReg(Type::UIntTy);
Chris Lattnerb2acc512003-10-19 21:09:10 +00003829
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003830 // Make sure we can back the iterator up to point to the first
3831 // instruction emitted.
3832 MachineBasicBlock::iterator BeforeIt = IP;
3833 if (IP == MBB->begin())
3834 BeforeIt = MBB->end();
3835 else
3836 --BeforeIt;
Chris Lattnerb2acc512003-10-19 21:09:10 +00003837 doMultiplyConst(MBB, IP, OffsetReg, Type::IntTy, idxReg, elementSize);
3838
Chris Lattner8a307e82002-12-16 19:32:50 +00003839 // Emit an ADD to add OffsetReg to the basePtr.
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003840 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003841 BuildMI(*MBB, IP, X86::ADD32rr, 2, TargetReg)
Chris Lattneree352852004-02-29 07:22:16 +00003842 .addReg(Reg).addReg(OffsetReg);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003843
3844 // Step to the first instruction of the multiply.
3845 if (BeforeIt == MBB->end())
3846 IP = MBB->begin();
3847 else
3848 IP = ++BeforeIt;
3849
3850 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00003851 }
Brian Gaeke20244b72002-12-12 15:33:40 +00003852 }
Brian Gaeke20244b72002-12-12 15:33:40 +00003853 }
Brian Gaeke20244b72002-12-12 15:33:40 +00003854}
3855
Chris Lattner065faeb2002-12-28 20:24:02 +00003856/// visitAllocaInst - If this is a fixed size alloca, allocate space from the
3857/// frame manager, otherwise do it the hard way.
3858///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003859void X86ISel::visitAllocaInst(AllocaInst &I) {
Chris Lattner9f1b5312004-05-13 15:12:43 +00003860 // If this is a fixed size alloca in the entry block for the function, we
3861 // statically stack allocate the space, so we don't need to do anything here.
3862 //
Chris Lattnercb2fd552004-05-13 07:40:27 +00003863 if (dyn_castFixedAlloca(&I)) return;
Chris Lattner9f1b5312004-05-13 15:12:43 +00003864
Brian Gaekee48ec012002-12-13 06:46:31 +00003865 // Find the data size of the alloca inst's getAllocatedType.
Chris Lattner065faeb2002-12-28 20:24:02 +00003866 const Type *Ty = I.getAllocatedType();
3867 unsigned TySize = TM.getTargetData().getTypeSize(Ty);
3868
Chris Lattner065faeb2002-12-28 20:24:02 +00003869 // Create a register to hold the temporary result of multiplying the type size
3870 // constant by the variable amount.
3871 unsigned TotalSizeReg = makeAnotherReg(Type::UIntTy);
3872 unsigned SrcReg1 = getReg(I.getArraySize());
Chris Lattner065faeb2002-12-28 20:24:02 +00003873
3874 // TotalSizeReg = mul <numelements>, <TypeSize>
3875 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattnerb2acc512003-10-19 21:09:10 +00003876 doMultiplyConst(BB, MBBI, TotalSizeReg, Type::UIntTy, SrcReg1, TySize);
Chris Lattner065faeb2002-12-28 20:24:02 +00003877
3878 // AddedSize = add <TotalSizeReg>, 15
3879 unsigned AddedSizeReg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003880 BuildMI(BB, X86::ADD32ri, 2, AddedSizeReg).addReg(TotalSizeReg).addImm(15);
Chris Lattner065faeb2002-12-28 20:24:02 +00003881
3882 // AlignedSize = and <AddedSize>, ~15
3883 unsigned AlignedSize = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003884 BuildMI(BB, X86::AND32ri, 2, AlignedSize).addReg(AddedSizeReg).addImm(~15);
Chris Lattner065faeb2002-12-28 20:24:02 +00003885
Brian Gaekee48ec012002-12-13 06:46:31 +00003886 // Subtract size from stack pointer, thereby allocating some space.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003887 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(AlignedSize);
Chris Lattner065faeb2002-12-28 20:24:02 +00003888
Brian Gaekee48ec012002-12-13 06:46:31 +00003889 // Put a pointer to the space into the result register, by copying
3890 // the stack pointer.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003891 BuildMI(BB, X86::MOV32rr, 1, getReg(I)).addReg(X86::ESP);
Chris Lattner065faeb2002-12-28 20:24:02 +00003892
Misha Brukman48196b32003-05-03 02:18:17 +00003893 // Inform the Frame Information that we have just allocated a variable-sized
Chris Lattner065faeb2002-12-28 20:24:02 +00003894 // object.
3895 F->getFrameInfo()->CreateVariableSizedObject();
Brian Gaeke20244b72002-12-12 15:33:40 +00003896}
Chris Lattner3e130a22003-01-13 00:32:26 +00003897
3898/// visitMallocInst - Malloc instructions are code generated into direct calls
3899/// to the library malloc.
3900///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003901void X86ISel::visitMallocInst(MallocInst &I) {
Chris Lattner3e130a22003-01-13 00:32:26 +00003902 unsigned AllocSize = TM.getTargetData().getTypeSize(I.getAllocatedType());
3903 unsigned Arg;
3904
3905 if (ConstantUInt *C = dyn_cast<ConstantUInt>(I.getOperand(0))) {
3906 Arg = getReg(ConstantUInt::get(Type::UIntTy, C->getValue() * AllocSize));
3907 } else {
3908 Arg = makeAnotherReg(Type::UIntTy);
Chris Lattnerb2acc512003-10-19 21:09:10 +00003909 unsigned Op0Reg = getReg(I.getOperand(0));
Chris Lattner3e130a22003-01-13 00:32:26 +00003910 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattnerb2acc512003-10-19 21:09:10 +00003911 doMultiplyConst(BB, MBBI, Arg, Type::UIntTy, Op0Reg, AllocSize);
Chris Lattner3e130a22003-01-13 00:32:26 +00003912 }
3913
3914 std::vector<ValueRecord> Args;
3915 Args.push_back(ValueRecord(Arg, Type::UIntTy));
3916 MachineInstr *TheCall = BuildMI(X86::CALLpcrel32,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003917 1).addExternalSymbol("malloc", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00003918 doCall(ValueRecord(getReg(I), I.getType()), TheCall, Args);
3919}
3920
3921
3922/// visitFreeInst - Free instructions are code gen'd to call the free libc
3923/// function.
3924///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003925void X86ISel::visitFreeInst(FreeInst &I) {
Chris Lattner3e130a22003-01-13 00:32:26 +00003926 std::vector<ValueRecord> Args;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00003927 Args.push_back(ValueRecord(I.getOperand(0)));
Chris Lattner3e130a22003-01-13 00:32:26 +00003928 MachineInstr *TheCall = BuildMI(X86::CALLpcrel32,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003929 1).addExternalSymbol("free", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00003930 doCall(ValueRecord(0, Type::VoidTy), TheCall, Args);
3931}
3932
Chris Lattnerd281de22003-07-26 23:49:58 +00003933/// createX86SimpleInstructionSelector - This pass converts an LLVM function
Chris Lattnerb4f68ed2002-10-29 22:37:54 +00003934/// into a machine code representation is a very simple peep-hole fashion. The
Chris Lattner72614082002-10-25 22:55:53 +00003935/// generated code sucks but the implementation is nice and simple.
3936///
Chris Lattnerf70e0c22003-12-28 21:23:38 +00003937FunctionPass *llvm::createX86SimpleInstructionSelector(TargetMachine &TM) {
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003938 return new X86ISel(TM);
Chris Lattner72614082002-10-25 22:55:53 +00003939}