blob: 71607b4ad8699048a331b9bb4622a60a250d8378 [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 Lattner3e130a22003-01-13 00:32:26 +0000178
179 struct ValueRecord {
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000180 Value *Val;
Chris Lattner3e130a22003-01-13 00:32:26 +0000181 unsigned Reg;
182 const Type *Ty;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000183 ValueRecord(unsigned R, const Type *T) : Val(0), Reg(R), Ty(T) {}
184 ValueRecord(Value *V) : Val(V), Reg(0), Ty(V->getType()) {}
Chris Lattner3e130a22003-01-13 00:32:26 +0000185 };
186 void doCall(const ValueRecord &Ret, MachineInstr *CallMI,
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000187 const std::vector<ValueRecord> &Args);
Brian Gaekefa8d5712002-11-22 11:07:01 +0000188 void visitCallInst(CallInst &I);
Brian Gaeked0fde302003-11-11 22:41:34 +0000189 void visitIntrinsicCall(Intrinsic::ID ID, CallInst &I);
Chris Lattnere2954c82002-11-02 20:04:26 +0000190
191 // Arithmetic operators
Chris Lattnerf01729e2002-11-02 20:54:46 +0000192 void visitSimpleBinary(BinaryOperator &B, unsigned OpcodeClass);
Chris Lattner68aad932002-11-02 20:13:22 +0000193 void visitAdd(BinaryOperator &B) { visitSimpleBinary(B, 0); }
194 void visitSub(BinaryOperator &B) { visitSimpleBinary(B, 1); }
Chris Lattnerca9671d2002-11-02 20:28:58 +0000195 void visitMul(BinaryOperator &B);
Chris Lattnere2954c82002-11-02 20:04:26 +0000196
Chris Lattnerf01729e2002-11-02 20:54:46 +0000197 void visitDiv(BinaryOperator &B) { visitDivRem(B); }
198 void visitRem(BinaryOperator &B) { visitDivRem(B); }
199 void visitDivRem(BinaryOperator &B);
200
Chris Lattnere2954c82002-11-02 20:04:26 +0000201 // Bitwise operators
Chris Lattner68aad932002-11-02 20:13:22 +0000202 void visitAnd(BinaryOperator &B) { visitSimpleBinary(B, 2); }
203 void visitOr (BinaryOperator &B) { visitSimpleBinary(B, 3); }
204 void visitXor(BinaryOperator &B) { visitSimpleBinary(B, 4); }
Chris Lattnere2954c82002-11-02 20:04:26 +0000205
Chris Lattner6d40c192003-01-16 16:43:00 +0000206 // Comparison operators...
207 void visitSetCondInst(SetCondInst &I);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000208 unsigned EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
209 MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000210 MachineBasicBlock::iterator MBBI);
Chris Lattner12d96a02004-03-30 21:22:00 +0000211 void visitSelectInst(SelectInst &SI);
212
Chris Lattnerb2acc512003-10-19 21:09:10 +0000213
Chris Lattner6fc3c522002-11-17 21:11:55 +0000214 // Memory Instructions
215 void visitLoadInst(LoadInst &I);
216 void visitStoreInst(StoreInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000217 void visitGetElementPtrInst(GetElementPtrInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000218 void visitAllocaInst(AllocaInst &I);
Chris Lattner3e130a22003-01-13 00:32:26 +0000219 void visitMallocInst(MallocInst &I);
220 void visitFreeInst(FreeInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000221
Chris Lattnere2954c82002-11-02 20:04:26 +0000222 // Other operators
Brian Gaekea1719c92002-10-31 23:03:59 +0000223 void visitShiftInst(ShiftInst &I);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000224 void visitPHINode(PHINode &I) {} // PHI nodes handled by second pass
Brian Gaekefa8d5712002-11-22 11:07:01 +0000225 void visitCastInst(CastInst &I);
Chris Lattner73815062003-10-18 05:56:40 +0000226 void visitVANextInst(VANextInst &I);
227 void visitVAArgInst(VAArgInst &I);
Chris Lattner72614082002-10-25 22:55:53 +0000228
229 void visitInstruction(Instruction &I) {
230 std::cerr << "Cannot instruction select: " << I;
231 abort();
232 }
233
Brian Gaeke95780cc2002-12-13 07:56:18 +0000234 /// promote32 - Make a value 32-bits wide, and put it somewhere.
Chris Lattner3e130a22003-01-13 00:32:26 +0000235 ///
236 void promote32(unsigned targetReg, const ValueRecord &VR);
237
Chris Lattner721d2d42004-03-08 01:18:36 +0000238 /// getAddressingMode - Get the addressing mode to use to address the
239 /// specified value. The returned value should be used with addFullAddress.
Reid Spencerfc989e12004-08-30 00:13:26 +0000240 void getAddressingMode(Value *Addr, X86AddressMode &AM);
Chris Lattner721d2d42004-03-08 01:18:36 +0000241
242
243 /// getGEPIndex - This is used to fold GEP instructions into X86 addressing
244 /// expressions.
Chris Lattnerb6bac512004-02-25 06:13:04 +0000245 void getGEPIndex(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
246 std::vector<Value*> &GEPOps,
Reid Spencerfc989e12004-08-30 00:13:26 +0000247 std::vector<const Type*> &GEPTypes,
248 X86AddressMode &AM);
Chris Lattnerb6bac512004-02-25 06:13:04 +0000249
250 /// isGEPFoldable - Return true if the specified GEP can be completely
251 /// folded into the addressing mode of a load/store or lea instruction.
252 bool isGEPFoldable(MachineBasicBlock *MBB,
253 Value *Src, User::op_iterator IdxBegin,
Reid Spencerfc989e12004-08-30 00:13:26 +0000254 User::op_iterator IdxEnd, X86AddressMode &AM);
Chris Lattnerb6bac512004-02-25 06:13:04 +0000255
Chris Lattner3e130a22003-01-13 00:32:26 +0000256 /// emitGEPOperation - Common code shared between visitGetElementPtrInst and
257 /// constant expression GEP support.
258 ///
Chris Lattner827832c2004-02-22 17:05:38 +0000259 void emitGEPOperation(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
Chris Lattner333b2fa2002-12-13 10:09:43 +0000260 Value *Src, User::op_iterator IdxBegin,
Chris Lattnerc0812d82002-12-13 06:56:29 +0000261 User::op_iterator IdxEnd, unsigned TargetReg);
262
Chris Lattner548f61d2003-04-23 17:22:12 +0000263 /// emitCastOperation - Common code shared between visitCastInst and
264 /// constant expression cast support.
Misha Brukman538607f2004-03-01 23:53:11 +0000265 ///
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000266 void emitCastOperation(MachineBasicBlock *BB,MachineBasicBlock::iterator IP,
Chris Lattner548f61d2003-04-23 17:22:12 +0000267 Value *Src, const Type *DestTy, unsigned TargetReg);
268
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000269 /// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
270 /// and constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000271 ///
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000272 void emitSimpleBinaryOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000273 MachineBasicBlock::iterator IP,
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000274 Value *Op0, Value *Op1,
275 unsigned OperatorClass, unsigned TargetReg);
276
Chris Lattner6621ed92004-04-11 21:23:56 +0000277 /// emitBinaryFPOperation - This method handles emission of floating point
278 /// Add (0), Sub (1), Mul (2), and Div (3) operations.
279 void emitBinaryFPOperation(MachineBasicBlock *BB,
280 MachineBasicBlock::iterator IP,
281 Value *Op0, Value *Op1,
282 unsigned OperatorClass, unsigned TargetReg);
283
Chris Lattner462fa822004-04-11 20:56:28 +0000284 void emitMultiply(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
285 Value *Op0, Value *Op1, unsigned TargetReg);
286
287 void doMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI,
288 unsigned DestReg, const Type *DestTy,
289 unsigned Op0Reg, unsigned Op1Reg);
290 void doMultiplyConst(MachineBasicBlock *MBB,
291 MachineBasicBlock::iterator MBBI,
292 unsigned DestReg, const Type *DestTy,
293 unsigned Op0Reg, unsigned Op1Val);
294
Chris Lattnercadff442003-10-23 17:21:43 +0000295 void emitDivRemOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000296 MachineBasicBlock::iterator IP,
Chris Lattner462fa822004-04-11 20:56:28 +0000297 Value *Op0, Value *Op1, bool isDiv,
298 unsigned TargetReg);
Chris Lattnercadff442003-10-23 17:21:43 +0000299
Chris Lattner58c41fe2003-08-24 19:19:47 +0000300 /// emitSetCCOperation - Common code shared between visitSetCondInst and
301 /// constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000302 ///
Chris Lattner58c41fe2003-08-24 19:19:47 +0000303 void emitSetCCOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000304 MachineBasicBlock::iterator IP,
Chris Lattner58c41fe2003-08-24 19:19:47 +0000305 Value *Op0, Value *Op1, unsigned Opcode,
306 unsigned TargetReg);
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000307
308 /// emitShiftOperation - Common code shared between visitShiftInst and
309 /// constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000310 ///
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000311 void emitShiftOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000312 MachineBasicBlock::iterator IP,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000313 Value *Op, Value *ShiftAmount, bool isLeftShift,
314 const Type *ResultTy, unsigned DestReg);
315
Chris Lattner12d96a02004-03-30 21:22:00 +0000316 /// emitSelectOperation - Common code shared between visitSelectInst and the
317 /// constant expression support.
318 void emitSelectOperation(MachineBasicBlock *MBB,
319 MachineBasicBlock::iterator IP,
320 Value *Cond, Value *TrueVal, Value *FalseVal,
321 unsigned DestReg);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000322
Chris Lattnerc5291f52002-10-27 21:16:59 +0000323 /// copyConstantToRegister - Output the instructions required to put the
324 /// specified constant into the specified register.
325 ///
Chris Lattner8a307e82002-12-16 19:32:50 +0000326 void copyConstantToRegister(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000327 MachineBasicBlock::iterator MBBI,
Chris Lattner8a307e82002-12-16 19:32:50 +0000328 Constant *C, unsigned Reg);
Chris Lattnerc5291f52002-10-27 21:16:59 +0000329
Chris Lattner01cdb1b2004-06-11 05:33:49 +0000330 void emitUCOMr(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI,
331 unsigned LHS, unsigned RHS);
332
Chris Lattner3e130a22003-01-13 00:32:26 +0000333 /// makeAnotherReg - This method returns the next register number we haven't
334 /// yet used.
335 ///
336 /// Long values are handled somewhat specially. They are always allocated
337 /// as pairs of 32 bit integer values. The register number returned is the
338 /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits
339 /// of the long value.
340 ///
Chris Lattnerc0812d82002-12-13 06:56:29 +0000341 unsigned makeAnotherReg(const Type *Ty) {
Chris Lattner7db1fa92003-07-30 05:33:48 +0000342 assert(dynamic_cast<const X86RegisterInfo*>(TM.getRegisterInfo()) &&
343 "Current target doesn't have X86 reg info??");
344 const X86RegisterInfo *MRI =
345 static_cast<const X86RegisterInfo*>(TM.getRegisterInfo());
Chris Lattner3e130a22003-01-13 00:32:26 +0000346 if (Ty == Type::LongTy || Ty == Type::ULongTy) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000347 const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy);
348 // Create the lower part
349 F->getSSARegMap()->createVirtualRegister(RC);
350 // Create the upper part.
351 return F->getSSARegMap()->createVirtualRegister(RC)-1;
Chris Lattner3e130a22003-01-13 00:32:26 +0000352 }
353
Chris Lattnerc0812d82002-12-13 06:56:29 +0000354 // Add the mapping of regnumber => reg class to MachineFunction
Chris Lattner7db1fa92003-07-30 05:33:48 +0000355 const TargetRegisterClass *RC = MRI->getRegClassForType(Ty);
Chris Lattner3e130a22003-01-13 00:32:26 +0000356 return F->getSSARegMap()->createVirtualRegister(RC);
Brian Gaeke20244b72002-12-12 15:33:40 +0000357 }
358
Chris Lattnercb2fd552004-05-13 07:40:27 +0000359 /// getReg - This method turns an LLVM value into a register number.
Chris Lattner72614082002-10-25 22:55:53 +0000360 ///
361 unsigned getReg(Value &V) { return getReg(&V); } // Allow references
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000362 unsigned getReg(Value *V) {
363 // Just append to the end of the current bb.
364 MachineBasicBlock::iterator It = BB->end();
365 return getReg(V, BB, It);
366 }
Brian Gaeke71794c02002-12-13 11:22:48 +0000367 unsigned getReg(Value *V, MachineBasicBlock *MBB,
Chris Lattnercb2fd552004-05-13 07:40:27 +0000368 MachineBasicBlock::iterator IPt);
Chris Lattner427aeb42004-04-11 19:21:59 +0000369
Chris Lattnercb2fd552004-05-13 07:40:27 +0000370 /// getFixedSizedAllocaFI - Return the frame index for a fixed sized alloca
371 /// that is to be statically allocated with the initial stack frame
372 /// adjustment.
373 unsigned getFixedSizedAllocaFI(AllocaInst *AI);
Chris Lattner72614082002-10-25 22:55:53 +0000374 };
375}
376
Chris Lattnercb2fd552004-05-13 07:40:27 +0000377/// dyn_castFixedAlloca - If the specified value is a fixed size alloca
378/// instruction in the entry block, return it. Otherwise, return a null
379/// pointer.
380static AllocaInst *dyn_castFixedAlloca(Value *V) {
381 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
382 BasicBlock *BB = AI->getParent();
383 if (isa<ConstantUInt>(AI->getArraySize()) && BB ==&BB->getParent()->front())
384 return AI;
385 }
386 return 0;
387}
388
389/// getReg - This method turns an LLVM value into a register number.
390///
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000391unsigned X86ISel::getReg(Value *V, MachineBasicBlock *MBB,
392 MachineBasicBlock::iterator IPt) {
Chris Lattnercb2fd552004-05-13 07:40:27 +0000393 // If this operand is a constant, emit the code to copy the constant into
394 // the register here...
Chris Lattnercb2fd552004-05-13 07:40:27 +0000395 if (Constant *C = dyn_cast<Constant>(V)) {
396 unsigned Reg = makeAnotherReg(V->getType());
397 copyConstantToRegister(MBB, IPt, C, Reg);
398 return Reg;
Chris Lattnercb2fd552004-05-13 07:40:27 +0000399 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
Chris Lattner8b486a12004-06-29 00:14:38 +0000400 // Do not emit noop casts at all, unless it's a double -> float cast.
401 if (getClassB(CI->getType()) == getClassB(CI->getOperand(0)->getType()) &&
402 (CI->getType() != Type::FloatTy ||
403 CI->getOperand(0)->getType() != Type::DoubleTy))
Chris Lattnercb2fd552004-05-13 07:40:27 +0000404 return getReg(CI->getOperand(0), MBB, IPt);
405 } else if (AllocaInst *AI = dyn_castFixedAlloca(V)) {
406 // If the alloca address couldn't be folded into the instruction addressing,
407 // emit an explicit LEA as appropriate.
408 unsigned Reg = makeAnotherReg(V->getType());
409 unsigned FI = getFixedSizedAllocaFI(AI);
410 addFrameReference(BuildMI(*MBB, IPt, X86::LEA32r, 4, Reg), FI);
411 return Reg;
412 }
413
414 unsigned &Reg = RegMap[V];
415 if (Reg == 0) {
416 Reg = makeAnotherReg(V->getType());
417 RegMap[V] = Reg;
418 }
419
420 return Reg;
421}
422
423/// getFixedSizedAllocaFI - Return the frame index for a fixed sized alloca
424/// that is to be statically allocated with the initial stack frame
425/// adjustment.
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000426unsigned X86ISel::getFixedSizedAllocaFI(AllocaInst *AI) {
Chris Lattnercb2fd552004-05-13 07:40:27 +0000427 // Already computed this?
428 std::map<AllocaInst*, unsigned>::iterator I = AllocaMap.lower_bound(AI);
429 if (I != AllocaMap.end() && I->first == AI) return I->second;
430
431 const Type *Ty = AI->getAllocatedType();
432 ConstantUInt *CUI = cast<ConstantUInt>(AI->getArraySize());
433 unsigned TySize = TM.getTargetData().getTypeSize(Ty);
434 TySize *= CUI->getValue(); // Get total allocated size...
435 unsigned Alignment = TM.getTargetData().getTypeAlignment(Ty);
436
437 // Create a new stack object using the frame manager...
438 int FrameIdx = F->getFrameInfo()->CreateStackObject(TySize, Alignment);
439 AllocaMap.insert(I, std::make_pair(AI, FrameIdx));
440 return FrameIdx;
441}
442
443
Chris Lattnerc5291f52002-10-27 21:16:59 +0000444/// copyConstantToRegister - Output the instructions required to put the
445/// specified constant into the specified register.
446///
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000447void X86ISel::copyConstantToRegister(MachineBasicBlock *MBB,
448 MachineBasicBlock::iterator IP,
449 Constant *C, unsigned R) {
Chris Lattnerc0812d82002-12-13 06:56:29 +0000450 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000451 unsigned Class = 0;
452 switch (CE->getOpcode()) {
453 case Instruction::GetElementPtr:
Brian Gaeke68b1edc2002-12-16 04:23:29 +0000454 emitGEPOperation(MBB, IP, CE->getOperand(0),
Chris Lattner333b2fa2002-12-13 10:09:43 +0000455 CE->op_begin()+1, CE->op_end(), R);
Chris Lattnerc0812d82002-12-13 06:56:29 +0000456 return;
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000457 case Instruction::Cast:
Chris Lattner548f61d2003-04-23 17:22:12 +0000458 emitCastOperation(MBB, IP, CE->getOperand(0), CE->getType(), R);
Chris Lattner4b12cde2003-04-21 21:33:44 +0000459 return;
Chris Lattnerc0812d82002-12-13 06:56:29 +0000460
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000461 case Instruction::Xor: ++Class; // FALL THROUGH
462 case Instruction::Or: ++Class; // FALL THROUGH
463 case Instruction::And: ++Class; // FALL THROUGH
464 case Instruction::Sub: ++Class; // FALL THROUGH
465 case Instruction::Add:
466 emitSimpleBinaryOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
467 Class, R);
468 return;
469
Chris Lattner462fa822004-04-11 20:56:28 +0000470 case Instruction::Mul:
471 emitMultiply(MBB, IP, CE->getOperand(0), CE->getOperand(1), R);
Chris Lattnercadff442003-10-23 17:21:43 +0000472 return;
Chris Lattner462fa822004-04-11 20:56:28 +0000473
Chris Lattnercadff442003-10-23 17:21:43 +0000474 case Instruction::Div:
Chris Lattner462fa822004-04-11 20:56:28 +0000475 case Instruction::Rem:
476 emitDivRemOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
477 CE->getOpcode() == Instruction::Div, R);
Chris Lattnercadff442003-10-23 17:21:43 +0000478 return;
Chris Lattnercadff442003-10-23 17:21:43 +0000479
Chris Lattner58c41fe2003-08-24 19:19:47 +0000480 case Instruction::SetNE:
481 case Instruction::SetEQ:
482 case Instruction::SetLT:
483 case Instruction::SetGT:
484 case Instruction::SetLE:
485 case Instruction::SetGE:
486 emitSetCCOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
487 CE->getOpcode(), R);
488 return;
489
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000490 case Instruction::Shl:
491 case Instruction::Shr:
492 emitShiftOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000493 CE->getOpcode() == Instruction::Shl, CE->getType(), R);
494 return;
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000495
Chris Lattner12d96a02004-03-30 21:22:00 +0000496 case Instruction::Select:
497 emitSelectOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
498 CE->getOperand(2), R);
499 return;
500
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000501 default:
Chris Lattner76e2df22004-07-15 02:14:30 +0000502 std::cerr << "Offending expr: " << *C << "\n";
Chris Lattnerb2acc512003-10-19 21:09:10 +0000503 assert(0 && "Constant expression not yet handled!\n");
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000504 }
Brian Gaeke20244b72002-12-12 15:33:40 +0000505 }
Chris Lattnerc5291f52002-10-27 21:16:59 +0000506
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000507 if (C->getType()->isIntegral()) {
Chris Lattner6b993cc2002-12-15 08:02:15 +0000508 unsigned Class = getClassB(C->getType());
Chris Lattner3e130a22003-01-13 00:32:26 +0000509
510 if (Class == cLong) {
511 // Copy the value into the register pair.
Chris Lattnerc07736a2003-07-23 15:22:26 +0000512 uint64_t Val = cast<ConstantInt>(C)->getRawValue();
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000513 BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addImm(Val & 0xFFFFFFFF);
514 BuildMI(*MBB, IP, X86::MOV32ri, 1, R+1).addImm(Val >> 32);
Chris Lattner3e130a22003-01-13 00:32:26 +0000515 return;
516 }
517
Chris Lattner94af4142002-12-25 05:13:53 +0000518 assert(Class <= cInt && "Type not handled yet!");
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000519
520 static const unsigned IntegralOpcodeTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000521 X86::MOV8ri, X86::MOV16ri, X86::MOV32ri
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000522 };
523
Chris Lattner6b993cc2002-12-15 08:02:15 +0000524 if (C->getType() == Type::BoolTy) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000525 BuildMI(*MBB, IP, X86::MOV8ri, 1, R).addImm(C == ConstantBool::True);
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000526 } else {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000527 ConstantInt *CI = cast<ConstantInt>(C);
Chris Lattneree352852004-02-29 07:22:16 +0000528 BuildMI(*MBB, IP, IntegralOpcodeTab[Class],1,R).addImm(CI->getRawValue());
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000529 }
Chris Lattner94af4142002-12-25 05:13:53 +0000530 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
Chris Lattneraf703622004-02-02 18:56:30 +0000531 if (CFP->isExactlyValue(+0.0))
Chris Lattneree352852004-02-29 07:22:16 +0000532 BuildMI(*MBB, IP, X86::FLD0, 0, R);
Chris Lattneraf703622004-02-02 18:56:30 +0000533 else if (CFP->isExactlyValue(+1.0))
Chris Lattneree352852004-02-29 07:22:16 +0000534 BuildMI(*MBB, IP, X86::FLD1, 0, R);
Chris Lattner94af4142002-12-25 05:13:53 +0000535 else {
Chris Lattner3e130a22003-01-13 00:32:26 +0000536 // Otherwise we need to spill the constant to memory...
537 MachineConstantPool *CP = F->getConstantPool();
538 unsigned CPI = CP->getConstantPoolIndex(CFP);
Chris Lattner6c09db22003-10-20 04:11:23 +0000539 const Type *Ty = CFP->getType();
540
541 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000542 unsigned LoadOpcode = Ty == Type::FloatTy ? X86::FLD32m : X86::FLD64m;
Chris Lattneree352852004-02-29 07:22:16 +0000543 addConstantPoolReference(BuildMI(*MBB, IP, LoadOpcode, 4, R), CPI);
Chris Lattner94af4142002-12-25 05:13:53 +0000544 }
545
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000546 } else if (isa<ConstantPointerNull>(C)) {
Brian Gaeke20244b72002-12-12 15:33:40 +0000547 // Copy zero (null pointer) to the register.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000548 BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addImm(0);
Reid Spencer8863f182004-07-18 00:38:32 +0000549 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) {
550 BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addGlobalAddress(GV);
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000551 } else {
Chris Lattner76e2df22004-07-15 02:14:30 +0000552 std::cerr << "Offending constant: " << *C << "\n";
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000553 assert(0 && "Type not handled yet!");
Chris Lattnerc5291f52002-10-27 21:16:59 +0000554 }
555}
556
Chris Lattner065faeb2002-12-28 20:24:02 +0000557/// LoadArgumentsToVirtualRegs - Load all of the arguments to this function from
558/// the stack into virtual registers.
559///
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000560void X86ISel::LoadArgumentsToVirtualRegs(Function &Fn) {
Chris Lattner065faeb2002-12-28 20:24:02 +0000561 // Emit instructions to load the arguments... On entry to a function on the
562 // X86, the stack frame looks like this:
563 //
564 // [ESP] -- return address
Chris Lattner3e130a22003-01-13 00:32:26 +0000565 // [ESP + 4] -- first argument (leftmost lexically)
566 // [ESP + 8] -- second argument, if first argument is four bytes in size
Chris Lattner065faeb2002-12-28 20:24:02 +0000567 // ...
568 //
Chris Lattnerf158da22003-01-16 02:20:12 +0000569 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattneraa09b752002-12-28 21:08:28 +0000570 MachineFrameInfo *MFI = F->getFrameInfo();
Chris Lattner065faeb2002-12-28 20:24:02 +0000571
572 for (Function::aiterator I = Fn.abegin(), E = Fn.aend(); I != E; ++I) {
Chris Lattner427aeb42004-04-11 19:21:59 +0000573 bool ArgLive = !I->use_empty();
574 unsigned Reg = ArgLive ? getReg(*I) : 0;
Chris Lattner065faeb2002-12-28 20:24:02 +0000575 int FI; // Frame object index
Chris Lattner427aeb42004-04-11 19:21:59 +0000576
Chris Lattner065faeb2002-12-28 20:24:02 +0000577 switch (getClassB(I->getType())) {
578 case cByte:
Chris Lattner427aeb42004-04-11 19:21:59 +0000579 if (ArgLive) {
580 FI = MFI->CreateFixedObject(1, ArgOffset);
581 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Reg), FI);
582 }
Chris Lattner065faeb2002-12-28 20:24:02 +0000583 break;
584 case cShort:
Chris Lattner427aeb42004-04-11 19:21:59 +0000585 if (ArgLive) {
586 FI = MFI->CreateFixedObject(2, ArgOffset);
587 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Reg), FI);
588 }
Chris Lattner065faeb2002-12-28 20:24:02 +0000589 break;
590 case cInt:
Chris Lattner427aeb42004-04-11 19:21:59 +0000591 if (ArgLive) {
592 FI = MFI->CreateFixedObject(4, ArgOffset);
593 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Reg), FI);
594 }
Chris Lattner065faeb2002-12-28 20:24:02 +0000595 break;
Chris Lattner3e130a22003-01-13 00:32:26 +0000596 case cLong:
Chris Lattner427aeb42004-04-11 19:21:59 +0000597 if (ArgLive) {
598 FI = MFI->CreateFixedObject(8, ArgOffset);
599 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Reg), FI);
600 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Reg+1), FI, 4);
601 }
Chris Lattner3e130a22003-01-13 00:32:26 +0000602 ArgOffset += 4; // longs require 4 additional bytes
603 break;
Chris Lattner065faeb2002-12-28 20:24:02 +0000604 case cFP:
Chris Lattner427aeb42004-04-11 19:21:59 +0000605 if (ArgLive) {
606 unsigned Opcode;
607 if (I->getType() == Type::FloatTy) {
608 Opcode = X86::FLD32m;
609 FI = MFI->CreateFixedObject(4, ArgOffset);
610 } else {
611 Opcode = X86::FLD64m;
612 FI = MFI->CreateFixedObject(8, ArgOffset);
613 }
614 addFrameReference(BuildMI(BB, Opcode, 4, Reg), FI);
Chris Lattner065faeb2002-12-28 20:24:02 +0000615 }
Chris Lattner427aeb42004-04-11 19:21:59 +0000616 if (I->getType() == Type::DoubleTy)
617 ArgOffset += 4; // doubles require 4 additional bytes
Chris Lattner065faeb2002-12-28 20:24:02 +0000618 break;
619 default:
620 assert(0 && "Unhandled argument type!");
621 }
Chris Lattner3e130a22003-01-13 00:32:26 +0000622 ArgOffset += 4; // Each argument takes at least 4 bytes on the stack...
Chris Lattner065faeb2002-12-28 20:24:02 +0000623 }
Chris Lattnereca195e2003-05-08 19:44:13 +0000624
625 // If the function takes variable number of arguments, add a frame offset for
626 // the start of the first vararg value... this is used to expand
627 // llvm.va_start.
628 if (Fn.getFunctionType()->isVarArg())
629 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner065faeb2002-12-28 20:24:02 +0000630}
631
632
Chris Lattner333b2fa2002-12-13 10:09:43 +0000633/// SelectPHINodes - Insert machine code to generate phis. This is tricky
634/// because we have to generate our sources into the source basic blocks, not
635/// the current one.
636///
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000637void X86ISel::SelectPHINodes() {
Chris Lattnerd029cd22004-06-02 05:55:25 +0000638 const TargetInstrInfo &TII = *TM.getInstrInfo();
Chris Lattner333b2fa2002-12-13 10:09:43 +0000639 const Function &LF = *F->getFunction(); // The LLVM function...
640 for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) {
641 const BasicBlock *BB = I;
Chris Lattner168aa902004-02-29 07:10:16 +0000642 MachineBasicBlock &MBB = *MBBMap[I];
Chris Lattner333b2fa2002-12-13 10:09:43 +0000643
644 // Loop over all of the PHI nodes in the LLVM basic block...
Chris Lattner168aa902004-02-29 07:10:16 +0000645 MachineBasicBlock::iterator PHIInsertPoint = MBB.begin();
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000646 for (BasicBlock::const_iterator I = BB->begin(); isa<PHINode>(I); ++I) {
647 PHINode *PN = const_cast<PHINode*>(dyn_cast<PHINode>(I));
Chris Lattner3e130a22003-01-13 00:32:26 +0000648
Chris Lattner333b2fa2002-12-13 10:09:43 +0000649 // Create a new machine instr PHI node, and insert it.
Chris Lattner3e130a22003-01-13 00:32:26 +0000650 unsigned PHIReg = getReg(*PN);
Chris Lattner168aa902004-02-29 07:10:16 +0000651 MachineInstr *PhiMI = BuildMI(MBB, PHIInsertPoint,
652 X86::PHI, PN->getNumOperands(), PHIReg);
Chris Lattner3e130a22003-01-13 00:32:26 +0000653
654 MachineInstr *LongPhiMI = 0;
Chris Lattner168aa902004-02-29 07:10:16 +0000655 if (PN->getType() == Type::LongTy || PN->getType() == Type::ULongTy)
656 LongPhiMI = BuildMI(MBB, PHIInsertPoint,
657 X86::PHI, PN->getNumOperands(), PHIReg+1);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000658
Chris Lattnera6e73f12003-05-12 14:22:21 +0000659 // PHIValues - Map of blocks to incoming virtual registers. We use this
660 // so that we only initialize one incoming value for a particular block,
661 // even if the block has multiple entries in the PHI node.
662 //
663 std::map<MachineBasicBlock*, unsigned> PHIValues;
664
Chris Lattner333b2fa2002-12-13 10:09:43 +0000665 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
666 MachineBasicBlock *PredMBB = MBBMap[PN->getIncomingBlock(i)];
Chris Lattnera6e73f12003-05-12 14:22:21 +0000667 unsigned ValReg;
668 std::map<MachineBasicBlock*, unsigned>::iterator EntryIt =
669 PHIValues.lower_bound(PredMBB);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000670
Chris Lattnera6e73f12003-05-12 14:22:21 +0000671 if (EntryIt != PHIValues.end() && EntryIt->first == PredMBB) {
672 // We already inserted an initialization of the register for this
673 // predecessor. Recycle it.
674 ValReg = EntryIt->second;
675
676 } else {
Chris Lattnera81fc682003-10-19 00:26:11 +0000677 // Get the incoming value into a virtual register.
Chris Lattnera6e73f12003-05-12 14:22:21 +0000678 //
Chris Lattnera81fc682003-10-19 00:26:11 +0000679 Value *Val = PN->getIncomingValue(i);
680
681 // If this is a constant or GlobalValue, we may have to insert code
682 // into the basic block to compute it into a virtual register.
Reid Spencer8863f182004-07-18 00:38:32 +0000683 if ((isa<Constant>(Val) && !isa<ConstantExpr>(Val))) {
Chris Lattnercb2fd552004-05-13 07:40:27 +0000684 // Simple constants get emitted at the end of the basic block,
685 // before any terminator instructions. We "know" that the code to
686 // move a constant into a register will never clobber any flags.
687 ValReg = getReg(Val, PredMBB, PredMBB->getFirstTerminator());
Chris Lattnera81fc682003-10-19 00:26:11 +0000688 } else {
Chris Lattnercb2fd552004-05-13 07:40:27 +0000689 // Because we don't want to clobber any values which might be in
690 // physical registers with the computation of this constant (which
691 // might be arbitrarily complex if it is a constant expression),
692 // just insert the computation at the top of the basic block.
693 MachineBasicBlock::iterator PI = PredMBB->begin();
694
695 // Skip over any PHI nodes though!
696 while (PI != PredMBB->end() && PI->getOpcode() == X86::PHI)
697 ++PI;
698
699 ValReg = getReg(Val, PredMBB, PI);
Chris Lattnera81fc682003-10-19 00:26:11 +0000700 }
Chris Lattnera6e73f12003-05-12 14:22:21 +0000701
702 // Remember that we inserted a value for this PHI for this predecessor
703 PHIValues.insert(EntryIt, std::make_pair(PredMBB, ValReg));
704 }
705
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000706 PhiMI->addRegOperand(ValReg);
Chris Lattner3e130a22003-01-13 00:32:26 +0000707 PhiMI->addMachineBasicBlockOperand(PredMBB);
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000708 if (LongPhiMI) {
709 LongPhiMI->addRegOperand(ValReg+1);
710 LongPhiMI->addMachineBasicBlockOperand(PredMBB);
711 }
Chris Lattner333b2fa2002-12-13 10:09:43 +0000712 }
Chris Lattner168aa902004-02-29 07:10:16 +0000713
714 // Now that we emitted all of the incoming values for the PHI node, make
715 // sure to reposition the InsertPoint after the PHI that we just added.
716 // This is needed because we might have inserted a constant into this
717 // block, right after the PHI's which is before the old insert point!
718 PHIInsertPoint = LongPhiMI ? LongPhiMI : PhiMI;
719 ++PHIInsertPoint;
Chris Lattner333b2fa2002-12-13 10:09:43 +0000720 }
721 }
722}
723
Chris Lattner986618e2004-02-22 19:47:26 +0000724/// RequiresFPRegKill - The floating point stackifier pass cannot insert
725/// compensation code on critical edges. As such, it requires that we kill all
726/// FP registers on the exit from any blocks that either ARE critical edges, or
727/// branch to a block that has incoming critical edges.
728///
729/// Note that this kill instruction will eventually be eliminated when
730/// restrictions in the stackifier are relaxed.
731///
Brian Gaeke1afe7732004-04-28 04:45:55 +0000732static bool RequiresFPRegKill(const MachineBasicBlock *MBB) {
Chris Lattner986618e2004-02-22 19:47:26 +0000733#if 0
Brian Gaeke1afe7732004-04-28 04:45:55 +0000734 const BasicBlock *BB = MBB->getBasicBlock ();
Chris Lattner986618e2004-02-22 19:47:26 +0000735 for (succ_const_iterator SI = succ_begin(BB), E = succ_end(BB); SI!=E; ++SI) {
736 const BasicBlock *Succ = *SI;
737 pred_const_iterator PI = pred_begin(Succ), PE = pred_end(Succ);
738 ++PI; // Block have at least one predecessory
739 if (PI != PE) { // If it has exactly one, this isn't crit edge
740 // If this block has more than one predecessor, check all of the
741 // predecessors to see if they have multiple successors. If so, then the
742 // block we are analyzing needs an FPRegKill.
743 for (PI = pred_begin(Succ); PI != PE; ++PI) {
744 const BasicBlock *Pred = *PI;
745 succ_const_iterator SI2 = succ_begin(Pred);
746 ++SI2; // There must be at least one successor of this block.
747 if (SI2 != succ_end(Pred))
748 return true; // Yes, we must insert the kill on this edge.
749 }
750 }
751 }
752 // If we got this far, there is no need to insert the kill instruction.
753 return false;
754#else
755 return true;
756#endif
757}
758
759// InsertFPRegKills - Insert FP_REG_KILL instructions into basic blocks that
760// need them. This only occurs due to the floating point stackifier not being
761// aggressive enough to handle arbitrary global stackification.
762//
763// Currently we insert an FP_REG_KILL instruction into each block that uses or
764// defines a floating point virtual register.
765//
766// When the global register allocators (like linear scan) finally update live
767// variable analysis, we can keep floating point values in registers across
768// portions of the CFG that do not involve critical edges. This will be a big
769// win, but we are waiting on the global allocators before we can do this.
770//
771// With a bit of work, the floating point stackifier pass can be enhanced to
772// break critical edges as needed (to make a place to put compensation code),
773// but this will require some infrastructure improvements as well.
774//
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000775void X86ISel::InsertFPRegKills() {
Chris Lattner986618e2004-02-22 19:47:26 +0000776 SSARegMap &RegMap = *F->getSSARegMap();
Chris Lattner986618e2004-02-22 19:47:26 +0000777
778 for (MachineFunction::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
Chris Lattner986618e2004-02-22 19:47:26 +0000779 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000780 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
781 MachineOperand& MO = I->getOperand(i);
782 if (MO.isRegister() && MO.getReg()) {
783 unsigned Reg = MO.getReg();
Chris Lattner986618e2004-02-22 19:47:26 +0000784 if (MRegisterInfo::isVirtualRegister(Reg))
Chris Lattner65cf42d2004-02-23 07:29:45 +0000785 if (RegMap.getRegClass(Reg)->getSize() == 10)
786 goto UsesFPReg;
Chris Lattner986618e2004-02-22 19:47:26 +0000787 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000788 }
Chris Lattner65cf42d2004-02-23 07:29:45 +0000789 // If we haven't found an FP register use or def in this basic block, check
790 // to see if any of our successors has an FP PHI node, which will cause a
791 // copy to be inserted into this block.
Brian Gaeke235aa5e2004-04-28 04:34:16 +0000792 for (MachineBasicBlock::const_succ_iterator SI = BB->succ_begin(),
793 SE = BB->succ_end(); SI != SE; ++SI) {
794 MachineBasicBlock *SBB = *SI;
Chris Lattnerfbc39d52004-02-23 07:42:19 +0000795 for (MachineBasicBlock::iterator I = SBB->begin();
796 I != SBB->end() && I->getOpcode() == X86::PHI; ++I) {
797 if (RegMap.getRegClass(I->getOperand(0).getReg())->getSize() == 10)
798 goto UsesFPReg;
Chris Lattner986618e2004-02-22 19:47:26 +0000799 }
Chris Lattnerfbc39d52004-02-23 07:42:19 +0000800 }
Chris Lattner65cf42d2004-02-23 07:29:45 +0000801 continue;
802 UsesFPReg:
803 // Okay, this block uses an FP register. If the block has successors (ie,
804 // it's not an unwind/return), insert the FP_REG_KILL instruction.
Brian Gaeke1afe7732004-04-28 04:45:55 +0000805 if (BB->succ_size () && RequiresFPRegKill(BB)) {
Chris Lattneree352852004-02-29 07:22:16 +0000806 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
Chris Lattner65cf42d2004-02-23 07:29:45 +0000807 ++NumFPKill;
Chris Lattner986618e2004-02-22 19:47:26 +0000808 }
809 }
810}
811
812
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000813void X86ISel::getAddressingMode(Value *Addr, X86AddressMode &AM) {
Reid Spencerfc989e12004-08-30 00:13:26 +0000814 AM.BaseType = X86AddressMode::RegBase;
815 AM.Base.Reg = 0; AM.Scale = 1; AM.IndexReg = 0; AM.Disp = 0;
Chris Lattner9f1b5312004-05-13 15:12:43 +0000816 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Addr)) {
817 if (isGEPFoldable(BB, GEP->getOperand(0), GEP->op_begin()+1, GEP->op_end(),
Reid Spencerfc989e12004-08-30 00:13:26 +0000818 AM))
Chris Lattner9f1b5312004-05-13 15:12:43 +0000819 return;
820 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Addr)) {
821 if (CE->getOpcode() == Instruction::GetElementPtr)
822 if (isGEPFoldable(BB, CE->getOperand(0), CE->op_begin()+1, CE->op_end(),
Reid Spencerfc989e12004-08-30 00:13:26 +0000823 AM))
Chris Lattner9f1b5312004-05-13 15:12:43 +0000824 return;
Reid Spencerfc989e12004-08-30 00:13:26 +0000825 } else if (AllocaInst *AI = dyn_castFixedAlloca(Addr)) {
826 AM.BaseType = X86AddressMode::FrameIndexBase;
827 AM.Base.FrameIndex = getFixedSizedAllocaFI(AI);
828 return;
Chris Lattner9f1b5312004-05-13 15:12:43 +0000829 }
830
831 // If it's not foldable, reset addr mode.
Reid Spencerfc989e12004-08-30 00:13:26 +0000832 AM.BaseType = X86AddressMode::RegBase;
833 AM.Base.Reg = getReg(Addr);
834 AM.Scale = 1; AM.IndexReg = 0; AM.Disp = 0;
Chris Lattner9f1b5312004-05-13 15:12:43 +0000835}
836
Chris Lattner307ecba2004-03-30 22:39:09 +0000837// canFoldSetCCIntoBranchOrSelect - Return the setcc instruction if we can fold
838// it into the conditional branch or select instruction which is the only user
839// of the cc instruction. This is the case if the conditional branch is the
Chris Lattnera6f9fe62004-06-18 00:29:22 +0000840// only user of the setcc. We also don't handle long arguments below, so we
841// reject them here as well.
Chris Lattner6d40c192003-01-16 16:43:00 +0000842//
Chris Lattner307ecba2004-03-30 22:39:09 +0000843static SetCondInst *canFoldSetCCIntoBranchOrSelect(Value *V) {
Chris Lattner6d40c192003-01-16 16:43:00 +0000844 if (SetCondInst *SCI = dyn_cast<SetCondInst>(V))
Chris Lattner307ecba2004-03-30 22:39:09 +0000845 if (SCI->hasOneUse()) {
846 Instruction *User = cast<Instruction>(SCI->use_back());
847 if ((isa<BranchInst>(User) || isa<SelectInst>(User)) &&
Chris Lattner48c937e2004-04-06 17:34:50 +0000848 (getClassB(SCI->getOperand(0)->getType()) != cLong ||
849 SCI->getOpcode() == Instruction::SetEQ ||
850 SCI->getOpcode() == Instruction::SetNE))
Chris Lattner6d40c192003-01-16 16:43:00 +0000851 return SCI;
852 }
853 return 0;
854}
Chris Lattner333b2fa2002-12-13 10:09:43 +0000855
Chris Lattner6d40c192003-01-16 16:43:00 +0000856// Return a fixed numbering for setcc instructions which does not depend on the
857// order of the opcodes.
858//
859static unsigned getSetCCNumber(unsigned Opcode) {
860 switch(Opcode) {
861 default: assert(0 && "Unknown setcc instruction!");
862 case Instruction::SetEQ: return 0;
863 case Instruction::SetNE: return 1;
864 case Instruction::SetLT: return 2;
Chris Lattner55f6fab2003-01-16 18:07:23 +0000865 case Instruction::SetGE: return 3;
866 case Instruction::SetGT: return 4;
867 case Instruction::SetLE: return 5;
Chris Lattner6d40c192003-01-16 16:43:00 +0000868 }
869}
Chris Lattner06925362002-11-17 21:56:38 +0000870
Chris Lattner6d40c192003-01-16 16:43:00 +0000871// LLVM -> X86 signed X86 unsigned
872// ----- ---------- ------------
873// seteq -> sete sete
874// setne -> setne setne
875// setlt -> setl setb
Chris Lattner55f6fab2003-01-16 18:07:23 +0000876// setge -> setge setae
Chris Lattner6d40c192003-01-16 16:43:00 +0000877// setgt -> setg seta
878// setle -> setle setbe
Chris Lattnerb2acc512003-10-19 21:09:10 +0000879// ----
880// sets // Used by comparison with 0 optimization
881// setns
882static const unsigned SetCCOpcodeTab[2][8] = {
883 { X86::SETEr, X86::SETNEr, X86::SETBr, X86::SETAEr, X86::SETAr, X86::SETBEr,
884 0, 0 },
885 { X86::SETEr, X86::SETNEr, X86::SETLr, X86::SETGEr, X86::SETGr, X86::SETLEr,
886 X86::SETSr, X86::SETNSr },
Chris Lattner6d40c192003-01-16 16:43:00 +0000887};
888
Chris Lattner01cdb1b2004-06-11 05:33:49 +0000889/// emitUCOMr - In the future when we support processors before the P6, this
890/// wraps the logic for emitting an FUCOMr vs FUCOMIr.
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000891void X86ISel::emitUCOMr(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
892 unsigned LHS, unsigned RHS) {
Chris Lattner01cdb1b2004-06-11 05:33:49 +0000893 if (0) { // for processors prior to the P6
894 BuildMI(*MBB, IP, X86::FUCOMr, 2).addReg(LHS).addReg(RHS);
895 BuildMI(*MBB, IP, X86::FNSTSW8r, 0);
896 BuildMI(*MBB, IP, X86::SAHF, 1);
897 } else {
898 BuildMI(*MBB, IP, X86::FUCOMIr, 2).addReg(LHS).addReg(RHS);
899 }
900}
901
Chris Lattnerb2acc512003-10-19 21:09:10 +0000902// EmitComparison - This function emits a comparison of the two operands,
903// returning the extended setcc code to use.
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000904unsigned X86ISel::EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
905 MachineBasicBlock *MBB,
906 MachineBasicBlock::iterator IP) {
Brian Gaeke1749d632002-11-07 17:59:21 +0000907 // The arguments are already supposed to be of the same type.
Chris Lattner6d40c192003-01-16 16:43:00 +0000908 const Type *CompTy = Op0->getType();
Chris Lattner3e130a22003-01-13 00:32:26 +0000909 unsigned Class = getClassB(CompTy);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000910 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattner333864d2003-06-05 19:30:30 +0000911
912 // Special case handling of: cmp R, i
Chris Lattner260195d2004-05-07 19:55:55 +0000913 if (isa<ConstantPointerNull>(Op1)) {
914 if (OpNum < 2) // seteq/setne -> test
915 BuildMI(*MBB, IP, X86::TEST32rr, 2).addReg(Op0r).addReg(Op0r);
916 else
917 BuildMI(*MBB, IP, X86::CMP32ri, 2).addReg(Op0r).addImm(0);
918 return OpNum;
919
920 } else if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
Chris Lattnere80e6372004-04-06 16:02:27 +0000921 if (Class == cByte || Class == cShort || Class == cInt) {
922 unsigned Op1v = CI->getRawValue();
Chris Lattnerc07736a2003-07-23 15:22:26 +0000923
Chris Lattner333864d2003-06-05 19:30:30 +0000924 // Mask off any upper bits of the constant, if there are any...
925 Op1v &= (1ULL << (8 << Class)) - 1;
926
Chris Lattnerb2acc512003-10-19 21:09:10 +0000927 // If this is a comparison against zero, emit more efficient code. We
928 // can't handle unsigned comparisons against zero unless they are == or
929 // !=. These should have been strength reduced already anyway.
930 if (Op1v == 0 && (CompTy->isSigned() || OpNum < 2)) {
931 static const unsigned TESTTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000932 X86::TEST8rr, X86::TEST16rr, X86::TEST32rr
Chris Lattnerb2acc512003-10-19 21:09:10 +0000933 };
Chris Lattneree352852004-02-29 07:22:16 +0000934 BuildMI(*MBB, IP, TESTTab[Class], 2).addReg(Op0r).addReg(Op0r);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000935
936 if (OpNum == 2) return 6; // Map jl -> js
937 if (OpNum == 3) return 7; // Map jg -> jns
938 return OpNum;
Chris Lattner333864d2003-06-05 19:30:30 +0000939 }
Chris Lattnerb2acc512003-10-19 21:09:10 +0000940
941 static const unsigned CMPTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000942 X86::CMP8ri, X86::CMP16ri, X86::CMP32ri
Chris Lattnerb2acc512003-10-19 21:09:10 +0000943 };
944
Chris Lattneree352852004-02-29 07:22:16 +0000945 BuildMI(*MBB, IP, CMPTab[Class], 2).addReg(Op0r).addImm(Op1v);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000946 return OpNum;
Chris Lattnere80e6372004-04-06 16:02:27 +0000947 } else {
948 assert(Class == cLong && "Unknown integer class!");
949 unsigned LowCst = CI->getRawValue();
950 unsigned HiCst = CI->getRawValue() >> 32;
951 if (OpNum < 2) { // seteq, setne
952 unsigned LoTmp = Op0r;
953 if (LowCst != 0) {
954 LoTmp = makeAnotherReg(Type::IntTy);
955 BuildMI(*MBB, IP, X86::XOR32ri, 2, LoTmp).addReg(Op0r).addImm(LowCst);
956 }
957 unsigned HiTmp = Op0r+1;
958 if (HiCst != 0) {
959 HiTmp = makeAnotherReg(Type::IntTy);
Chris Lattner48c937e2004-04-06 17:34:50 +0000960 BuildMI(*MBB, IP, X86::XOR32ri, 2,HiTmp).addReg(Op0r+1).addImm(HiCst);
Chris Lattnere80e6372004-04-06 16:02:27 +0000961 }
962 unsigned FinalTmp = makeAnotherReg(Type::IntTy);
963 BuildMI(*MBB, IP, X86::OR32rr, 2, FinalTmp).addReg(LoTmp).addReg(HiTmp);
964 return OpNum;
Chris Lattner48c937e2004-04-06 17:34:50 +0000965 } else {
966 // Emit a sequence of code which compares the high and low parts once
967 // each, then uses a conditional move to handle the overflow case. For
968 // example, a setlt for long would generate code like this:
969 //
Chris Lattner9984fd02004-05-09 23:16:33 +0000970 // AL = lo(op1) < lo(op2) // Always unsigned comparison
971 // BL = hi(op1) < hi(op2) // Signedness depends on operands
972 // dest = hi(op1) == hi(op2) ? BL : AL;
Chris Lattner48c937e2004-04-06 17:34:50 +0000973 //
974
975 // FIXME: This would be much better if we had hierarchical register
976 // classes! Until then, hardcode registers so that we can deal with
977 // their aliases (because we don't have conditional byte moves).
978 //
979 BuildMI(*MBB, IP, X86::CMP32ri, 2).addReg(Op0r).addImm(LowCst);
980 BuildMI(*MBB, IP, SetCCOpcodeTab[0][OpNum], 0, X86::AL);
981 BuildMI(*MBB, IP, X86::CMP32ri, 2).addReg(Op0r+1).addImm(HiCst);
982 BuildMI(*MBB, IP, SetCCOpcodeTab[CompTy->isSigned()][OpNum], 0,X86::BL);
983 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::BH);
984 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::AH);
985 BuildMI(*MBB, IP, X86::CMOVE16rr, 2, X86::BX).addReg(X86::BX)
986 .addReg(X86::AX);
987 // NOTE: visitSetCondInst knows that the value is dumped into the BL
988 // register at this point for long values...
989 return OpNum;
Chris Lattnere80e6372004-04-06 16:02:27 +0000990 }
Chris Lattner333864d2003-06-05 19:30:30 +0000991 }
Chris Lattnere80e6372004-04-06 16:02:27 +0000992 }
Chris Lattner333864d2003-06-05 19:30:30 +0000993
Chris Lattner9f08a922004-02-03 18:54:04 +0000994 // Special case handling of comparison against +/- 0.0
995 if (ConstantFP *CFP = dyn_cast<ConstantFP>(Op1))
996 if (CFP->isExactlyValue(+0.0) || CFP->isExactlyValue(-0.0)) {
Chris Lattneree352852004-02-29 07:22:16 +0000997 BuildMI(*MBB, IP, X86::FTST, 1).addReg(Op0r);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000998 BuildMI(*MBB, IP, X86::FNSTSW8r, 0);
Chris Lattneree352852004-02-29 07:22:16 +0000999 BuildMI(*MBB, IP, X86::SAHF, 1);
Chris Lattner9f08a922004-02-03 18:54:04 +00001000 return OpNum;
1001 }
1002
Chris Lattner58c41fe2003-08-24 19:19:47 +00001003 unsigned Op1r = getReg(Op1, MBB, IP);
Chris Lattner3e130a22003-01-13 00:32:26 +00001004 switch (Class) {
1005 default: assert(0 && "Unknown type class!");
1006 // Emit: cmp <var1>, <var2> (do the comparison). We can
1007 // compare 8-bit with 8-bit, 16-bit with 16-bit, 32-bit with
1008 // 32-bit.
1009 case cByte:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001010 BuildMI(*MBB, IP, X86::CMP8rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +00001011 break;
1012 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001013 BuildMI(*MBB, IP, X86::CMP16rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +00001014 break;
1015 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001016 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +00001017 break;
1018 case cFP:
Chris Lattner01cdb1b2004-06-11 05:33:49 +00001019 emitUCOMr(MBB, IP, Op0r, Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +00001020 break;
1021
1022 case cLong:
1023 if (OpNum < 2) { // seteq, setne
1024 unsigned LoTmp = makeAnotherReg(Type::IntTy);
1025 unsigned HiTmp = makeAnotherReg(Type::IntTy);
1026 unsigned FinalTmp = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001027 BuildMI(*MBB, IP, X86::XOR32rr, 2, LoTmp).addReg(Op0r).addReg(Op1r);
1028 BuildMI(*MBB, IP, X86::XOR32rr, 2, HiTmp).addReg(Op0r+1).addReg(Op1r+1);
1029 BuildMI(*MBB, IP, X86::OR32rr, 2, FinalTmp).addReg(LoTmp).addReg(HiTmp);
Chris Lattner3e130a22003-01-13 00:32:26 +00001030 break; // Allow the sete or setne to be generated from flags set by OR
1031 } else {
1032 // Emit a sequence of code which compares the high and low parts once
1033 // each, then uses a conditional move to handle the overflow case. For
1034 // example, a setlt for long would generate code like this:
1035 //
1036 // AL = lo(op1) < lo(op2) // Signedness depends on operands
1037 // BL = hi(op1) < hi(op2) // Always unsigned comparison
Chris Lattner9984fd02004-05-09 23:16:33 +00001038 // dest = hi(op1) == hi(op2) ? BL : AL;
Chris Lattner3e130a22003-01-13 00:32:26 +00001039 //
1040
Chris Lattner6d40c192003-01-16 16:43:00 +00001041 // FIXME: This would be much better if we had hierarchical register
Chris Lattner3e130a22003-01-13 00:32:26 +00001042 // classes! Until then, hardcode registers so that we can deal with their
1043 // aliases (because we don't have conditional byte moves).
1044 //
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001045 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattneree352852004-02-29 07:22:16 +00001046 BuildMI(*MBB, IP, SetCCOpcodeTab[0][OpNum], 0, X86::AL);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001047 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r+1).addReg(Op1r+1);
Chris Lattneree352852004-02-29 07:22:16 +00001048 BuildMI(*MBB, IP, SetCCOpcodeTab[CompTy->isSigned()][OpNum], 0, X86::BL);
1049 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::BH);
1050 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::AH);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001051 BuildMI(*MBB, IP, X86::CMOVE16rr, 2, X86::BX).addReg(X86::BX)
Chris Lattneree352852004-02-29 07:22:16 +00001052 .addReg(X86::AX);
Chris Lattner6d40c192003-01-16 16:43:00 +00001053 // NOTE: visitSetCondInst knows that the value is dumped into the BL
1054 // register at this point for long values...
Chris Lattnerb2acc512003-10-19 21:09:10 +00001055 return OpNum;
Chris Lattner3e130a22003-01-13 00:32:26 +00001056 }
1057 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001058 return OpNum;
Chris Lattner6d40c192003-01-16 16:43:00 +00001059}
Chris Lattner3e130a22003-01-13 00:32:26 +00001060
Chris Lattner6d40c192003-01-16 16:43:00 +00001061/// SetCC instructions - Here we just emit boilerplate code to set a byte-sized
1062/// register, then move it to wherever the result should be.
1063///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001064void X86ISel::visitSetCondInst(SetCondInst &I) {
Chris Lattner307ecba2004-03-30 22:39:09 +00001065 if (canFoldSetCCIntoBranchOrSelect(&I))
1066 return; // Fold this into a branch or select.
Chris Lattner6d40c192003-01-16 16:43:00 +00001067
Chris Lattner6d40c192003-01-16 16:43:00 +00001068 unsigned DestReg = getReg(I);
Chris Lattner58c41fe2003-08-24 19:19:47 +00001069 MachineBasicBlock::iterator MII = BB->end();
1070 emitSetCCOperation(BB, MII, I.getOperand(0), I.getOperand(1), I.getOpcode(),
1071 DestReg);
1072}
Chris Lattner6d40c192003-01-16 16:43:00 +00001073
Chris Lattner58c41fe2003-08-24 19:19:47 +00001074/// emitSetCCOperation - Common code shared between visitSetCondInst and
1075/// constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +00001076///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001077void X86ISel::emitSetCCOperation(MachineBasicBlock *MBB,
1078 MachineBasicBlock::iterator IP,
1079 Value *Op0, Value *Op1, unsigned Opcode,
1080 unsigned TargetReg) {
Chris Lattner58c41fe2003-08-24 19:19:47 +00001081 unsigned OpNum = getSetCCNumber(Opcode);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001082 OpNum = EmitComparison(OpNum, Op0, Op1, MBB, IP);
Chris Lattner58c41fe2003-08-24 19:19:47 +00001083
Chris Lattnerb2acc512003-10-19 21:09:10 +00001084 const Type *CompTy = Op0->getType();
1085 unsigned CompClass = getClassB(CompTy);
1086 bool isSigned = CompTy->isSigned() && CompClass != cFP;
1087
1088 if (CompClass != cLong || OpNum < 2) {
Chris Lattner6d40c192003-01-16 16:43:00 +00001089 // Handle normal comparisons with a setcc instruction...
Chris Lattneree352852004-02-29 07:22:16 +00001090 BuildMI(*MBB, IP, SetCCOpcodeTab[isSigned][OpNum], 0, TargetReg);
Chris Lattner6d40c192003-01-16 16:43:00 +00001091 } else {
1092 // Handle long comparisons by copying the value which is already in BL into
1093 // the register we want...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001094 BuildMI(*MBB, IP, X86::MOV8rr, 1, TargetReg).addReg(X86::BL);
Chris Lattner6d40c192003-01-16 16:43:00 +00001095 }
Brian Gaeke1749d632002-11-07 17:59:21 +00001096}
Chris Lattner51b49a92002-11-02 19:45:49 +00001097
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001098void X86ISel::visitSelectInst(SelectInst &SI) {
Chris Lattner12d96a02004-03-30 21:22:00 +00001099 unsigned DestReg = getReg(SI);
1100 MachineBasicBlock::iterator MII = BB->end();
1101 emitSelectOperation(BB, MII, SI.getCondition(), SI.getTrueValue(),
1102 SI.getFalseValue(), DestReg);
1103}
1104
1105/// emitSelect - Common code shared between visitSelectInst and the constant
1106/// expression support.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001107void X86ISel::emitSelectOperation(MachineBasicBlock *MBB,
1108 MachineBasicBlock::iterator IP,
1109 Value *Cond, Value *TrueVal, Value *FalseVal,
1110 unsigned DestReg) {
Chris Lattner12d96a02004-03-30 21:22:00 +00001111 unsigned SelectClass = getClassB(TrueVal->getType());
1112
1113 // We don't support 8-bit conditional moves. If we have incoming constants,
1114 // transform them into 16-bit constants to avoid having a run-time conversion.
1115 if (SelectClass == cByte) {
1116 if (Constant *T = dyn_cast<Constant>(TrueVal))
1117 TrueVal = ConstantExpr::getCast(T, Type::ShortTy);
1118 if (Constant *F = dyn_cast<Constant>(FalseVal))
1119 FalseVal = ConstantExpr::getCast(F, Type::ShortTy);
1120 }
1121
Chris Lattner82c5a992004-04-13 21:56:09 +00001122 unsigned TrueReg = getReg(TrueVal, MBB, IP);
1123 unsigned FalseReg = getReg(FalseVal, MBB, IP);
1124 if (TrueReg == FalseReg) {
1125 static const unsigned Opcode[] = {
1126 X86::MOV8rr, X86::MOV16rr, X86::MOV32rr, X86::FpMOV, X86::MOV32rr
1127 };
1128 BuildMI(*MBB, IP, Opcode[SelectClass], 1, DestReg).addReg(TrueReg);
1129 if (SelectClass == cLong)
1130 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg+1).addReg(TrueReg+1);
1131 return;
1132 }
1133
Chris Lattner307ecba2004-03-30 22:39:09 +00001134 unsigned Opcode;
1135 if (SetCondInst *SCI = canFoldSetCCIntoBranchOrSelect(Cond)) {
1136 // We successfully folded the setcc into the select instruction.
1137
1138 unsigned OpNum = getSetCCNumber(SCI->getOpcode());
1139 OpNum = EmitComparison(OpNum, SCI->getOperand(0), SCI->getOperand(1), MBB,
1140 IP);
1141
1142 const Type *CompTy = SCI->getOperand(0)->getType();
1143 bool isSigned = CompTy->isSigned() && getClassB(CompTy) != cFP;
1144
1145 // LLVM -> X86 signed X86 unsigned
1146 // ----- ---------- ------------
1147 // seteq -> cmovNE cmovNE
1148 // setne -> cmovE cmovE
1149 // setlt -> cmovGE cmovAE
1150 // setge -> cmovL cmovB
1151 // setgt -> cmovLE cmovBE
1152 // setle -> cmovG cmovA
1153 // ----
1154 // cmovNS // Used by comparison with 0 optimization
1155 // cmovS
1156
1157 switch (SelectClass) {
Chris Lattner352eb482004-03-31 22:03:35 +00001158 default: assert(0 && "Unknown value class!");
1159 case cFP: {
1160 // Annoyingly, we don't have a full set of floating point conditional
1161 // moves. :(
1162 static const unsigned OpcodeTab[2][8] = {
1163 { X86::FCMOVNE, X86::FCMOVE, X86::FCMOVAE, X86::FCMOVB,
1164 X86::FCMOVBE, X86::FCMOVA, 0, 0 },
1165 { X86::FCMOVNE, X86::FCMOVE, 0, 0, 0, 0, 0, 0 },
1166 };
1167 Opcode = OpcodeTab[isSigned][OpNum];
1168
1169 // If opcode == 0, we hit a case that we don't support. Output a setcc
1170 // and compare the result against zero.
1171 if (Opcode == 0) {
1172 unsigned CompClass = getClassB(CompTy);
1173 unsigned CondReg;
1174 if (CompClass != cLong || OpNum < 2) {
1175 CondReg = makeAnotherReg(Type::BoolTy);
1176 // Handle normal comparisons with a setcc instruction...
1177 BuildMI(*MBB, IP, SetCCOpcodeTab[isSigned][OpNum], 0, CondReg);
1178 } else {
1179 // Long comparisons end up in the BL register.
1180 CondReg = X86::BL;
1181 }
1182
Chris Lattner68626c22004-03-31 22:22:36 +00001183 BuildMI(*MBB, IP, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
Chris Lattner352eb482004-03-31 22:03:35 +00001184 Opcode = X86::FCMOVE;
1185 }
1186 break;
1187 }
Chris Lattner307ecba2004-03-30 22:39:09 +00001188 case cByte:
1189 case cShort: {
1190 static const unsigned OpcodeTab[2][8] = {
1191 { X86::CMOVNE16rr, X86::CMOVE16rr, X86::CMOVAE16rr, X86::CMOVB16rr,
1192 X86::CMOVBE16rr, X86::CMOVA16rr, 0, 0 },
1193 { X86::CMOVNE16rr, X86::CMOVE16rr, X86::CMOVGE16rr, X86::CMOVL16rr,
1194 X86::CMOVLE16rr, X86::CMOVG16rr, X86::CMOVNS16rr, X86::CMOVS16rr },
1195 };
1196 Opcode = OpcodeTab[isSigned][OpNum];
1197 break;
1198 }
1199 case cInt:
1200 case cLong: {
1201 static const unsigned OpcodeTab[2][8] = {
1202 { X86::CMOVNE32rr, X86::CMOVE32rr, X86::CMOVAE32rr, X86::CMOVB32rr,
1203 X86::CMOVBE32rr, X86::CMOVA32rr, 0, 0 },
1204 { X86::CMOVNE32rr, X86::CMOVE32rr, X86::CMOVGE32rr, X86::CMOVL32rr,
1205 X86::CMOVLE32rr, X86::CMOVG32rr, X86::CMOVNS32rr, X86::CMOVS32rr },
1206 };
1207 Opcode = OpcodeTab[isSigned][OpNum];
1208 break;
1209 }
1210 }
1211 } else {
1212 // Get the value being branched on, and use it to set the condition codes.
1213 unsigned CondReg = getReg(Cond, MBB, IP);
Chris Lattner68626c22004-03-31 22:22:36 +00001214 BuildMI(*MBB, IP, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
Chris Lattner307ecba2004-03-30 22:39:09 +00001215 switch (SelectClass) {
Chris Lattner352eb482004-03-31 22:03:35 +00001216 default: assert(0 && "Unknown value class!");
1217 case cFP: Opcode = X86::FCMOVE; break;
Chris Lattner307ecba2004-03-30 22:39:09 +00001218 case cByte:
Chris Lattner352eb482004-03-31 22:03:35 +00001219 case cShort: Opcode = X86::CMOVE16rr; break;
Chris Lattner307ecba2004-03-30 22:39:09 +00001220 case cInt:
Chris Lattner352eb482004-03-31 22:03:35 +00001221 case cLong: Opcode = X86::CMOVE32rr; break;
Chris Lattner307ecba2004-03-30 22:39:09 +00001222 }
1223 }
Chris Lattner12d96a02004-03-30 21:22:00 +00001224
Chris Lattner12d96a02004-03-30 21:22:00 +00001225 unsigned RealDestReg = DestReg;
Chris Lattner12d96a02004-03-30 21:22:00 +00001226
Chris Lattner12d96a02004-03-30 21:22:00 +00001227
1228 // Annoyingly enough, X86 doesn't HAVE 8-bit conditional moves. Because of
1229 // this, we have to promote the incoming values to 16 bits, perform a 16-bit
1230 // cmove, then truncate the result.
1231 if (SelectClass == cByte) {
1232 DestReg = makeAnotherReg(Type::ShortTy);
1233 if (getClassB(TrueVal->getType()) == cByte) {
1234 // Promote the true value, by storing it into AL, and reading from AX.
1235 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::AL).addReg(TrueReg);
1236 BuildMI(*MBB, IP, X86::MOV8ri, 1, X86::AH).addImm(0);
1237 TrueReg = makeAnotherReg(Type::ShortTy);
1238 BuildMI(*MBB, IP, X86::MOV16rr, 1, TrueReg).addReg(X86::AX);
1239 }
1240 if (getClassB(FalseVal->getType()) == cByte) {
1241 // Promote the true value, by storing it into CL, and reading from CX.
1242 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(FalseReg);
1243 BuildMI(*MBB, IP, X86::MOV8ri, 1, X86::CH).addImm(0);
1244 FalseReg = makeAnotherReg(Type::ShortTy);
1245 BuildMI(*MBB, IP, X86::MOV16rr, 1, FalseReg).addReg(X86::CX);
1246 }
1247 }
1248
1249 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(TrueReg).addReg(FalseReg);
1250
1251 switch (SelectClass) {
1252 case cByte:
1253 // We did the computation with 16-bit registers. Truncate back to our
1254 // result by copying into AX then copying out AL.
1255 BuildMI(*MBB, IP, X86::MOV16rr, 1, X86::AX).addReg(DestReg);
1256 BuildMI(*MBB, IP, X86::MOV8rr, 1, RealDestReg).addReg(X86::AL);
1257 break;
1258 case cLong:
1259 // Move the upper half of the value as well.
1260 BuildMI(*MBB, IP, Opcode, 2,DestReg+1).addReg(TrueReg+1).addReg(FalseReg+1);
1261 break;
1262 }
1263}
Chris Lattner58c41fe2003-08-24 19:19:47 +00001264
1265
1266
Brian Gaekec2505982002-11-30 11:57:28 +00001267/// promote32 - Emit instructions to turn a narrow operand into a 32-bit-wide
1268/// operand, in the specified target register.
Misha Brukman538607f2004-03-01 23:53:11 +00001269///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001270void X86ISel::promote32(unsigned targetReg, const ValueRecord &VR) {
Chris Lattner9984fd02004-05-09 23:16:33 +00001271 bool isUnsigned = VR.Ty->isUnsigned() || VR.Ty == Type::BoolTy;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001272
Chris Lattner29bf0622004-04-06 01:21:00 +00001273 Value *Val = VR.Val;
1274 const Type *Ty = VR.Ty;
Chris Lattner502e36c2004-04-06 01:25:33 +00001275 if (Val) {
Chris Lattner29bf0622004-04-06 01:21:00 +00001276 if (Constant *C = dyn_cast<Constant>(Val)) {
1277 Val = ConstantExpr::getCast(C, Type::IntTy);
1278 Ty = Type::IntTy;
1279 }
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001280
Chris Lattner502e36c2004-04-06 01:25:33 +00001281 // If this is a simple constant, just emit a MOVri directly to avoid the
1282 // copy.
1283 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
1284 int TheVal = CI->getRawValue() & 0xFFFFFFFF;
Chris Lattner2b10b082004-05-12 16:35:04 +00001285 BuildMI(BB, X86::MOV32ri, 1, targetReg).addImm(TheVal);
Chris Lattner502e36c2004-04-06 01:25:33 +00001286 return;
1287 }
1288 }
1289
Chris Lattner29bf0622004-04-06 01:21:00 +00001290 // Make sure we have the register number for this value...
1291 unsigned Reg = Val ? getReg(Val) : VR.Reg;
1292
1293 switch (getClassB(Ty)) {
Chris Lattner94af4142002-12-25 05:13:53 +00001294 case cByte:
1295 // Extend value into target register (8->32)
1296 if (isUnsigned)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001297 BuildMI(BB, X86::MOVZX32rr8, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001298 else
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001299 BuildMI(BB, X86::MOVSX32rr8, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001300 break;
1301 case cShort:
1302 // Extend value into target register (16->32)
1303 if (isUnsigned)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001304 BuildMI(BB, X86::MOVZX32rr16, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001305 else
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001306 BuildMI(BB, X86::MOVSX32rr16, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001307 break;
1308 case cInt:
1309 // Move value into target register (32->32)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001310 BuildMI(BB, X86::MOV32rr, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001311 break;
1312 default:
1313 assert(0 && "Unpromotable operand class in promote32");
1314 }
Brian Gaekec2505982002-11-30 11:57:28 +00001315}
Chris Lattnerc5291f52002-10-27 21:16:59 +00001316
Chris Lattner72614082002-10-25 22:55:53 +00001317/// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such,
1318/// we have the following possibilities:
1319///
1320/// ret void: No return value, simply emit a 'ret' instruction
1321/// ret sbyte, ubyte : Extend value into EAX and return
1322/// ret short, ushort: Extend value into EAX and return
1323/// ret int, uint : Move value into EAX and return
1324/// ret pointer : Move value into EAX and return
Chris Lattner06925362002-11-17 21:56:38 +00001325/// ret long, ulong : Move value into EAX/EDX and return
1326/// ret float/double : Top of FP stack
Chris Lattner72614082002-10-25 22:55:53 +00001327///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001328void X86ISel::visitReturnInst(ReturnInst &I) {
Chris Lattner94af4142002-12-25 05:13:53 +00001329 if (I.getNumOperands() == 0) {
1330 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
1331 return;
1332 }
1333
1334 Value *RetVal = I.getOperand(0);
Chris Lattner3e130a22003-01-13 00:32:26 +00001335 switch (getClassB(RetVal->getType())) {
Chris Lattner94af4142002-12-25 05:13:53 +00001336 case cByte: // integral return values: extend or move into EAX and return
1337 case cShort:
1338 case cInt:
Chris Lattner29bf0622004-04-06 01:21:00 +00001339 promote32(X86::EAX, ValueRecord(RetVal));
Chris Lattnerdbd73722003-05-06 21:32:22 +00001340 // Declare that EAX is live on exit
Chris Lattnerc2489032003-05-07 19:21:28 +00001341 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
Chris Lattner94af4142002-12-25 05:13:53 +00001342 break;
Chris Lattner29bf0622004-04-06 01:21:00 +00001343 case cFP: { // Floats & Doubles: Return in ST(0)
1344 unsigned RetReg = getReg(RetVal);
Chris Lattner3e130a22003-01-13 00:32:26 +00001345 BuildMI(BB, X86::FpSETRESULT, 1).addReg(RetReg);
Chris Lattnerdbd73722003-05-06 21:32:22 +00001346 // Declare that top-of-stack is live on exit
Chris Lattnerc2489032003-05-07 19:21:28 +00001347 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
Chris Lattner94af4142002-12-25 05:13:53 +00001348 break;
Chris Lattner29bf0622004-04-06 01:21:00 +00001349 }
1350 case cLong: {
1351 unsigned RetReg = getReg(RetVal);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001352 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(RetReg);
1353 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RetReg+1);
Chris Lattnerdbd73722003-05-06 21:32:22 +00001354 // Declare that EAX & EDX are live on exit
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001355 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
1356 .addReg(X86::ESP);
Chris Lattner3e130a22003-01-13 00:32:26 +00001357 break;
Chris Lattner29bf0622004-04-06 01:21:00 +00001358 }
Chris Lattner94af4142002-12-25 05:13:53 +00001359 default:
Chris Lattner3e130a22003-01-13 00:32:26 +00001360 visitInstruction(I);
Chris Lattner94af4142002-12-25 05:13:53 +00001361 }
Chris Lattner43189d12002-11-17 20:07:45 +00001362 // Emit a 'ret' instruction
Chris Lattner94af4142002-12-25 05:13:53 +00001363 BuildMI(BB, X86::RET, 0);
Chris Lattner72614082002-10-25 22:55:53 +00001364}
1365
Chris Lattner55f6fab2003-01-16 18:07:23 +00001366// getBlockAfter - Return the basic block which occurs lexically after the
1367// specified one.
1368static inline BasicBlock *getBlockAfter(BasicBlock *BB) {
1369 Function::iterator I = BB; ++I; // Get iterator to next block
1370 return I != BB->getParent()->end() ? &*I : 0;
1371}
1372
Chris Lattner51b49a92002-11-02 19:45:49 +00001373/// visitBranchInst - Handle conditional and unconditional branches here. Note
1374/// that since code layout is frozen at this point, that if we are trying to
1375/// jump to a block that is the immediate successor of the current block, we can
Chris Lattner6d40c192003-01-16 16:43:00 +00001376/// just make a fall-through (but we don't currently).
Chris Lattner51b49a92002-11-02 19:45:49 +00001377///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001378void X86ISel::visitBranchInst(BranchInst &BI) {
Brian Gaekeea9ca672004-04-28 04:19:37 +00001379 // Update machine-CFG edges
1380 BB->addSuccessor (MBBMap[BI.getSuccessor(0)]);
1381 if (BI.isConditional())
1382 BB->addSuccessor (MBBMap[BI.getSuccessor(1)]);
1383
Chris Lattner55f6fab2003-01-16 18:07:23 +00001384 BasicBlock *NextBB = getBlockAfter(BI.getParent()); // BB after current one
1385
1386 if (!BI.isConditional()) { // Unconditional branch?
Chris Lattnercf93cdd2004-01-30 22:13:44 +00001387 if (BI.getSuccessor(0) != NextBB)
Brian Gaeke9f088e42004-05-14 06:54:56 +00001388 BuildMI(BB, X86::JMP, 1).addMBB(MBBMap[BI.getSuccessor(0)]);
Chris Lattner6d40c192003-01-16 16:43:00 +00001389 return;
1390 }
1391
1392 // See if we can fold the setcc into the branch itself...
Chris Lattner307ecba2004-03-30 22:39:09 +00001393 SetCondInst *SCI = canFoldSetCCIntoBranchOrSelect(BI.getCondition());
Chris Lattner6d40c192003-01-16 16:43:00 +00001394 if (SCI == 0) {
1395 // Nope, cannot fold setcc into this branch. Emit a branch on a condition
1396 // computed some other way...
Chris Lattner065faeb2002-12-28 20:24:02 +00001397 unsigned condReg = getReg(BI.getCondition());
Chris Lattner68626c22004-03-31 22:22:36 +00001398 BuildMI(BB, X86::TEST8rr, 2).addReg(condReg).addReg(condReg);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001399 if (BI.getSuccessor(1) == NextBB) {
1400 if (BI.getSuccessor(0) != NextBB)
Brian Gaeke9f088e42004-05-14 06:54:56 +00001401 BuildMI(BB, X86::JNE, 1).addMBB(MBBMap[BI.getSuccessor(0)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001402 } else {
Brian Gaeke9f088e42004-05-14 06:54:56 +00001403 BuildMI(BB, X86::JE, 1).addMBB(MBBMap[BI.getSuccessor(1)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001404
1405 if (BI.getSuccessor(0) != NextBB)
Brian Gaeke9f088e42004-05-14 06:54:56 +00001406 BuildMI(BB, X86::JMP, 1).addMBB(MBBMap[BI.getSuccessor(0)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001407 }
Chris Lattner6d40c192003-01-16 16:43:00 +00001408 return;
Chris Lattner94af4142002-12-25 05:13:53 +00001409 }
Chris Lattner6d40c192003-01-16 16:43:00 +00001410
1411 unsigned OpNum = getSetCCNumber(SCI->getOpcode());
Chris Lattner58c41fe2003-08-24 19:19:47 +00001412 MachineBasicBlock::iterator MII = BB->end();
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001413 OpNum = EmitComparison(OpNum, SCI->getOperand(0), SCI->getOperand(1), BB,MII);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001414
1415 const Type *CompTy = SCI->getOperand(0)->getType();
1416 bool isSigned = CompTy->isSigned() && getClassB(CompTy) != cFP;
Chris Lattner6d40c192003-01-16 16:43:00 +00001417
Chris Lattnerb2acc512003-10-19 21:09:10 +00001418
Chris Lattner6d40c192003-01-16 16:43:00 +00001419 // LLVM -> X86 signed X86 unsigned
1420 // ----- ---------- ------------
1421 // seteq -> je je
1422 // setne -> jne jne
1423 // setlt -> jl jb
Chris Lattner55f6fab2003-01-16 18:07:23 +00001424 // setge -> jge jae
Chris Lattner6d40c192003-01-16 16:43:00 +00001425 // setgt -> jg ja
1426 // setle -> jle jbe
Chris Lattnerb2acc512003-10-19 21:09:10 +00001427 // ----
1428 // js // Used by comparison with 0 optimization
1429 // jns
1430
1431 static const unsigned OpcodeTab[2][8] = {
1432 { X86::JE, X86::JNE, X86::JB, X86::JAE, X86::JA, X86::JBE, 0, 0 },
1433 { X86::JE, X86::JNE, X86::JL, X86::JGE, X86::JG, X86::JLE,
1434 X86::JS, X86::JNS },
Chris Lattner6d40c192003-01-16 16:43:00 +00001435 };
1436
Chris Lattner55f6fab2003-01-16 18:07:23 +00001437 if (BI.getSuccessor(0) != NextBB) {
Brian Gaeke9f088e42004-05-14 06:54:56 +00001438 BuildMI(BB, OpcodeTab[isSigned][OpNum], 1)
1439 .addMBB(MBBMap[BI.getSuccessor(0)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001440 if (BI.getSuccessor(1) != NextBB)
Brian Gaeke9f088e42004-05-14 06:54:56 +00001441 BuildMI(BB, X86::JMP, 1).addMBB(MBBMap[BI.getSuccessor(1)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001442 } else {
1443 // Change to the inverse condition...
1444 if (BI.getSuccessor(1) != NextBB) {
1445 OpNum ^= 1;
Brian Gaeke9f088e42004-05-14 06:54:56 +00001446 BuildMI(BB, OpcodeTab[isSigned][OpNum], 1)
1447 .addMBB(MBBMap[BI.getSuccessor(1)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001448 }
1449 }
Chris Lattner2df035b2002-11-02 19:27:56 +00001450}
1451
Chris Lattner3e130a22003-01-13 00:32:26 +00001452
1453/// doCall - This emits an abstract call instruction, setting up the arguments
1454/// and the return value as appropriate. For the actual function call itself,
1455/// it inserts the specified CallMI instruction into the stream.
1456///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001457void X86ISel::doCall(const ValueRecord &Ret, MachineInstr *CallMI,
1458 const std::vector<ValueRecord> &Args) {
Chris Lattner065faeb2002-12-28 20:24:02 +00001459 // Count how many bytes are to be pushed on the stack...
1460 unsigned NumBytes = 0;
Misha Brukman0d2cf3a2002-12-04 19:22:53 +00001461
Chris Lattner3e130a22003-01-13 00:32:26 +00001462 if (!Args.empty()) {
1463 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1464 switch (getClassB(Args[i].Ty)) {
Chris Lattner065faeb2002-12-28 20:24:02 +00001465 case cByte: case cShort: case cInt:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001466 NumBytes += 4; break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001467 case cLong:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001468 NumBytes += 8; break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001469 case cFP:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001470 NumBytes += Args[i].Ty == Type::FloatTy ? 4 : 8;
1471 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001472 default: assert(0 && "Unknown class!");
1473 }
1474
1475 // Adjust the stack pointer for the new arguments...
Chris Lattneree352852004-02-29 07:22:16 +00001476 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(NumBytes);
Chris Lattner065faeb2002-12-28 20:24:02 +00001477
1478 // Arguments go on the stack in reverse order, as specified by the ABI.
1479 unsigned ArgOffset = 0;
Chris Lattner3e130a22003-01-13 00:32:26 +00001480 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattnerce6096f2004-03-01 02:34:08 +00001481 unsigned ArgReg;
Chris Lattner3e130a22003-01-13 00:32:26 +00001482 switch (getClassB(Args[i].Ty)) {
Chris Lattner065faeb2002-12-28 20:24:02 +00001483 case cByte:
Chris Lattner2b10b082004-05-12 16:35:04 +00001484 if (Args[i].Val && isa<ConstantBool>(Args[i].Val)) {
1485 addRegOffset(BuildMI(BB, X86::MOV32mi, 5), X86::ESP, ArgOffset)
1486 .addImm(Args[i].Val == ConstantBool::True);
1487 break;
1488 }
1489 // FALL THROUGH
Chris Lattner21585222004-03-01 02:42:43 +00001490 case cShort:
1491 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1492 // Zero/Sign extend constant, then stuff into memory.
1493 ConstantInt *Val = cast<ConstantInt>(Args[i].Val);
1494 Val = cast<ConstantInt>(ConstantExpr::getCast(Val, Type::IntTy));
1495 addRegOffset(BuildMI(BB, X86::MOV32mi, 5), X86::ESP, ArgOffset)
1496 .addImm(Val->getRawValue() & 0xFFFFFFFF);
1497 } else {
1498 // Promote arg to 32 bits wide into a temporary register...
1499 ArgReg = makeAnotherReg(Type::UIntTy);
1500 promote32(ArgReg, Args[i]);
1501 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1502 X86::ESP, ArgOffset).addReg(ArgReg);
1503 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001504 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001505 case cInt:
Chris Lattner21585222004-03-01 02:42:43 +00001506 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1507 unsigned Val = cast<ConstantInt>(Args[i].Val)->getRawValue();
1508 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1509 X86::ESP, ArgOffset).addImm(Val);
Chris Lattnerb7cb9ff2004-05-13 15:26:48 +00001510 } else if (Args[i].Val && isa<ConstantPointerNull>(Args[i].Val)) {
1511 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1512 X86::ESP, ArgOffset).addImm(0);
Chris Lattner21585222004-03-01 02:42:43 +00001513 } else {
1514 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1515 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1516 X86::ESP, ArgOffset).addReg(ArgReg);
1517 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001518 break;
Chris Lattner3e130a22003-01-13 00:32:26 +00001519 case cLong:
Chris Lattner92900a62004-04-06 03:23:00 +00001520 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1521 uint64_t Val = cast<ConstantInt>(Args[i].Val)->getRawValue();
1522 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1523 X86::ESP, ArgOffset).addImm(Val & ~0U);
1524 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1525 X86::ESP, ArgOffset+4).addImm(Val >> 32ULL);
1526 } else {
1527 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1528 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1529 X86::ESP, ArgOffset).addReg(ArgReg);
1530 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1531 X86::ESP, ArgOffset+4).addReg(ArgReg+1);
1532 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001533 ArgOffset += 4; // 8 byte entry, not 4.
1534 break;
1535
Chris Lattner065faeb2002-12-28 20:24:02 +00001536 case cFP:
Chris Lattnerce6096f2004-03-01 02:34:08 +00001537 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001538 if (Args[i].Ty == Type::FloatTy) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001539 addRegOffset(BuildMI(BB, X86::FST32m, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001540 X86::ESP, ArgOffset).addReg(ArgReg);
1541 } else {
1542 assert(Args[i].Ty == Type::DoubleTy && "Unknown FP type!");
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001543 addRegOffset(BuildMI(BB, X86::FST64m, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001544 X86::ESP, ArgOffset).addReg(ArgReg);
1545 ArgOffset += 4; // 8 byte entry, not 4.
1546 }
1547 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001548
Chris Lattner3e130a22003-01-13 00:32:26 +00001549 default: assert(0 && "Unknown class!");
Chris Lattner065faeb2002-12-28 20:24:02 +00001550 }
1551 ArgOffset += 4;
Chris Lattner94af4142002-12-25 05:13:53 +00001552 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001553 } else {
Chris Lattneree352852004-02-29 07:22:16 +00001554 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(0);
Chris Lattner94af4142002-12-25 05:13:53 +00001555 }
Chris Lattner6e49a4b2002-12-13 14:13:27 +00001556
Chris Lattner3e130a22003-01-13 00:32:26 +00001557 BB->push_back(CallMI);
Misha Brukman0d2cf3a2002-12-04 19:22:53 +00001558
Chris Lattneree352852004-02-29 07:22:16 +00001559 BuildMI(BB, X86::ADJCALLSTACKUP, 1).addImm(NumBytes);
Chris Lattnera3243642002-12-04 23:45:28 +00001560
1561 // If there is a return value, scavenge the result from the location the call
1562 // leaves it in...
1563 //
Chris Lattner3e130a22003-01-13 00:32:26 +00001564 if (Ret.Ty != Type::VoidTy) {
1565 unsigned DestClass = getClassB(Ret.Ty);
1566 switch (DestClass) {
Brian Gaeke20244b72002-12-12 15:33:40 +00001567 case cByte:
1568 case cShort:
1569 case cInt: {
1570 // Integral results are in %eax, or the appropriate portion
1571 // thereof.
1572 static const unsigned regRegMove[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001573 X86::MOV8rr, X86::MOV16rr, X86::MOV32rr
Brian Gaeke20244b72002-12-12 15:33:40 +00001574 };
1575 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX };
Chris Lattner3e130a22003-01-13 00:32:26 +00001576 BuildMI(BB, regRegMove[DestClass], 1, Ret.Reg).addReg(AReg[DestClass]);
Chris Lattner4fa1acc2002-12-04 23:50:28 +00001577 break;
Brian Gaeke20244b72002-12-12 15:33:40 +00001578 }
Chris Lattner94af4142002-12-25 05:13:53 +00001579 case cFP: // Floating-point return values live in %ST(0)
Chris Lattner3e130a22003-01-13 00:32:26 +00001580 BuildMI(BB, X86::FpGETRESULT, 1, Ret.Reg);
Brian Gaeke20244b72002-12-12 15:33:40 +00001581 break;
Chris Lattner3e130a22003-01-13 00:32:26 +00001582 case cLong: // Long values are left in EDX:EAX
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001583 BuildMI(BB, X86::MOV32rr, 1, Ret.Reg).addReg(X86::EAX);
1584 BuildMI(BB, X86::MOV32rr, 1, Ret.Reg+1).addReg(X86::EDX);
Chris Lattner3e130a22003-01-13 00:32:26 +00001585 break;
1586 default: assert(0 && "Unknown class!");
Chris Lattner4fa1acc2002-12-04 23:50:28 +00001587 }
Chris Lattnera3243642002-12-04 23:45:28 +00001588 }
Brian Gaekefa8d5712002-11-22 11:07:01 +00001589}
Chris Lattner2df035b2002-11-02 19:27:56 +00001590
Chris Lattner3e130a22003-01-13 00:32:26 +00001591
1592/// visitCallInst - Push args on stack and do a procedure call instruction.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001593void X86ISel::visitCallInst(CallInst &CI) {
Chris Lattner3e130a22003-01-13 00:32:26 +00001594 MachineInstr *TheCall;
1595 if (Function *F = CI.getCalledFunction()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001596 // Is it an intrinsic function call?
Brian Gaeked0fde302003-11-11 22:41:34 +00001597 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001598 visitIntrinsicCall(ID, CI); // Special intrinsics are not handled here
1599 return;
1600 }
1601
Chris Lattner3e130a22003-01-13 00:32:26 +00001602 // Emit a CALL instruction with PC-relative displacement.
1603 TheCall = BuildMI(X86::CALLpcrel32, 1).addGlobalAddress(F, true);
1604 } else { // Emit an indirect call...
1605 unsigned Reg = getReg(CI.getCalledValue());
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001606 TheCall = BuildMI(X86::CALL32r, 1).addReg(Reg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001607 }
1608
1609 std::vector<ValueRecord> Args;
1610 for (unsigned i = 1, e = CI.getNumOperands(); i != e; ++i)
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001611 Args.push_back(ValueRecord(CI.getOperand(i)));
Chris Lattner3e130a22003-01-13 00:32:26 +00001612
1613 unsigned DestReg = CI.getType() != Type::VoidTy ? getReg(CI) : 0;
1614 doCall(ValueRecord(DestReg, CI.getType()), TheCall, Args);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001615}
Chris Lattner3e130a22003-01-13 00:32:26 +00001616
Chris Lattner44827152003-12-28 09:47:19 +00001617/// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
1618/// function, lowering any calls to unknown intrinsic functions into the
1619/// equivalent LLVM code.
Misha Brukman538607f2004-03-01 23:53:11 +00001620///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001621void X86ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) {
Chris Lattner44827152003-12-28 09:47:19 +00001622 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1623 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
1624 if (CallInst *CI = dyn_cast<CallInst>(I++))
1625 if (Function *F = CI->getCalledFunction())
1626 switch (F->getIntrinsicID()) {
Chris Lattneraed386e2003-12-28 09:53:23 +00001627 case Intrinsic::not_intrinsic:
Chris Lattner317201d2004-03-13 00:24:00 +00001628 case Intrinsic::vastart:
1629 case Intrinsic::vacopy:
1630 case Intrinsic::vaend:
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001631 case Intrinsic::returnaddress:
1632 case Intrinsic::frameaddress:
Chris Lattner915e5e52004-02-12 17:53:22 +00001633 case Intrinsic::memcpy:
Chris Lattner2a0f2242004-02-14 04:46:05 +00001634 case Intrinsic::memset:
Chris Lattnerdc572442004-06-15 21:36:44 +00001635 case Intrinsic::isunordered:
John Criswell4ffff9e2004-04-08 20:31:47 +00001636 case Intrinsic::readport:
1637 case Intrinsic::writeport:
Chris Lattner44827152003-12-28 09:47:19 +00001638 // We directly implement these intrinsics
1639 break;
John Criswelle5a4c152004-04-13 22:13:14 +00001640 case Intrinsic::readio: {
1641 // On X86, memory operations are in-order. Lower this intrinsic
1642 // into a volatile load.
1643 Instruction *Before = CI->getPrev();
Chris Lattnerb4fe76c2004-06-11 04:31:10 +00001644 LoadInst * LI = new LoadInst(CI->getOperand(1), "", true, CI);
1645 CI->replaceAllUsesWith(LI);
1646 BB->getInstList().erase(CI);
John Criswelle5a4c152004-04-13 22:13:14 +00001647 break;
1648 }
1649 case Intrinsic::writeio: {
1650 // On X86, memory operations are in-order. Lower this intrinsic
1651 // into a volatile store.
1652 Instruction *Before = CI->getPrev();
Chris Lattnerb4fe76c2004-06-11 04:31:10 +00001653 StoreInst *LI = new StoreInst(CI->getOperand(1),
1654 CI->getOperand(2), true, CI);
1655 CI->replaceAllUsesWith(LI);
1656 BB->getInstList().erase(CI);
John Criswelle5a4c152004-04-13 22:13:14 +00001657 break;
1658 }
Chris Lattner44827152003-12-28 09:47:19 +00001659 default:
1660 // All other intrinsic calls we must lower.
1661 Instruction *Before = CI->getPrev();
Chris Lattnerf70e0c22003-12-28 21:23:38 +00001662 TM.getIntrinsicLowering().LowerIntrinsicCall(CI);
Chris Lattner44827152003-12-28 09:47:19 +00001663 if (Before) { // Move iterator to instruction after call
Chris Lattnerb4fe76c2004-06-11 04:31:10 +00001664 I = Before; ++I;
Chris Lattner44827152003-12-28 09:47:19 +00001665 } else {
1666 I = BB->begin();
1667 }
1668 }
Chris Lattner44827152003-12-28 09:47:19 +00001669}
1670
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001671void X86ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001672 unsigned TmpReg1, TmpReg2;
1673 switch (ID) {
Chris Lattner5634b9f2004-03-13 00:24:52 +00001674 case Intrinsic::vastart:
Chris Lattnereca195e2003-05-08 19:44:13 +00001675 // Get the address of the first vararg value...
Chris Lattner73815062003-10-18 05:56:40 +00001676 TmpReg1 = getReg(CI);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001677 addFrameReference(BuildMI(BB, X86::LEA32r, 5, TmpReg1), VarArgsFrameIndex);
Chris Lattnereca195e2003-05-08 19:44:13 +00001678 return;
1679
Chris Lattner5634b9f2004-03-13 00:24:52 +00001680 case Intrinsic::vacopy:
Chris Lattner73815062003-10-18 05:56:40 +00001681 TmpReg1 = getReg(CI);
1682 TmpReg2 = getReg(CI.getOperand(1));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001683 BuildMI(BB, X86::MOV32rr, 1, TmpReg1).addReg(TmpReg2);
Chris Lattnereca195e2003-05-08 19:44:13 +00001684 return;
Chris Lattner5634b9f2004-03-13 00:24:52 +00001685 case Intrinsic::vaend: return; // Noop on X86
Chris Lattnereca195e2003-05-08 19:44:13 +00001686
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001687 case Intrinsic::returnaddress:
1688 case Intrinsic::frameaddress:
1689 TmpReg1 = getReg(CI);
1690 if (cast<Constant>(CI.getOperand(1))->isNullValue()) {
1691 if (ID == Intrinsic::returnaddress) {
1692 // Just load the return address
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001693 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, TmpReg1),
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001694 ReturnAddressIndex);
1695 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001696 addFrameReference(BuildMI(BB, X86::LEA32r, 4, TmpReg1),
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001697 ReturnAddressIndex, -4);
1698 }
1699 } else {
1700 // Values other than zero are not implemented yet.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001701 BuildMI(BB, X86::MOV32ri, 1, TmpReg1).addImm(0);
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001702 }
1703 return;
1704
Chris Lattnerdc572442004-06-15 21:36:44 +00001705 case Intrinsic::isunordered:
1706 TmpReg1 = getReg(CI.getOperand(1));
1707 TmpReg2 = getReg(CI.getOperand(2));
1708 emitUCOMr(BB, BB->end(), TmpReg2, TmpReg1);
1709 TmpReg2 = getReg(CI);
1710 BuildMI(BB, X86::SETPr, 0, TmpReg2);
1711 return;
1712
Chris Lattner915e5e52004-02-12 17:53:22 +00001713 case Intrinsic::memcpy: {
1714 assert(CI.getNumOperands() == 5 && "Illegal llvm.memcpy call!");
1715 unsigned Align = 1;
1716 if (ConstantInt *AlignC = dyn_cast<ConstantInt>(CI.getOperand(4))) {
1717 Align = AlignC->getRawValue();
1718 if (Align == 0) Align = 1;
1719 }
1720
1721 // Turn the byte code into # iterations
Chris Lattner915e5e52004-02-12 17:53:22 +00001722 unsigned CountReg;
Chris Lattner2a0f2242004-02-14 04:46:05 +00001723 unsigned Opcode;
Chris Lattner915e5e52004-02-12 17:53:22 +00001724 switch (Align & 3) {
1725 case 2: // WORD aligned
Chris Lattner07122832004-02-13 23:36:47 +00001726 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
1727 CountReg = getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/2));
1728 } else {
1729 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001730 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001731 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
Chris Lattner07122832004-02-13 23:36:47 +00001732 }
Chris Lattner2a0f2242004-02-14 04:46:05 +00001733 Opcode = X86::REP_MOVSW;
Chris Lattner915e5e52004-02-12 17:53:22 +00001734 break;
1735 case 0: // DWORD aligned
Chris Lattner07122832004-02-13 23:36:47 +00001736 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
1737 CountReg = getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/4));
1738 } else {
1739 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001740 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001741 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
Chris Lattner07122832004-02-13 23:36:47 +00001742 }
Chris Lattner2a0f2242004-02-14 04:46:05 +00001743 Opcode = X86::REP_MOVSD;
Chris Lattner915e5e52004-02-12 17:53:22 +00001744 break;
Chris Lattner8dd8d262004-02-26 01:20:02 +00001745 default: // BYTE aligned
Chris Lattner07122832004-02-13 23:36:47 +00001746 CountReg = getReg(CI.getOperand(3));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001747 Opcode = X86::REP_MOVSB;
Chris Lattner915e5e52004-02-12 17:53:22 +00001748 break;
1749 }
1750
1751 // No matter what the alignment is, we put the source in ESI, the
1752 // destination in EDI, and the count in ECX.
1753 TmpReg1 = getReg(CI.getOperand(1));
1754 TmpReg2 = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001755 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
1756 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
1757 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001758 BuildMI(BB, Opcode, 0);
1759 return;
1760 }
1761 case Intrinsic::memset: {
1762 assert(CI.getNumOperands() == 5 && "Illegal llvm.memset call!");
1763 unsigned Align = 1;
1764 if (ConstantInt *AlignC = dyn_cast<ConstantInt>(CI.getOperand(4))) {
1765 Align = AlignC->getRawValue();
1766 if (Align == 0) Align = 1;
Chris Lattner915e5e52004-02-12 17:53:22 +00001767 }
1768
Chris Lattner2a0f2242004-02-14 04:46:05 +00001769 // Turn the byte code into # iterations
Chris Lattner2a0f2242004-02-14 04:46:05 +00001770 unsigned CountReg;
1771 unsigned Opcode;
1772 if (ConstantInt *ValC = dyn_cast<ConstantInt>(CI.getOperand(2))) {
1773 unsigned Val = ValC->getRawValue() & 255;
1774
1775 // If the value is a constant, then we can potentially use larger copies.
1776 switch (Align & 3) {
1777 case 2: // WORD aligned
1778 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
Chris Lattner300d0ed2004-02-14 06:00:36 +00001779 CountReg =getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/2));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001780 } else {
1781 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001782 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001783 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001784 }
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001785 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001786 Opcode = X86::REP_STOSW;
1787 break;
1788 case 0: // DWORD aligned
1789 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
Chris Lattner300d0ed2004-02-14 06:00:36 +00001790 CountReg =getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/4));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001791 } else {
1792 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001793 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001794 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001795 }
1796 Val = (Val << 8) | Val;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001797 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001798 Opcode = X86::REP_STOSD;
1799 break;
Chris Lattner8dd8d262004-02-26 01:20:02 +00001800 default: // BYTE aligned
Chris Lattner2a0f2242004-02-14 04:46:05 +00001801 CountReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001802 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001803 Opcode = X86::REP_STOSB;
1804 break;
1805 }
1806 } else {
1807 // If it's not a constant value we are storing, just fall back. We could
1808 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
1809 unsigned ValReg = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001810 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001811 CountReg = getReg(CI.getOperand(3));
1812 Opcode = X86::REP_STOSB;
1813 }
1814
1815 // No matter what the alignment is, we put the source in ESI, the
1816 // destination in EDI, and the count in ECX.
1817 TmpReg1 = getReg(CI.getOperand(1));
1818 //TmpReg2 = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001819 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
1820 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001821 BuildMI(BB, Opcode, 0);
Chris Lattner915e5e52004-02-12 17:53:22 +00001822 return;
1823 }
1824
Chris Lattner87e18de2004-04-13 17:20:37 +00001825 case Intrinsic::readport: {
1826 // First, determine that the size of the operand falls within the acceptable
1827 // range for this architecture.
John Criswell4ffff9e2004-04-08 20:31:47 +00001828 //
Chris Lattner87e18de2004-04-13 17:20:37 +00001829 if (getClassB(CI.getOperand(1)->getType()) != cShort) {
John Criswellca6ea0f2004-04-08 22:39:13 +00001830 std::cerr << "llvm.readport: Address size is not 16 bits\n";
Chris Lattner87e18de2004-04-13 17:20:37 +00001831 exit(1);
John Criswellca6ea0f2004-04-08 22:39:13 +00001832 }
John Criswell4ffff9e2004-04-08 20:31:47 +00001833
John Criswell4ffff9e2004-04-08 20:31:47 +00001834 // Now, move the I/O port address into the DX register and use the IN
1835 // instruction to get the input data.
1836 //
Chris Lattner87e18de2004-04-13 17:20:37 +00001837 unsigned Class = getClass(CI.getCalledFunction()->getReturnType());
1838 unsigned DestReg = getReg(CI);
John Criswell4ffff9e2004-04-08 20:31:47 +00001839
Chris Lattner87e18de2004-04-13 17:20:37 +00001840 // If the port is a single-byte constant, use the immediate form.
1841 if (ConstantInt *C = dyn_cast<ConstantInt>(CI.getOperand(1)))
1842 if ((C->getRawValue() & 255) == C->getRawValue()) {
1843 switch (Class) {
1844 case cByte:
1845 BuildMI(BB, X86::IN8ri, 1).addImm((unsigned char)C->getRawValue());
1846 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AL);
1847 return;
1848 case cShort:
1849 BuildMI(BB, X86::IN16ri, 1).addImm((unsigned char)C->getRawValue());
1850 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AX);
1851 return;
1852 case cInt:
1853 BuildMI(BB, X86::IN32ri, 1).addImm((unsigned char)C->getRawValue());
1854 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::EAX);
1855 return;
1856 }
1857 }
1858
1859 unsigned Reg = getReg(CI.getOperand(1));
1860 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
1861 switch (Class) {
1862 case cByte:
1863 BuildMI(BB, X86::IN8rr, 0);
1864 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AL);
1865 break;
1866 case cShort:
1867 BuildMI(BB, X86::IN16rr, 0);
1868 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AX);
1869 break;
1870 case cInt:
1871 BuildMI(BB, X86::IN32rr, 0);
1872 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::EAX);
1873 break;
1874 default:
1875 std::cerr << "Cannot do input on this data type";
John Criswellca6ea0f2004-04-08 22:39:13 +00001876 exit (1);
1877 }
John Criswell4ffff9e2004-04-08 20:31:47 +00001878 return;
Chris Lattner87e18de2004-04-13 17:20:37 +00001879 }
John Criswell4ffff9e2004-04-08 20:31:47 +00001880
Chris Lattner87e18de2004-04-13 17:20:37 +00001881 case Intrinsic::writeport: {
1882 // First, determine that the size of the operand falls within the
1883 // acceptable range for this architecture.
1884 if (getClass(CI.getOperand(2)->getType()) != cShort) {
1885 std::cerr << "llvm.writeport: Address size is not 16 bits\n";
1886 exit(1);
1887 }
1888
1889 unsigned Class = getClassB(CI.getOperand(1)->getType());
1890 unsigned ValReg = getReg(CI.getOperand(1));
1891 switch (Class) {
1892 case cByte:
1893 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
1894 break;
1895 case cShort:
1896 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(ValReg);
1897 break;
1898 case cInt:
1899 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(ValReg);
1900 break;
1901 default:
1902 std::cerr << "llvm.writeport: invalid data type for X86 target";
1903 exit(1);
1904 }
1905
1906
1907 // If the port is a single-byte constant, use the immediate form.
1908 if (ConstantInt *C = dyn_cast<ConstantInt>(CI.getOperand(2)))
1909 if ((C->getRawValue() & 255) == C->getRawValue()) {
1910 static const unsigned O[] = { X86::OUT8ir, X86::OUT16ir, X86::OUT32ir };
1911 BuildMI(BB, O[Class], 1).addImm((unsigned char)C->getRawValue());
1912 return;
1913 }
1914
1915 // Otherwise, move the I/O port address into the DX register and the value
1916 // to write into the AL/AX/EAX register.
1917 static const unsigned Opc[] = { X86::OUT8rr, X86::OUT16rr, X86::OUT32rr };
1918 unsigned Reg = getReg(CI.getOperand(2));
1919 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
1920 BuildMI(BB, Opc[Class], 0);
1921 return;
1922 }
1923
Chris Lattner44827152003-12-28 09:47:19 +00001924 default: assert(0 && "Error: unknown intrinsics should have been lowered!");
Chris Lattnereca195e2003-05-08 19:44:13 +00001925 }
1926}
1927
Chris Lattner7dee5da2004-03-08 01:58:35 +00001928static bool isSafeToFoldLoadIntoInstruction(LoadInst &LI, Instruction &User) {
1929 if (LI.getParent() != User.getParent())
1930 return false;
1931 BasicBlock::iterator It = &LI;
1932 // Check all of the instructions between the load and the user. We should
1933 // really use alias analysis here, but for now we just do something simple.
1934 for (++It; It != BasicBlock::iterator(&User); ++It) {
1935 switch (It->getOpcode()) {
Chris Lattner85c84e72004-03-18 06:29:54 +00001936 case Instruction::Free:
Chris Lattner7dee5da2004-03-08 01:58:35 +00001937 case Instruction::Store:
1938 case Instruction::Call:
1939 case Instruction::Invoke:
1940 return false;
Chris Lattner133dbb12004-04-12 03:02:48 +00001941 case Instruction::Load:
1942 if (cast<LoadInst>(It)->isVolatile() && LI.isVolatile())
1943 return false;
1944 break;
Chris Lattner7dee5da2004-03-08 01:58:35 +00001945 }
1946 }
1947 return true;
1948}
1949
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001950/// visitSimpleBinary - Implement simple binary operators for integral types...
1951/// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or, 4 for
1952/// Xor.
Misha Brukman538607f2004-03-01 23:53:11 +00001953///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001954void X86ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001955 unsigned DestReg = getReg(B);
1956 MachineBasicBlock::iterator MI = BB->end();
Chris Lattner721d2d42004-03-08 01:18:36 +00001957 Value *Op0 = B.getOperand(0), *Op1 = B.getOperand(1);
Chris Lattnerc81e6ba2004-05-10 15:15:55 +00001958 unsigned Class = getClassB(B.getType());
Chris Lattner721d2d42004-03-08 01:18:36 +00001959
Chris Lattner7dee5da2004-03-08 01:58:35 +00001960 // Special case: op Reg, load [mem]
Chris Lattnerc81e6ba2004-05-10 15:15:55 +00001961 if (isa<LoadInst>(Op0) && !isa<LoadInst>(Op1) && Class != cLong &&
Chris Lattnerccd97962004-06-17 22:15:25 +00001962 Op0->hasOneUse() &&
Chris Lattnerc81e6ba2004-05-10 15:15:55 +00001963 isSafeToFoldLoadIntoInstruction(*cast<LoadInst>(Op0), B))
Chris Lattner7dee5da2004-03-08 01:58:35 +00001964 if (!B.swapOperands())
1965 std::swap(Op0, Op1); // Make sure any loads are in the RHS.
1966
Chris Lattnerccd97962004-06-17 22:15:25 +00001967 if (isa<LoadInst>(Op1) && Class != cLong && Op1->hasOneUse() &&
Chris Lattner7dee5da2004-03-08 01:58:35 +00001968 isSafeToFoldLoadIntoInstruction(*cast<LoadInst>(Op1), B)) {
1969
Chris Lattner95157f72004-04-11 22:05:45 +00001970 unsigned Opcode;
1971 if (Class != cFP) {
1972 static const unsigned OpcodeTab[][3] = {
1973 // Arithmetic operators
1974 { X86::ADD8rm, X86::ADD16rm, X86::ADD32rm }, // ADD
1975 { X86::SUB8rm, X86::SUB16rm, X86::SUB32rm }, // SUB
1976
1977 // Bitwise operators
1978 { X86::AND8rm, X86::AND16rm, X86::AND32rm }, // AND
1979 { X86:: OR8rm, X86:: OR16rm, X86:: OR32rm }, // OR
1980 { X86::XOR8rm, X86::XOR16rm, X86::XOR32rm }, // XOR
1981 };
1982 Opcode = OpcodeTab[OperatorClass][Class];
1983 } else {
1984 static const unsigned OpcodeTab[][2] = {
1985 { X86::FADD32m, X86::FADD64m }, // ADD
1986 { X86::FSUB32m, X86::FSUB64m }, // SUB
1987 };
1988 const Type *Ty = Op0->getType();
1989 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
1990 Opcode = OpcodeTab[OperatorClass][Ty == Type::DoubleTy];
1991 }
Chris Lattner7dee5da2004-03-08 01:58:35 +00001992
Chris Lattner7dee5da2004-03-08 01:58:35 +00001993 unsigned Op0r = getReg(Op0);
Chris Lattner9f1b5312004-05-13 15:12:43 +00001994 if (AllocaInst *AI =
1995 dyn_castFixedAlloca(cast<LoadInst>(Op1)->getOperand(0))) {
1996 unsigned FI = getFixedSizedAllocaFI(AI);
1997 addFrameReference(BuildMI(BB, Opcode, 5, DestReg).addReg(Op0r), FI);
1998
1999 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002000 X86AddressMode AM;
2001 getAddressingMode(cast<LoadInst>(Op1)->getOperand(0), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002002
Reid Spencerfc989e12004-08-30 00:13:26 +00002003 addFullAddress(BuildMI(BB, Opcode, 5, DestReg).addReg(Op0r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002004 }
Chris Lattner7dee5da2004-03-08 01:58:35 +00002005 return;
2006 }
2007
Chris Lattner95157f72004-04-11 22:05:45 +00002008 // If this is a floating point subtract, check to see if we can fold the first
2009 // operand in.
2010 if (Class == cFP && OperatorClass == 1 &&
2011 isa<LoadInst>(Op0) &&
2012 isSafeToFoldLoadIntoInstruction(*cast<LoadInst>(Op0), B)) {
2013 const Type *Ty = Op0->getType();
2014 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
2015 unsigned Opcode = Ty == Type::FloatTy ? X86::FSUBR32m : X86::FSUBR64m;
2016
Chris Lattner95157f72004-04-11 22:05:45 +00002017 unsigned Op1r = getReg(Op1);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002018 if (AllocaInst *AI =
2019 dyn_castFixedAlloca(cast<LoadInst>(Op0)->getOperand(0))) {
2020 unsigned FI = getFixedSizedAllocaFI(AI);
2021 addFrameReference(BuildMI(BB, Opcode, 5, DestReg).addReg(Op1r), FI);
2022 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002023 X86AddressMode AM;
2024 getAddressingMode(cast<LoadInst>(Op0)->getOperand(0), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002025
Reid Spencerfc989e12004-08-30 00:13:26 +00002026 addFullAddress(BuildMI(BB, Opcode, 5, DestReg).addReg(Op1r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002027 }
Chris Lattner95157f72004-04-11 22:05:45 +00002028 return;
2029 }
2030
Chris Lattner721d2d42004-03-08 01:18:36 +00002031 emitSimpleBinaryOperation(BB, MI, Op0, Op1, OperatorClass, DestReg);
Chris Lattnerb515f6d2003-05-08 20:49:25 +00002032}
Chris Lattner3e130a22003-01-13 00:32:26 +00002033
Chris Lattner6621ed92004-04-11 21:23:56 +00002034
2035/// emitBinaryFPOperation - This method handles emission of floating point
2036/// Add (0), Sub (1), Mul (2), and Div (3) operations.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002037void X86ISel::emitBinaryFPOperation(MachineBasicBlock *BB,
2038 MachineBasicBlock::iterator IP,
2039 Value *Op0, Value *Op1,
2040 unsigned OperatorClass, unsigned DestReg) {
Chris Lattner6621ed92004-04-11 21:23:56 +00002041 // Special case: op Reg, <const fp>
2042 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1))
2043 if (!Op1C->isExactlyValue(+0.0) && !Op1C->isExactlyValue(+1.0)) {
2044 // Create a constant pool entry for this constant.
2045 MachineConstantPool *CP = F->getConstantPool();
2046 unsigned CPI = CP->getConstantPoolIndex(Op1C);
2047 const Type *Ty = Op1->getType();
2048
2049 static const unsigned OpcodeTab[][4] = {
2050 { X86::FADD32m, X86::FSUB32m, X86::FMUL32m, X86::FDIV32m }, // Float
2051 { X86::FADD64m, X86::FSUB64m, X86::FMUL64m, X86::FDIV64m }, // Double
2052 };
2053
2054 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
2055 unsigned Opcode = OpcodeTab[Ty != Type::FloatTy][OperatorClass];
2056 unsigned Op0r = getReg(Op0, BB, IP);
2057 addConstantPoolReference(BuildMI(*BB, IP, Opcode, 5,
2058 DestReg).addReg(Op0r), CPI);
2059 return;
2060 }
2061
Chris Lattner13c07fe2004-04-12 00:12:04 +00002062 // Special case: R1 = op <const fp>, R2
Chris Lattner6621ed92004-04-11 21:23:56 +00002063 if (ConstantFP *CFP = dyn_cast<ConstantFP>(Op0))
2064 if (CFP->isExactlyValue(-0.0) && OperatorClass == 1) {
2065 // -0.0 - X === -X
2066 unsigned op1Reg = getReg(Op1, BB, IP);
2067 BuildMI(*BB, IP, X86::FCHS, 1, DestReg).addReg(op1Reg);
2068 return;
2069 } else if (!CFP->isExactlyValue(+0.0) && !CFP->isExactlyValue(+1.0)) {
Chris Lattner13c07fe2004-04-12 00:12:04 +00002070 // R1 = op CST, R2 --> R1 = opr R2, CST
Chris Lattner6621ed92004-04-11 21:23:56 +00002071
2072 // Create a constant pool entry for this constant.
2073 MachineConstantPool *CP = F->getConstantPool();
2074 unsigned CPI = CP->getConstantPoolIndex(CFP);
2075 const Type *Ty = CFP->getType();
2076
2077 static const unsigned OpcodeTab[][4] = {
2078 { X86::FADD32m, X86::FSUBR32m, X86::FMUL32m, X86::FDIVR32m }, // Float
2079 { X86::FADD64m, X86::FSUBR64m, X86::FMUL64m, X86::FDIVR64m }, // Double
2080 };
2081
2082 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2083 unsigned Opcode = OpcodeTab[Ty != Type::FloatTy][OperatorClass];
2084 unsigned Op1r = getReg(Op1, BB, IP);
2085 addConstantPoolReference(BuildMI(*BB, IP, Opcode, 5,
2086 DestReg).addReg(Op1r), CPI);
2087 return;
2088 }
2089
2090 // General case.
2091 static const unsigned OpcodeTab[4] = {
2092 X86::FpADD, X86::FpSUB, X86::FpMUL, X86::FpDIV
2093 };
2094
2095 unsigned Opcode = OpcodeTab[OperatorClass];
2096 unsigned Op0r = getReg(Op0, BB, IP);
2097 unsigned Op1r = getReg(Op1, BB, IP);
2098 BuildMI(*BB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r);
2099}
2100
Chris Lattnerb2acc512003-10-19 21:09:10 +00002101/// emitSimpleBinaryOperation - Implement simple binary operators for integral
2102/// types... OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for
2103/// Or, 4 for Xor.
Chris Lattner68aad932002-11-02 20:13:22 +00002104///
Chris Lattnerb515f6d2003-05-08 20:49:25 +00002105/// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
2106/// and constant expression support.
Chris Lattnerb2acc512003-10-19 21:09:10 +00002107///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002108void X86ISel::emitSimpleBinaryOperation(MachineBasicBlock *MBB,
2109 MachineBasicBlock::iterator IP,
2110 Value *Op0, Value *Op1,
2111 unsigned OperatorClass,
2112 unsigned DestReg) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +00002113 unsigned Class = getClassB(Op0->getType());
Chris Lattnerb2acc512003-10-19 21:09:10 +00002114
Chris Lattner6621ed92004-04-11 21:23:56 +00002115 if (Class == cFP) {
2116 assert(OperatorClass < 2 && "No logical ops for FP!");
2117 emitBinaryFPOperation(MBB, IP, Op0, Op1, OperatorClass, DestReg);
2118 return;
2119 }
2120
Chris Lattner48b0c972004-04-11 20:26:20 +00002121 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0))
Chris Lattner667ea022004-06-18 00:50:37 +00002122 if (OperatorClass == 1) {
Chris Lattner48b0c972004-04-11 20:26:20 +00002123 static unsigned const NEGTab[] = {
2124 X86::NEG8r, X86::NEG16r, X86::NEG32r, 0, X86::NEG32r
2125 };
Chris Lattner667ea022004-06-18 00:50:37 +00002126
2127 // sub 0, X -> neg X
2128 if (CI->isNullValue()) {
2129 unsigned op1Reg = getReg(Op1, MBB, IP);
2130 BuildMI(*MBB, IP, NEGTab[Class], 1, DestReg).addReg(op1Reg);
Chris Lattner48b0c972004-04-11 20:26:20 +00002131
Chris Lattner667ea022004-06-18 00:50:37 +00002132 if (Class == cLong) {
2133 // We just emitted: Dl = neg Sl
2134 // Now emit : T = addc Sh, 0
2135 // : Dh = neg T
2136 unsigned T = makeAnotherReg(Type::IntTy);
2137 BuildMI(*MBB, IP, X86::ADC32ri, 2, T).addReg(op1Reg+1).addImm(0);
2138 BuildMI(*MBB, IP, X86::NEG32r, 1, DestReg+1).addReg(T);
2139 }
2140 return;
2141 } else if (Op1->hasOneUse() && Class != cLong) {
2142 // sub C, X -> tmp = neg X; DestReg = add tmp, C. This is better
2143 // than copying C into a temporary register, because of register
2144 // pressure (tmp and destreg can share a register.
2145 static unsigned const ADDRITab[] = {
2146 X86::ADD8ri, X86::ADD16ri, X86::ADD32ri, 0, X86::ADD32ri
2147 };
2148 unsigned op1Reg = getReg(Op1, MBB, IP);
2149 unsigned Tmp = makeAnotherReg(Op0->getType());
2150 BuildMI(*MBB, IP, NEGTab[Class], 1, Tmp).addReg(op1Reg);
Chris Lattner30483732004-06-20 07:49:54 +00002151 BuildMI(*MBB, IP, ADDRITab[Class], 2,
2152 DestReg).addReg(Tmp).addImm(CI->getRawValue());
Chris Lattner667ea022004-06-18 00:50:37 +00002153 return;
Chris Lattnerb2acc512003-10-19 21:09:10 +00002154 }
Chris Lattner48b0c972004-04-11 20:26:20 +00002155 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002156
Chris Lattner48b0c972004-04-11 20:26:20 +00002157 // Special case: op Reg, <const int>
2158 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner721d2d42004-03-08 01:18:36 +00002159 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002160
Chris Lattner721d2d42004-03-08 01:18:36 +00002161 // xor X, -1 -> not X
2162 if (OperatorClass == 4 && Op1C->isAllOnesValue()) {
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002163 static unsigned const NOTTab[] = {
2164 X86::NOT8r, X86::NOT16r, X86::NOT32r, 0, X86::NOT32r
2165 };
Chris Lattner721d2d42004-03-08 01:18:36 +00002166 BuildMI(*MBB, IP, NOTTab[Class], 1, DestReg).addReg(Op0r);
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002167 if (Class == cLong) // Invert the top part too
2168 BuildMI(*MBB, IP, X86::NOT32r, 1, DestReg+1).addReg(Op0r+1);
Chris Lattner721d2d42004-03-08 01:18:36 +00002169 return;
2170 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002171
Chris Lattner721d2d42004-03-08 01:18:36 +00002172 // add X, -1 -> dec X
Chris Lattner06521672004-04-06 03:36:57 +00002173 if (OperatorClass == 0 && Op1C->isAllOnesValue() && Class != cLong) {
2174 // Note that we can't use dec for 64-bit decrements, because it does not
2175 // set the carry flag!
2176 static unsigned const DECTab[] = { X86::DEC8r, X86::DEC16r, X86::DEC32r };
Chris Lattner721d2d42004-03-08 01:18:36 +00002177 BuildMI(*MBB, IP, DECTab[Class], 1, DestReg).addReg(Op0r);
2178 return;
2179 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002180
Chris Lattner721d2d42004-03-08 01:18:36 +00002181 // add X, 1 -> inc X
Chris Lattner06521672004-04-06 03:36:57 +00002182 if (OperatorClass == 0 && Op1C->equalsInt(1) && Class != cLong) {
2183 // Note that we can't use inc for 64-bit increments, because it does not
2184 // set the carry flag!
2185 static unsigned const INCTab[] = { X86::INC8r, X86::INC16r, X86::INC32r };
Alkis Evlogimenosbee8a092004-04-02 18:11:32 +00002186 BuildMI(*MBB, IP, INCTab[Class], 1, DestReg).addReg(Op0r);
Chris Lattner721d2d42004-03-08 01:18:36 +00002187 return;
2188 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002189
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002190 static const unsigned OpcodeTab[][5] = {
Chris Lattner721d2d42004-03-08 01:18:36 +00002191 // Arithmetic operators
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002192 { X86::ADD8ri, X86::ADD16ri, X86::ADD32ri, 0, X86::ADD32ri }, // ADD
2193 { X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, X86::SUB32ri }, // SUB
Chris Lattnerb2acc512003-10-19 21:09:10 +00002194
Chris Lattner721d2d42004-03-08 01:18:36 +00002195 // Bitwise operators
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002196 { X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, X86::AND32ri }, // AND
2197 { X86:: OR8ri, X86:: OR16ri, X86:: OR32ri, 0, X86::OR32ri }, // OR
2198 { X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, X86::XOR32ri }, // XOR
Chris Lattner721d2d42004-03-08 01:18:36 +00002199 };
2200
Chris Lattner721d2d42004-03-08 01:18:36 +00002201 unsigned Opcode = OpcodeTab[OperatorClass][Class];
Chris Lattner33f7fa32004-04-06 03:15:53 +00002202 unsigned Op1l = cast<ConstantInt>(Op1C)->getRawValue();
Chris Lattner721d2d42004-03-08 01:18:36 +00002203
Chris Lattner33f7fa32004-04-06 03:15:53 +00002204 if (Class != cLong) {
2205 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addImm(Op1l);
2206 return;
Chris Lattner6621ed92004-04-11 21:23:56 +00002207 }
2208
2209 // If this is a long value and the high or low bits have a special
2210 // property, emit some special cases.
2211 unsigned Op1h = cast<ConstantInt>(Op1C)->getRawValue() >> 32LL;
2212
2213 // If the constant is zero in the low 32-bits, just copy the low part
2214 // across and apply the normal 32-bit operation to the high parts. There
2215 // will be no carry or borrow into the top.
2216 if (Op1l == 0) {
2217 if (OperatorClass != 2) // All but and...
2218 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg).addReg(Op0r);
2219 else
2220 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
2221 BuildMI(*MBB, IP, OpcodeTab[OperatorClass][cLong], 2, DestReg+1)
2222 .addReg(Op0r+1).addImm(Op1h);
Chris Lattner33f7fa32004-04-06 03:15:53 +00002223 return;
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002224 }
Chris Lattner6621ed92004-04-11 21:23:56 +00002225
2226 // If this is a logical operation and the top 32-bits are zero, just
2227 // operate on the lower 32.
2228 if (Op1h == 0 && OperatorClass > 1) {
2229 BuildMI(*MBB, IP, OpcodeTab[OperatorClass][cLong], 2, DestReg)
2230 .addReg(Op0r).addImm(Op1l);
2231 if (OperatorClass != 2) // All but and
2232 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg+1).addReg(Op0r+1);
2233 else
2234 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
2235 return;
2236 }
2237
2238 // TODO: We could handle lots of other special cases here, such as AND'ing
2239 // with 0xFFFFFFFF00000000 -> noop, etc.
2240
2241 // Otherwise, code generate the full operation with a constant.
2242 static const unsigned TopTab[] = {
2243 X86::ADC32ri, X86::SBB32ri, X86::AND32ri, X86::OR32ri, X86::XOR32ri
2244 };
2245
2246 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addImm(Op1l);
2247 BuildMI(*MBB, IP, TopTab[OperatorClass], 2, DestReg+1)
2248 .addReg(Op0r+1).addImm(Op1h);
2249 return;
Chris Lattner721d2d42004-03-08 01:18:36 +00002250 }
2251
2252 // Finally, handle the general case now.
Chris Lattner7ba92302004-04-06 02:13:25 +00002253 static const unsigned OpcodeTab[][5] = {
Chris Lattner721d2d42004-03-08 01:18:36 +00002254 // Arithmetic operators
Chris Lattner6621ed92004-04-11 21:23:56 +00002255 { X86::ADD8rr, X86::ADD16rr, X86::ADD32rr, 0, X86::ADD32rr }, // ADD
2256 { X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, 0, X86::SUB32rr }, // SUB
Chris Lattner721d2d42004-03-08 01:18:36 +00002257
Chris Lattnerb2acc512003-10-19 21:09:10 +00002258 // Bitwise operators
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002259 { X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, X86::AND32rr }, // AND
2260 { X86:: OR8rr, X86:: OR16rr, X86:: OR32rr, 0, X86:: OR32rr }, // OR
2261 { X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, X86::XOR32rr }, // XOR
Chris Lattnerb2acc512003-10-19 21:09:10 +00002262 };
Chris Lattner721d2d42004-03-08 01:18:36 +00002263
Chris Lattnerb2acc512003-10-19 21:09:10 +00002264 unsigned Opcode = OpcodeTab[OperatorClass][Class];
Chris Lattner721d2d42004-03-08 01:18:36 +00002265 unsigned Op0r = getReg(Op0, MBB, IP);
2266 unsigned Op1r = getReg(Op1, MBB, IP);
2267 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r);
2268
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002269 if (Class == cLong) { // Handle the upper 32 bits of long values...
Chris Lattner721d2d42004-03-08 01:18:36 +00002270 static const unsigned TopTab[] = {
2271 X86::ADC32rr, X86::SBB32rr, X86::AND32rr, X86::OR32rr, X86::XOR32rr
2272 };
2273 BuildMI(*MBB, IP, TopTab[OperatorClass], 2,
2274 DestReg+1).addReg(Op0r+1).addReg(Op1r+1);
2275 }
Chris Lattnere2954c82002-11-02 20:04:26 +00002276}
2277
Chris Lattner3e130a22003-01-13 00:32:26 +00002278/// doMultiply - Emit appropriate instructions to multiply together the
2279/// registers op0Reg and op1Reg, and put the result in DestReg. The type of the
2280/// result should be given as DestTy.
2281///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002282void X86ISel::doMultiply(MachineBasicBlock *MBB,
2283 MachineBasicBlock::iterator MBBI,
2284 unsigned DestReg, const Type *DestTy,
2285 unsigned op0Reg, unsigned op1Reg) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002286 unsigned Class = getClass(DestTy);
Chris Lattner94af4142002-12-25 05:13:53 +00002287 switch (Class) {
Chris Lattner0f1c4612003-06-21 17:16:58 +00002288 case cInt:
2289 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002290 BuildMI(*MBB, MBBI, Class == cInt ? X86::IMUL32rr:X86::IMUL16rr, 2, DestReg)
Chris Lattner0f1c4612003-06-21 17:16:58 +00002291 .addReg(op0Reg).addReg(op1Reg);
2292 return;
2293 case cByte:
2294 // Must use the MUL instruction, which forces use of AL...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002295 BuildMI(*MBB, MBBI, X86::MOV8rr, 1, X86::AL).addReg(op0Reg);
2296 BuildMI(*MBB, MBBI, X86::MUL8r, 1).addReg(op1Reg);
2297 BuildMI(*MBB, MBBI, X86::MOV8rr, 1, DestReg).addReg(X86::AL);
Chris Lattner0f1c4612003-06-21 17:16:58 +00002298 return;
Chris Lattner94af4142002-12-25 05:13:53 +00002299 default:
Chris Lattner3e130a22003-01-13 00:32:26 +00002300 case cLong: assert(0 && "doMultiply cannot operate on LONG values!");
Chris Lattner94af4142002-12-25 05:13:53 +00002301 }
Brian Gaeke20244b72002-12-12 15:33:40 +00002302}
2303
Chris Lattnerb2acc512003-10-19 21:09:10 +00002304// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
2305// returns zero when the input is not exactly a power of two.
2306static unsigned ExactLog2(unsigned Val) {
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002307 if (Val == 0 || (Val & (Val-1))) return 0;
Chris Lattnerb2acc512003-10-19 21:09:10 +00002308 unsigned Count = 0;
2309 while (Val != 1) {
Chris Lattnerb2acc512003-10-19 21:09:10 +00002310 Val >>= 1;
2311 ++Count;
2312 }
2313 return Count+1;
2314}
2315
Chris Lattner462fa822004-04-11 20:56:28 +00002316
2317/// doMultiplyConst - This function is specialized to efficiently codegen an 8,
2318/// 16, or 32-bit integer multiply by a constant.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002319void X86ISel::doMultiplyConst(MachineBasicBlock *MBB,
2320 MachineBasicBlock::iterator IP,
2321 unsigned DestReg, const Type *DestTy,
2322 unsigned op0Reg, unsigned ConstRHS) {
Chris Lattner6ab06d52004-04-06 04:55:43 +00002323 static const unsigned MOVrrTab[] = {X86::MOV8rr, X86::MOV16rr, X86::MOV32rr};
2324 static const unsigned MOVriTab[] = {X86::MOV8ri, X86::MOV16ri, X86::MOV32ri};
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002325 static const unsigned ADDrrTab[] = {X86::ADD8rr, X86::ADD16rr, X86::ADD32rr};
Chris Lattner596b97f2004-07-19 23:47:21 +00002326 static const unsigned NEGrTab[] = {X86::NEG8r , X86::NEG16r , X86::NEG32r };
Chris Lattner6ab06d52004-04-06 04:55:43 +00002327
Chris Lattnerb2acc512003-10-19 21:09:10 +00002328 unsigned Class = getClass(DestTy);
Chris Lattner596b97f2004-07-19 23:47:21 +00002329 unsigned TmpReg;
Chris Lattnerb2acc512003-10-19 21:09:10 +00002330
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002331 // Handle special cases here.
2332 switch (ConstRHS) {
Chris Lattner596b97f2004-07-19 23:47:21 +00002333 case -2:
2334 TmpReg = makeAnotherReg(DestTy);
2335 BuildMI(*MBB, IP, NEGrTab[Class], 1, TmpReg).addReg(op0Reg);
2336 BuildMI(*MBB, IP, ADDrrTab[Class], 1,DestReg).addReg(TmpReg).addReg(TmpReg);
2337 return;
2338 case -1:
2339 BuildMI(*MBB, IP, NEGrTab[Class], 1, DestReg).addReg(op0Reg);
2340 return;
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002341 case 0:
Chris Lattner6ab06d52004-04-06 04:55:43 +00002342 BuildMI(*MBB, IP, MOVriTab[Class], 1, DestReg).addImm(0);
2343 return;
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002344 case 1:
Chris Lattner6ab06d52004-04-06 04:55:43 +00002345 BuildMI(*MBB, IP, MOVrrTab[Class], 1, DestReg).addReg(op0Reg);
2346 return;
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002347 case 2:
2348 BuildMI(*MBB, IP, ADDrrTab[Class], 1,DestReg).addReg(op0Reg).addReg(op0Reg);
2349 return;
2350 case 3:
2351 case 5:
2352 case 9:
2353 if (Class == cInt) {
Reid Spencerfc989e12004-08-30 00:13:26 +00002354 X86AddressMode AM;
2355 AM.BaseType = X86AddressMode::RegBase;
2356 AM.Base.Reg = op0Reg;
2357 AM.Scale = ConstRHS-1;
2358 AM.IndexReg = op0Reg;
2359 AM.Disp = 0;
2360 addFullAddress(BuildMI(*MBB, IP, X86::LEA32r, 5, DestReg), AM);
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002361 return;
2362 }
Chris Lattner596b97f2004-07-19 23:47:21 +00002363 case -3:
2364 case -5:
2365 case -9:
2366 if (Class == cInt) {
2367 TmpReg = makeAnotherReg(DestTy);
Reid Spencerfc989e12004-08-30 00:13:26 +00002368 X86AddressMode AM;
2369 AM.BaseType = X86AddressMode::RegBase;
2370 AM.Base.Reg = op0Reg;
2371 AM.Scale = -ConstRHS-1;
2372 AM.IndexReg = op0Reg;
2373 AM.Disp = 0;
2374 addFullAddress(BuildMI(*MBB, IP, X86::LEA32r, 5, TmpReg), AM);
Chris Lattner596b97f2004-07-19 23:47:21 +00002375 BuildMI(*MBB, IP, NEGrTab[Class], 1, DestReg).addReg(TmpReg);
2376 return;
2377 }
Chris Lattner6ab06d52004-04-06 04:55:43 +00002378 }
2379
Chris Lattnerb2acc512003-10-19 21:09:10 +00002380 // If the element size is exactly a power of 2, use a shift to get it.
2381 if (unsigned Shift = ExactLog2(ConstRHS)) {
2382 switch (Class) {
2383 default: assert(0 && "Unknown class for this function!");
2384 case cByte:
Chris Lattner596b97f2004-07-19 23:47:21 +00002385 BuildMI(*MBB, IP, X86::SHL8ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002386 return;
2387 case cShort:
Chris Lattner596b97f2004-07-19 23:47:21 +00002388 BuildMI(*MBB, IP, X86::SHL16ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002389 return;
2390 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002391 BuildMI(*MBB, IP, X86::SHL32ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002392 return;
2393 }
2394 }
Chris Lattner596b97f2004-07-19 23:47:21 +00002395
2396 // If the element size is a negative power of 2, use a shift/neg to get it.
2397 if (unsigned Shift = ExactLog2(-ConstRHS)) {
2398 TmpReg = makeAnotherReg(DestTy);
2399 BuildMI(*MBB, IP, NEGrTab[Class], 1, TmpReg).addReg(op0Reg);
2400 switch (Class) {
2401 default: assert(0 && "Unknown class for this function!");
2402 case cByte:
2403 BuildMI(*MBB, IP, X86::SHL8ri,2, DestReg).addReg(TmpReg).addImm(Shift-1);
2404 return;
2405 case cShort:
2406 BuildMI(*MBB, IP, X86::SHL16ri,2, DestReg).addReg(TmpReg).addImm(Shift-1);
2407 return;
2408 case cInt:
2409 BuildMI(*MBB, IP, X86::SHL32ri,2, DestReg).addReg(TmpReg).addImm(Shift-1);
2410 return;
2411 }
2412 }
Chris Lattnerc01d1232003-10-20 03:42:58 +00002413
2414 if (Class == cShort) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002415 BuildMI(*MBB, IP, X86::IMUL16rri,2,DestReg).addReg(op0Reg).addImm(ConstRHS);
Chris Lattnerc01d1232003-10-20 03:42:58 +00002416 return;
2417 } else if (Class == cInt) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002418 BuildMI(*MBB, IP, X86::IMUL32rri,2,DestReg).addReg(op0Reg).addImm(ConstRHS);
Chris Lattnerc01d1232003-10-20 03:42:58 +00002419 return;
2420 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002421
2422 // Most general case, emit a normal multiply...
Chris Lattner596b97f2004-07-19 23:47:21 +00002423 TmpReg = makeAnotherReg(DestTy);
Chris Lattneree352852004-02-29 07:22:16 +00002424 BuildMI(*MBB, IP, MOVriTab[Class], 1, TmpReg).addImm(ConstRHS);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002425
2426 // Emit a MUL to multiply the register holding the index by
2427 // elementSize, putting the result in OffsetReg.
2428 doMultiply(MBB, IP, DestReg, DestTy, op0Reg, TmpReg);
2429}
2430
Chris Lattnerca9671d2002-11-02 20:28:58 +00002431/// visitMul - Multiplies are not simple binary operators because they must deal
2432/// with the EAX register explicitly.
2433///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002434void X86ISel::visitMul(BinaryOperator &I) {
Chris Lattner462fa822004-04-11 20:56:28 +00002435 unsigned ResultReg = getReg(I);
2436
Chris Lattner95157f72004-04-11 22:05:45 +00002437 Value *Op0 = I.getOperand(0);
2438 Value *Op1 = I.getOperand(1);
2439
2440 // Fold loads into floating point multiplies.
2441 if (getClass(Op0->getType()) == cFP) {
2442 if (isa<LoadInst>(Op0) && !isa<LoadInst>(Op1))
2443 if (!I.swapOperands())
2444 std::swap(Op0, Op1); // Make sure any loads are in the RHS.
2445 if (LoadInst *LI = dyn_cast<LoadInst>(Op1))
2446 if (isSafeToFoldLoadIntoInstruction(*LI, I)) {
2447 const Type *Ty = Op0->getType();
2448 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2449 unsigned Opcode = Ty == Type::FloatTy ? X86::FMUL32m : X86::FMUL64m;
2450
Chris Lattner95157f72004-04-11 22:05:45 +00002451 unsigned Op0r = getReg(Op0);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002452 if (AllocaInst *AI = dyn_castFixedAlloca(LI->getOperand(0))) {
2453 unsigned FI = getFixedSizedAllocaFI(AI);
2454 addFrameReference(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op0r), FI);
2455 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002456 X86AddressMode AM;
2457 getAddressingMode(LI->getOperand(0), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002458
Reid Spencerfc989e12004-08-30 00:13:26 +00002459 addFullAddress(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op0r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002460 }
Chris Lattner95157f72004-04-11 22:05:45 +00002461 return;
2462 }
2463 }
2464
Chris Lattner462fa822004-04-11 20:56:28 +00002465 MachineBasicBlock::iterator IP = BB->end();
Chris Lattner95157f72004-04-11 22:05:45 +00002466 emitMultiply(BB, IP, Op0, Op1, ResultReg);
Chris Lattner462fa822004-04-11 20:56:28 +00002467}
2468
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002469void X86ISel::emitMultiply(MachineBasicBlock *MBB,
2470 MachineBasicBlock::iterator IP,
2471 Value *Op0, Value *Op1, unsigned DestReg) {
Chris Lattner462fa822004-04-11 20:56:28 +00002472 MachineBasicBlock &BB = *MBB;
2473 TypeClass Class = getClass(Op0->getType());
Chris Lattner3e130a22003-01-13 00:32:26 +00002474
2475 // Simple scalar multiply?
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002476 unsigned Op0Reg = getReg(Op0, &BB, IP);
Chris Lattner462fa822004-04-11 20:56:28 +00002477 switch (Class) {
2478 case cByte:
2479 case cShort:
2480 case cInt:
2481 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner462fa822004-04-11 20:56:28 +00002482 unsigned Val = (unsigned)CI->getRawValue(); // Isn't a 64-bit constant
2483 doMultiplyConst(&BB, IP, DestReg, Op0->getType(), Op0Reg, Val);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002484 } else {
Chris Lattner462fa822004-04-11 20:56:28 +00002485 unsigned Op1Reg = getReg(Op1, &BB, IP);
2486 doMultiply(&BB, IP, DestReg, Op1->getType(), Op0Reg, Op1Reg);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002487 }
Chris Lattner462fa822004-04-11 20:56:28 +00002488 return;
2489 case cFP:
Chris Lattner6621ed92004-04-11 21:23:56 +00002490 emitBinaryFPOperation(MBB, IP, Op0, Op1, 2, DestReg);
2491 return;
Chris Lattner462fa822004-04-11 20:56:28 +00002492 case cLong:
2493 break;
2494 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002495
Chris Lattner462fa822004-04-11 20:56:28 +00002496 // Long value. We have to do things the hard way...
2497 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
2498 unsigned CLow = CI->getRawValue();
2499 unsigned CHi = CI->getRawValue() >> 32;
2500
2501 if (CLow == 0) {
2502 // If the low part of the constant is all zeros, things are simple.
2503 BuildMI(BB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
2504 doMultiplyConst(&BB, IP, DestReg+1, Type::UIntTy, Op0Reg, CHi);
2505 return;
2506 }
2507
2508 // Multiply the two low parts... capturing carry into EDX
2509 unsigned OverflowReg = 0;
2510 if (CLow == 1) {
2511 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg).addReg(Op0Reg);
Chris Lattner028adc42004-04-06 04:29:36 +00002512 } else {
Chris Lattner462fa822004-04-11 20:56:28 +00002513 unsigned Op1RegL = makeAnotherReg(Type::UIntTy);
2514 OverflowReg = makeAnotherReg(Type::UIntTy);
2515 BuildMI(BB, IP, X86::MOV32ri, 1, Op1RegL).addImm(CLow);
2516 BuildMI(BB, IP, X86::MOV32rr, 1, X86::EAX).addReg(Op0Reg);
2517 BuildMI(BB, IP, X86::MUL32r, 1).addReg(Op1RegL); // AL*BL
Chris Lattner028adc42004-04-06 04:29:36 +00002518
Chris Lattner462fa822004-04-11 20:56:28 +00002519 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg).addReg(X86::EAX); // AL*BL
2520 BuildMI(BB, IP, X86::MOV32rr, 1,
2521 OverflowReg).addReg(X86::EDX); // AL*BL >> 32
2522 }
2523
2524 unsigned AHBLReg = makeAnotherReg(Type::UIntTy); // AH*BL
2525 doMultiplyConst(&BB, IP, AHBLReg, Type::UIntTy, Op0Reg+1, CLow);
2526
2527 unsigned AHBLplusOverflowReg;
2528 if (OverflowReg) {
2529 AHBLplusOverflowReg = makeAnotherReg(Type::UIntTy);
2530 BuildMI(BB, IP, X86::ADD32rr, 2, // AH*BL+(AL*BL >> 32)
Chris Lattner028adc42004-04-06 04:29:36 +00002531 AHBLplusOverflowReg).addReg(AHBLReg).addReg(OverflowReg);
Chris Lattner462fa822004-04-11 20:56:28 +00002532 } else {
2533 AHBLplusOverflowReg = AHBLReg;
2534 }
2535
2536 if (CHi == 0) {
2537 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg+1).addReg(AHBLplusOverflowReg);
2538 } else {
Chris Lattner028adc42004-04-06 04:29:36 +00002539 unsigned ALBHReg = makeAnotherReg(Type::UIntTy); // AL*BH
Chris Lattner462fa822004-04-11 20:56:28 +00002540 doMultiplyConst(&BB, IP, ALBHReg, Type::UIntTy, Op0Reg, CHi);
Chris Lattner028adc42004-04-06 04:29:36 +00002541
Chris Lattner462fa822004-04-11 20:56:28 +00002542 BuildMI(BB, IP, X86::ADD32rr, 2, // AL*BH + AH*BL + (AL*BL >> 32)
Chris Lattner028adc42004-04-06 04:29:36 +00002543 DestReg+1).addReg(AHBLplusOverflowReg).addReg(ALBHReg);
2544 }
Chris Lattner462fa822004-04-11 20:56:28 +00002545 return;
Chris Lattner3e130a22003-01-13 00:32:26 +00002546 }
Chris Lattner462fa822004-04-11 20:56:28 +00002547
2548 // General 64x64 multiply
2549
2550 unsigned Op1Reg = getReg(Op1, &BB, IP);
2551 // Multiply the two low parts... capturing carry into EDX
2552 BuildMI(BB, IP, X86::MOV32rr, 1, X86::EAX).addReg(Op0Reg);
2553 BuildMI(BB, IP, X86::MUL32r, 1).addReg(Op1Reg); // AL*BL
2554
2555 unsigned OverflowReg = makeAnotherReg(Type::UIntTy);
2556 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg).addReg(X86::EAX); // AL*BL
2557 BuildMI(BB, IP, X86::MOV32rr, 1,
2558 OverflowReg).addReg(X86::EDX); // AL*BL >> 32
2559
2560 unsigned AHBLReg = makeAnotherReg(Type::UIntTy); // AH*BL
2561 BuildMI(BB, IP, X86::IMUL32rr, 2,
2562 AHBLReg).addReg(Op0Reg+1).addReg(Op1Reg);
2563
2564 unsigned AHBLplusOverflowReg = makeAnotherReg(Type::UIntTy);
2565 BuildMI(BB, IP, X86::ADD32rr, 2, // AH*BL+(AL*BL >> 32)
2566 AHBLplusOverflowReg).addReg(AHBLReg).addReg(OverflowReg);
2567
2568 unsigned ALBHReg = makeAnotherReg(Type::UIntTy); // AL*BH
2569 BuildMI(BB, IP, X86::IMUL32rr, 2,
2570 ALBHReg).addReg(Op0Reg).addReg(Op1Reg+1);
2571
2572 BuildMI(BB, IP, X86::ADD32rr, 2, // AL*BH + AH*BL + (AL*BL >> 32)
2573 DestReg+1).addReg(AHBLplusOverflowReg).addReg(ALBHReg);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002574}
Chris Lattnerca9671d2002-11-02 20:28:58 +00002575
Chris Lattner06925362002-11-17 21:56:38 +00002576
Chris Lattnerf01729e2002-11-02 20:54:46 +00002577/// visitDivRem - Handle division and remainder instructions... these
2578/// instruction both require the same instructions to be generated, they just
2579/// select the result from a different register. Note that both of these
2580/// instructions work differently for signed and unsigned operands.
2581///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002582void X86ISel::visitDivRem(BinaryOperator &I) {
Chris Lattnercadff442003-10-23 17:21:43 +00002583 unsigned ResultReg = getReg(I);
Chris Lattner95157f72004-04-11 22:05:45 +00002584 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2585
2586 // Fold loads into floating point divides.
2587 if (getClass(Op0->getType()) == cFP) {
2588 if (LoadInst *LI = dyn_cast<LoadInst>(Op1))
2589 if (isSafeToFoldLoadIntoInstruction(*LI, I)) {
2590 const Type *Ty = Op0->getType();
2591 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2592 unsigned Opcode = Ty == Type::FloatTy ? X86::FDIV32m : X86::FDIV64m;
2593
Chris Lattner95157f72004-04-11 22:05:45 +00002594 unsigned Op0r = getReg(Op0);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002595 if (AllocaInst *AI = dyn_castFixedAlloca(LI->getOperand(0))) {
2596 unsigned FI = getFixedSizedAllocaFI(AI);
2597 addFrameReference(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op0r), FI);
2598 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002599 X86AddressMode AM;
2600 getAddressingMode(LI->getOperand(0), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002601
Reid Spencerfc989e12004-08-30 00:13:26 +00002602 addFullAddress(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op0r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002603 }
Chris Lattner95157f72004-04-11 22:05:45 +00002604 return;
2605 }
2606
2607 if (LoadInst *LI = dyn_cast<LoadInst>(Op0))
2608 if (isSafeToFoldLoadIntoInstruction(*LI, I)) {
2609 const Type *Ty = Op0->getType();
2610 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2611 unsigned Opcode = Ty == Type::FloatTy ? X86::FDIVR32m : X86::FDIVR64m;
2612
Chris Lattner95157f72004-04-11 22:05:45 +00002613 unsigned Op1r = getReg(Op1);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002614 if (AllocaInst *AI = dyn_castFixedAlloca(LI->getOperand(0))) {
2615 unsigned FI = getFixedSizedAllocaFI(AI);
2616 addFrameReference(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op1r), FI);
2617 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002618 X86AddressMode AM;
2619 getAddressingMode(LI->getOperand(0), AM);
2620 addFullAddress(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op1r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002621 }
Chris Lattner95157f72004-04-11 22:05:45 +00002622 return;
2623 }
2624 }
2625
Chris Lattner94af4142002-12-25 05:13:53 +00002626
Chris Lattnercadff442003-10-23 17:21:43 +00002627 MachineBasicBlock::iterator IP = BB->end();
Chris Lattner95157f72004-04-11 22:05:45 +00002628 emitDivRemOperation(BB, IP, Op0, Op1,
Chris Lattner462fa822004-04-11 20:56:28 +00002629 I.getOpcode() == Instruction::Div, ResultReg);
Chris Lattnercadff442003-10-23 17:21:43 +00002630}
2631
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002632void X86ISel::emitDivRemOperation(MachineBasicBlock *BB,
2633 MachineBasicBlock::iterator IP,
2634 Value *Op0, Value *Op1, bool isDiv,
2635 unsigned ResultReg) {
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002636 const Type *Ty = Op0->getType();
2637 unsigned Class = getClass(Ty);
Chris Lattner94af4142002-12-25 05:13:53 +00002638 switch (Class) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002639 case cFP: // Floating point divide
Chris Lattnercadff442003-10-23 17:21:43 +00002640 if (isDiv) {
Chris Lattner6621ed92004-04-11 21:23:56 +00002641 emitBinaryFPOperation(BB, IP, Op0, Op1, 3, ResultReg);
2642 return;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00002643 } else { // Floating point remainder...
Chris Lattner462fa822004-04-11 20:56:28 +00002644 unsigned Op0Reg = getReg(Op0, BB, IP);
2645 unsigned Op1Reg = getReg(Op1, BB, IP);
Chris Lattner3e130a22003-01-13 00:32:26 +00002646 MachineInstr *TheCall =
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002647 BuildMI(X86::CALLpcrel32, 1).addExternalSymbol("fmod", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00002648 std::vector<ValueRecord> Args;
Chris Lattnercadff442003-10-23 17:21:43 +00002649 Args.push_back(ValueRecord(Op0Reg, Type::DoubleTy));
2650 Args.push_back(ValueRecord(Op1Reg, Type::DoubleTy));
Chris Lattner3e130a22003-01-13 00:32:26 +00002651 doCall(ValueRecord(ResultReg, Type::DoubleTy), TheCall, Args);
2652 }
Chris Lattner94af4142002-12-25 05:13:53 +00002653 return;
Chris Lattner3e130a22003-01-13 00:32:26 +00002654 case cLong: {
2655 static const char *FnName[] =
2656 { "__moddi3", "__divdi3", "__umoddi3", "__udivdi3" };
Chris Lattner462fa822004-04-11 20:56:28 +00002657 unsigned Op0Reg = getReg(Op0, BB, IP);
2658 unsigned Op1Reg = getReg(Op1, BB, IP);
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002659 unsigned NameIdx = Ty->isUnsigned()*2 + isDiv;
Chris Lattner3e130a22003-01-13 00:32:26 +00002660 MachineInstr *TheCall =
2661 BuildMI(X86::CALLpcrel32, 1).addExternalSymbol(FnName[NameIdx], true);
2662
2663 std::vector<ValueRecord> Args;
Chris Lattnercadff442003-10-23 17:21:43 +00002664 Args.push_back(ValueRecord(Op0Reg, Type::LongTy));
2665 Args.push_back(ValueRecord(Op1Reg, Type::LongTy));
Chris Lattner3e130a22003-01-13 00:32:26 +00002666 doCall(ValueRecord(ResultReg, Type::LongTy), TheCall, Args);
2667 return;
2668 }
2669 case cByte: case cShort: case cInt:
Misha Brukmancf00c4a2003-10-10 17:57:28 +00002670 break; // Small integrals, handled below...
Chris Lattner3e130a22003-01-13 00:32:26 +00002671 default: assert(0 && "Unknown class!");
Chris Lattner94af4142002-12-25 05:13:53 +00002672 }
Chris Lattnerf01729e2002-11-02 20:54:46 +00002673
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002674 static const unsigned MovOpcode[]={ X86::MOV8rr, X86::MOV16rr, X86::MOV32rr };
Chris Lattner2483f672004-10-06 05:01:07 +00002675 static const unsigned NEGOpcode[]={ X86::NEG8r, X86::NEG16r, X86::NEG32r };
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002676 static const unsigned SAROpcode[]={ X86::SAR8ri, X86::SAR16ri, X86::SAR32ri };
2677 static const unsigned SHROpcode[]={ X86::SHR8ri, X86::SHR16ri, X86::SHR32ri };
2678 static const unsigned ADDOpcode[]={ X86::ADD8rr, X86::ADD16rr, X86::ADD32rr };
2679
2680 // Special case signed division by power of 2.
Chris Lattner2483f672004-10-06 05:01:07 +00002681 if (ConstantSInt *CI = dyn_cast<ConstantSInt>(Op1))
2682 if (isDiv) {
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002683 assert(Class != cLong && "This doesn't handle 64-bit divides!");
2684 int V = CI->getValue();
2685
2686 if (V == 1) { // X /s 1 => X
2687 unsigned Op0Reg = getReg(Op0, BB, IP);
2688 BuildMI(*BB, IP, MovOpcode[Class], 1, ResultReg).addReg(Op0Reg);
2689 return;
2690 }
2691
2692 if (V == -1) { // X /s -1 => -X
2693 unsigned Op0Reg = getReg(Op0, BB, IP);
2694 BuildMI(*BB, IP, NEGOpcode[Class], 1, ResultReg).addReg(Op0Reg);
2695 return;
2696 }
2697
Chris Lattner610f1e22004-10-06 04:02:39 +00002698 if (V == 2 || V == -2) { // X /s 2
2699 static const unsigned CMPOpcode[] = {
2700 X86::CMP8ri, X86::CMP16ri, X86::CMP32ri
2701 };
2702 static const unsigned SBBOpcode[] = {
2703 X86::SBB8ri, X86::SBB16ri, X86::SBB32ri
2704 };
2705 unsigned Op0Reg = getReg(Op0, BB, IP);
2706 unsigned SignBit = 1 << (CI->getType()->getPrimitiveSize()*8-1);
2707 BuildMI(*BB, IP, CMPOpcode[Class], 2).addReg(Op0Reg).addImm(SignBit);
2708
2709 unsigned TmpReg = makeAnotherReg(Op0->getType());
2710 BuildMI(*BB, IP, SBBOpcode[Class], 2, TmpReg).addReg(Op0Reg).addImm(-1);
2711
2712 unsigned TmpReg2 = V == 2 ? ResultReg : makeAnotherReg(Op0->getType());
2713 BuildMI(*BB, IP, SAROpcode[Class], 2, TmpReg2).addReg(TmpReg).addImm(1);
2714 if (V == -2) {
2715 BuildMI(*BB, IP, NEGOpcode[Class], 1, ResultReg).addReg(TmpReg2);
2716 }
2717 return;
2718 }
2719
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002720 bool isNeg = false;
2721 if (V < 0) { // Not a positive power of 2?
2722 V = -V;
2723 isNeg = true; // Maybe it's a negative power of 2.
2724 }
2725 if (unsigned Log = ExactLog2(V)) {
2726 --Log;
2727 unsigned Op0Reg = getReg(Op0, BB, IP);
2728 unsigned TmpReg = makeAnotherReg(Op0->getType());
Chris Lattner3ffdff62004-10-06 04:19:43 +00002729 BuildMI(*BB, IP, SAROpcode[Class], 2, TmpReg)
2730 .addReg(Op0Reg).addImm(Log-1);
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002731 unsigned TmpReg2 = makeAnotherReg(Op0->getType());
2732 BuildMI(*BB, IP, SHROpcode[Class], 2, TmpReg2)
2733 .addReg(TmpReg).addImm(32-Log);
2734 unsigned TmpReg3 = makeAnotherReg(Op0->getType());
2735 BuildMI(*BB, IP, ADDOpcode[Class], 2, TmpReg3)
2736 .addReg(Op0Reg).addReg(TmpReg2);
2737
2738 unsigned TmpReg4 = isNeg ? makeAnotherReg(Op0->getType()) : ResultReg;
2739 BuildMI(*BB, IP, SAROpcode[Class], 2, TmpReg4)
Chris Lattner3ffdff62004-10-06 04:19:43 +00002740 .addReg(TmpReg3).addImm(Log);
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002741 if (isNeg)
2742 BuildMI(*BB, IP, NEGOpcode[Class], 1, ResultReg).addReg(TmpReg4);
2743 return;
2744 }
Chris Lattner2483f672004-10-06 05:01:07 +00002745 } else { // X % C
2746 assert(Class != cLong && "This doesn't handle 64-bit remainder!");
2747 int V = CI->getValue();
2748
2749 if (V == 2 || V == -2) { // X % 2, X % -2
Chris Lattner2483f672004-10-06 05:01:07 +00002750 static const unsigned SExtOpcode[] = { X86::CBW, X86::CWD, X86::CDQ };
2751 static const unsigned BaseReg[] = { X86::AL , X86::AX , X86::EAX };
2752 static const unsigned SExtReg[] = { X86::AH , X86::DX , X86::EDX };
2753 static const unsigned ANDOpcode[] = {
2754 X86::AND8ri, X86::AND16ri, X86::AND32ri
2755 };
2756 static const unsigned XOROpcode[] = {
2757 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr
2758 };
2759 static const unsigned SUBOpcode[] = {
2760 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr
2761 };
2762
2763 // Sign extend result into reg of -1 or 0.
2764 unsigned Op0Reg = getReg(Op0, BB, IP);
2765 BuildMI(*BB, IP, MovOpcode[Class], 1, BaseReg[Class]).addReg(Op0Reg);
2766 BuildMI(*BB, IP, SExtOpcode[Class], 0);
2767 unsigned TmpReg0 = makeAnotherReg(Op0->getType());
2768 BuildMI(*BB, IP, MovOpcode[Class], 1, TmpReg0).addReg(SExtReg[Class]);
2769
2770 unsigned TmpReg1 = makeAnotherReg(Op0->getType());
2771 BuildMI(*BB, IP, ANDOpcode[Class], 2, TmpReg1).addReg(Op0Reg).addImm(1);
2772
2773 unsigned TmpReg2 = makeAnotherReg(Op0->getType());
2774 BuildMI(*BB, IP, XOROpcode[Class], 2,
2775 TmpReg2).addReg(TmpReg1).addReg(TmpReg0);
2776 BuildMI(*BB, IP, SUBOpcode[Class], 2,
2777 ResultReg).addReg(TmpReg2).addReg(TmpReg0);
2778 return;
2779 }
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002780 }
2781
2782 static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX };
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002783 static const unsigned ClrOpcode[]={ X86::MOV8ri, X86::MOV16ri, X86::MOV32ri };
Chris Lattnerf01729e2002-11-02 20:54:46 +00002784 static const unsigned ExtRegs[] ={ X86::AH , X86::DX , X86::EDX };
2785
2786 static const unsigned DivOpcode[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002787 { X86::DIV8r , X86::DIV16r , X86::DIV32r , 0 }, // Unsigned division
2788 { X86::IDIV8r, X86::IDIV16r, X86::IDIV32r, 0 }, // Signed division
Chris Lattnerf01729e2002-11-02 20:54:46 +00002789 };
2790
Chris Lattnerf01729e2002-11-02 20:54:46 +00002791 unsigned Reg = Regs[Class];
2792 unsigned ExtReg = ExtRegs[Class];
Chris Lattnerf01729e2002-11-02 20:54:46 +00002793
2794 // Put the first operand into one of the A registers...
Chris Lattner462fa822004-04-11 20:56:28 +00002795 unsigned Op0Reg = getReg(Op0, BB, IP);
2796 unsigned Op1Reg = getReg(Op1, BB, IP);
Chris Lattneree352852004-02-29 07:22:16 +00002797 BuildMI(*BB, IP, MovOpcode[Class], 1, Reg).addReg(Op0Reg);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002798
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002799 if (Ty->isSigned()) {
Chris Lattnerf01729e2002-11-02 20:54:46 +00002800 // Emit a sign extension instruction...
Chris Lattner462fa822004-04-11 20:56:28 +00002801 unsigned ShiftResult = makeAnotherReg(Op0->getType());
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002802 BuildMI(*BB, IP, SAROpcode[Class], 2,ShiftResult).addReg(Op0Reg).addImm(31);
Chris Lattneree352852004-02-29 07:22:16 +00002803 BuildMI(*BB, IP, MovOpcode[Class], 1, ExtReg).addReg(ShiftResult);
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002804
2805 // Emit the appropriate divide or remainder instruction...
2806 BuildMI(*BB, IP, DivOpcode[1][Class], 1).addReg(Op1Reg);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002807 } else {
Alkis Evlogimenosf998a7e2004-01-12 07:22:45 +00002808 // If unsigned, emit a zeroing instruction... (reg = 0)
Chris Lattneree352852004-02-29 07:22:16 +00002809 BuildMI(*BB, IP, ClrOpcode[Class], 2, ExtReg).addImm(0);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002810
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002811 // Emit the appropriate divide or remainder instruction...
2812 BuildMI(*BB, IP, DivOpcode[0][Class], 1).addReg(Op1Reg);
2813 }
Chris Lattner06925362002-11-17 21:56:38 +00002814
Chris Lattnerf01729e2002-11-02 20:54:46 +00002815 // Figure out which register we want to pick the result out of...
Chris Lattnercadff442003-10-23 17:21:43 +00002816 unsigned DestReg = isDiv ? Reg : ExtReg;
Chris Lattnerf01729e2002-11-02 20:54:46 +00002817
Chris Lattnerf01729e2002-11-02 20:54:46 +00002818 // Put the result into the destination register...
Chris Lattneree352852004-02-29 07:22:16 +00002819 BuildMI(*BB, IP, MovOpcode[Class], 1, ResultReg).addReg(DestReg);
Chris Lattnerca9671d2002-11-02 20:28:58 +00002820}
Chris Lattnere2954c82002-11-02 20:04:26 +00002821
Chris Lattner06925362002-11-17 21:56:38 +00002822
Brian Gaekea1719c92002-10-31 23:03:59 +00002823/// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here
2824/// for constant immediate shift values, and for constant immediate
2825/// shift values equal to 1. Even the general case is sort of special,
2826/// because the shift amount has to be in CL, not just any old register.
2827///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002828void X86ISel::visitShiftInst(ShiftInst &I) {
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002829 MachineBasicBlock::iterator IP = BB->end ();
2830 emitShiftOperation (BB, IP, I.getOperand (0), I.getOperand (1),
2831 I.getOpcode () == Instruction::Shl, I.getType (),
2832 getReg (I));
2833}
2834
2835/// emitShiftOperation - Common code shared between visitShiftInst and
2836/// constant expression support.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002837void X86ISel::emitShiftOperation(MachineBasicBlock *MBB,
2838 MachineBasicBlock::iterator IP,
2839 Value *Op, Value *ShiftAmount,
2840 bool isLeftShift, const Type *ResultTy,
2841 unsigned DestReg) {
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002842 unsigned SrcReg = getReg (Op, MBB, IP);
2843 bool isSigned = ResultTy->isSigned ();
2844 unsigned Class = getClass (ResultTy);
Chris Lattner3e130a22003-01-13 00:32:26 +00002845
2846 static const unsigned ConstantOperand[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002847 { X86::SHR8ri, X86::SHR16ri, X86::SHR32ri, X86::SHRD32rri8 }, // SHR
2848 { X86::SAR8ri, X86::SAR16ri, X86::SAR32ri, X86::SHRD32rri8 }, // SAR
2849 { X86::SHL8ri, X86::SHL16ri, X86::SHL32ri, X86::SHLD32rri8 }, // SHL
2850 { X86::SHL8ri, X86::SHL16ri, X86::SHL32ri, X86::SHLD32rri8 }, // SAL = SHL
Chris Lattner3e130a22003-01-13 00:32:26 +00002851 };
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002852
Chris Lattner3e130a22003-01-13 00:32:26 +00002853 static const unsigned NonConstantOperand[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002854 { X86::SHR8rCL, X86::SHR16rCL, X86::SHR32rCL }, // SHR
2855 { X86::SAR8rCL, X86::SAR16rCL, X86::SAR32rCL }, // SAR
2856 { X86::SHL8rCL, X86::SHL16rCL, X86::SHL32rCL }, // SHL
2857 { X86::SHL8rCL, X86::SHL16rCL, X86::SHL32rCL }, // SAL = SHL
Chris Lattner3e130a22003-01-13 00:32:26 +00002858 };
Chris Lattner796df732002-11-02 00:44:25 +00002859
Chris Lattner3e130a22003-01-13 00:32:26 +00002860 // Longs, as usual, are handled specially...
2861 if (Class == cLong) {
2862 // If we have a constant shift, we can generate much more efficient code
2863 // than otherwise...
2864 //
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002865 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002866 unsigned Amount = CUI->getValue();
2867 if (Amount < 32) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002868 const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned];
2869 if (isLeftShift) {
Chris Lattneree352852004-02-29 07:22:16 +00002870 BuildMI(*MBB, IP, Opc[3], 3,
2871 DestReg+1).addReg(SrcReg+1).addReg(SrcReg).addImm(Amount);
2872 BuildMI(*MBB, IP, Opc[2], 2, DestReg).addReg(SrcReg).addImm(Amount);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002873 } else {
Chris Lattneree352852004-02-29 07:22:16 +00002874 BuildMI(*MBB, IP, Opc[3], 3,
2875 DestReg).addReg(SrcReg ).addReg(SrcReg+1).addImm(Amount);
2876 BuildMI(*MBB, IP, Opc[2],2,DestReg+1).addReg(SrcReg+1).addImm(Amount);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002877 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002878 } else { // Shifting more than 32 bits
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002879 Amount -= 32;
2880 if (isLeftShift) {
Chris Lattner722070e2004-04-06 03:42:38 +00002881 if (Amount != 0) {
2882 BuildMI(*MBB, IP, X86::SHL32ri, 2,
2883 DestReg + 1).addReg(SrcReg).addImm(Amount);
2884 } else {
2885 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg+1).addReg(SrcReg);
2886 }
2887 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002888 } else {
Chris Lattner722070e2004-04-06 03:42:38 +00002889 if (Amount != 0) {
2890 BuildMI(*MBB, IP, isSigned ? X86::SAR32ri : X86::SHR32ri, 2,
2891 DestReg).addReg(SrcReg+1).addImm(Amount);
2892 } else {
2893 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg+1);
2894 }
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002895 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002896 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002897 }
2898 } else {
Chris Lattner9171ef52003-06-01 01:56:54 +00002899 unsigned TmpReg = makeAnotherReg(Type::IntTy);
2900
2901 if (!isLeftShift && isSigned) {
2902 // If this is a SHR of a Long, then we need to do funny sign extension
2903 // stuff. TmpReg gets the value to use as the high-part if we are
2904 // shifting more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002905 BuildMI(*MBB, IP, X86::SAR32ri, 2, TmpReg).addReg(SrcReg).addImm(31);
Chris Lattner9171ef52003-06-01 01:56:54 +00002906 } else {
2907 // Other shifts use a fixed zero value if the shift is more than 32
2908 // bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002909 BuildMI(*MBB, IP, X86::MOV32ri, 1, TmpReg).addImm(0);
Chris Lattner9171ef52003-06-01 01:56:54 +00002910 }
2911
2912 // Initialize CL with the shift amount...
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002913 unsigned ShiftAmountReg = getReg(ShiftAmount, MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002914 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002915
2916 unsigned TmpReg2 = makeAnotherReg(Type::IntTy);
2917 unsigned TmpReg3 = makeAnotherReg(Type::IntTy);
2918 if (isLeftShift) {
2919 // TmpReg2 = shld inHi, inLo
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002920 BuildMI(*MBB, IP, X86::SHLD32rrCL,2,TmpReg2).addReg(SrcReg+1)
Chris Lattneree352852004-02-29 07:22:16 +00002921 .addReg(SrcReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002922 // TmpReg3 = shl inLo, CL
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002923 BuildMI(*MBB, IP, X86::SHL32rCL, 1, TmpReg3).addReg(SrcReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002924
2925 // Set the flags to indicate whether the shift was by more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002926 BuildMI(*MBB, IP, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Chris Lattner9171ef52003-06-01 01:56:54 +00002927
2928 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002929 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00002930 DestReg+1).addReg(TmpReg2).addReg(TmpReg3);
2931 // DestLo = (>32) ? TmpReg : TmpReg3;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002932 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002933 DestReg).addReg(TmpReg3).addReg(TmpReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002934 } else {
2935 // TmpReg2 = shrd inLo, inHi
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002936 BuildMI(*MBB, IP, X86::SHRD32rrCL,2,TmpReg2).addReg(SrcReg)
Chris Lattneree352852004-02-29 07:22:16 +00002937 .addReg(SrcReg+1);
Chris Lattner9171ef52003-06-01 01:56:54 +00002938 // TmpReg3 = s[ah]r inHi, CL
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002939 BuildMI(*MBB, IP, isSigned ? X86::SAR32rCL : X86::SHR32rCL, 1, TmpReg3)
Chris Lattner9171ef52003-06-01 01:56:54 +00002940 .addReg(SrcReg+1);
2941
2942 // Set the flags to indicate whether the shift was by more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002943 BuildMI(*MBB, IP, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Chris Lattner9171ef52003-06-01 01:56:54 +00002944
2945 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002946 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00002947 DestReg).addReg(TmpReg2).addReg(TmpReg3);
2948
2949 // DestHi = (>32) ? TmpReg : TmpReg3;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002950 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00002951 DestReg+1).addReg(TmpReg3).addReg(TmpReg);
2952 }
Brian Gaekea1719c92002-10-31 23:03:59 +00002953 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002954 return;
2955 }
Chris Lattnere9913f22002-11-02 01:41:55 +00002956
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002957 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002958 // The shift amount is constant, guaranteed to be a ubyte. Get its value.
2959 assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?");
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002960
Chris Lattner3e130a22003-01-13 00:32:26 +00002961 const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned];
Chris Lattneree352852004-02-29 07:22:16 +00002962 BuildMI(*MBB, IP, Opc[Class], 2,
2963 DestReg).addReg(SrcReg).addImm(CUI->getValue());
Chris Lattner3e130a22003-01-13 00:32:26 +00002964 } else { // The shift amount is non-constant.
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002965 unsigned ShiftAmountReg = getReg (ShiftAmount, MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002966 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002967
Chris Lattner3e130a22003-01-13 00:32:26 +00002968 const unsigned *Opc = NonConstantOperand[isLeftShift*2+isSigned];
Chris Lattneree352852004-02-29 07:22:16 +00002969 BuildMI(*MBB, IP, Opc[Class], 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002970 }
2971}
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002972
Chris Lattner3e130a22003-01-13 00:32:26 +00002973
Chris Lattner6fc3c522002-11-17 21:11:55 +00002974/// visitLoadInst - Implement LLVM load instructions in terms of the x86 'mov'
Chris Lattnere8f0d922002-12-24 00:03:11 +00002975/// instruction. The load and store instructions are the only place where we
2976/// need to worry about the memory layout of the target machine.
Chris Lattner6fc3c522002-11-17 21:11:55 +00002977///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002978void X86ISel::visitLoadInst(LoadInst &I) {
Chris Lattner7dee5da2004-03-08 01:58:35 +00002979 // Check to see if this load instruction is going to be folded into a binary
2980 // instruction, like add. If so, we don't want to emit it. Wouldn't a real
2981 // pattern matching instruction selector be nice?
Chris Lattner95157f72004-04-11 22:05:45 +00002982 unsigned Class = getClassB(I.getType());
Chris Lattnerfeac3e12004-04-11 23:21:26 +00002983 if (I.hasOneUse()) {
Chris Lattner7dee5da2004-03-08 01:58:35 +00002984 Instruction *User = cast<Instruction>(I.use_back());
2985 switch (User->getOpcode()) {
Chris Lattnerfeac3e12004-04-11 23:21:26 +00002986 case Instruction::Cast:
2987 // If this is a cast from a signed-integer type to a floating point type,
2988 // fold the cast here.
John Criswell6b5bd582004-06-09 15:18:51 +00002989 if (getClassB(User->getType()) == cFP &&
Chris Lattnerfeac3e12004-04-11 23:21:26 +00002990 (I.getType() == Type::ShortTy || I.getType() == Type::IntTy ||
2991 I.getType() == Type::LongTy)) {
2992 unsigned DestReg = getReg(User);
2993 static const unsigned Opcode[] = {
2994 0/*BYTE*/, X86::FILD16m, X86::FILD32m, 0/*FP*/, X86::FILD64m
2995 };
Chris Lattner9f1b5312004-05-13 15:12:43 +00002996
2997 if (AllocaInst *AI = dyn_castFixedAlloca(I.getOperand(0))) {
2998 unsigned FI = getFixedSizedAllocaFI(AI);
2999 addFrameReference(BuildMI(BB, Opcode[Class], 4, DestReg), FI);
3000 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00003001 X86AddressMode AM;
3002 getAddressingMode(I.getOperand(0), AM);
3003 addFullAddress(BuildMI(BB, Opcode[Class], 4, DestReg), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00003004 }
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003005 return;
3006 } else {
3007 User = 0;
3008 }
3009 break;
Chris Lattner13c07fe2004-04-12 00:12:04 +00003010
Chris Lattner7dee5da2004-03-08 01:58:35 +00003011 case Instruction::Add:
3012 case Instruction::Sub:
3013 case Instruction::And:
3014 case Instruction::Or:
3015 case Instruction::Xor:
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003016 if (Class == cLong) User = 0;
Chris Lattner7dee5da2004-03-08 01:58:35 +00003017 break;
Chris Lattner95157f72004-04-11 22:05:45 +00003018 case Instruction::Mul:
3019 case Instruction::Div:
Chris Lattner13c07fe2004-04-12 00:12:04 +00003020 if (Class != cFP) User = 0;
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003021 break; // Folding only implemented for floating point.
Chris Lattner95157f72004-04-11 22:05:45 +00003022 default: User = 0; break;
Chris Lattner7dee5da2004-03-08 01:58:35 +00003023 }
3024
3025 if (User) {
3026 // Okay, we found a user. If the load is the first operand and there is
3027 // no second operand load, reverse the operand ordering. Note that this
3028 // can fail for a subtract (ie, no change will be made).
Chris Lattner3dbb5042004-07-21 21:28:26 +00003029 bool Swapped = false;
Chris Lattner7dee5da2004-03-08 01:58:35 +00003030 if (!isa<LoadInst>(User->getOperand(1)))
Chris Lattner3dbb5042004-07-21 21:28:26 +00003031 Swapped = !cast<BinaryOperator>(User)->swapOperands();
Chris Lattner7dee5da2004-03-08 01:58:35 +00003032
3033 // Okay, now that everything is set up, if this load is used by the second
3034 // operand, and if there are no instructions that invalidate the load
3035 // before the binary operator, eliminate the load.
3036 if (User->getOperand(1) == &I &&
3037 isSafeToFoldLoadIntoInstruction(I, *User))
3038 return; // Eliminate the load!
Chris Lattner95157f72004-04-11 22:05:45 +00003039
3040 // If this is a floating point sub or div, we won't be able to swap the
3041 // operands, but we will still be able to eliminate the load.
3042 if (Class == cFP && User->getOperand(0) == &I &&
3043 !isa<LoadInst>(User->getOperand(1)) &&
3044 (User->getOpcode() == Instruction::Sub ||
3045 User->getOpcode() == Instruction::Div) &&
3046 isSafeToFoldLoadIntoInstruction(I, *User))
3047 return; // Eliminate the load!
Chris Lattner3dbb5042004-07-21 21:28:26 +00003048
3049 // If we swapped the operands to the instruction, but couldn't fold the
3050 // load anyway, swap them back. We don't want to break add X, int
3051 // folding.
3052 if (Swapped) cast<BinaryOperator>(User)->swapOperands();
Chris Lattner7dee5da2004-03-08 01:58:35 +00003053 }
3054 }
3055
Chris Lattner6ac1d712003-10-20 04:48:06 +00003056 static const unsigned Opcodes[] = {
Chris Lattner9f1b5312004-05-13 15:12:43 +00003057 X86::MOV8rm, X86::MOV16rm, X86::MOV32rm, X86::FLD32m, X86::MOV32rm
Chris Lattner3e130a22003-01-13 00:32:26 +00003058 };
Chris Lattner6ac1d712003-10-20 04:48:06 +00003059 unsigned Opcode = Opcodes[Class];
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003060 if (I.getType() == Type::DoubleTy) Opcode = X86::FLD64m;
Chris Lattner9f1b5312004-05-13 15:12:43 +00003061
3062 unsigned DestReg = getReg(I);
3063
3064 if (AllocaInst *AI = dyn_castFixedAlloca(I.getOperand(0))) {
3065 unsigned FI = getFixedSizedAllocaFI(AI);
3066 if (Class == cLong) {
3067 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, DestReg), FI);
3068 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, DestReg+1), FI, 4);
3069 } else {
3070 addFrameReference(BuildMI(BB, Opcode, 4, DestReg), FI);
3071 }
3072 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00003073 X86AddressMode AM;
3074 getAddressingMode(I.getOperand(0), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00003075
3076 if (Class == cLong) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003077 addFullAddress(BuildMI(BB, X86::MOV32rm, 4, DestReg), AM);
3078 AM.Disp += 4;
3079 addFullAddress(BuildMI(BB, X86::MOV32rm, 4, DestReg+1), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00003080 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00003081 addFullAddress(BuildMI(BB, Opcode, 4, DestReg), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00003082 }
3083 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003084}
3085
Chris Lattner6fc3c522002-11-17 21:11:55 +00003086/// visitStoreInst - Implement LLVM store instructions in terms of the x86 'mov'
3087/// instruction.
3088///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003089void X86ISel::visitStoreInst(StoreInst &I) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003090 X86AddressMode AM;
3091 getAddressingMode(I.getOperand(1), AM);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003092
Chris Lattner6c09db22003-10-20 04:11:23 +00003093 const Type *ValTy = I.getOperand(0)->getType();
3094 unsigned Class = getClassB(ValTy);
Chris Lattner6ac1d712003-10-20 04:48:06 +00003095
Chris Lattner5a830962004-02-25 02:56:58 +00003096 if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(0))) {
3097 uint64_t Val = CI->getRawValue();
3098 if (Class == cLong) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003099 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(Val & ~0U);
3100 AM.Disp += 4;
3101 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(Val>>32);
Chris Lattner5a830962004-02-25 02:56:58 +00003102 } else {
3103 static const unsigned Opcodes[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003104 X86::MOV8mi, X86::MOV16mi, X86::MOV32mi
Chris Lattner5a830962004-02-25 02:56:58 +00003105 };
3106 unsigned Opcode = Opcodes[Class];
Reid Spencerfc989e12004-08-30 00:13:26 +00003107 addFullAddress(BuildMI(BB, Opcode, 5), AM).addImm(Val);
Chris Lattner5a830962004-02-25 02:56:58 +00003108 }
Chris Lattnerb7cb9ff2004-05-13 15:26:48 +00003109 } else if (isa<ConstantPointerNull>(I.getOperand(0))) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003110 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(0);
Chris Lattner5a830962004-02-25 02:56:58 +00003111 } else if (ConstantBool *CB = dyn_cast<ConstantBool>(I.getOperand(0))) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003112 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CB->getValue());
Chris Lattnere7a31c92004-05-07 21:18:15 +00003113 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0))) {
3114 // Store constant FP values with integer instructions to avoid having to
3115 // load the constants from the constant pool then do a store.
3116 if (CFP->getType() == Type::FloatTy) {
3117 union {
3118 unsigned I;
3119 float F;
3120 } V;
3121 V.F = CFP->getValue();
Reid Spencerfc989e12004-08-30 00:13:26 +00003122 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(V.I);
Chris Lattner5a830962004-02-25 02:56:58 +00003123 } else {
Chris Lattnere7a31c92004-05-07 21:18:15 +00003124 union {
3125 uint64_t I;
3126 double F;
3127 } V;
3128 V.F = CFP->getValue();
Reid Spencerfc989e12004-08-30 00:13:26 +00003129 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm((unsigned)V.I);
3130 AM.Disp += 4;
3131 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(
Chris Lattnere7a31c92004-05-07 21:18:15 +00003132 unsigned(V.I >> 32));
Chris Lattner5a830962004-02-25 02:56:58 +00003133 }
Chris Lattnere7a31c92004-05-07 21:18:15 +00003134
3135 } else if (Class == cLong) {
3136 unsigned ValReg = getReg(I.getOperand(0));
Reid Spencerfc989e12004-08-30 00:13:26 +00003137 addFullAddress(BuildMI(BB, X86::MOV32mr, 5), AM).addReg(ValReg);
3138 AM.Disp += 4;
3139 addFullAddress(BuildMI(BB, X86::MOV32mr, 5), AM).addReg(ValReg+1);
Chris Lattnere7a31c92004-05-07 21:18:15 +00003140 } else {
3141 unsigned ValReg = getReg(I.getOperand(0));
3142 static const unsigned Opcodes[] = {
3143 X86::MOV8mr, X86::MOV16mr, X86::MOV32mr, X86::FST32m
3144 };
3145 unsigned Opcode = Opcodes[Class];
3146 if (ValTy == Type::DoubleTy) Opcode = X86::FST64m;
Chris Lattner9f1b5312004-05-13 15:12:43 +00003147
Reid Spencerfc989e12004-08-30 00:13:26 +00003148 addFullAddress(BuildMI(BB, Opcode, 1+4), AM).addReg(ValReg);
Chris Lattner94af4142002-12-25 05:13:53 +00003149 }
Chris Lattner6fc3c522002-11-17 21:11:55 +00003150}
3151
3152
Misha Brukman538607f2004-03-01 23:53:11 +00003153/// visitCastInst - Here we have various kinds of copying with or without sign
3154/// extension going on.
3155///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003156void X86ISel::visitCastInst(CastInst &CI) {
Chris Lattnerf5854472003-06-21 16:01:24 +00003157 Value *Op = CI.getOperand(0);
Chris Lattner427aeb42004-04-11 19:21:59 +00003158
Chris Lattner99382862004-04-12 00:23:04 +00003159 unsigned SrcClass = getClassB(Op->getType());
3160 unsigned DestClass = getClassB(CI.getType());
3161 // Noop casts are not emitted: getReg will return the source operand as the
3162 // register to use for any uses of the noop cast.
Chris Lattner8b486a12004-06-29 00:14:38 +00003163 if (DestClass == SrcClass) {
3164 // The only detail in this plan is that casts from double -> float are
3165 // truncating operations that we have to codegen through memory (despite
3166 // the fact that the source/dest registers are the same class).
3167 if (CI.getType() != Type::FloatTy || Op->getType() != Type::DoubleTy)
3168 return;
3169 }
Chris Lattner427aeb42004-04-11 19:21:59 +00003170
Chris Lattnerf5854472003-06-21 16:01:24 +00003171 // If this is a cast from a 32-bit integer to a Long type, and the only uses
3172 // of the case are GEP instructions, then the cast does not need to be
3173 // generated explicitly, it will be folded into the GEP.
Chris Lattner99382862004-04-12 00:23:04 +00003174 if (DestClass == cLong && SrcClass == cInt) {
Chris Lattnerf5854472003-06-21 16:01:24 +00003175 bool AllUsesAreGEPs = true;
3176 for (Value::use_iterator I = CI.use_begin(), E = CI.use_end(); I != E; ++I)
3177 if (!isa<GetElementPtrInst>(*I)) {
3178 AllUsesAreGEPs = false;
3179 break;
3180 }
3181
3182 // No need to codegen this cast if all users are getelementptr instrs...
3183 if (AllUsesAreGEPs) return;
3184 }
3185
Chris Lattner99382862004-04-12 00:23:04 +00003186 // If this cast converts a load from a short,int, or long integer to a FP
3187 // value, we will have folded this cast away.
3188 if (DestClass == cFP && isa<LoadInst>(Op) && Op->hasOneUse() &&
3189 (Op->getType() == Type::ShortTy || Op->getType() == Type::IntTy ||
3190 Op->getType() == Type::LongTy))
3191 return;
3192
3193
Chris Lattner548f61d2003-04-23 17:22:12 +00003194 unsigned DestReg = getReg(CI);
3195 MachineBasicBlock::iterator MI = BB->end();
Chris Lattnerf5854472003-06-21 16:01:24 +00003196 emitCastOperation(BB, MI, Op, CI.getType(), DestReg);
Chris Lattner548f61d2003-04-23 17:22:12 +00003197}
3198
Misha Brukman538607f2004-03-01 23:53:11 +00003199/// emitCastOperation - Common code shared between visitCastInst and constant
3200/// expression cast support.
3201///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003202void X86ISel::emitCastOperation(MachineBasicBlock *BB,
3203 MachineBasicBlock::iterator IP,
3204 Value *Src, const Type *DestTy,
3205 unsigned DestReg) {
Chris Lattner3e130a22003-01-13 00:32:26 +00003206 const Type *SrcTy = Src->getType();
3207 unsigned SrcClass = getClassB(SrcTy);
Chris Lattner3e130a22003-01-13 00:32:26 +00003208 unsigned DestClass = getClassB(DestTy);
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003209 unsigned SrcReg = getReg(Src, BB, IP);
3210
Chris Lattner3e130a22003-01-13 00:32:26 +00003211 // Implement casts to bool by using compare on the operand followed by set if
3212 // not zero on the result.
3213 if (DestTy == Type::BoolTy) {
Chris Lattner20772542003-06-01 03:38:24 +00003214 switch (SrcClass) {
3215 case cByte:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003216 BuildMI(*BB, IP, X86::TEST8rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00003217 break;
3218 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003219 BuildMI(*BB, IP, X86::TEST16rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00003220 break;
3221 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003222 BuildMI(*BB, IP, X86::TEST32rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00003223 break;
3224 case cLong: {
3225 unsigned TmpReg = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003226 BuildMI(*BB, IP, X86::OR32rr, 2, TmpReg).addReg(SrcReg).addReg(SrcReg+1);
Chris Lattner20772542003-06-01 03:38:24 +00003227 break;
3228 }
3229 case cFP:
Chris Lattneree352852004-02-29 07:22:16 +00003230 BuildMI(*BB, IP, X86::FTST, 1).addReg(SrcReg);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003231 BuildMI(*BB, IP, X86::FNSTSW8r, 0);
Chris Lattneree352852004-02-29 07:22:16 +00003232 BuildMI(*BB, IP, X86::SAHF, 1);
Chris Lattner311ca2e2004-02-23 03:21:41 +00003233 break;
Chris Lattner20772542003-06-01 03:38:24 +00003234 }
3235
3236 // If the zero flag is not set, then the value is true, set the byte to
3237 // true.
Chris Lattneree352852004-02-29 07:22:16 +00003238 BuildMI(*BB, IP, X86::SETNEr, 1, DestReg);
Chris Lattner94af4142002-12-25 05:13:53 +00003239 return;
3240 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003241
3242 static const unsigned RegRegMove[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003243 X86::MOV8rr, X86::MOV16rr, X86::MOV32rr, X86::FpMOV, X86::MOV32rr
Chris Lattner3e130a22003-01-13 00:32:26 +00003244 };
3245
3246 // Implement casts between values of the same type class (as determined by
3247 // getClass) by using a register-to-register move.
3248 if (SrcClass == DestClass) {
3249 if (SrcClass <= cInt || (SrcClass == cFP && SrcTy == DestTy)) {
Chris Lattneree352852004-02-29 07:22:16 +00003250 BuildMI(*BB, IP, RegRegMove[SrcClass], 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003251 } else if (SrcClass == cFP) {
3252 if (SrcTy == Type::FloatTy) { // double -> float
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003253 assert(DestTy == Type::DoubleTy && "Unknown cFP member!");
Chris Lattneree352852004-02-29 07:22:16 +00003254 BuildMI(*BB, IP, X86::FpMOV, 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003255 } else { // float -> double
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003256 assert(SrcTy == Type::DoubleTy && DestTy == Type::FloatTy &&
3257 "Unknown cFP member!");
3258 // Truncate from double to float by storing to memory as short, then
3259 // reading it back.
3260 unsigned FltAlign = TM.getTargetData().getFloatAlignment();
Chris Lattner3e130a22003-01-13 00:32:26 +00003261 int FrameIdx = F->getFrameInfo()->CreateStackObject(4, FltAlign);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003262 addFrameReference(BuildMI(*BB, IP, X86::FST32m, 5), FrameIdx).addReg(SrcReg);
3263 addFrameReference(BuildMI(*BB, IP, X86::FLD32m, 5, DestReg), FrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003264 }
3265 } else if (SrcClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003266 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg);
3267 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg+1).addReg(SrcReg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00003268 } else {
Chris Lattnerc53544a2003-05-12 20:16:58 +00003269 assert(0 && "Cannot handle this type of cast instruction!");
Chris Lattner548f61d2003-04-23 17:22:12 +00003270 abort();
Brian Gaeked474e9c2002-12-06 10:49:33 +00003271 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003272 return;
3273 }
3274
3275 // Handle cast of SMALLER int to LARGER int using a move with sign extension
3276 // or zero extension, depending on whether the source type was signed.
3277 if (SrcClass <= cInt && (DestClass <= cInt || DestClass == cLong) &&
3278 SrcClass < DestClass) {
3279 bool isLong = DestClass == cLong;
3280 if (isLong) DestClass = cInt;
3281
3282 static const unsigned Opc[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003283 { X86::MOVSX16rr8, X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOV32rr }, // s
3284 { X86::MOVZX16rr8, X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOV32rr } // u
Chris Lattner3e130a22003-01-13 00:32:26 +00003285 };
3286
Chris Lattner96e3b422004-05-09 22:28:45 +00003287 bool isUnsigned = SrcTy->isUnsigned() || SrcTy == Type::BoolTy;
Chris Lattneree352852004-02-29 07:22:16 +00003288 BuildMI(*BB, IP, Opc[isUnsigned][SrcClass + DestClass - 1], 1,
Chris Lattner548f61d2003-04-23 17:22:12 +00003289 DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003290
3291 if (isLong) { // Handle upper 32 bits as appropriate...
3292 if (isUnsigned) // Zero out top bits...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003293 BuildMI(*BB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
Chris Lattner3e130a22003-01-13 00:32:26 +00003294 else // Sign extend bottom half...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003295 BuildMI(*BB, IP, X86::SAR32ri, 2, DestReg+1).addReg(DestReg).addImm(31);
Brian Gaeked474e9c2002-12-06 10:49:33 +00003296 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003297 return;
3298 }
3299
3300 // Special case long -> int ...
3301 if (SrcClass == cLong && DestClass == cInt) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003302 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003303 return;
3304 }
3305
3306 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by a
3307 // move out of AX or AL.
3308 if ((SrcClass <= cInt || SrcClass == cLong) && DestClass <= cInt
3309 && SrcClass > DestClass) {
3310 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX, 0, X86::EAX };
Chris Lattneree352852004-02-29 07:22:16 +00003311 BuildMI(*BB, IP, RegRegMove[SrcClass], 1, AReg[SrcClass]).addReg(SrcReg);
3312 BuildMI(*BB, IP, RegRegMove[DestClass], 1, DestReg).addReg(AReg[DestClass]);
Chris Lattner3e130a22003-01-13 00:32:26 +00003313 return;
3314 }
3315
3316 // Handle casts from integer to floating point now...
3317 if (DestClass == cFP) {
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003318 // Promote the integer to a type supported by FLD. We do this because there
3319 // are no unsigned FLD instructions, so we must promote an unsigned value to
3320 // a larger signed value, then use FLD on the larger value.
3321 //
3322 const Type *PromoteType = 0;
Chris Lattner85aa7092004-04-10 18:32:01 +00003323 unsigned PromoteOpcode = 0;
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003324 unsigned RealDestReg = DestReg;
Chris Lattnerf70c22b2004-06-17 18:19:28 +00003325 switch (SrcTy->getTypeID()) {
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003326 case Type::BoolTyID:
3327 case Type::SByteTyID:
3328 // We don't have the facilities for directly loading byte sized data from
3329 // memory (even signed). Promote it to 16 bits.
3330 PromoteType = Type::ShortTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003331 PromoteOpcode = X86::MOVSX16rr8;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003332 break;
3333 case Type::UByteTyID:
3334 PromoteType = Type::ShortTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003335 PromoteOpcode = X86::MOVZX16rr8;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003336 break;
3337 case Type::UShortTyID:
3338 PromoteType = Type::IntTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003339 PromoteOpcode = X86::MOVZX32rr16;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003340 break;
3341 case Type::UIntTyID: {
3342 // Make a 64 bit temporary... and zero out the top of it...
3343 unsigned TmpReg = makeAnotherReg(Type::LongTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003344 BuildMI(*BB, IP, X86::MOV32rr, 1, TmpReg).addReg(SrcReg);
3345 BuildMI(*BB, IP, X86::MOV32ri, 1, TmpReg+1).addImm(0);
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003346 SrcTy = Type::LongTy;
3347 SrcClass = cLong;
3348 SrcReg = TmpReg;
3349 break;
3350 }
3351 case Type::ULongTyID:
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003352 // Don't fild into the read destination.
3353 DestReg = makeAnotherReg(Type::DoubleTy);
3354 break;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003355 default: // No promotion needed...
3356 break;
3357 }
3358
3359 if (PromoteType) {
3360 unsigned TmpReg = makeAnotherReg(PromoteType);
Chris Lattner7b92de1e2004-04-06 19:29:36 +00003361 BuildMI(*BB, IP, PromoteOpcode, 1, TmpReg).addReg(SrcReg);
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003362 SrcTy = PromoteType;
3363 SrcClass = getClass(PromoteType);
Chris Lattner3e130a22003-01-13 00:32:26 +00003364 SrcReg = TmpReg;
3365 }
3366
3367 // Spill the integer to memory and reload it from there...
Chris Lattnerb6bac512004-02-25 06:13:04 +00003368 int FrameIdx =
3369 F->getFrameInfo()->CreateStackObject(SrcTy, TM.getTargetData());
Chris Lattner3e130a22003-01-13 00:32:26 +00003370
3371 if (SrcClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003372 addFrameReference(BuildMI(*BB, IP, X86::MOV32mr, 5),
Chris Lattneree352852004-02-29 07:22:16 +00003373 FrameIdx).addReg(SrcReg);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003374 addFrameReference(BuildMI(*BB, IP, X86::MOV32mr, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003375 FrameIdx, 4).addReg(SrcReg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00003376 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003377 static const unsigned Op1[] = { X86::MOV8mr, X86::MOV16mr, X86::MOV32mr };
Chris Lattneree352852004-02-29 07:22:16 +00003378 addFrameReference(BuildMI(*BB, IP, Op1[SrcClass], 5),
3379 FrameIdx).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003380 }
3381
3382 static const unsigned Op2[] =
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003383 { 0/*byte*/, X86::FILD16m, X86::FILD32m, 0/*FP*/, X86::FILD64m };
Chris Lattneree352852004-02-29 07:22:16 +00003384 addFrameReference(BuildMI(*BB, IP, Op2[SrcClass], 5, DestReg), FrameIdx);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003385
3386 // We need special handling for unsigned 64-bit integer sources. If the
3387 // input number has the "sign bit" set, then we loaded it incorrectly as a
3388 // negative 64-bit number. In this case, add an offset value.
3389 if (SrcTy == Type::ULongTy) {
3390 // Emit a test instruction to see if the dynamic input value was signed.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003391 BuildMI(*BB, IP, X86::TEST32rr, 2).addReg(SrcReg+1).addReg(SrcReg+1);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003392
Chris Lattnerb6bac512004-02-25 06:13:04 +00003393 // If the sign bit is set, get a pointer to an offset, otherwise get a
3394 // pointer to a zero.
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003395 MachineConstantPool *CP = F->getConstantPool();
3396 unsigned Zero = makeAnotherReg(Type::IntTy);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003397 Constant *Null = Constant::getNullValue(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003398 addConstantPoolReference(BuildMI(*BB, IP, X86::LEA32r, 5, Zero),
Chris Lattnerb6bac512004-02-25 06:13:04 +00003399 CP->getConstantPoolIndex(Null));
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003400 unsigned Offset = makeAnotherReg(Type::IntTy);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003401 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
3402
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003403 addConstantPoolReference(BuildMI(*BB, IP, X86::LEA32r, 5, Offset),
Chris Lattnerb6bac512004-02-25 06:13:04 +00003404 CP->getConstantPoolIndex(OffsetCst));
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003405 unsigned Addr = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003406 BuildMI(*BB, IP, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003407
3408 // Load the constant for an add. FIXME: this could make an 'fadd' that
3409 // reads directly from memory, but we don't support these yet.
3410 unsigned ConstReg = makeAnotherReg(Type::DoubleTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003411 addDirectMem(BuildMI(*BB, IP, X86::FLD32m, 4, ConstReg), Addr);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003412
Chris Lattneree352852004-02-29 07:22:16 +00003413 BuildMI(*BB, IP, X86::FpADD, 2, RealDestReg)
3414 .addReg(ConstReg).addReg(DestReg);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003415 }
3416
Chris Lattner3e130a22003-01-13 00:32:26 +00003417 return;
3418 }
3419
3420 // Handle casts from floating point to integer now...
3421 if (SrcClass == cFP) {
3422 // Change the floating point control register to use "round towards zero"
3423 // mode when truncating to an integer value.
3424 //
3425 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003426 addFrameReference(BuildMI(*BB, IP, X86::FNSTCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003427
3428 // Load the old value of the high byte of the control word...
3429 unsigned HighPartOfCW = makeAnotherReg(Type::UByteTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003430 addFrameReference(BuildMI(*BB, IP, X86::MOV8rm, 4, HighPartOfCW),
Chris Lattneree352852004-02-29 07:22:16 +00003431 CWFrameIdx, 1);
Chris Lattner3e130a22003-01-13 00:32:26 +00003432
3433 // Set the high part to be round to zero...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003434 addFrameReference(BuildMI(*BB, IP, X86::MOV8mi, 5),
Chris Lattneree352852004-02-29 07:22:16 +00003435 CWFrameIdx, 1).addImm(12);
Chris Lattner3e130a22003-01-13 00:32:26 +00003436
3437 // Reload the modified control word now...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003438 addFrameReference(BuildMI(*BB, IP, X86::FLDCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003439
3440 // Restore the memory image of control word to original value
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003441 addFrameReference(BuildMI(*BB, IP, X86::MOV8mr, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003442 CWFrameIdx, 1).addReg(HighPartOfCW);
Chris Lattner3e130a22003-01-13 00:32:26 +00003443
3444 // We don't have the facilities for directly storing byte sized data to
3445 // memory. Promote it to 16 bits. We also must promote unsigned values to
3446 // larger classes because we only have signed FP stores.
3447 unsigned StoreClass = DestClass;
3448 const Type *StoreTy = DestTy;
3449 if (StoreClass == cByte || DestTy->isUnsigned())
3450 switch (StoreClass) {
3451 case cByte: StoreTy = Type::ShortTy; StoreClass = cShort; break;
3452 case cShort: StoreTy = Type::IntTy; StoreClass = cInt; break;
3453 case cInt: StoreTy = Type::LongTy; StoreClass = cLong; break;
Brian Gaeked4615052003-07-18 20:23:43 +00003454 // The following treatment of cLong may not be perfectly right,
3455 // but it survives chains of casts of the form
3456 // double->ulong->double.
3457 case cLong: StoreTy = Type::LongTy; StoreClass = cLong; break;
Chris Lattner3e130a22003-01-13 00:32:26 +00003458 default: assert(0 && "Unknown store class!");
3459 }
3460
3461 // Spill the integer to memory and reload it from there...
3462 int FrameIdx =
3463 F->getFrameInfo()->CreateStackObject(StoreTy, TM.getTargetData());
3464
3465 static const unsigned Op1[] =
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003466 { 0, X86::FIST16m, X86::FIST32m, 0, X86::FISTP64m };
Chris Lattneree352852004-02-29 07:22:16 +00003467 addFrameReference(BuildMI(*BB, IP, Op1[StoreClass], 5),
3468 FrameIdx).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003469
3470 if (DestClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003471 addFrameReference(BuildMI(*BB, IP, X86::MOV32rm, 4, DestReg), FrameIdx);
3472 addFrameReference(BuildMI(*BB, IP, X86::MOV32rm, 4, DestReg+1),
Chris Lattneree352852004-02-29 07:22:16 +00003473 FrameIdx, 4);
Chris Lattner3e130a22003-01-13 00:32:26 +00003474 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003475 static const unsigned Op2[] = { X86::MOV8rm, X86::MOV16rm, X86::MOV32rm };
Chris Lattneree352852004-02-29 07:22:16 +00003476 addFrameReference(BuildMI(*BB, IP, Op2[DestClass], 4, DestReg), FrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003477 }
3478
3479 // Reload the original control word now...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003480 addFrameReference(BuildMI(*BB, IP, X86::FLDCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003481 return;
3482 }
3483
Brian Gaeked474e9c2002-12-06 10:49:33 +00003484 // Anything we haven't handled already, we can't (yet) handle at all.
Chris Lattnerc53544a2003-05-12 20:16:58 +00003485 assert(0 && "Unhandled cast instruction!");
Chris Lattner548f61d2003-04-23 17:22:12 +00003486 abort();
Brian Gaekefa8d5712002-11-22 11:07:01 +00003487}
Brian Gaekea1719c92002-10-31 23:03:59 +00003488
Chris Lattner73815062003-10-18 05:56:40 +00003489/// visitVANextInst - Implement the va_next instruction...
Chris Lattnereca195e2003-05-08 19:44:13 +00003490///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003491void X86ISel::visitVANextInst(VANextInst &I) {
Chris Lattner73815062003-10-18 05:56:40 +00003492 unsigned VAList = getReg(I.getOperand(0));
Chris Lattnereca195e2003-05-08 19:44:13 +00003493 unsigned DestReg = getReg(I);
3494
Chris Lattnereca195e2003-05-08 19:44:13 +00003495 unsigned Size;
Chris Lattnerf70c22b2004-06-17 18:19:28 +00003496 switch (I.getArgType()->getTypeID()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00003497 default:
3498 std::cerr << I;
Chris Lattner73815062003-10-18 05:56:40 +00003499 assert(0 && "Error: bad type for va_next instruction!");
Chris Lattnereca195e2003-05-08 19:44:13 +00003500 return;
3501 case Type::PointerTyID:
3502 case Type::UIntTyID:
3503 case Type::IntTyID:
3504 Size = 4;
Chris Lattnereca195e2003-05-08 19:44:13 +00003505 break;
3506 case Type::ULongTyID:
3507 case Type::LongTyID:
Chris Lattnereca195e2003-05-08 19:44:13 +00003508 case Type::DoubleTyID:
3509 Size = 8;
Chris Lattnereca195e2003-05-08 19:44:13 +00003510 break;
3511 }
3512
3513 // Increment the VAList pointer...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003514 BuildMI(BB, X86::ADD32ri, 2, DestReg).addReg(VAList).addImm(Size);
Chris Lattner73815062003-10-18 05:56:40 +00003515}
Chris Lattnereca195e2003-05-08 19:44:13 +00003516
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003517void X86ISel::visitVAArgInst(VAArgInst &I) {
Chris Lattner73815062003-10-18 05:56:40 +00003518 unsigned VAList = getReg(I.getOperand(0));
3519 unsigned DestReg = getReg(I);
3520
Chris Lattnerf70c22b2004-06-17 18:19:28 +00003521 switch (I.getType()->getTypeID()) {
Chris Lattner73815062003-10-18 05:56:40 +00003522 default:
3523 std::cerr << I;
3524 assert(0 && "Error: bad type for va_next instruction!");
3525 return;
3526 case Type::PointerTyID:
3527 case Type::UIntTyID:
3528 case Type::IntTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003529 addDirectMem(BuildMI(BB, X86::MOV32rm, 4, DestReg), VAList);
Chris Lattner73815062003-10-18 05:56:40 +00003530 break;
3531 case Type::ULongTyID:
3532 case Type::LongTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003533 addDirectMem(BuildMI(BB, X86::MOV32rm, 4, DestReg), VAList);
3534 addRegOffset(BuildMI(BB, X86::MOV32rm, 4, DestReg+1), VAList, 4);
Chris Lattner73815062003-10-18 05:56:40 +00003535 break;
3536 case Type::DoubleTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003537 addDirectMem(BuildMI(BB, X86::FLD64m, 4, DestReg), VAList);
Chris Lattner73815062003-10-18 05:56:40 +00003538 break;
3539 }
Chris Lattnereca195e2003-05-08 19:44:13 +00003540}
3541
Misha Brukman538607f2004-03-01 23:53:11 +00003542/// visitGetElementPtrInst - instruction-select GEP instructions
3543///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003544void X86ISel::visitGetElementPtrInst(GetElementPtrInst &I) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00003545 // If this GEP instruction will be folded into all of its users, we don't need
3546 // to explicitly calculate it!
Reid Spencerfc989e12004-08-30 00:13:26 +00003547 X86AddressMode AM;
3548 if (isGEPFoldable(0, I.getOperand(0), I.op_begin()+1, I.op_end(), AM)) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00003549 // Check all of the users of the instruction to see if they are loads and
3550 // stores.
3551 bool AllWillFold = true;
3552 for (Value::use_iterator UI = I.use_begin(), E = I.use_end(); UI != E; ++UI)
3553 if (cast<Instruction>(*UI)->getOpcode() != Instruction::Load)
3554 if (cast<Instruction>(*UI)->getOpcode() != Instruction::Store ||
3555 cast<Instruction>(*UI)->getOperand(0) == &I) {
3556 AllWillFold = false;
3557 break;
3558 }
3559
3560 // If the instruction is foldable, and will be folded into all users, don't
3561 // emit it!
3562 if (AllWillFold) return;
3563 }
3564
Chris Lattner3e130a22003-01-13 00:32:26 +00003565 unsigned outputReg = getReg(I);
Chris Lattner827832c2004-02-22 17:05:38 +00003566 emitGEPOperation(BB, BB->end(), I.getOperand(0),
Brian Gaeke68b1edc2002-12-16 04:23:29 +00003567 I.op_begin()+1, I.op_end(), outputReg);
Chris Lattnerc0812d82002-12-13 06:56:29 +00003568}
3569
Chris Lattner985fe3d2004-02-25 03:45:50 +00003570/// getGEPIndex - Inspect the getelementptr operands specified with GEPOps and
3571/// GEPTypes (the derived types being stepped through at each level). On return
3572/// from this function, if some indexes of the instruction are representable as
3573/// an X86 lea instruction, the machine operands are put into the Ops
3574/// instruction and the consumed indexes are poped from the GEPOps/GEPTypes
3575/// lists. Otherwise, GEPOps.size() is returned. If this returns a an
3576/// addressing mode that only partially consumes the input, the BaseReg input of
3577/// the addressing mode must be left free.
3578///
3579/// Note that there is one fewer entry in GEPTypes than there is in GEPOps.
3580///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003581void X86ISel::getGEPIndex(MachineBasicBlock *MBB,
3582 MachineBasicBlock::iterator IP,
3583 std::vector<Value*> &GEPOps,
3584 std::vector<const Type*> &GEPTypes,
3585 X86AddressMode &AM) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00003586 const TargetData &TD = TM.getTargetData();
3587
Chris Lattner985fe3d2004-02-25 03:45:50 +00003588 // Clear out the state we are working with...
Reid Spencerfc989e12004-08-30 00:13:26 +00003589 AM.BaseType = X86AddressMode::RegBase;
3590 AM.Base.Reg = 0; // No base register
3591 AM.Scale = 1; // Unit scale
3592 AM.IndexReg = 0; // No index register
3593 AM.Disp = 0; // No displacement
Chris Lattnerb6bac512004-02-25 06:13:04 +00003594
Chris Lattner985fe3d2004-02-25 03:45:50 +00003595 // While there are GEP indexes that can be folded into the current address,
3596 // keep processing them.
3597 while (!GEPTypes.empty()) {
3598 if (const StructType *StTy = dyn_cast<StructType>(GEPTypes.back())) {
3599 // It's a struct access. CUI is the index into the structure,
3600 // which names the field. This index must have unsigned type.
3601 const ConstantUInt *CUI = cast<ConstantUInt>(GEPOps.back());
3602
3603 // Use the TargetData structure to pick out what the layout of the
3604 // structure is in memory. Since the structure index must be constant, we
3605 // can get its value and use it to find the right byte offset from the
3606 // StructLayout class's list of structure member offsets.
Reid Spencerfc989e12004-08-30 00:13:26 +00003607 AM.Disp += TD.getStructLayout(StTy)->MemberOffsets[CUI->getValue()];
Chris Lattner985fe3d2004-02-25 03:45:50 +00003608 GEPOps.pop_back(); // Consume a GEP operand
3609 GEPTypes.pop_back();
3610 } else {
3611 // It's an array or pointer access: [ArraySize x ElementType].
3612 const SequentialType *SqTy = cast<SequentialType>(GEPTypes.back());
3613 Value *idx = GEPOps.back();
3614
3615 // idx is the index into the array. Unlike with structure
3616 // indices, we may not know its actual value at code-generation
3617 // time.
Chris Lattner985fe3d2004-02-25 03:45:50 +00003618
3619 // If idx is a constant, fold it into the offset.
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003620 unsigned TypeSize = TD.getTypeSize(SqTy->getElementType());
Chris Lattner985fe3d2004-02-25 03:45:50 +00003621 if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(idx)) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003622 AM.Disp += TypeSize*CSI->getValue();
Chris Lattner28977af2004-04-05 01:30:19 +00003623 } else if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(idx)) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003624 AM.Disp += TypeSize*CUI->getValue();
Chris Lattner985fe3d2004-02-25 03:45:50 +00003625 } else {
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003626 // If the index reg is already taken, we can't handle this index.
Reid Spencerfc989e12004-08-30 00:13:26 +00003627 if (AM.IndexReg) return;
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003628
3629 // If this is a size that we can handle, then add the index as
3630 switch (TypeSize) {
3631 case 1: case 2: case 4: case 8:
3632 // These are all acceptable scales on X86.
Reid Spencerfc989e12004-08-30 00:13:26 +00003633 AM.Scale = TypeSize;
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003634 break;
3635 default:
3636 // Otherwise, we can't handle this scale
3637 return;
3638 }
3639
3640 if (CastInst *CI = dyn_cast<CastInst>(idx))
3641 if (CI->getOperand(0)->getType() == Type::IntTy ||
3642 CI->getOperand(0)->getType() == Type::UIntTy)
3643 idx = CI->getOperand(0);
3644
Reid Spencerfc989e12004-08-30 00:13:26 +00003645 AM.IndexReg = MBB ? getReg(idx, MBB, IP) : 1;
Chris Lattner985fe3d2004-02-25 03:45:50 +00003646 }
3647
3648 GEPOps.pop_back(); // Consume a GEP operand
3649 GEPTypes.pop_back();
3650 }
3651 }
Chris Lattnerb6bac512004-02-25 06:13:04 +00003652
Chris Lattnerdf040972004-05-23 21:23:12 +00003653 // GEPTypes is empty, which means we have a single operand left. Set it as
3654 // the base register.
Chris Lattnerb6bac512004-02-25 06:13:04 +00003655 //
Reid Spencerfc989e12004-08-30 00:13:26 +00003656 assert(AM.Base.Reg == 0);
Chris Lattnerdf040972004-05-23 21:23:12 +00003657
Reid Spencerfc989e12004-08-30 00:13:26 +00003658 if (AllocaInst *AI = dyn_castFixedAlloca(GEPOps.back())) {
3659 AM.BaseType = X86AddressMode::FrameIndexBase;
3660 AM.Base.FrameIndex = getFixedSizedAllocaFI(AI);
Chris Lattnerdf040972004-05-23 21:23:12 +00003661 GEPOps.pop_back();
3662 return;
Reid Spencerfc989e12004-08-30 00:13:26 +00003663 }
3664
3665#if 0 // FIXME: TODO!
3666 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattnerdf040972004-05-23 21:23:12 +00003667 // FIXME: When addressing modes are more powerful/correct, we could load
3668 // global addresses directly as 32-bit immediates.
3669 }
3670#endif
3671
Reid Spencerfc989e12004-08-30 00:13:26 +00003672 AM.Base.Reg = MBB ? getReg(GEPOps[0], MBB, IP) : 1;
Chris Lattnerb6bac512004-02-25 06:13:04 +00003673 GEPOps.pop_back(); // Consume the last GEP operand
Chris Lattner985fe3d2004-02-25 03:45:50 +00003674}
3675
3676
Chris Lattnerb6bac512004-02-25 06:13:04 +00003677/// isGEPFoldable - Return true if the specified GEP can be completely
3678/// folded into the addressing mode of a load/store or lea instruction.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003679bool X86ISel::isGEPFoldable(MachineBasicBlock *MBB,
3680 Value *Src, User::op_iterator IdxBegin,
3681 User::op_iterator IdxEnd, X86AddressMode &AM) {
Chris Lattner7ca04092004-02-22 17:35:42 +00003682
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003683 std::vector<Value*> GEPOps;
3684 GEPOps.resize(IdxEnd-IdxBegin+1);
3685 GEPOps[0] = Src;
3686 std::copy(IdxBegin, IdxEnd, GEPOps.begin()+1);
3687
Chris Lattnerdf040972004-05-23 21:23:12 +00003688 std::vector<const Type*>
3689 GEPTypes(gep_type_begin(Src->getType(), IdxBegin, IdxEnd),
3690 gep_type_end(Src->getType(), IdxBegin, IdxEnd));
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003691
Chris Lattnerb6bac512004-02-25 06:13:04 +00003692 MachineBasicBlock::iterator IP;
3693 if (MBB) IP = MBB->end();
Reid Spencerfc989e12004-08-30 00:13:26 +00003694 getGEPIndex(MBB, IP, GEPOps, GEPTypes, AM);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003695
3696 // We can fold it away iff the getGEPIndex call eliminated all operands.
3697 return GEPOps.empty();
3698}
3699
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003700void X86ISel::emitGEPOperation(MachineBasicBlock *MBB,
3701 MachineBasicBlock::iterator IP,
3702 Value *Src, User::op_iterator IdxBegin,
3703 User::op_iterator IdxEnd, unsigned TargetReg) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00003704 const TargetData &TD = TM.getTargetData();
Chris Lattnerb6bac512004-02-25 06:13:04 +00003705
Chris Lattnerd2995df2004-07-15 00:58:53 +00003706 // If this is a getelementptr null, with all constant integer indices, just
3707 // replace it with TargetReg = 42.
3708 if (isa<ConstantPointerNull>(Src)) {
3709 User::op_iterator I = IdxBegin;
3710 for (; I != IdxEnd; ++I)
3711 if (!isa<ConstantInt>(*I))
3712 break;
3713 if (I == IdxEnd) { // All constant indices
3714 unsigned Offset = TD.getIndexedOffset(Src->getType(),
3715 std::vector<Value*>(IdxBegin, IdxEnd));
3716 BuildMI(*MBB, IP, X86::MOV32ri, 1, TargetReg).addImm(Offset);
3717 return;
3718 }
3719 }
3720
Chris Lattnerb6bac512004-02-25 06:13:04 +00003721 std::vector<Value*> GEPOps;
3722 GEPOps.resize(IdxEnd-IdxBegin+1);
3723 GEPOps[0] = Src;
3724 std::copy(IdxBegin, IdxEnd, GEPOps.begin()+1);
3725
3726 std::vector<const Type*> GEPTypes;
3727 GEPTypes.assign(gep_type_begin(Src->getType(), IdxBegin, IdxEnd),
3728 gep_type_end(Src->getType(), IdxBegin, IdxEnd));
Chris Lattner985fe3d2004-02-25 03:45:50 +00003729
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003730 // Keep emitting instructions until we consume the entire GEP instruction.
3731 while (!GEPOps.empty()) {
3732 unsigned OldSize = GEPOps.size();
Reid Spencerfc989e12004-08-30 00:13:26 +00003733 X86AddressMode AM;
3734 getGEPIndex(MBB, IP, GEPOps, GEPTypes, AM);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003735
Chris Lattner985fe3d2004-02-25 03:45:50 +00003736 if (GEPOps.size() != OldSize) {
3737 // getGEPIndex consumed some of the input. Build an LEA instruction here.
Chris Lattnerb6bac512004-02-25 06:13:04 +00003738 unsigned NextTarget = 0;
3739 if (!GEPOps.empty()) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003740 assert(AM.Base.Reg == 0 &&
Chris Lattnerb6bac512004-02-25 06:13:04 +00003741 "getGEPIndex should have left the base register open for chaining!");
Reid Spencerfc989e12004-08-30 00:13:26 +00003742 NextTarget = AM.Base.Reg = makeAnotherReg(Type::UIntTy);
Chris Lattner985fe3d2004-02-25 03:45:50 +00003743 }
Chris Lattnerb6bac512004-02-25 06:13:04 +00003744
Reid Spencerfc989e12004-08-30 00:13:26 +00003745 if (AM.BaseType == X86AddressMode::RegBase &&
3746 AM.IndexReg == 0 && AM.Disp == 0)
3747 BuildMI(*MBB, IP, X86::MOV32rr, 1, TargetReg).addReg(AM.Base.Reg);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003748 else
Reid Spencerfc989e12004-08-30 00:13:26 +00003749 addFullAddress(BuildMI(*MBB, IP, X86::LEA32r, 5, TargetReg), AM);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003750 --IP;
3751 TargetReg = NextTarget;
Chris Lattner985fe3d2004-02-25 03:45:50 +00003752 } else if (GEPTypes.empty()) {
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003753 // The getGEPIndex operation didn't want to build an LEA. Check to see if
3754 // all operands are consumed but the base pointer. If so, just load it
3755 // into the register.
Chris Lattner7ca04092004-02-22 17:35:42 +00003756 if (GlobalValue *GV = dyn_cast<GlobalValue>(GEPOps[0])) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003757 BuildMI(*MBB, IP, X86::MOV32ri, 1, TargetReg).addGlobalAddress(GV);
Chris Lattner7ca04092004-02-22 17:35:42 +00003758 } else {
3759 unsigned BaseReg = getReg(GEPOps[0], MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003760 BuildMI(*MBB, IP, X86::MOV32rr, 1, TargetReg).addReg(BaseReg);
Chris Lattner7ca04092004-02-22 17:35:42 +00003761 }
3762 break; // we are now done
Chris Lattnerb6bac512004-02-25 06:13:04 +00003763
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003764 } else {
Brian Gaeke20244b72002-12-12 15:33:40 +00003765 // It's an array or pointer access: [ArraySize x ElementType].
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003766 const SequentialType *SqTy = cast<SequentialType>(GEPTypes.back());
3767 Value *idx = GEPOps.back();
3768 GEPOps.pop_back(); // Consume a GEP operand
3769 GEPTypes.pop_back();
Chris Lattner8a307e82002-12-16 19:32:50 +00003770
Chris Lattner28977af2004-04-05 01:30:19 +00003771 // Many GEP instructions use a [cast (int/uint) to LongTy] as their
Chris Lattnerf5854472003-06-21 16:01:24 +00003772 // operand on X86. Handle this case directly now...
3773 if (CastInst *CI = dyn_cast<CastInst>(idx))
3774 if (CI->getOperand(0)->getType() == Type::IntTy ||
3775 CI->getOperand(0)->getType() == Type::UIntTy)
3776 idx = CI->getOperand(0);
3777
Chris Lattner3e130a22003-01-13 00:32:26 +00003778 // We want to add BaseReg to(idxReg * sizeof ElementType). First, we
Chris Lattner8a307e82002-12-16 19:32:50 +00003779 // must find the size of the pointed-to type (Not coincidentally, the next
3780 // type is the type of the elements in the array).
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003781 const Type *ElTy = SqTy->getElementType();
3782 unsigned elementSize = TD.getTypeSize(ElTy);
Chris Lattner8a307e82002-12-16 19:32:50 +00003783
3784 // If idxReg is a constant, we don't need to perform the multiply!
Chris Lattner28977af2004-04-05 01:30:19 +00003785 if (ConstantInt *CSI = dyn_cast<ConstantInt>(idx)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00003786 if (!CSI->isNullValue()) {
Chris Lattner28977af2004-04-05 01:30:19 +00003787 unsigned Offset = elementSize*CSI->getRawValue();
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003788 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003789 BuildMI(*MBB, IP, X86::ADD32ri, 2, TargetReg)
Chris Lattneree352852004-02-29 07:22:16 +00003790 .addReg(Reg).addImm(Offset);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003791 --IP; // Insert the next instruction before this one.
3792 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00003793 }
3794 } else if (elementSize == 1) {
3795 // If the element size is 1, we don't have to multiply, just add
3796 unsigned idxReg = getReg(idx, MBB, IP);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003797 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003798 BuildMI(*MBB, IP, X86::ADD32rr, 2,TargetReg).addReg(Reg).addReg(idxReg);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003799 --IP; // Insert the next instruction before this one.
3800 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00003801 } else {
3802 unsigned idxReg = getReg(idx, MBB, IP);
3803 unsigned OffsetReg = makeAnotherReg(Type::UIntTy);
Chris Lattnerb2acc512003-10-19 21:09:10 +00003804
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003805 // Make sure we can back the iterator up to point to the first
3806 // instruction emitted.
3807 MachineBasicBlock::iterator BeforeIt = IP;
3808 if (IP == MBB->begin())
3809 BeforeIt = MBB->end();
3810 else
3811 --BeforeIt;
Chris Lattnerb2acc512003-10-19 21:09:10 +00003812 doMultiplyConst(MBB, IP, OffsetReg, Type::IntTy, idxReg, elementSize);
3813
Chris Lattner8a307e82002-12-16 19:32:50 +00003814 // Emit an ADD to add OffsetReg to the basePtr.
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003815 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003816 BuildMI(*MBB, IP, X86::ADD32rr, 2, TargetReg)
Chris Lattneree352852004-02-29 07:22:16 +00003817 .addReg(Reg).addReg(OffsetReg);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003818
3819 // Step to the first instruction of the multiply.
3820 if (BeforeIt == MBB->end())
3821 IP = MBB->begin();
3822 else
3823 IP = ++BeforeIt;
3824
3825 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00003826 }
Brian Gaeke20244b72002-12-12 15:33:40 +00003827 }
Brian Gaeke20244b72002-12-12 15:33:40 +00003828 }
Brian Gaeke20244b72002-12-12 15:33:40 +00003829}
3830
Chris Lattner065faeb2002-12-28 20:24:02 +00003831/// visitAllocaInst - If this is a fixed size alloca, allocate space from the
3832/// frame manager, otherwise do it the hard way.
3833///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003834void X86ISel::visitAllocaInst(AllocaInst &I) {
Chris Lattner9f1b5312004-05-13 15:12:43 +00003835 // If this is a fixed size alloca in the entry block for the function, we
3836 // statically stack allocate the space, so we don't need to do anything here.
3837 //
Chris Lattnercb2fd552004-05-13 07:40:27 +00003838 if (dyn_castFixedAlloca(&I)) return;
Chris Lattner9f1b5312004-05-13 15:12:43 +00003839
Brian Gaekee48ec012002-12-13 06:46:31 +00003840 // Find the data size of the alloca inst's getAllocatedType.
Chris Lattner065faeb2002-12-28 20:24:02 +00003841 const Type *Ty = I.getAllocatedType();
3842 unsigned TySize = TM.getTargetData().getTypeSize(Ty);
3843
Chris Lattner065faeb2002-12-28 20:24:02 +00003844 // Create a register to hold the temporary result of multiplying the type size
3845 // constant by the variable amount.
3846 unsigned TotalSizeReg = makeAnotherReg(Type::UIntTy);
3847 unsigned SrcReg1 = getReg(I.getArraySize());
Chris Lattner065faeb2002-12-28 20:24:02 +00003848
3849 // TotalSizeReg = mul <numelements>, <TypeSize>
3850 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattnerb2acc512003-10-19 21:09:10 +00003851 doMultiplyConst(BB, MBBI, TotalSizeReg, Type::UIntTy, SrcReg1, TySize);
Chris Lattner065faeb2002-12-28 20:24:02 +00003852
3853 // AddedSize = add <TotalSizeReg>, 15
3854 unsigned AddedSizeReg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003855 BuildMI(BB, X86::ADD32ri, 2, AddedSizeReg).addReg(TotalSizeReg).addImm(15);
Chris Lattner065faeb2002-12-28 20:24:02 +00003856
3857 // AlignedSize = and <AddedSize>, ~15
3858 unsigned AlignedSize = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003859 BuildMI(BB, X86::AND32ri, 2, AlignedSize).addReg(AddedSizeReg).addImm(~15);
Chris Lattner065faeb2002-12-28 20:24:02 +00003860
Brian Gaekee48ec012002-12-13 06:46:31 +00003861 // Subtract size from stack pointer, thereby allocating some space.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003862 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(AlignedSize);
Chris Lattner065faeb2002-12-28 20:24:02 +00003863
Brian Gaekee48ec012002-12-13 06:46:31 +00003864 // Put a pointer to the space into the result register, by copying
3865 // the stack pointer.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003866 BuildMI(BB, X86::MOV32rr, 1, getReg(I)).addReg(X86::ESP);
Chris Lattner065faeb2002-12-28 20:24:02 +00003867
Misha Brukman48196b32003-05-03 02:18:17 +00003868 // Inform the Frame Information that we have just allocated a variable-sized
Chris Lattner065faeb2002-12-28 20:24:02 +00003869 // object.
3870 F->getFrameInfo()->CreateVariableSizedObject();
Brian Gaeke20244b72002-12-12 15:33:40 +00003871}
Chris Lattner3e130a22003-01-13 00:32:26 +00003872
3873/// visitMallocInst - Malloc instructions are code generated into direct calls
3874/// to the library malloc.
3875///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003876void X86ISel::visitMallocInst(MallocInst &I) {
Chris Lattner3e130a22003-01-13 00:32:26 +00003877 unsigned AllocSize = TM.getTargetData().getTypeSize(I.getAllocatedType());
3878 unsigned Arg;
3879
3880 if (ConstantUInt *C = dyn_cast<ConstantUInt>(I.getOperand(0))) {
3881 Arg = getReg(ConstantUInt::get(Type::UIntTy, C->getValue() * AllocSize));
3882 } else {
3883 Arg = makeAnotherReg(Type::UIntTy);
Chris Lattnerb2acc512003-10-19 21:09:10 +00003884 unsigned Op0Reg = getReg(I.getOperand(0));
Chris Lattner3e130a22003-01-13 00:32:26 +00003885 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattnerb2acc512003-10-19 21:09:10 +00003886 doMultiplyConst(BB, MBBI, Arg, Type::UIntTy, Op0Reg, AllocSize);
Chris Lattner3e130a22003-01-13 00:32:26 +00003887 }
3888
3889 std::vector<ValueRecord> Args;
3890 Args.push_back(ValueRecord(Arg, Type::UIntTy));
3891 MachineInstr *TheCall = BuildMI(X86::CALLpcrel32,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003892 1).addExternalSymbol("malloc", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00003893 doCall(ValueRecord(getReg(I), I.getType()), TheCall, Args);
3894}
3895
3896
3897/// visitFreeInst - Free instructions are code gen'd to call the free libc
3898/// function.
3899///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003900void X86ISel::visitFreeInst(FreeInst &I) {
Chris Lattner3e130a22003-01-13 00:32:26 +00003901 std::vector<ValueRecord> Args;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00003902 Args.push_back(ValueRecord(I.getOperand(0)));
Chris Lattner3e130a22003-01-13 00:32:26 +00003903 MachineInstr *TheCall = BuildMI(X86::CALLpcrel32,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003904 1).addExternalSymbol("free", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00003905 doCall(ValueRecord(0, Type::VoidTy), TheCall, Args);
3906}
3907
Chris Lattnerd281de22003-07-26 23:49:58 +00003908/// createX86SimpleInstructionSelector - This pass converts an LLVM function
Chris Lattnerb4f68ed2002-10-29 22:37:54 +00003909/// into a machine code representation is a very simple peep-hole fashion. The
Chris Lattner72614082002-10-25 22:55:53 +00003910/// generated code sucks but the implementation is nice and simple.
3911///
Chris Lattnerf70e0c22003-12-28 21:23:38 +00003912FunctionPass *llvm::createX86SimpleInstructionSelector(TargetMachine &TM) {
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003913 return new X86ISel(TM);
Chris Lattner72614082002-10-25 22:55:53 +00003914}