blob: 3b779363e0b0bb469d56245e54ac27657225cb1d [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 ||
Chris Lattnerd04cd552004-10-08 16:34:13 +0000850 SCI->getOpcode() == Instruction::SetNE) &&
Chris Lattnerb0f4e382004-10-08 22:24:31 +0000851 (isa<BranchInst>(User) || User->getOperand(0) == V))
Chris Lattner6d40c192003-01-16 16:43:00 +0000852 return SCI;
853 }
854 return 0;
855}
Chris Lattner333b2fa2002-12-13 10:09:43 +0000856
Chris Lattner6d40c192003-01-16 16:43:00 +0000857// Return a fixed numbering for setcc instructions which does not depend on the
858// order of the opcodes.
859//
860static unsigned getSetCCNumber(unsigned Opcode) {
861 switch(Opcode) {
862 default: assert(0 && "Unknown setcc instruction!");
863 case Instruction::SetEQ: return 0;
864 case Instruction::SetNE: return 1;
865 case Instruction::SetLT: return 2;
Chris Lattner55f6fab2003-01-16 18:07:23 +0000866 case Instruction::SetGE: return 3;
867 case Instruction::SetGT: return 4;
868 case Instruction::SetLE: return 5;
Chris Lattner6d40c192003-01-16 16:43:00 +0000869 }
870}
Chris Lattner06925362002-11-17 21:56:38 +0000871
Chris Lattner6d40c192003-01-16 16:43:00 +0000872// LLVM -> X86 signed X86 unsigned
873// ----- ---------- ------------
874// seteq -> sete sete
875// setne -> setne setne
876// setlt -> setl setb
Chris Lattner55f6fab2003-01-16 18:07:23 +0000877// setge -> setge setae
Chris Lattner6d40c192003-01-16 16:43:00 +0000878// setgt -> setg seta
879// setle -> setle setbe
Chris Lattnerb2acc512003-10-19 21:09:10 +0000880// ----
881// sets // Used by comparison with 0 optimization
882// setns
883static const unsigned SetCCOpcodeTab[2][8] = {
884 { X86::SETEr, X86::SETNEr, X86::SETBr, X86::SETAEr, X86::SETAr, X86::SETBEr,
885 0, 0 },
886 { X86::SETEr, X86::SETNEr, X86::SETLr, X86::SETGEr, X86::SETGr, X86::SETLEr,
887 X86::SETSr, X86::SETNSr },
Chris Lattner6d40c192003-01-16 16:43:00 +0000888};
889
Chris Lattner01cdb1b2004-06-11 05:33:49 +0000890/// emitUCOMr - In the future when we support processors before the P6, this
891/// wraps the logic for emitting an FUCOMr vs FUCOMIr.
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000892void X86ISel::emitUCOMr(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
893 unsigned LHS, unsigned RHS) {
Chris Lattner01cdb1b2004-06-11 05:33:49 +0000894 if (0) { // for processors prior to the P6
895 BuildMI(*MBB, IP, X86::FUCOMr, 2).addReg(LHS).addReg(RHS);
896 BuildMI(*MBB, IP, X86::FNSTSW8r, 0);
897 BuildMI(*MBB, IP, X86::SAHF, 1);
898 } else {
899 BuildMI(*MBB, IP, X86::FUCOMIr, 2).addReg(LHS).addReg(RHS);
900 }
901}
902
Chris Lattnerb2acc512003-10-19 21:09:10 +0000903// EmitComparison - This function emits a comparison of the two operands,
904// returning the extended setcc code to use.
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000905unsigned X86ISel::EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
906 MachineBasicBlock *MBB,
907 MachineBasicBlock::iterator IP) {
Brian Gaeke1749d632002-11-07 17:59:21 +0000908 // The arguments are already supposed to be of the same type.
Chris Lattner6d40c192003-01-16 16:43:00 +0000909 const Type *CompTy = Op0->getType();
Chris Lattner3e130a22003-01-13 00:32:26 +0000910 unsigned Class = getClassB(CompTy);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000911 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattner333864d2003-06-05 19:30:30 +0000912
913 // Special case handling of: cmp R, i
Chris Lattner260195d2004-05-07 19:55:55 +0000914 if (isa<ConstantPointerNull>(Op1)) {
915 if (OpNum < 2) // seteq/setne -> test
916 BuildMI(*MBB, IP, X86::TEST32rr, 2).addReg(Op0r).addReg(Op0r);
917 else
918 BuildMI(*MBB, IP, X86::CMP32ri, 2).addReg(Op0r).addImm(0);
919 return OpNum;
920
921 } else if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
Chris Lattnere80e6372004-04-06 16:02:27 +0000922 if (Class == cByte || Class == cShort || Class == cInt) {
923 unsigned Op1v = CI->getRawValue();
Chris Lattnerc07736a2003-07-23 15:22:26 +0000924
Chris Lattner333864d2003-06-05 19:30:30 +0000925 // Mask off any upper bits of the constant, if there are any...
926 Op1v &= (1ULL << (8 << Class)) - 1;
927
Chris Lattnerb2acc512003-10-19 21:09:10 +0000928 // If this is a comparison against zero, emit more efficient code. We
929 // can't handle unsigned comparisons against zero unless they are == or
930 // !=. These should have been strength reduced already anyway.
931 if (Op1v == 0 && (CompTy->isSigned() || OpNum < 2)) {
932 static const unsigned TESTTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000933 X86::TEST8rr, X86::TEST16rr, X86::TEST32rr
Chris Lattnerb2acc512003-10-19 21:09:10 +0000934 };
Chris Lattneree352852004-02-29 07:22:16 +0000935 BuildMI(*MBB, IP, TESTTab[Class], 2).addReg(Op0r).addReg(Op0r);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000936
937 if (OpNum == 2) return 6; // Map jl -> js
938 if (OpNum == 3) return 7; // Map jg -> jns
939 return OpNum;
Chris Lattner333864d2003-06-05 19:30:30 +0000940 }
Chris Lattnerb2acc512003-10-19 21:09:10 +0000941
942 static const unsigned CMPTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000943 X86::CMP8ri, X86::CMP16ri, X86::CMP32ri
Chris Lattnerb2acc512003-10-19 21:09:10 +0000944 };
945
Chris Lattneree352852004-02-29 07:22:16 +0000946 BuildMI(*MBB, IP, CMPTab[Class], 2).addReg(Op0r).addImm(Op1v);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000947 return OpNum;
Chris Lattnere80e6372004-04-06 16:02:27 +0000948 } else {
949 assert(Class == cLong && "Unknown integer class!");
950 unsigned LowCst = CI->getRawValue();
951 unsigned HiCst = CI->getRawValue() >> 32;
952 if (OpNum < 2) { // seteq, setne
953 unsigned LoTmp = Op0r;
954 if (LowCst != 0) {
955 LoTmp = makeAnotherReg(Type::IntTy);
956 BuildMI(*MBB, IP, X86::XOR32ri, 2, LoTmp).addReg(Op0r).addImm(LowCst);
957 }
958 unsigned HiTmp = Op0r+1;
959 if (HiCst != 0) {
960 HiTmp = makeAnotherReg(Type::IntTy);
Chris Lattner48c937e2004-04-06 17:34:50 +0000961 BuildMI(*MBB, IP, X86::XOR32ri, 2,HiTmp).addReg(Op0r+1).addImm(HiCst);
Chris Lattnere80e6372004-04-06 16:02:27 +0000962 }
963 unsigned FinalTmp = makeAnotherReg(Type::IntTy);
964 BuildMI(*MBB, IP, X86::OR32rr, 2, FinalTmp).addReg(LoTmp).addReg(HiTmp);
965 return OpNum;
Chris Lattner48c937e2004-04-06 17:34:50 +0000966 } else {
967 // Emit a sequence of code which compares the high and low parts once
968 // each, then uses a conditional move to handle the overflow case. For
969 // example, a setlt for long would generate code like this:
970 //
Chris Lattner9984fd02004-05-09 23:16:33 +0000971 // AL = lo(op1) < lo(op2) // Always unsigned comparison
972 // BL = hi(op1) < hi(op2) // Signedness depends on operands
973 // dest = hi(op1) == hi(op2) ? BL : AL;
Chris Lattner48c937e2004-04-06 17:34:50 +0000974 //
975
976 // FIXME: This would be much better if we had hierarchical register
977 // classes! Until then, hardcode registers so that we can deal with
978 // their aliases (because we don't have conditional byte moves).
979 //
980 BuildMI(*MBB, IP, X86::CMP32ri, 2).addReg(Op0r).addImm(LowCst);
981 BuildMI(*MBB, IP, SetCCOpcodeTab[0][OpNum], 0, X86::AL);
982 BuildMI(*MBB, IP, X86::CMP32ri, 2).addReg(Op0r+1).addImm(HiCst);
983 BuildMI(*MBB, IP, SetCCOpcodeTab[CompTy->isSigned()][OpNum], 0,X86::BL);
984 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::BH);
985 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::AH);
986 BuildMI(*MBB, IP, X86::CMOVE16rr, 2, X86::BX).addReg(X86::BX)
987 .addReg(X86::AX);
988 // NOTE: visitSetCondInst knows that the value is dumped into the BL
989 // register at this point for long values...
990 return OpNum;
Chris Lattnere80e6372004-04-06 16:02:27 +0000991 }
Chris Lattner333864d2003-06-05 19:30:30 +0000992 }
Chris Lattnere80e6372004-04-06 16:02:27 +0000993 }
Chris Lattner333864d2003-06-05 19:30:30 +0000994
Chris Lattner9f08a922004-02-03 18:54:04 +0000995 // Special case handling of comparison against +/- 0.0
996 if (ConstantFP *CFP = dyn_cast<ConstantFP>(Op1))
997 if (CFP->isExactlyValue(+0.0) || CFP->isExactlyValue(-0.0)) {
Chris Lattneree352852004-02-29 07:22:16 +0000998 BuildMI(*MBB, IP, X86::FTST, 1).addReg(Op0r);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000999 BuildMI(*MBB, IP, X86::FNSTSW8r, 0);
Chris Lattneree352852004-02-29 07:22:16 +00001000 BuildMI(*MBB, IP, X86::SAHF, 1);
Chris Lattner9f08a922004-02-03 18:54:04 +00001001 return OpNum;
1002 }
1003
Chris Lattner58c41fe2003-08-24 19:19:47 +00001004 unsigned Op1r = getReg(Op1, MBB, IP);
Chris Lattner3e130a22003-01-13 00:32:26 +00001005 switch (Class) {
1006 default: assert(0 && "Unknown type class!");
1007 // Emit: cmp <var1>, <var2> (do the comparison). We can
1008 // compare 8-bit with 8-bit, 16-bit with 16-bit, 32-bit with
1009 // 32-bit.
1010 case cByte:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001011 BuildMI(*MBB, IP, X86::CMP8rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +00001012 break;
1013 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001014 BuildMI(*MBB, IP, X86::CMP16rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +00001015 break;
1016 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001017 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +00001018 break;
1019 case cFP:
Chris Lattner01cdb1b2004-06-11 05:33:49 +00001020 emitUCOMr(MBB, IP, Op0r, Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +00001021 break;
1022
1023 case cLong:
1024 if (OpNum < 2) { // seteq, setne
1025 unsigned LoTmp = makeAnotherReg(Type::IntTy);
1026 unsigned HiTmp = makeAnotherReg(Type::IntTy);
1027 unsigned FinalTmp = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001028 BuildMI(*MBB, IP, X86::XOR32rr, 2, LoTmp).addReg(Op0r).addReg(Op1r);
1029 BuildMI(*MBB, IP, X86::XOR32rr, 2, HiTmp).addReg(Op0r+1).addReg(Op1r+1);
1030 BuildMI(*MBB, IP, X86::OR32rr, 2, FinalTmp).addReg(LoTmp).addReg(HiTmp);
Chris Lattner3e130a22003-01-13 00:32:26 +00001031 break; // Allow the sete or setne to be generated from flags set by OR
1032 } else {
1033 // Emit a sequence of code which compares the high and low parts once
1034 // each, then uses a conditional move to handle the overflow case. For
1035 // example, a setlt for long would generate code like this:
1036 //
1037 // AL = lo(op1) < lo(op2) // Signedness depends on operands
1038 // BL = hi(op1) < hi(op2) // Always unsigned comparison
Chris Lattner9984fd02004-05-09 23:16:33 +00001039 // dest = hi(op1) == hi(op2) ? BL : AL;
Chris Lattner3e130a22003-01-13 00:32:26 +00001040 //
1041
Chris Lattner6d40c192003-01-16 16:43:00 +00001042 // FIXME: This would be much better if we had hierarchical register
Chris Lattner3e130a22003-01-13 00:32:26 +00001043 // classes! Until then, hardcode registers so that we can deal with their
1044 // aliases (because we don't have conditional byte moves).
1045 //
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001046 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattneree352852004-02-29 07:22:16 +00001047 BuildMI(*MBB, IP, SetCCOpcodeTab[0][OpNum], 0, X86::AL);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001048 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r+1).addReg(Op1r+1);
Chris Lattneree352852004-02-29 07:22:16 +00001049 BuildMI(*MBB, IP, SetCCOpcodeTab[CompTy->isSigned()][OpNum], 0, X86::BL);
1050 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::BH);
1051 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::AH);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001052 BuildMI(*MBB, IP, X86::CMOVE16rr, 2, X86::BX).addReg(X86::BX)
Chris Lattneree352852004-02-29 07:22:16 +00001053 .addReg(X86::AX);
Chris Lattner6d40c192003-01-16 16:43:00 +00001054 // NOTE: visitSetCondInst knows that the value is dumped into the BL
1055 // register at this point for long values...
Chris Lattnerb2acc512003-10-19 21:09:10 +00001056 return OpNum;
Chris Lattner3e130a22003-01-13 00:32:26 +00001057 }
1058 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001059 return OpNum;
Chris Lattner6d40c192003-01-16 16:43:00 +00001060}
Chris Lattner3e130a22003-01-13 00:32:26 +00001061
Chris Lattner6d40c192003-01-16 16:43:00 +00001062/// SetCC instructions - Here we just emit boilerplate code to set a byte-sized
1063/// register, then move it to wherever the result should be.
1064///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001065void X86ISel::visitSetCondInst(SetCondInst &I) {
Chris Lattner307ecba2004-03-30 22:39:09 +00001066 if (canFoldSetCCIntoBranchOrSelect(&I))
1067 return; // Fold this into a branch or select.
Chris Lattner6d40c192003-01-16 16:43:00 +00001068
Chris Lattner6d40c192003-01-16 16:43:00 +00001069 unsigned DestReg = getReg(I);
Chris Lattner58c41fe2003-08-24 19:19:47 +00001070 MachineBasicBlock::iterator MII = BB->end();
1071 emitSetCCOperation(BB, MII, I.getOperand(0), I.getOperand(1), I.getOpcode(),
1072 DestReg);
1073}
Chris Lattner6d40c192003-01-16 16:43:00 +00001074
Chris Lattner58c41fe2003-08-24 19:19:47 +00001075/// emitSetCCOperation - Common code shared between visitSetCondInst and
1076/// constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +00001077///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001078void X86ISel::emitSetCCOperation(MachineBasicBlock *MBB,
1079 MachineBasicBlock::iterator IP,
1080 Value *Op0, Value *Op1, unsigned Opcode,
1081 unsigned TargetReg) {
Chris Lattner58c41fe2003-08-24 19:19:47 +00001082 unsigned OpNum = getSetCCNumber(Opcode);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001083 OpNum = EmitComparison(OpNum, Op0, Op1, MBB, IP);
Chris Lattner58c41fe2003-08-24 19:19:47 +00001084
Chris Lattnerb2acc512003-10-19 21:09:10 +00001085 const Type *CompTy = Op0->getType();
1086 unsigned CompClass = getClassB(CompTy);
1087 bool isSigned = CompTy->isSigned() && CompClass != cFP;
1088
1089 if (CompClass != cLong || OpNum < 2) {
Chris Lattner6d40c192003-01-16 16:43:00 +00001090 // Handle normal comparisons with a setcc instruction...
Chris Lattneree352852004-02-29 07:22:16 +00001091 BuildMI(*MBB, IP, SetCCOpcodeTab[isSigned][OpNum], 0, TargetReg);
Chris Lattner6d40c192003-01-16 16:43:00 +00001092 } else {
1093 // Handle long comparisons by copying the value which is already in BL into
1094 // the register we want...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001095 BuildMI(*MBB, IP, X86::MOV8rr, 1, TargetReg).addReg(X86::BL);
Chris Lattner6d40c192003-01-16 16:43:00 +00001096 }
Brian Gaeke1749d632002-11-07 17:59:21 +00001097}
Chris Lattner51b49a92002-11-02 19:45:49 +00001098
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001099void X86ISel::visitSelectInst(SelectInst &SI) {
Chris Lattner12d96a02004-03-30 21:22:00 +00001100 unsigned DestReg = getReg(SI);
1101 MachineBasicBlock::iterator MII = BB->end();
1102 emitSelectOperation(BB, MII, SI.getCondition(), SI.getTrueValue(),
1103 SI.getFalseValue(), DestReg);
1104}
1105
1106/// emitSelect - Common code shared between visitSelectInst and the constant
1107/// expression support.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001108void X86ISel::emitSelectOperation(MachineBasicBlock *MBB,
1109 MachineBasicBlock::iterator IP,
1110 Value *Cond, Value *TrueVal, Value *FalseVal,
1111 unsigned DestReg) {
Chris Lattner12d96a02004-03-30 21:22:00 +00001112 unsigned SelectClass = getClassB(TrueVal->getType());
1113
1114 // We don't support 8-bit conditional moves. If we have incoming constants,
1115 // transform them into 16-bit constants to avoid having a run-time conversion.
1116 if (SelectClass == cByte) {
1117 if (Constant *T = dyn_cast<Constant>(TrueVal))
1118 TrueVal = ConstantExpr::getCast(T, Type::ShortTy);
1119 if (Constant *F = dyn_cast<Constant>(FalseVal))
1120 FalseVal = ConstantExpr::getCast(F, Type::ShortTy);
1121 }
1122
Chris Lattner82c5a992004-04-13 21:56:09 +00001123 unsigned TrueReg = getReg(TrueVal, MBB, IP);
1124 unsigned FalseReg = getReg(FalseVal, MBB, IP);
1125 if (TrueReg == FalseReg) {
1126 static const unsigned Opcode[] = {
1127 X86::MOV8rr, X86::MOV16rr, X86::MOV32rr, X86::FpMOV, X86::MOV32rr
1128 };
1129 BuildMI(*MBB, IP, Opcode[SelectClass], 1, DestReg).addReg(TrueReg);
1130 if (SelectClass == cLong)
1131 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg+1).addReg(TrueReg+1);
1132 return;
1133 }
1134
Chris Lattner307ecba2004-03-30 22:39:09 +00001135 unsigned Opcode;
1136 if (SetCondInst *SCI = canFoldSetCCIntoBranchOrSelect(Cond)) {
1137 // We successfully folded the setcc into the select instruction.
1138
1139 unsigned OpNum = getSetCCNumber(SCI->getOpcode());
1140 OpNum = EmitComparison(OpNum, SCI->getOperand(0), SCI->getOperand(1), MBB,
1141 IP);
1142
1143 const Type *CompTy = SCI->getOperand(0)->getType();
1144 bool isSigned = CompTy->isSigned() && getClassB(CompTy) != cFP;
1145
1146 // LLVM -> X86 signed X86 unsigned
1147 // ----- ---------- ------------
1148 // seteq -> cmovNE cmovNE
1149 // setne -> cmovE cmovE
1150 // setlt -> cmovGE cmovAE
1151 // setge -> cmovL cmovB
1152 // setgt -> cmovLE cmovBE
1153 // setle -> cmovG cmovA
1154 // ----
1155 // cmovNS // Used by comparison with 0 optimization
1156 // cmovS
1157
1158 switch (SelectClass) {
Chris Lattner352eb482004-03-31 22:03:35 +00001159 default: assert(0 && "Unknown value class!");
1160 case cFP: {
1161 // Annoyingly, we don't have a full set of floating point conditional
1162 // moves. :(
1163 static const unsigned OpcodeTab[2][8] = {
1164 { X86::FCMOVNE, X86::FCMOVE, X86::FCMOVAE, X86::FCMOVB,
1165 X86::FCMOVBE, X86::FCMOVA, 0, 0 },
1166 { X86::FCMOVNE, X86::FCMOVE, 0, 0, 0, 0, 0, 0 },
1167 };
1168 Opcode = OpcodeTab[isSigned][OpNum];
1169
1170 // If opcode == 0, we hit a case that we don't support. Output a setcc
1171 // and compare the result against zero.
1172 if (Opcode == 0) {
1173 unsigned CompClass = getClassB(CompTy);
1174 unsigned CondReg;
1175 if (CompClass != cLong || OpNum < 2) {
1176 CondReg = makeAnotherReg(Type::BoolTy);
1177 // Handle normal comparisons with a setcc instruction...
1178 BuildMI(*MBB, IP, SetCCOpcodeTab[isSigned][OpNum], 0, CondReg);
1179 } else {
1180 // Long comparisons end up in the BL register.
1181 CondReg = X86::BL;
1182 }
1183
Chris Lattner68626c22004-03-31 22:22:36 +00001184 BuildMI(*MBB, IP, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
Chris Lattner352eb482004-03-31 22:03:35 +00001185 Opcode = X86::FCMOVE;
1186 }
1187 break;
1188 }
Chris Lattner307ecba2004-03-30 22:39:09 +00001189 case cByte:
1190 case cShort: {
1191 static const unsigned OpcodeTab[2][8] = {
1192 { X86::CMOVNE16rr, X86::CMOVE16rr, X86::CMOVAE16rr, X86::CMOVB16rr,
1193 X86::CMOVBE16rr, X86::CMOVA16rr, 0, 0 },
1194 { X86::CMOVNE16rr, X86::CMOVE16rr, X86::CMOVGE16rr, X86::CMOVL16rr,
1195 X86::CMOVLE16rr, X86::CMOVG16rr, X86::CMOVNS16rr, X86::CMOVS16rr },
1196 };
1197 Opcode = OpcodeTab[isSigned][OpNum];
1198 break;
1199 }
1200 case cInt:
1201 case cLong: {
1202 static const unsigned OpcodeTab[2][8] = {
1203 { X86::CMOVNE32rr, X86::CMOVE32rr, X86::CMOVAE32rr, X86::CMOVB32rr,
1204 X86::CMOVBE32rr, X86::CMOVA32rr, 0, 0 },
1205 { X86::CMOVNE32rr, X86::CMOVE32rr, X86::CMOVGE32rr, X86::CMOVL32rr,
1206 X86::CMOVLE32rr, X86::CMOVG32rr, X86::CMOVNS32rr, X86::CMOVS32rr },
1207 };
1208 Opcode = OpcodeTab[isSigned][OpNum];
1209 break;
1210 }
1211 }
1212 } else {
1213 // Get the value being branched on, and use it to set the condition codes.
1214 unsigned CondReg = getReg(Cond, MBB, IP);
Chris Lattner68626c22004-03-31 22:22:36 +00001215 BuildMI(*MBB, IP, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
Chris Lattner307ecba2004-03-30 22:39:09 +00001216 switch (SelectClass) {
Chris Lattner352eb482004-03-31 22:03:35 +00001217 default: assert(0 && "Unknown value class!");
1218 case cFP: Opcode = X86::FCMOVE; break;
Chris Lattner307ecba2004-03-30 22:39:09 +00001219 case cByte:
Chris Lattner352eb482004-03-31 22:03:35 +00001220 case cShort: Opcode = X86::CMOVE16rr; break;
Chris Lattner307ecba2004-03-30 22:39:09 +00001221 case cInt:
Chris Lattner352eb482004-03-31 22:03:35 +00001222 case cLong: Opcode = X86::CMOVE32rr; break;
Chris Lattner307ecba2004-03-30 22:39:09 +00001223 }
1224 }
Chris Lattner12d96a02004-03-30 21:22:00 +00001225
Chris Lattner12d96a02004-03-30 21:22:00 +00001226 unsigned RealDestReg = DestReg;
Chris Lattner12d96a02004-03-30 21:22:00 +00001227
Chris Lattner12d96a02004-03-30 21:22:00 +00001228
1229 // Annoyingly enough, X86 doesn't HAVE 8-bit conditional moves. Because of
1230 // this, we have to promote the incoming values to 16 bits, perform a 16-bit
1231 // cmove, then truncate the result.
1232 if (SelectClass == cByte) {
1233 DestReg = makeAnotherReg(Type::ShortTy);
1234 if (getClassB(TrueVal->getType()) == cByte) {
1235 // Promote the true value, by storing it into AL, and reading from AX.
1236 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::AL).addReg(TrueReg);
1237 BuildMI(*MBB, IP, X86::MOV8ri, 1, X86::AH).addImm(0);
1238 TrueReg = makeAnotherReg(Type::ShortTy);
1239 BuildMI(*MBB, IP, X86::MOV16rr, 1, TrueReg).addReg(X86::AX);
1240 }
1241 if (getClassB(FalseVal->getType()) == cByte) {
1242 // Promote the true value, by storing it into CL, and reading from CX.
1243 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(FalseReg);
1244 BuildMI(*MBB, IP, X86::MOV8ri, 1, X86::CH).addImm(0);
1245 FalseReg = makeAnotherReg(Type::ShortTy);
1246 BuildMI(*MBB, IP, X86::MOV16rr, 1, FalseReg).addReg(X86::CX);
1247 }
1248 }
1249
1250 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(TrueReg).addReg(FalseReg);
1251
1252 switch (SelectClass) {
1253 case cByte:
1254 // We did the computation with 16-bit registers. Truncate back to our
1255 // result by copying into AX then copying out AL.
1256 BuildMI(*MBB, IP, X86::MOV16rr, 1, X86::AX).addReg(DestReg);
1257 BuildMI(*MBB, IP, X86::MOV8rr, 1, RealDestReg).addReg(X86::AL);
1258 break;
1259 case cLong:
1260 // Move the upper half of the value as well.
1261 BuildMI(*MBB, IP, Opcode, 2,DestReg+1).addReg(TrueReg+1).addReg(FalseReg+1);
1262 break;
1263 }
1264}
Chris Lattner58c41fe2003-08-24 19:19:47 +00001265
1266
1267
Brian Gaekec2505982002-11-30 11:57:28 +00001268/// promote32 - Emit instructions to turn a narrow operand into a 32-bit-wide
1269/// operand, in the specified target register.
Misha Brukman538607f2004-03-01 23:53:11 +00001270///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001271void X86ISel::promote32(unsigned targetReg, const ValueRecord &VR) {
Chris Lattner9984fd02004-05-09 23:16:33 +00001272 bool isUnsigned = VR.Ty->isUnsigned() || VR.Ty == Type::BoolTy;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001273
Chris Lattner29bf0622004-04-06 01:21:00 +00001274 Value *Val = VR.Val;
1275 const Type *Ty = VR.Ty;
Chris Lattner502e36c2004-04-06 01:25:33 +00001276 if (Val) {
Chris Lattner29bf0622004-04-06 01:21:00 +00001277 if (Constant *C = dyn_cast<Constant>(Val)) {
1278 Val = ConstantExpr::getCast(C, Type::IntTy);
1279 Ty = Type::IntTy;
1280 }
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001281
Chris Lattner502e36c2004-04-06 01:25:33 +00001282 // If this is a simple constant, just emit a MOVri directly to avoid the
1283 // copy.
1284 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
1285 int TheVal = CI->getRawValue() & 0xFFFFFFFF;
Chris Lattner2b10b082004-05-12 16:35:04 +00001286 BuildMI(BB, X86::MOV32ri, 1, targetReg).addImm(TheVal);
Chris Lattner502e36c2004-04-06 01:25:33 +00001287 return;
1288 }
1289 }
1290
Chris Lattner29bf0622004-04-06 01:21:00 +00001291 // Make sure we have the register number for this value...
1292 unsigned Reg = Val ? getReg(Val) : VR.Reg;
1293
1294 switch (getClassB(Ty)) {
Chris Lattner94af4142002-12-25 05:13:53 +00001295 case cByte:
1296 // Extend value into target register (8->32)
1297 if (isUnsigned)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001298 BuildMI(BB, X86::MOVZX32rr8, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001299 else
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001300 BuildMI(BB, X86::MOVSX32rr8, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001301 break;
1302 case cShort:
1303 // Extend value into target register (16->32)
1304 if (isUnsigned)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001305 BuildMI(BB, X86::MOVZX32rr16, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001306 else
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001307 BuildMI(BB, X86::MOVSX32rr16, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001308 break;
1309 case cInt:
1310 // Move value into target register (32->32)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001311 BuildMI(BB, X86::MOV32rr, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001312 break;
1313 default:
1314 assert(0 && "Unpromotable operand class in promote32");
1315 }
Brian Gaekec2505982002-11-30 11:57:28 +00001316}
Chris Lattnerc5291f52002-10-27 21:16:59 +00001317
Chris Lattner72614082002-10-25 22:55:53 +00001318/// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such,
1319/// we have the following possibilities:
1320///
1321/// ret void: No return value, simply emit a 'ret' instruction
1322/// ret sbyte, ubyte : Extend value into EAX and return
1323/// ret short, ushort: Extend value into EAX and return
1324/// ret int, uint : Move value into EAX and return
1325/// ret pointer : Move value into EAX and return
Chris Lattner06925362002-11-17 21:56:38 +00001326/// ret long, ulong : Move value into EAX/EDX and return
1327/// ret float/double : Top of FP stack
Chris Lattner72614082002-10-25 22:55:53 +00001328///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001329void X86ISel::visitReturnInst(ReturnInst &I) {
Chris Lattner94af4142002-12-25 05:13:53 +00001330 if (I.getNumOperands() == 0) {
1331 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
1332 return;
1333 }
1334
1335 Value *RetVal = I.getOperand(0);
Chris Lattner3e130a22003-01-13 00:32:26 +00001336 switch (getClassB(RetVal->getType())) {
Chris Lattner94af4142002-12-25 05:13:53 +00001337 case cByte: // integral return values: extend or move into EAX and return
1338 case cShort:
1339 case cInt:
Chris Lattner29bf0622004-04-06 01:21:00 +00001340 promote32(X86::EAX, ValueRecord(RetVal));
Chris Lattnerdbd73722003-05-06 21:32:22 +00001341 // Declare that EAX is live on exit
Chris Lattnerc2489032003-05-07 19:21:28 +00001342 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
Chris Lattner94af4142002-12-25 05:13:53 +00001343 break;
Chris Lattner29bf0622004-04-06 01:21:00 +00001344 case cFP: { // Floats & Doubles: Return in ST(0)
1345 unsigned RetReg = getReg(RetVal);
Chris Lattner3e130a22003-01-13 00:32:26 +00001346 BuildMI(BB, X86::FpSETRESULT, 1).addReg(RetReg);
Chris Lattnerdbd73722003-05-06 21:32:22 +00001347 // Declare that top-of-stack is live on exit
Chris Lattnerc2489032003-05-07 19:21:28 +00001348 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
Chris Lattner94af4142002-12-25 05:13:53 +00001349 break;
Chris Lattner29bf0622004-04-06 01:21:00 +00001350 }
1351 case cLong: {
1352 unsigned RetReg = getReg(RetVal);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001353 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(RetReg);
1354 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RetReg+1);
Chris Lattnerdbd73722003-05-06 21:32:22 +00001355 // Declare that EAX & EDX are live on exit
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001356 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
1357 .addReg(X86::ESP);
Chris Lattner3e130a22003-01-13 00:32:26 +00001358 break;
Chris Lattner29bf0622004-04-06 01:21:00 +00001359 }
Chris Lattner94af4142002-12-25 05:13:53 +00001360 default:
Chris Lattner3e130a22003-01-13 00:32:26 +00001361 visitInstruction(I);
Chris Lattner94af4142002-12-25 05:13:53 +00001362 }
Chris Lattner43189d12002-11-17 20:07:45 +00001363 // Emit a 'ret' instruction
Chris Lattner94af4142002-12-25 05:13:53 +00001364 BuildMI(BB, X86::RET, 0);
Chris Lattner72614082002-10-25 22:55:53 +00001365}
1366
Chris Lattner55f6fab2003-01-16 18:07:23 +00001367// getBlockAfter - Return the basic block which occurs lexically after the
1368// specified one.
1369static inline BasicBlock *getBlockAfter(BasicBlock *BB) {
1370 Function::iterator I = BB; ++I; // Get iterator to next block
1371 return I != BB->getParent()->end() ? &*I : 0;
1372}
1373
Chris Lattner51b49a92002-11-02 19:45:49 +00001374/// visitBranchInst - Handle conditional and unconditional branches here. Note
1375/// that since code layout is frozen at this point, that if we are trying to
1376/// jump to a block that is the immediate successor of the current block, we can
Chris Lattner6d40c192003-01-16 16:43:00 +00001377/// just make a fall-through (but we don't currently).
Chris Lattner51b49a92002-11-02 19:45:49 +00001378///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001379void X86ISel::visitBranchInst(BranchInst &BI) {
Brian Gaekeea9ca672004-04-28 04:19:37 +00001380 // Update machine-CFG edges
1381 BB->addSuccessor (MBBMap[BI.getSuccessor(0)]);
1382 if (BI.isConditional())
1383 BB->addSuccessor (MBBMap[BI.getSuccessor(1)]);
1384
Chris Lattner55f6fab2003-01-16 18:07:23 +00001385 BasicBlock *NextBB = getBlockAfter(BI.getParent()); // BB after current one
1386
1387 if (!BI.isConditional()) { // Unconditional branch?
Chris Lattnercf93cdd2004-01-30 22:13:44 +00001388 if (BI.getSuccessor(0) != NextBB)
Brian Gaeke9f088e42004-05-14 06:54:56 +00001389 BuildMI(BB, X86::JMP, 1).addMBB(MBBMap[BI.getSuccessor(0)]);
Chris Lattner6d40c192003-01-16 16:43:00 +00001390 return;
1391 }
1392
1393 // See if we can fold the setcc into the branch itself...
Chris Lattner307ecba2004-03-30 22:39:09 +00001394 SetCondInst *SCI = canFoldSetCCIntoBranchOrSelect(BI.getCondition());
Chris Lattner6d40c192003-01-16 16:43:00 +00001395 if (SCI == 0) {
1396 // Nope, cannot fold setcc into this branch. Emit a branch on a condition
1397 // computed some other way...
Chris Lattner065faeb2002-12-28 20:24:02 +00001398 unsigned condReg = getReg(BI.getCondition());
Chris Lattner68626c22004-03-31 22:22:36 +00001399 BuildMI(BB, X86::TEST8rr, 2).addReg(condReg).addReg(condReg);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001400 if (BI.getSuccessor(1) == NextBB) {
1401 if (BI.getSuccessor(0) != NextBB)
Brian Gaeke9f088e42004-05-14 06:54:56 +00001402 BuildMI(BB, X86::JNE, 1).addMBB(MBBMap[BI.getSuccessor(0)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001403 } else {
Brian Gaeke9f088e42004-05-14 06:54:56 +00001404 BuildMI(BB, X86::JE, 1).addMBB(MBBMap[BI.getSuccessor(1)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001405
1406 if (BI.getSuccessor(0) != NextBB)
Brian Gaeke9f088e42004-05-14 06:54:56 +00001407 BuildMI(BB, X86::JMP, 1).addMBB(MBBMap[BI.getSuccessor(0)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001408 }
Chris Lattner6d40c192003-01-16 16:43:00 +00001409 return;
Chris Lattner94af4142002-12-25 05:13:53 +00001410 }
Chris Lattner6d40c192003-01-16 16:43:00 +00001411
1412 unsigned OpNum = getSetCCNumber(SCI->getOpcode());
Chris Lattner58c41fe2003-08-24 19:19:47 +00001413 MachineBasicBlock::iterator MII = BB->end();
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001414 OpNum = EmitComparison(OpNum, SCI->getOperand(0), SCI->getOperand(1), BB,MII);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001415
1416 const Type *CompTy = SCI->getOperand(0)->getType();
1417 bool isSigned = CompTy->isSigned() && getClassB(CompTy) != cFP;
Chris Lattner6d40c192003-01-16 16:43:00 +00001418
Chris Lattnerb2acc512003-10-19 21:09:10 +00001419
Chris Lattner6d40c192003-01-16 16:43:00 +00001420 // LLVM -> X86 signed X86 unsigned
1421 // ----- ---------- ------------
1422 // seteq -> je je
1423 // setne -> jne jne
1424 // setlt -> jl jb
Chris Lattner55f6fab2003-01-16 18:07:23 +00001425 // setge -> jge jae
Chris Lattner6d40c192003-01-16 16:43:00 +00001426 // setgt -> jg ja
1427 // setle -> jle jbe
Chris Lattnerb2acc512003-10-19 21:09:10 +00001428 // ----
1429 // js // Used by comparison with 0 optimization
1430 // jns
1431
1432 static const unsigned OpcodeTab[2][8] = {
1433 { X86::JE, X86::JNE, X86::JB, X86::JAE, X86::JA, X86::JBE, 0, 0 },
1434 { X86::JE, X86::JNE, X86::JL, X86::JGE, X86::JG, X86::JLE,
1435 X86::JS, X86::JNS },
Chris Lattner6d40c192003-01-16 16:43:00 +00001436 };
1437
Chris Lattner55f6fab2003-01-16 18:07:23 +00001438 if (BI.getSuccessor(0) != NextBB) {
Brian Gaeke9f088e42004-05-14 06:54:56 +00001439 BuildMI(BB, OpcodeTab[isSigned][OpNum], 1)
1440 .addMBB(MBBMap[BI.getSuccessor(0)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001441 if (BI.getSuccessor(1) != NextBB)
Brian Gaeke9f088e42004-05-14 06:54:56 +00001442 BuildMI(BB, X86::JMP, 1).addMBB(MBBMap[BI.getSuccessor(1)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001443 } else {
1444 // Change to the inverse condition...
1445 if (BI.getSuccessor(1) != NextBB) {
1446 OpNum ^= 1;
Brian Gaeke9f088e42004-05-14 06:54:56 +00001447 BuildMI(BB, OpcodeTab[isSigned][OpNum], 1)
1448 .addMBB(MBBMap[BI.getSuccessor(1)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001449 }
1450 }
Chris Lattner2df035b2002-11-02 19:27:56 +00001451}
1452
Chris Lattner3e130a22003-01-13 00:32:26 +00001453
1454/// doCall - This emits an abstract call instruction, setting up the arguments
1455/// and the return value as appropriate. For the actual function call itself,
1456/// it inserts the specified CallMI instruction into the stream.
1457///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001458void X86ISel::doCall(const ValueRecord &Ret, MachineInstr *CallMI,
1459 const std::vector<ValueRecord> &Args) {
Chris Lattner065faeb2002-12-28 20:24:02 +00001460 // Count how many bytes are to be pushed on the stack...
1461 unsigned NumBytes = 0;
Misha Brukman0d2cf3a2002-12-04 19:22:53 +00001462
Chris Lattner3e130a22003-01-13 00:32:26 +00001463 if (!Args.empty()) {
1464 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1465 switch (getClassB(Args[i].Ty)) {
Chris Lattner065faeb2002-12-28 20:24:02 +00001466 case cByte: case cShort: case cInt:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001467 NumBytes += 4; break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001468 case cLong:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001469 NumBytes += 8; break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001470 case cFP:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001471 NumBytes += Args[i].Ty == Type::FloatTy ? 4 : 8;
1472 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001473 default: assert(0 && "Unknown class!");
1474 }
1475
1476 // Adjust the stack pointer for the new arguments...
Chris Lattneree352852004-02-29 07:22:16 +00001477 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(NumBytes);
Chris Lattner065faeb2002-12-28 20:24:02 +00001478
1479 // Arguments go on the stack in reverse order, as specified by the ABI.
1480 unsigned ArgOffset = 0;
Chris Lattner3e130a22003-01-13 00:32:26 +00001481 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattnerce6096f2004-03-01 02:34:08 +00001482 unsigned ArgReg;
Chris Lattner3e130a22003-01-13 00:32:26 +00001483 switch (getClassB(Args[i].Ty)) {
Chris Lattner065faeb2002-12-28 20:24:02 +00001484 case cByte:
Chris Lattner2b10b082004-05-12 16:35:04 +00001485 if (Args[i].Val && isa<ConstantBool>(Args[i].Val)) {
1486 addRegOffset(BuildMI(BB, X86::MOV32mi, 5), X86::ESP, ArgOffset)
1487 .addImm(Args[i].Val == ConstantBool::True);
1488 break;
1489 }
1490 // FALL THROUGH
Chris Lattner21585222004-03-01 02:42:43 +00001491 case cShort:
1492 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1493 // Zero/Sign extend constant, then stuff into memory.
1494 ConstantInt *Val = cast<ConstantInt>(Args[i].Val);
1495 Val = cast<ConstantInt>(ConstantExpr::getCast(Val, Type::IntTy));
1496 addRegOffset(BuildMI(BB, X86::MOV32mi, 5), X86::ESP, ArgOffset)
1497 .addImm(Val->getRawValue() & 0xFFFFFFFF);
1498 } else {
1499 // Promote arg to 32 bits wide into a temporary register...
1500 ArgReg = makeAnotherReg(Type::UIntTy);
1501 promote32(ArgReg, Args[i]);
1502 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1503 X86::ESP, ArgOffset).addReg(ArgReg);
1504 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001505 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001506 case cInt:
Chris Lattner21585222004-03-01 02:42:43 +00001507 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1508 unsigned Val = cast<ConstantInt>(Args[i].Val)->getRawValue();
1509 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1510 X86::ESP, ArgOffset).addImm(Val);
Chris Lattnerb7cb9ff2004-05-13 15:26:48 +00001511 } else if (Args[i].Val && isa<ConstantPointerNull>(Args[i].Val)) {
1512 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1513 X86::ESP, ArgOffset).addImm(0);
Chris Lattner21585222004-03-01 02:42:43 +00001514 } else {
1515 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1516 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1517 X86::ESP, ArgOffset).addReg(ArgReg);
1518 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001519 break;
Chris Lattner3e130a22003-01-13 00:32:26 +00001520 case cLong:
Chris Lattner92900a62004-04-06 03:23:00 +00001521 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1522 uint64_t Val = cast<ConstantInt>(Args[i].Val)->getRawValue();
1523 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1524 X86::ESP, ArgOffset).addImm(Val & ~0U);
1525 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1526 X86::ESP, ArgOffset+4).addImm(Val >> 32ULL);
1527 } else {
1528 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1529 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1530 X86::ESP, ArgOffset).addReg(ArgReg);
1531 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1532 X86::ESP, ArgOffset+4).addReg(ArgReg+1);
1533 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001534 ArgOffset += 4; // 8 byte entry, not 4.
1535 break;
1536
Chris Lattner065faeb2002-12-28 20:24:02 +00001537 case cFP:
Chris Lattnerce6096f2004-03-01 02:34:08 +00001538 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001539 if (Args[i].Ty == Type::FloatTy) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001540 addRegOffset(BuildMI(BB, X86::FST32m, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001541 X86::ESP, ArgOffset).addReg(ArgReg);
1542 } else {
1543 assert(Args[i].Ty == Type::DoubleTy && "Unknown FP type!");
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001544 addRegOffset(BuildMI(BB, X86::FST64m, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001545 X86::ESP, ArgOffset).addReg(ArgReg);
1546 ArgOffset += 4; // 8 byte entry, not 4.
1547 }
1548 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001549
Chris Lattner3e130a22003-01-13 00:32:26 +00001550 default: assert(0 && "Unknown class!");
Chris Lattner065faeb2002-12-28 20:24:02 +00001551 }
1552 ArgOffset += 4;
Chris Lattner94af4142002-12-25 05:13:53 +00001553 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001554 } else {
Chris Lattneree352852004-02-29 07:22:16 +00001555 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(0);
Chris Lattner94af4142002-12-25 05:13:53 +00001556 }
Chris Lattner6e49a4b2002-12-13 14:13:27 +00001557
Chris Lattner3e130a22003-01-13 00:32:26 +00001558 BB->push_back(CallMI);
Misha Brukman0d2cf3a2002-12-04 19:22:53 +00001559
Chris Lattneree352852004-02-29 07:22:16 +00001560 BuildMI(BB, X86::ADJCALLSTACKUP, 1).addImm(NumBytes);
Chris Lattnera3243642002-12-04 23:45:28 +00001561
1562 // If there is a return value, scavenge the result from the location the call
1563 // leaves it in...
1564 //
Chris Lattner3e130a22003-01-13 00:32:26 +00001565 if (Ret.Ty != Type::VoidTy) {
1566 unsigned DestClass = getClassB(Ret.Ty);
1567 switch (DestClass) {
Brian Gaeke20244b72002-12-12 15:33:40 +00001568 case cByte:
1569 case cShort:
1570 case cInt: {
1571 // Integral results are in %eax, or the appropriate portion
1572 // thereof.
1573 static const unsigned regRegMove[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001574 X86::MOV8rr, X86::MOV16rr, X86::MOV32rr
Brian Gaeke20244b72002-12-12 15:33:40 +00001575 };
1576 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX };
Chris Lattner3e130a22003-01-13 00:32:26 +00001577 BuildMI(BB, regRegMove[DestClass], 1, Ret.Reg).addReg(AReg[DestClass]);
Chris Lattner4fa1acc2002-12-04 23:50:28 +00001578 break;
Brian Gaeke20244b72002-12-12 15:33:40 +00001579 }
Chris Lattner94af4142002-12-25 05:13:53 +00001580 case cFP: // Floating-point return values live in %ST(0)
Chris Lattner3e130a22003-01-13 00:32:26 +00001581 BuildMI(BB, X86::FpGETRESULT, 1, Ret.Reg);
Brian Gaeke20244b72002-12-12 15:33:40 +00001582 break;
Chris Lattner3e130a22003-01-13 00:32:26 +00001583 case cLong: // Long values are left in EDX:EAX
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001584 BuildMI(BB, X86::MOV32rr, 1, Ret.Reg).addReg(X86::EAX);
1585 BuildMI(BB, X86::MOV32rr, 1, Ret.Reg+1).addReg(X86::EDX);
Chris Lattner3e130a22003-01-13 00:32:26 +00001586 break;
1587 default: assert(0 && "Unknown class!");
Chris Lattner4fa1acc2002-12-04 23:50:28 +00001588 }
Chris Lattnera3243642002-12-04 23:45:28 +00001589 }
Brian Gaekefa8d5712002-11-22 11:07:01 +00001590}
Chris Lattner2df035b2002-11-02 19:27:56 +00001591
Chris Lattner3e130a22003-01-13 00:32:26 +00001592
1593/// visitCallInst - Push args on stack and do a procedure call instruction.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001594void X86ISel::visitCallInst(CallInst &CI) {
Chris Lattner3e130a22003-01-13 00:32:26 +00001595 MachineInstr *TheCall;
1596 if (Function *F = CI.getCalledFunction()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001597 // Is it an intrinsic function call?
Brian Gaeked0fde302003-11-11 22:41:34 +00001598 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001599 visitIntrinsicCall(ID, CI); // Special intrinsics are not handled here
1600 return;
1601 }
1602
Chris Lattner3e130a22003-01-13 00:32:26 +00001603 // Emit a CALL instruction with PC-relative displacement.
1604 TheCall = BuildMI(X86::CALLpcrel32, 1).addGlobalAddress(F, true);
1605 } else { // Emit an indirect call...
1606 unsigned Reg = getReg(CI.getCalledValue());
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001607 TheCall = BuildMI(X86::CALL32r, 1).addReg(Reg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001608 }
1609
1610 std::vector<ValueRecord> Args;
1611 for (unsigned i = 1, e = CI.getNumOperands(); i != e; ++i)
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001612 Args.push_back(ValueRecord(CI.getOperand(i)));
Chris Lattner3e130a22003-01-13 00:32:26 +00001613
1614 unsigned DestReg = CI.getType() != Type::VoidTy ? getReg(CI) : 0;
1615 doCall(ValueRecord(DestReg, CI.getType()), TheCall, Args);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001616}
Chris Lattner3e130a22003-01-13 00:32:26 +00001617
Chris Lattner44827152003-12-28 09:47:19 +00001618/// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
1619/// function, lowering any calls to unknown intrinsic functions into the
1620/// equivalent LLVM code.
Misha Brukman538607f2004-03-01 23:53:11 +00001621///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001622void X86ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) {
Chris Lattner44827152003-12-28 09:47:19 +00001623 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1624 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
1625 if (CallInst *CI = dyn_cast<CallInst>(I++))
1626 if (Function *F = CI->getCalledFunction())
1627 switch (F->getIntrinsicID()) {
Chris Lattneraed386e2003-12-28 09:53:23 +00001628 case Intrinsic::not_intrinsic:
Chris Lattner317201d2004-03-13 00:24:00 +00001629 case Intrinsic::vastart:
1630 case Intrinsic::vacopy:
1631 case Intrinsic::vaend:
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001632 case Intrinsic::returnaddress:
1633 case Intrinsic::frameaddress:
Chris Lattner915e5e52004-02-12 17:53:22 +00001634 case Intrinsic::memcpy:
Chris Lattner2a0f2242004-02-14 04:46:05 +00001635 case Intrinsic::memset:
Chris Lattnerdc572442004-06-15 21:36:44 +00001636 case Intrinsic::isunordered:
John Criswell4ffff9e2004-04-08 20:31:47 +00001637 case Intrinsic::readport:
1638 case Intrinsic::writeport:
Chris Lattner44827152003-12-28 09:47:19 +00001639 // We directly implement these intrinsics
1640 break;
John Criswelle5a4c152004-04-13 22:13:14 +00001641 case Intrinsic::readio: {
1642 // On X86, memory operations are in-order. Lower this intrinsic
1643 // into a volatile load.
1644 Instruction *Before = CI->getPrev();
Chris Lattnerb4fe76c2004-06-11 04:31:10 +00001645 LoadInst * LI = new LoadInst(CI->getOperand(1), "", true, CI);
1646 CI->replaceAllUsesWith(LI);
1647 BB->getInstList().erase(CI);
John Criswelle5a4c152004-04-13 22:13:14 +00001648 break;
1649 }
1650 case Intrinsic::writeio: {
1651 // On X86, memory operations are in-order. Lower this intrinsic
1652 // into a volatile store.
1653 Instruction *Before = CI->getPrev();
Chris Lattnerb4fe76c2004-06-11 04:31:10 +00001654 StoreInst *LI = new StoreInst(CI->getOperand(1),
1655 CI->getOperand(2), true, CI);
1656 CI->replaceAllUsesWith(LI);
1657 BB->getInstList().erase(CI);
John Criswelle5a4c152004-04-13 22:13:14 +00001658 break;
1659 }
Chris Lattner44827152003-12-28 09:47:19 +00001660 default:
1661 // All other intrinsic calls we must lower.
1662 Instruction *Before = CI->getPrev();
Chris Lattnerf70e0c22003-12-28 21:23:38 +00001663 TM.getIntrinsicLowering().LowerIntrinsicCall(CI);
Chris Lattner44827152003-12-28 09:47:19 +00001664 if (Before) { // Move iterator to instruction after call
Chris Lattnerb4fe76c2004-06-11 04:31:10 +00001665 I = Before; ++I;
Chris Lattner44827152003-12-28 09:47:19 +00001666 } else {
1667 I = BB->begin();
1668 }
1669 }
Chris Lattner44827152003-12-28 09:47:19 +00001670}
1671
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001672void X86ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001673 unsigned TmpReg1, TmpReg2;
1674 switch (ID) {
Chris Lattner5634b9f2004-03-13 00:24:52 +00001675 case Intrinsic::vastart:
Chris Lattnereca195e2003-05-08 19:44:13 +00001676 // Get the address of the first vararg value...
Chris Lattner73815062003-10-18 05:56:40 +00001677 TmpReg1 = getReg(CI);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001678 addFrameReference(BuildMI(BB, X86::LEA32r, 5, TmpReg1), VarArgsFrameIndex);
Chris Lattnereca195e2003-05-08 19:44:13 +00001679 return;
1680
Chris Lattner5634b9f2004-03-13 00:24:52 +00001681 case Intrinsic::vacopy:
Chris Lattner73815062003-10-18 05:56:40 +00001682 TmpReg1 = getReg(CI);
1683 TmpReg2 = getReg(CI.getOperand(1));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001684 BuildMI(BB, X86::MOV32rr, 1, TmpReg1).addReg(TmpReg2);
Chris Lattnereca195e2003-05-08 19:44:13 +00001685 return;
Chris Lattner5634b9f2004-03-13 00:24:52 +00001686 case Intrinsic::vaend: return; // Noop on X86
Chris Lattnereca195e2003-05-08 19:44:13 +00001687
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001688 case Intrinsic::returnaddress:
1689 case Intrinsic::frameaddress:
1690 TmpReg1 = getReg(CI);
1691 if (cast<Constant>(CI.getOperand(1))->isNullValue()) {
1692 if (ID == Intrinsic::returnaddress) {
1693 // Just load the return address
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001694 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, TmpReg1),
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001695 ReturnAddressIndex);
1696 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001697 addFrameReference(BuildMI(BB, X86::LEA32r, 4, TmpReg1),
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001698 ReturnAddressIndex, -4);
1699 }
1700 } else {
1701 // Values other than zero are not implemented yet.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001702 BuildMI(BB, X86::MOV32ri, 1, TmpReg1).addImm(0);
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001703 }
1704 return;
1705
Chris Lattnerdc572442004-06-15 21:36:44 +00001706 case Intrinsic::isunordered:
1707 TmpReg1 = getReg(CI.getOperand(1));
1708 TmpReg2 = getReg(CI.getOperand(2));
1709 emitUCOMr(BB, BB->end(), TmpReg2, TmpReg1);
1710 TmpReg2 = getReg(CI);
1711 BuildMI(BB, X86::SETPr, 0, TmpReg2);
1712 return;
1713
Chris Lattner915e5e52004-02-12 17:53:22 +00001714 case Intrinsic::memcpy: {
1715 assert(CI.getNumOperands() == 5 && "Illegal llvm.memcpy call!");
1716 unsigned Align = 1;
1717 if (ConstantInt *AlignC = dyn_cast<ConstantInt>(CI.getOperand(4))) {
1718 Align = AlignC->getRawValue();
1719 if (Align == 0) Align = 1;
1720 }
1721
1722 // Turn the byte code into # iterations
Chris Lattner915e5e52004-02-12 17:53:22 +00001723 unsigned CountReg;
Chris Lattner2a0f2242004-02-14 04:46:05 +00001724 unsigned Opcode;
Chris Lattner915e5e52004-02-12 17:53:22 +00001725 switch (Align & 3) {
1726 case 2: // WORD aligned
Chris Lattner07122832004-02-13 23:36:47 +00001727 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
1728 CountReg = getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/2));
1729 } else {
1730 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001731 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001732 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
Chris Lattner07122832004-02-13 23:36:47 +00001733 }
Chris Lattner2a0f2242004-02-14 04:46:05 +00001734 Opcode = X86::REP_MOVSW;
Chris Lattner915e5e52004-02-12 17:53:22 +00001735 break;
1736 case 0: // DWORD aligned
Chris Lattner07122832004-02-13 23:36:47 +00001737 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
1738 CountReg = getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/4));
1739 } else {
1740 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001741 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001742 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
Chris Lattner07122832004-02-13 23:36:47 +00001743 }
Chris Lattner2a0f2242004-02-14 04:46:05 +00001744 Opcode = X86::REP_MOVSD;
Chris Lattner915e5e52004-02-12 17:53:22 +00001745 break;
Chris Lattner8dd8d262004-02-26 01:20:02 +00001746 default: // BYTE aligned
Chris Lattner07122832004-02-13 23:36:47 +00001747 CountReg = getReg(CI.getOperand(3));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001748 Opcode = X86::REP_MOVSB;
Chris Lattner915e5e52004-02-12 17:53:22 +00001749 break;
1750 }
1751
1752 // No matter what the alignment is, we put the source in ESI, the
1753 // destination in EDI, and the count in ECX.
1754 TmpReg1 = getReg(CI.getOperand(1));
1755 TmpReg2 = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001756 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
1757 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
1758 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001759 BuildMI(BB, Opcode, 0);
1760 return;
1761 }
1762 case Intrinsic::memset: {
1763 assert(CI.getNumOperands() == 5 && "Illegal llvm.memset call!");
1764 unsigned Align = 1;
1765 if (ConstantInt *AlignC = dyn_cast<ConstantInt>(CI.getOperand(4))) {
1766 Align = AlignC->getRawValue();
1767 if (Align == 0) Align = 1;
Chris Lattner915e5e52004-02-12 17:53:22 +00001768 }
1769
Chris Lattner2a0f2242004-02-14 04:46:05 +00001770 // Turn the byte code into # iterations
Chris Lattner2a0f2242004-02-14 04:46:05 +00001771 unsigned CountReg;
1772 unsigned Opcode;
1773 if (ConstantInt *ValC = dyn_cast<ConstantInt>(CI.getOperand(2))) {
1774 unsigned Val = ValC->getRawValue() & 255;
1775
1776 // If the value is a constant, then we can potentially use larger copies.
1777 switch (Align & 3) {
1778 case 2: // WORD aligned
1779 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
Chris Lattner300d0ed2004-02-14 06:00:36 +00001780 CountReg =getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/2));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001781 } else {
1782 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001783 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001784 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001785 }
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001786 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001787 Opcode = X86::REP_STOSW;
1788 break;
1789 case 0: // DWORD aligned
1790 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
Chris Lattner300d0ed2004-02-14 06:00:36 +00001791 CountReg =getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/4));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001792 } else {
1793 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001794 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001795 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001796 }
1797 Val = (Val << 8) | Val;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001798 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001799 Opcode = X86::REP_STOSD;
1800 break;
Chris Lattner8dd8d262004-02-26 01:20:02 +00001801 default: // BYTE aligned
Chris Lattner2a0f2242004-02-14 04:46:05 +00001802 CountReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001803 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001804 Opcode = X86::REP_STOSB;
1805 break;
1806 }
1807 } else {
1808 // If it's not a constant value we are storing, just fall back. We could
1809 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
1810 unsigned ValReg = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001811 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001812 CountReg = getReg(CI.getOperand(3));
1813 Opcode = X86::REP_STOSB;
1814 }
1815
1816 // No matter what the alignment is, we put the source in ESI, the
1817 // destination in EDI, and the count in ECX.
1818 TmpReg1 = getReg(CI.getOperand(1));
1819 //TmpReg2 = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001820 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
1821 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001822 BuildMI(BB, Opcode, 0);
Chris Lattner915e5e52004-02-12 17:53:22 +00001823 return;
1824 }
1825
Chris Lattner87e18de2004-04-13 17:20:37 +00001826 case Intrinsic::readport: {
1827 // First, determine that the size of the operand falls within the acceptable
1828 // range for this architecture.
John Criswell4ffff9e2004-04-08 20:31:47 +00001829 //
Chris Lattner87e18de2004-04-13 17:20:37 +00001830 if (getClassB(CI.getOperand(1)->getType()) != cShort) {
John Criswellca6ea0f2004-04-08 22:39:13 +00001831 std::cerr << "llvm.readport: Address size is not 16 bits\n";
Chris Lattner87e18de2004-04-13 17:20:37 +00001832 exit(1);
John Criswellca6ea0f2004-04-08 22:39:13 +00001833 }
John Criswell4ffff9e2004-04-08 20:31:47 +00001834
John Criswell4ffff9e2004-04-08 20:31:47 +00001835 // Now, move the I/O port address into the DX register and use the IN
1836 // instruction to get the input data.
1837 //
Chris Lattner87e18de2004-04-13 17:20:37 +00001838 unsigned Class = getClass(CI.getCalledFunction()->getReturnType());
1839 unsigned DestReg = getReg(CI);
John Criswell4ffff9e2004-04-08 20:31:47 +00001840
Chris Lattner87e18de2004-04-13 17:20:37 +00001841 // If the port is a single-byte constant, use the immediate form.
1842 if (ConstantInt *C = dyn_cast<ConstantInt>(CI.getOperand(1)))
1843 if ((C->getRawValue() & 255) == C->getRawValue()) {
1844 switch (Class) {
1845 case cByte:
1846 BuildMI(BB, X86::IN8ri, 1).addImm((unsigned char)C->getRawValue());
1847 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AL);
1848 return;
1849 case cShort:
1850 BuildMI(BB, X86::IN16ri, 1).addImm((unsigned char)C->getRawValue());
1851 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AX);
1852 return;
1853 case cInt:
1854 BuildMI(BB, X86::IN32ri, 1).addImm((unsigned char)C->getRawValue());
1855 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::EAX);
1856 return;
1857 }
1858 }
1859
1860 unsigned Reg = getReg(CI.getOperand(1));
1861 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
1862 switch (Class) {
1863 case cByte:
1864 BuildMI(BB, X86::IN8rr, 0);
1865 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AL);
1866 break;
1867 case cShort:
1868 BuildMI(BB, X86::IN16rr, 0);
1869 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AX);
1870 break;
1871 case cInt:
1872 BuildMI(BB, X86::IN32rr, 0);
1873 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::EAX);
1874 break;
1875 default:
1876 std::cerr << "Cannot do input on this data type";
John Criswellca6ea0f2004-04-08 22:39:13 +00001877 exit (1);
1878 }
John Criswell4ffff9e2004-04-08 20:31:47 +00001879 return;
Chris Lattner87e18de2004-04-13 17:20:37 +00001880 }
John Criswell4ffff9e2004-04-08 20:31:47 +00001881
Chris Lattner87e18de2004-04-13 17:20:37 +00001882 case Intrinsic::writeport: {
1883 // First, determine that the size of the operand falls within the
1884 // acceptable range for this architecture.
1885 if (getClass(CI.getOperand(2)->getType()) != cShort) {
1886 std::cerr << "llvm.writeport: Address size is not 16 bits\n";
1887 exit(1);
1888 }
1889
1890 unsigned Class = getClassB(CI.getOperand(1)->getType());
1891 unsigned ValReg = getReg(CI.getOperand(1));
1892 switch (Class) {
1893 case cByte:
1894 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
1895 break;
1896 case cShort:
1897 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(ValReg);
1898 break;
1899 case cInt:
1900 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(ValReg);
1901 break;
1902 default:
1903 std::cerr << "llvm.writeport: invalid data type for X86 target";
1904 exit(1);
1905 }
1906
1907
1908 // If the port is a single-byte constant, use the immediate form.
1909 if (ConstantInt *C = dyn_cast<ConstantInt>(CI.getOperand(2)))
1910 if ((C->getRawValue() & 255) == C->getRawValue()) {
1911 static const unsigned O[] = { X86::OUT8ir, X86::OUT16ir, X86::OUT32ir };
1912 BuildMI(BB, O[Class], 1).addImm((unsigned char)C->getRawValue());
1913 return;
1914 }
1915
1916 // Otherwise, move the I/O port address into the DX register and the value
1917 // to write into the AL/AX/EAX register.
1918 static const unsigned Opc[] = { X86::OUT8rr, X86::OUT16rr, X86::OUT32rr };
1919 unsigned Reg = getReg(CI.getOperand(2));
1920 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
1921 BuildMI(BB, Opc[Class], 0);
1922 return;
1923 }
1924
Chris Lattner44827152003-12-28 09:47:19 +00001925 default: assert(0 && "Error: unknown intrinsics should have been lowered!");
Chris Lattnereca195e2003-05-08 19:44:13 +00001926 }
1927}
1928
Chris Lattner7dee5da2004-03-08 01:58:35 +00001929static bool isSafeToFoldLoadIntoInstruction(LoadInst &LI, Instruction &User) {
1930 if (LI.getParent() != User.getParent())
1931 return false;
1932 BasicBlock::iterator It = &LI;
1933 // Check all of the instructions between the load and the user. We should
1934 // really use alias analysis here, but for now we just do something simple.
1935 for (++It; It != BasicBlock::iterator(&User); ++It) {
1936 switch (It->getOpcode()) {
Chris Lattner85c84e72004-03-18 06:29:54 +00001937 case Instruction::Free:
Chris Lattner7dee5da2004-03-08 01:58:35 +00001938 case Instruction::Store:
1939 case Instruction::Call:
1940 case Instruction::Invoke:
1941 return false;
Chris Lattner133dbb12004-04-12 03:02:48 +00001942 case Instruction::Load:
1943 if (cast<LoadInst>(It)->isVolatile() && LI.isVolatile())
1944 return false;
1945 break;
Chris Lattner7dee5da2004-03-08 01:58:35 +00001946 }
1947 }
1948 return true;
1949}
1950
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001951/// visitSimpleBinary - Implement simple binary operators for integral types...
1952/// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or, 4 for
1953/// Xor.
Misha Brukman538607f2004-03-01 23:53:11 +00001954///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001955void X86ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001956 unsigned DestReg = getReg(B);
1957 MachineBasicBlock::iterator MI = BB->end();
Chris Lattner721d2d42004-03-08 01:18:36 +00001958 Value *Op0 = B.getOperand(0), *Op1 = B.getOperand(1);
Chris Lattnerc81e6ba2004-05-10 15:15:55 +00001959 unsigned Class = getClassB(B.getType());
Chris Lattner721d2d42004-03-08 01:18:36 +00001960
Chris Lattner7dee5da2004-03-08 01:58:35 +00001961 // Special case: op Reg, load [mem]
Chris Lattnerc81e6ba2004-05-10 15:15:55 +00001962 if (isa<LoadInst>(Op0) && !isa<LoadInst>(Op1) && Class != cLong &&
Chris Lattnerccd97962004-06-17 22:15:25 +00001963 Op0->hasOneUse() &&
Chris Lattnerc81e6ba2004-05-10 15:15:55 +00001964 isSafeToFoldLoadIntoInstruction(*cast<LoadInst>(Op0), B))
Chris Lattner7dee5da2004-03-08 01:58:35 +00001965 if (!B.swapOperands())
1966 std::swap(Op0, Op1); // Make sure any loads are in the RHS.
1967
Chris Lattnerccd97962004-06-17 22:15:25 +00001968 if (isa<LoadInst>(Op1) && Class != cLong && Op1->hasOneUse() &&
Chris Lattner7dee5da2004-03-08 01:58:35 +00001969 isSafeToFoldLoadIntoInstruction(*cast<LoadInst>(Op1), B)) {
1970
Chris Lattner95157f72004-04-11 22:05:45 +00001971 unsigned Opcode;
1972 if (Class != cFP) {
1973 static const unsigned OpcodeTab[][3] = {
1974 // Arithmetic operators
1975 { X86::ADD8rm, X86::ADD16rm, X86::ADD32rm }, // ADD
1976 { X86::SUB8rm, X86::SUB16rm, X86::SUB32rm }, // SUB
1977
1978 // Bitwise operators
1979 { X86::AND8rm, X86::AND16rm, X86::AND32rm }, // AND
1980 { X86:: OR8rm, X86:: OR16rm, X86:: OR32rm }, // OR
1981 { X86::XOR8rm, X86::XOR16rm, X86::XOR32rm }, // XOR
1982 };
1983 Opcode = OpcodeTab[OperatorClass][Class];
1984 } else {
1985 static const unsigned OpcodeTab[][2] = {
1986 { X86::FADD32m, X86::FADD64m }, // ADD
1987 { X86::FSUB32m, X86::FSUB64m }, // SUB
1988 };
1989 const Type *Ty = Op0->getType();
1990 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
1991 Opcode = OpcodeTab[OperatorClass][Ty == Type::DoubleTy];
1992 }
Chris Lattner7dee5da2004-03-08 01:58:35 +00001993
Chris Lattner7dee5da2004-03-08 01:58:35 +00001994 unsigned Op0r = getReg(Op0);
Chris Lattner9f1b5312004-05-13 15:12:43 +00001995 if (AllocaInst *AI =
1996 dyn_castFixedAlloca(cast<LoadInst>(Op1)->getOperand(0))) {
1997 unsigned FI = getFixedSizedAllocaFI(AI);
1998 addFrameReference(BuildMI(BB, Opcode, 5, DestReg).addReg(Op0r), FI);
1999
2000 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002001 X86AddressMode AM;
2002 getAddressingMode(cast<LoadInst>(Op1)->getOperand(0), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002003
Reid Spencerfc989e12004-08-30 00:13:26 +00002004 addFullAddress(BuildMI(BB, Opcode, 5, DestReg).addReg(Op0r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002005 }
Chris Lattner7dee5da2004-03-08 01:58:35 +00002006 return;
2007 }
2008
Chris Lattner95157f72004-04-11 22:05:45 +00002009 // If this is a floating point subtract, check to see if we can fold the first
2010 // operand in.
2011 if (Class == cFP && OperatorClass == 1 &&
2012 isa<LoadInst>(Op0) &&
2013 isSafeToFoldLoadIntoInstruction(*cast<LoadInst>(Op0), B)) {
2014 const Type *Ty = Op0->getType();
2015 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
2016 unsigned Opcode = Ty == Type::FloatTy ? X86::FSUBR32m : X86::FSUBR64m;
2017
Chris Lattner95157f72004-04-11 22:05:45 +00002018 unsigned Op1r = getReg(Op1);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002019 if (AllocaInst *AI =
2020 dyn_castFixedAlloca(cast<LoadInst>(Op0)->getOperand(0))) {
2021 unsigned FI = getFixedSizedAllocaFI(AI);
2022 addFrameReference(BuildMI(BB, Opcode, 5, DestReg).addReg(Op1r), FI);
2023 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002024 X86AddressMode AM;
2025 getAddressingMode(cast<LoadInst>(Op0)->getOperand(0), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002026
Reid Spencerfc989e12004-08-30 00:13:26 +00002027 addFullAddress(BuildMI(BB, Opcode, 5, DestReg).addReg(Op1r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002028 }
Chris Lattner95157f72004-04-11 22:05:45 +00002029 return;
2030 }
2031
Chris Lattner721d2d42004-03-08 01:18:36 +00002032 emitSimpleBinaryOperation(BB, MI, Op0, Op1, OperatorClass, DestReg);
Chris Lattnerb515f6d2003-05-08 20:49:25 +00002033}
Chris Lattner3e130a22003-01-13 00:32:26 +00002034
Chris Lattner6621ed92004-04-11 21:23:56 +00002035
2036/// emitBinaryFPOperation - This method handles emission of floating point
2037/// Add (0), Sub (1), Mul (2), and Div (3) operations.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002038void X86ISel::emitBinaryFPOperation(MachineBasicBlock *BB,
2039 MachineBasicBlock::iterator IP,
2040 Value *Op0, Value *Op1,
2041 unsigned OperatorClass, unsigned DestReg) {
Chris Lattner6621ed92004-04-11 21:23:56 +00002042 // Special case: op Reg, <const fp>
2043 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1))
2044 if (!Op1C->isExactlyValue(+0.0) && !Op1C->isExactlyValue(+1.0)) {
2045 // Create a constant pool entry for this constant.
2046 MachineConstantPool *CP = F->getConstantPool();
2047 unsigned CPI = CP->getConstantPoolIndex(Op1C);
2048 const Type *Ty = Op1->getType();
2049
2050 static const unsigned OpcodeTab[][4] = {
2051 { X86::FADD32m, X86::FSUB32m, X86::FMUL32m, X86::FDIV32m }, // Float
2052 { X86::FADD64m, X86::FSUB64m, X86::FMUL64m, X86::FDIV64m }, // Double
2053 };
2054
2055 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
2056 unsigned Opcode = OpcodeTab[Ty != Type::FloatTy][OperatorClass];
2057 unsigned Op0r = getReg(Op0, BB, IP);
2058 addConstantPoolReference(BuildMI(*BB, IP, Opcode, 5,
2059 DestReg).addReg(Op0r), CPI);
2060 return;
2061 }
2062
Chris Lattner13c07fe2004-04-12 00:12:04 +00002063 // Special case: R1 = op <const fp>, R2
Chris Lattner6621ed92004-04-11 21:23:56 +00002064 if (ConstantFP *CFP = dyn_cast<ConstantFP>(Op0))
2065 if (CFP->isExactlyValue(-0.0) && OperatorClass == 1) {
2066 // -0.0 - X === -X
2067 unsigned op1Reg = getReg(Op1, BB, IP);
2068 BuildMI(*BB, IP, X86::FCHS, 1, DestReg).addReg(op1Reg);
2069 return;
2070 } else if (!CFP->isExactlyValue(+0.0) && !CFP->isExactlyValue(+1.0)) {
Chris Lattner13c07fe2004-04-12 00:12:04 +00002071 // R1 = op CST, R2 --> R1 = opr R2, CST
Chris Lattner6621ed92004-04-11 21:23:56 +00002072
2073 // Create a constant pool entry for this constant.
2074 MachineConstantPool *CP = F->getConstantPool();
2075 unsigned CPI = CP->getConstantPoolIndex(CFP);
2076 const Type *Ty = CFP->getType();
2077
2078 static const unsigned OpcodeTab[][4] = {
2079 { X86::FADD32m, X86::FSUBR32m, X86::FMUL32m, X86::FDIVR32m }, // Float
2080 { X86::FADD64m, X86::FSUBR64m, X86::FMUL64m, X86::FDIVR64m }, // Double
2081 };
2082
2083 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2084 unsigned Opcode = OpcodeTab[Ty != Type::FloatTy][OperatorClass];
2085 unsigned Op1r = getReg(Op1, BB, IP);
2086 addConstantPoolReference(BuildMI(*BB, IP, Opcode, 5,
2087 DestReg).addReg(Op1r), CPI);
2088 return;
2089 }
2090
2091 // General case.
2092 static const unsigned OpcodeTab[4] = {
2093 X86::FpADD, X86::FpSUB, X86::FpMUL, X86::FpDIV
2094 };
2095
2096 unsigned Opcode = OpcodeTab[OperatorClass];
2097 unsigned Op0r = getReg(Op0, BB, IP);
2098 unsigned Op1r = getReg(Op1, BB, IP);
2099 BuildMI(*BB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r);
2100}
2101
Chris Lattnerb2acc512003-10-19 21:09:10 +00002102/// emitSimpleBinaryOperation - Implement simple binary operators for integral
2103/// types... OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for
2104/// Or, 4 for Xor.
Chris Lattner68aad932002-11-02 20:13:22 +00002105///
Chris Lattnerb515f6d2003-05-08 20:49:25 +00002106/// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
2107/// and constant expression support.
Chris Lattnerb2acc512003-10-19 21:09:10 +00002108///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002109void X86ISel::emitSimpleBinaryOperation(MachineBasicBlock *MBB,
2110 MachineBasicBlock::iterator IP,
2111 Value *Op0, Value *Op1,
2112 unsigned OperatorClass,
2113 unsigned DestReg) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +00002114 unsigned Class = getClassB(Op0->getType());
Chris Lattnerb2acc512003-10-19 21:09:10 +00002115
Chris Lattner6621ed92004-04-11 21:23:56 +00002116 if (Class == cFP) {
2117 assert(OperatorClass < 2 && "No logical ops for FP!");
2118 emitBinaryFPOperation(MBB, IP, Op0, Op1, OperatorClass, DestReg);
2119 return;
2120 }
2121
Chris Lattner48b0c972004-04-11 20:26:20 +00002122 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0))
Chris Lattner667ea022004-06-18 00:50:37 +00002123 if (OperatorClass == 1) {
Chris Lattner48b0c972004-04-11 20:26:20 +00002124 static unsigned const NEGTab[] = {
2125 X86::NEG8r, X86::NEG16r, X86::NEG32r, 0, X86::NEG32r
2126 };
Chris Lattner667ea022004-06-18 00:50:37 +00002127
2128 // sub 0, X -> neg X
2129 if (CI->isNullValue()) {
2130 unsigned op1Reg = getReg(Op1, MBB, IP);
2131 BuildMI(*MBB, IP, NEGTab[Class], 1, DestReg).addReg(op1Reg);
Chris Lattner48b0c972004-04-11 20:26:20 +00002132
Chris Lattner667ea022004-06-18 00:50:37 +00002133 if (Class == cLong) {
2134 // We just emitted: Dl = neg Sl
2135 // Now emit : T = addc Sh, 0
2136 // : Dh = neg T
2137 unsigned T = makeAnotherReg(Type::IntTy);
2138 BuildMI(*MBB, IP, X86::ADC32ri, 2, T).addReg(op1Reg+1).addImm(0);
2139 BuildMI(*MBB, IP, X86::NEG32r, 1, DestReg+1).addReg(T);
2140 }
2141 return;
2142 } else if (Op1->hasOneUse() && Class != cLong) {
2143 // sub C, X -> tmp = neg X; DestReg = add tmp, C. This is better
2144 // than copying C into a temporary register, because of register
2145 // pressure (tmp and destreg can share a register.
2146 static unsigned const ADDRITab[] = {
2147 X86::ADD8ri, X86::ADD16ri, X86::ADD32ri, 0, X86::ADD32ri
2148 };
2149 unsigned op1Reg = getReg(Op1, MBB, IP);
2150 unsigned Tmp = makeAnotherReg(Op0->getType());
2151 BuildMI(*MBB, IP, NEGTab[Class], 1, Tmp).addReg(op1Reg);
Chris Lattner30483732004-06-20 07:49:54 +00002152 BuildMI(*MBB, IP, ADDRITab[Class], 2,
2153 DestReg).addReg(Tmp).addImm(CI->getRawValue());
Chris Lattner667ea022004-06-18 00:50:37 +00002154 return;
Chris Lattnerb2acc512003-10-19 21:09:10 +00002155 }
Chris Lattner48b0c972004-04-11 20:26:20 +00002156 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002157
Chris Lattner48b0c972004-04-11 20:26:20 +00002158 // Special case: op Reg, <const int>
2159 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner721d2d42004-03-08 01:18:36 +00002160 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002161
Chris Lattner721d2d42004-03-08 01:18:36 +00002162 // xor X, -1 -> not X
2163 if (OperatorClass == 4 && Op1C->isAllOnesValue()) {
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002164 static unsigned const NOTTab[] = {
2165 X86::NOT8r, X86::NOT16r, X86::NOT32r, 0, X86::NOT32r
2166 };
Chris Lattner721d2d42004-03-08 01:18:36 +00002167 BuildMI(*MBB, IP, NOTTab[Class], 1, DestReg).addReg(Op0r);
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002168 if (Class == cLong) // Invert the top part too
2169 BuildMI(*MBB, IP, X86::NOT32r, 1, DestReg+1).addReg(Op0r+1);
Chris Lattner721d2d42004-03-08 01:18:36 +00002170 return;
2171 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002172
Chris Lattner721d2d42004-03-08 01:18:36 +00002173 // add X, -1 -> dec X
Chris Lattner06521672004-04-06 03:36:57 +00002174 if (OperatorClass == 0 && Op1C->isAllOnesValue() && Class != cLong) {
2175 // Note that we can't use dec for 64-bit decrements, because it does not
2176 // set the carry flag!
2177 static unsigned const DECTab[] = { X86::DEC8r, X86::DEC16r, X86::DEC32r };
Chris Lattner721d2d42004-03-08 01:18:36 +00002178 BuildMI(*MBB, IP, DECTab[Class], 1, DestReg).addReg(Op0r);
2179 return;
2180 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002181
Chris Lattner721d2d42004-03-08 01:18:36 +00002182 // add X, 1 -> inc X
Chris Lattner06521672004-04-06 03:36:57 +00002183 if (OperatorClass == 0 && Op1C->equalsInt(1) && Class != cLong) {
2184 // Note that we can't use inc for 64-bit increments, because it does not
2185 // set the carry flag!
2186 static unsigned const INCTab[] = { X86::INC8r, X86::INC16r, X86::INC32r };
Alkis Evlogimenosbee8a092004-04-02 18:11:32 +00002187 BuildMI(*MBB, IP, INCTab[Class], 1, DestReg).addReg(Op0r);
Chris Lattner721d2d42004-03-08 01:18:36 +00002188 return;
2189 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002190
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002191 static const unsigned OpcodeTab[][5] = {
Chris Lattner721d2d42004-03-08 01:18:36 +00002192 // Arithmetic operators
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002193 { X86::ADD8ri, X86::ADD16ri, X86::ADD32ri, 0, X86::ADD32ri }, // ADD
2194 { X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, X86::SUB32ri }, // SUB
Chris Lattnerb2acc512003-10-19 21:09:10 +00002195
Chris Lattner721d2d42004-03-08 01:18:36 +00002196 // Bitwise operators
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002197 { X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, X86::AND32ri }, // AND
2198 { X86:: OR8ri, X86:: OR16ri, X86:: OR32ri, 0, X86::OR32ri }, // OR
2199 { X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, X86::XOR32ri }, // XOR
Chris Lattner721d2d42004-03-08 01:18:36 +00002200 };
2201
Chris Lattner721d2d42004-03-08 01:18:36 +00002202 unsigned Opcode = OpcodeTab[OperatorClass][Class];
Chris Lattner33f7fa32004-04-06 03:15:53 +00002203 unsigned Op1l = cast<ConstantInt>(Op1C)->getRawValue();
Chris Lattner721d2d42004-03-08 01:18:36 +00002204
Chris Lattner33f7fa32004-04-06 03:15:53 +00002205 if (Class != cLong) {
2206 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addImm(Op1l);
2207 return;
Chris Lattner6621ed92004-04-11 21:23:56 +00002208 }
2209
2210 // If this is a long value and the high or low bits have a special
2211 // property, emit some special cases.
2212 unsigned Op1h = cast<ConstantInt>(Op1C)->getRawValue() >> 32LL;
2213
2214 // If the constant is zero in the low 32-bits, just copy the low part
2215 // across and apply the normal 32-bit operation to the high parts. There
2216 // will be no carry or borrow into the top.
2217 if (Op1l == 0) {
2218 if (OperatorClass != 2) // All but and...
2219 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg).addReg(Op0r);
2220 else
2221 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
2222 BuildMI(*MBB, IP, OpcodeTab[OperatorClass][cLong], 2, DestReg+1)
2223 .addReg(Op0r+1).addImm(Op1h);
Chris Lattner33f7fa32004-04-06 03:15:53 +00002224 return;
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002225 }
Chris Lattner6621ed92004-04-11 21:23:56 +00002226
2227 // If this is a logical operation and the top 32-bits are zero, just
2228 // operate on the lower 32.
2229 if (Op1h == 0 && OperatorClass > 1) {
2230 BuildMI(*MBB, IP, OpcodeTab[OperatorClass][cLong], 2, DestReg)
2231 .addReg(Op0r).addImm(Op1l);
2232 if (OperatorClass != 2) // All but and
2233 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg+1).addReg(Op0r+1);
2234 else
2235 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
2236 return;
2237 }
2238
2239 // TODO: We could handle lots of other special cases here, such as AND'ing
2240 // with 0xFFFFFFFF00000000 -> noop, etc.
2241
2242 // Otherwise, code generate the full operation with a constant.
2243 static const unsigned TopTab[] = {
2244 X86::ADC32ri, X86::SBB32ri, X86::AND32ri, X86::OR32ri, X86::XOR32ri
2245 };
2246
2247 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addImm(Op1l);
2248 BuildMI(*MBB, IP, TopTab[OperatorClass], 2, DestReg+1)
2249 .addReg(Op0r+1).addImm(Op1h);
2250 return;
Chris Lattner721d2d42004-03-08 01:18:36 +00002251 }
2252
2253 // Finally, handle the general case now.
Chris Lattner7ba92302004-04-06 02:13:25 +00002254 static const unsigned OpcodeTab[][5] = {
Chris Lattner721d2d42004-03-08 01:18:36 +00002255 // Arithmetic operators
Chris Lattner6621ed92004-04-11 21:23:56 +00002256 { X86::ADD8rr, X86::ADD16rr, X86::ADD32rr, 0, X86::ADD32rr }, // ADD
2257 { X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, 0, X86::SUB32rr }, // SUB
Chris Lattner721d2d42004-03-08 01:18:36 +00002258
Chris Lattnerb2acc512003-10-19 21:09:10 +00002259 // Bitwise operators
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002260 { X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, X86::AND32rr }, // AND
2261 { X86:: OR8rr, X86:: OR16rr, X86:: OR32rr, 0, X86:: OR32rr }, // OR
2262 { X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, X86::XOR32rr }, // XOR
Chris Lattnerb2acc512003-10-19 21:09:10 +00002263 };
Chris Lattner721d2d42004-03-08 01:18:36 +00002264
Chris Lattnerb2acc512003-10-19 21:09:10 +00002265 unsigned Opcode = OpcodeTab[OperatorClass][Class];
Chris Lattner721d2d42004-03-08 01:18:36 +00002266 unsigned Op0r = getReg(Op0, MBB, IP);
2267 unsigned Op1r = getReg(Op1, MBB, IP);
2268 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r);
2269
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002270 if (Class == cLong) { // Handle the upper 32 bits of long values...
Chris Lattner721d2d42004-03-08 01:18:36 +00002271 static const unsigned TopTab[] = {
2272 X86::ADC32rr, X86::SBB32rr, X86::AND32rr, X86::OR32rr, X86::XOR32rr
2273 };
2274 BuildMI(*MBB, IP, TopTab[OperatorClass], 2,
2275 DestReg+1).addReg(Op0r+1).addReg(Op1r+1);
2276 }
Chris Lattnere2954c82002-11-02 20:04:26 +00002277}
2278
Chris Lattner3e130a22003-01-13 00:32:26 +00002279/// doMultiply - Emit appropriate instructions to multiply together the
2280/// registers op0Reg and op1Reg, and put the result in DestReg. The type of the
2281/// result should be given as DestTy.
2282///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002283void X86ISel::doMultiply(MachineBasicBlock *MBB,
2284 MachineBasicBlock::iterator MBBI,
2285 unsigned DestReg, const Type *DestTy,
2286 unsigned op0Reg, unsigned op1Reg) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002287 unsigned Class = getClass(DestTy);
Chris Lattner94af4142002-12-25 05:13:53 +00002288 switch (Class) {
Chris Lattner0f1c4612003-06-21 17:16:58 +00002289 case cInt:
2290 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002291 BuildMI(*MBB, MBBI, Class == cInt ? X86::IMUL32rr:X86::IMUL16rr, 2, DestReg)
Chris Lattner0f1c4612003-06-21 17:16:58 +00002292 .addReg(op0Reg).addReg(op1Reg);
2293 return;
2294 case cByte:
2295 // Must use the MUL instruction, which forces use of AL...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002296 BuildMI(*MBB, MBBI, X86::MOV8rr, 1, X86::AL).addReg(op0Reg);
2297 BuildMI(*MBB, MBBI, X86::MUL8r, 1).addReg(op1Reg);
2298 BuildMI(*MBB, MBBI, X86::MOV8rr, 1, DestReg).addReg(X86::AL);
Chris Lattner0f1c4612003-06-21 17:16:58 +00002299 return;
Chris Lattner94af4142002-12-25 05:13:53 +00002300 default:
Chris Lattner3e130a22003-01-13 00:32:26 +00002301 case cLong: assert(0 && "doMultiply cannot operate on LONG values!");
Chris Lattner94af4142002-12-25 05:13:53 +00002302 }
Brian Gaeke20244b72002-12-12 15:33:40 +00002303}
2304
Chris Lattnerb2acc512003-10-19 21:09:10 +00002305// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
2306// returns zero when the input is not exactly a power of two.
2307static unsigned ExactLog2(unsigned Val) {
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002308 if (Val == 0 || (Val & (Val-1))) return 0;
Chris Lattnerb2acc512003-10-19 21:09:10 +00002309 unsigned Count = 0;
2310 while (Val != 1) {
Chris Lattnerb2acc512003-10-19 21:09:10 +00002311 Val >>= 1;
2312 ++Count;
2313 }
2314 return Count+1;
2315}
2316
Chris Lattner462fa822004-04-11 20:56:28 +00002317
2318/// doMultiplyConst - This function is specialized to efficiently codegen an 8,
2319/// 16, or 32-bit integer multiply by a constant.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002320void X86ISel::doMultiplyConst(MachineBasicBlock *MBB,
2321 MachineBasicBlock::iterator IP,
2322 unsigned DestReg, const Type *DestTy,
2323 unsigned op0Reg, unsigned ConstRHS) {
Chris Lattner6ab06d52004-04-06 04:55:43 +00002324 static const unsigned MOVrrTab[] = {X86::MOV8rr, X86::MOV16rr, X86::MOV32rr};
2325 static const unsigned MOVriTab[] = {X86::MOV8ri, X86::MOV16ri, X86::MOV32ri};
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002326 static const unsigned ADDrrTab[] = {X86::ADD8rr, X86::ADD16rr, X86::ADD32rr};
Chris Lattner596b97f2004-07-19 23:47:21 +00002327 static const unsigned NEGrTab[] = {X86::NEG8r , X86::NEG16r , X86::NEG32r };
Chris Lattner6ab06d52004-04-06 04:55:43 +00002328
Chris Lattnerb2acc512003-10-19 21:09:10 +00002329 unsigned Class = getClass(DestTy);
Chris Lattner596b97f2004-07-19 23:47:21 +00002330 unsigned TmpReg;
Chris Lattnerb2acc512003-10-19 21:09:10 +00002331
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002332 // Handle special cases here.
2333 switch (ConstRHS) {
Chris Lattner596b97f2004-07-19 23:47:21 +00002334 case -2:
2335 TmpReg = makeAnotherReg(DestTy);
2336 BuildMI(*MBB, IP, NEGrTab[Class], 1, TmpReg).addReg(op0Reg);
2337 BuildMI(*MBB, IP, ADDrrTab[Class], 1,DestReg).addReg(TmpReg).addReg(TmpReg);
2338 return;
2339 case -1:
2340 BuildMI(*MBB, IP, NEGrTab[Class], 1, DestReg).addReg(op0Reg);
2341 return;
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002342 case 0:
Chris Lattner6ab06d52004-04-06 04:55:43 +00002343 BuildMI(*MBB, IP, MOVriTab[Class], 1, DestReg).addImm(0);
2344 return;
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002345 case 1:
Chris Lattner6ab06d52004-04-06 04:55:43 +00002346 BuildMI(*MBB, IP, MOVrrTab[Class], 1, DestReg).addReg(op0Reg);
2347 return;
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002348 case 2:
2349 BuildMI(*MBB, IP, ADDrrTab[Class], 1,DestReg).addReg(op0Reg).addReg(op0Reg);
2350 return;
2351 case 3:
2352 case 5:
2353 case 9:
2354 if (Class == cInt) {
Reid Spencerfc989e12004-08-30 00:13:26 +00002355 X86AddressMode AM;
2356 AM.BaseType = X86AddressMode::RegBase;
2357 AM.Base.Reg = op0Reg;
2358 AM.Scale = ConstRHS-1;
2359 AM.IndexReg = op0Reg;
2360 AM.Disp = 0;
2361 addFullAddress(BuildMI(*MBB, IP, X86::LEA32r, 5, DestReg), AM);
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002362 return;
2363 }
Chris Lattner596b97f2004-07-19 23:47:21 +00002364 case -3:
2365 case -5:
2366 case -9:
2367 if (Class == cInt) {
2368 TmpReg = makeAnotherReg(DestTy);
Reid Spencerfc989e12004-08-30 00:13:26 +00002369 X86AddressMode AM;
2370 AM.BaseType = X86AddressMode::RegBase;
2371 AM.Base.Reg = op0Reg;
2372 AM.Scale = -ConstRHS-1;
2373 AM.IndexReg = op0Reg;
2374 AM.Disp = 0;
2375 addFullAddress(BuildMI(*MBB, IP, X86::LEA32r, 5, TmpReg), AM);
Chris Lattner596b97f2004-07-19 23:47:21 +00002376 BuildMI(*MBB, IP, NEGrTab[Class], 1, DestReg).addReg(TmpReg);
2377 return;
2378 }
Chris Lattner6ab06d52004-04-06 04:55:43 +00002379 }
2380
Chris Lattnerb2acc512003-10-19 21:09:10 +00002381 // If the element size is exactly a power of 2, use a shift to get it.
2382 if (unsigned Shift = ExactLog2(ConstRHS)) {
2383 switch (Class) {
2384 default: assert(0 && "Unknown class for this function!");
2385 case cByte:
Chris Lattner596b97f2004-07-19 23:47:21 +00002386 BuildMI(*MBB, IP, X86::SHL8ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002387 return;
2388 case cShort:
Chris Lattner596b97f2004-07-19 23:47:21 +00002389 BuildMI(*MBB, IP, X86::SHL16ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002390 return;
2391 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002392 BuildMI(*MBB, IP, X86::SHL32ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002393 return;
2394 }
2395 }
Chris Lattner596b97f2004-07-19 23:47:21 +00002396
2397 // If the element size is a negative power of 2, use a shift/neg to get it.
2398 if (unsigned Shift = ExactLog2(-ConstRHS)) {
2399 TmpReg = makeAnotherReg(DestTy);
2400 BuildMI(*MBB, IP, NEGrTab[Class], 1, TmpReg).addReg(op0Reg);
2401 switch (Class) {
2402 default: assert(0 && "Unknown class for this function!");
2403 case cByte:
2404 BuildMI(*MBB, IP, X86::SHL8ri,2, DestReg).addReg(TmpReg).addImm(Shift-1);
2405 return;
2406 case cShort:
2407 BuildMI(*MBB, IP, X86::SHL16ri,2, DestReg).addReg(TmpReg).addImm(Shift-1);
2408 return;
2409 case cInt:
2410 BuildMI(*MBB, IP, X86::SHL32ri,2, DestReg).addReg(TmpReg).addImm(Shift-1);
2411 return;
2412 }
2413 }
Chris Lattnerc01d1232003-10-20 03:42:58 +00002414
2415 if (Class == cShort) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002416 BuildMI(*MBB, IP, X86::IMUL16rri,2,DestReg).addReg(op0Reg).addImm(ConstRHS);
Chris Lattnerc01d1232003-10-20 03:42:58 +00002417 return;
2418 } else if (Class == cInt) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002419 BuildMI(*MBB, IP, X86::IMUL32rri,2,DestReg).addReg(op0Reg).addImm(ConstRHS);
Chris Lattnerc01d1232003-10-20 03:42:58 +00002420 return;
2421 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002422
2423 // Most general case, emit a normal multiply...
Chris Lattner596b97f2004-07-19 23:47:21 +00002424 TmpReg = makeAnotherReg(DestTy);
Chris Lattneree352852004-02-29 07:22:16 +00002425 BuildMI(*MBB, IP, MOVriTab[Class], 1, TmpReg).addImm(ConstRHS);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002426
2427 // Emit a MUL to multiply the register holding the index by
2428 // elementSize, putting the result in OffsetReg.
2429 doMultiply(MBB, IP, DestReg, DestTy, op0Reg, TmpReg);
2430}
2431
Chris Lattnerca9671d2002-11-02 20:28:58 +00002432/// visitMul - Multiplies are not simple binary operators because they must deal
2433/// with the EAX register explicitly.
2434///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002435void X86ISel::visitMul(BinaryOperator &I) {
Chris Lattner462fa822004-04-11 20:56:28 +00002436 unsigned ResultReg = getReg(I);
2437
Chris Lattner95157f72004-04-11 22:05:45 +00002438 Value *Op0 = I.getOperand(0);
2439 Value *Op1 = I.getOperand(1);
2440
2441 // Fold loads into floating point multiplies.
2442 if (getClass(Op0->getType()) == cFP) {
2443 if (isa<LoadInst>(Op0) && !isa<LoadInst>(Op1))
2444 if (!I.swapOperands())
2445 std::swap(Op0, Op1); // Make sure any loads are in the RHS.
2446 if (LoadInst *LI = dyn_cast<LoadInst>(Op1))
2447 if (isSafeToFoldLoadIntoInstruction(*LI, I)) {
2448 const Type *Ty = Op0->getType();
2449 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2450 unsigned Opcode = Ty == Type::FloatTy ? X86::FMUL32m : X86::FMUL64m;
2451
Chris Lattner95157f72004-04-11 22:05:45 +00002452 unsigned Op0r = getReg(Op0);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002453 if (AllocaInst *AI = dyn_castFixedAlloca(LI->getOperand(0))) {
2454 unsigned FI = getFixedSizedAllocaFI(AI);
2455 addFrameReference(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op0r), FI);
2456 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002457 X86AddressMode AM;
2458 getAddressingMode(LI->getOperand(0), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002459
Reid Spencerfc989e12004-08-30 00:13:26 +00002460 addFullAddress(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op0r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002461 }
Chris Lattner95157f72004-04-11 22:05:45 +00002462 return;
2463 }
2464 }
2465
Chris Lattner462fa822004-04-11 20:56:28 +00002466 MachineBasicBlock::iterator IP = BB->end();
Chris Lattner95157f72004-04-11 22:05:45 +00002467 emitMultiply(BB, IP, Op0, Op1, ResultReg);
Chris Lattner462fa822004-04-11 20:56:28 +00002468}
2469
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002470void X86ISel::emitMultiply(MachineBasicBlock *MBB,
2471 MachineBasicBlock::iterator IP,
2472 Value *Op0, Value *Op1, unsigned DestReg) {
Chris Lattner462fa822004-04-11 20:56:28 +00002473 MachineBasicBlock &BB = *MBB;
2474 TypeClass Class = getClass(Op0->getType());
Chris Lattner3e130a22003-01-13 00:32:26 +00002475
2476 // Simple scalar multiply?
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002477 unsigned Op0Reg = getReg(Op0, &BB, IP);
Chris Lattner462fa822004-04-11 20:56:28 +00002478 switch (Class) {
2479 case cByte:
2480 case cShort:
2481 case cInt:
2482 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner462fa822004-04-11 20:56:28 +00002483 unsigned Val = (unsigned)CI->getRawValue(); // Isn't a 64-bit constant
2484 doMultiplyConst(&BB, IP, DestReg, Op0->getType(), Op0Reg, Val);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002485 } else {
Chris Lattner462fa822004-04-11 20:56:28 +00002486 unsigned Op1Reg = getReg(Op1, &BB, IP);
2487 doMultiply(&BB, IP, DestReg, Op1->getType(), Op0Reg, Op1Reg);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002488 }
Chris Lattner462fa822004-04-11 20:56:28 +00002489 return;
2490 case cFP:
Chris Lattner6621ed92004-04-11 21:23:56 +00002491 emitBinaryFPOperation(MBB, IP, Op0, Op1, 2, DestReg);
2492 return;
Chris Lattner462fa822004-04-11 20:56:28 +00002493 case cLong:
2494 break;
2495 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002496
Chris Lattner462fa822004-04-11 20:56:28 +00002497 // Long value. We have to do things the hard way...
2498 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
2499 unsigned CLow = CI->getRawValue();
2500 unsigned CHi = CI->getRawValue() >> 32;
2501
2502 if (CLow == 0) {
2503 // If the low part of the constant is all zeros, things are simple.
2504 BuildMI(BB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
2505 doMultiplyConst(&BB, IP, DestReg+1, Type::UIntTy, Op0Reg, CHi);
2506 return;
2507 }
2508
2509 // Multiply the two low parts... capturing carry into EDX
2510 unsigned OverflowReg = 0;
2511 if (CLow == 1) {
2512 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg).addReg(Op0Reg);
Chris Lattner028adc42004-04-06 04:29:36 +00002513 } else {
Chris Lattner462fa822004-04-11 20:56:28 +00002514 unsigned Op1RegL = makeAnotherReg(Type::UIntTy);
2515 OverflowReg = makeAnotherReg(Type::UIntTy);
2516 BuildMI(BB, IP, X86::MOV32ri, 1, Op1RegL).addImm(CLow);
2517 BuildMI(BB, IP, X86::MOV32rr, 1, X86::EAX).addReg(Op0Reg);
2518 BuildMI(BB, IP, X86::MUL32r, 1).addReg(Op1RegL); // AL*BL
Chris Lattner028adc42004-04-06 04:29:36 +00002519
Chris Lattner462fa822004-04-11 20:56:28 +00002520 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg).addReg(X86::EAX); // AL*BL
2521 BuildMI(BB, IP, X86::MOV32rr, 1,
2522 OverflowReg).addReg(X86::EDX); // AL*BL >> 32
2523 }
2524
2525 unsigned AHBLReg = makeAnotherReg(Type::UIntTy); // AH*BL
2526 doMultiplyConst(&BB, IP, AHBLReg, Type::UIntTy, Op0Reg+1, CLow);
2527
2528 unsigned AHBLplusOverflowReg;
2529 if (OverflowReg) {
2530 AHBLplusOverflowReg = makeAnotherReg(Type::UIntTy);
2531 BuildMI(BB, IP, X86::ADD32rr, 2, // AH*BL+(AL*BL >> 32)
Chris Lattner028adc42004-04-06 04:29:36 +00002532 AHBLplusOverflowReg).addReg(AHBLReg).addReg(OverflowReg);
Chris Lattner462fa822004-04-11 20:56:28 +00002533 } else {
2534 AHBLplusOverflowReg = AHBLReg;
2535 }
2536
2537 if (CHi == 0) {
2538 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg+1).addReg(AHBLplusOverflowReg);
2539 } else {
Chris Lattner028adc42004-04-06 04:29:36 +00002540 unsigned ALBHReg = makeAnotherReg(Type::UIntTy); // AL*BH
Chris Lattner462fa822004-04-11 20:56:28 +00002541 doMultiplyConst(&BB, IP, ALBHReg, Type::UIntTy, Op0Reg, CHi);
Chris Lattner028adc42004-04-06 04:29:36 +00002542
Chris Lattner462fa822004-04-11 20:56:28 +00002543 BuildMI(BB, IP, X86::ADD32rr, 2, // AL*BH + AH*BL + (AL*BL >> 32)
Chris Lattner028adc42004-04-06 04:29:36 +00002544 DestReg+1).addReg(AHBLplusOverflowReg).addReg(ALBHReg);
2545 }
Chris Lattner462fa822004-04-11 20:56:28 +00002546 return;
Chris Lattner3e130a22003-01-13 00:32:26 +00002547 }
Chris Lattner462fa822004-04-11 20:56:28 +00002548
2549 // General 64x64 multiply
2550
2551 unsigned Op1Reg = getReg(Op1, &BB, IP);
2552 // Multiply the two low parts... capturing carry into EDX
2553 BuildMI(BB, IP, X86::MOV32rr, 1, X86::EAX).addReg(Op0Reg);
2554 BuildMI(BB, IP, X86::MUL32r, 1).addReg(Op1Reg); // AL*BL
2555
2556 unsigned OverflowReg = makeAnotherReg(Type::UIntTy);
2557 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg).addReg(X86::EAX); // AL*BL
2558 BuildMI(BB, IP, X86::MOV32rr, 1,
2559 OverflowReg).addReg(X86::EDX); // AL*BL >> 32
2560
2561 unsigned AHBLReg = makeAnotherReg(Type::UIntTy); // AH*BL
2562 BuildMI(BB, IP, X86::IMUL32rr, 2,
2563 AHBLReg).addReg(Op0Reg+1).addReg(Op1Reg);
2564
2565 unsigned AHBLplusOverflowReg = makeAnotherReg(Type::UIntTy);
2566 BuildMI(BB, IP, X86::ADD32rr, 2, // AH*BL+(AL*BL >> 32)
2567 AHBLplusOverflowReg).addReg(AHBLReg).addReg(OverflowReg);
2568
2569 unsigned ALBHReg = makeAnotherReg(Type::UIntTy); // AL*BH
2570 BuildMI(BB, IP, X86::IMUL32rr, 2,
2571 ALBHReg).addReg(Op0Reg).addReg(Op1Reg+1);
2572
2573 BuildMI(BB, IP, X86::ADD32rr, 2, // AL*BH + AH*BL + (AL*BL >> 32)
2574 DestReg+1).addReg(AHBLplusOverflowReg).addReg(ALBHReg);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002575}
Chris Lattnerca9671d2002-11-02 20:28:58 +00002576
Chris Lattner06925362002-11-17 21:56:38 +00002577
Chris Lattnerf01729e2002-11-02 20:54:46 +00002578/// visitDivRem - Handle division and remainder instructions... these
2579/// instruction both require the same instructions to be generated, they just
2580/// select the result from a different register. Note that both of these
2581/// instructions work differently for signed and unsigned operands.
2582///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002583void X86ISel::visitDivRem(BinaryOperator &I) {
Chris Lattnercadff442003-10-23 17:21:43 +00002584 unsigned ResultReg = getReg(I);
Chris Lattner95157f72004-04-11 22:05:45 +00002585 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2586
2587 // Fold loads into floating point divides.
2588 if (getClass(Op0->getType()) == cFP) {
2589 if (LoadInst *LI = dyn_cast<LoadInst>(Op1))
2590 if (isSafeToFoldLoadIntoInstruction(*LI, I)) {
2591 const Type *Ty = Op0->getType();
2592 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2593 unsigned Opcode = Ty == Type::FloatTy ? X86::FDIV32m : X86::FDIV64m;
2594
Chris Lattner95157f72004-04-11 22:05:45 +00002595 unsigned Op0r = getReg(Op0);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002596 if (AllocaInst *AI = dyn_castFixedAlloca(LI->getOperand(0))) {
2597 unsigned FI = getFixedSizedAllocaFI(AI);
2598 addFrameReference(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op0r), FI);
2599 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002600 X86AddressMode AM;
2601 getAddressingMode(LI->getOperand(0), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002602
Reid Spencerfc989e12004-08-30 00:13:26 +00002603 addFullAddress(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op0r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002604 }
Chris Lattner95157f72004-04-11 22:05:45 +00002605 return;
2606 }
2607
2608 if (LoadInst *LI = dyn_cast<LoadInst>(Op0))
2609 if (isSafeToFoldLoadIntoInstruction(*LI, I)) {
2610 const Type *Ty = Op0->getType();
2611 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2612 unsigned Opcode = Ty == Type::FloatTy ? X86::FDIVR32m : X86::FDIVR64m;
2613
Chris Lattner95157f72004-04-11 22:05:45 +00002614 unsigned Op1r = getReg(Op1);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002615 if (AllocaInst *AI = dyn_castFixedAlloca(LI->getOperand(0))) {
2616 unsigned FI = getFixedSizedAllocaFI(AI);
2617 addFrameReference(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op1r), FI);
2618 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002619 X86AddressMode AM;
2620 getAddressingMode(LI->getOperand(0), AM);
2621 addFullAddress(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op1r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002622 }
Chris Lattner95157f72004-04-11 22:05:45 +00002623 return;
2624 }
2625 }
2626
Chris Lattner94af4142002-12-25 05:13:53 +00002627
Chris Lattnercadff442003-10-23 17:21:43 +00002628 MachineBasicBlock::iterator IP = BB->end();
Chris Lattner95157f72004-04-11 22:05:45 +00002629 emitDivRemOperation(BB, IP, Op0, Op1,
Chris Lattner462fa822004-04-11 20:56:28 +00002630 I.getOpcode() == Instruction::Div, ResultReg);
Chris Lattnercadff442003-10-23 17:21:43 +00002631}
2632
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002633void X86ISel::emitDivRemOperation(MachineBasicBlock *BB,
2634 MachineBasicBlock::iterator IP,
2635 Value *Op0, Value *Op1, bool isDiv,
2636 unsigned ResultReg) {
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002637 const Type *Ty = Op0->getType();
2638 unsigned Class = getClass(Ty);
Chris Lattner94af4142002-12-25 05:13:53 +00002639 switch (Class) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002640 case cFP: // Floating point divide
Chris Lattnercadff442003-10-23 17:21:43 +00002641 if (isDiv) {
Chris Lattner6621ed92004-04-11 21:23:56 +00002642 emitBinaryFPOperation(BB, IP, Op0, Op1, 3, ResultReg);
2643 return;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00002644 } else { // Floating point remainder...
Chris Lattner462fa822004-04-11 20:56:28 +00002645 unsigned Op0Reg = getReg(Op0, BB, IP);
2646 unsigned Op1Reg = getReg(Op1, BB, IP);
Chris Lattner3e130a22003-01-13 00:32:26 +00002647 MachineInstr *TheCall =
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002648 BuildMI(X86::CALLpcrel32, 1).addExternalSymbol("fmod", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00002649 std::vector<ValueRecord> Args;
Chris Lattnercadff442003-10-23 17:21:43 +00002650 Args.push_back(ValueRecord(Op0Reg, Type::DoubleTy));
2651 Args.push_back(ValueRecord(Op1Reg, Type::DoubleTy));
Chris Lattner3e130a22003-01-13 00:32:26 +00002652 doCall(ValueRecord(ResultReg, Type::DoubleTy), TheCall, Args);
2653 }
Chris Lattner94af4142002-12-25 05:13:53 +00002654 return;
Chris Lattner3e130a22003-01-13 00:32:26 +00002655 case cLong: {
2656 static const char *FnName[] =
2657 { "__moddi3", "__divdi3", "__umoddi3", "__udivdi3" };
Chris Lattner462fa822004-04-11 20:56:28 +00002658 unsigned Op0Reg = getReg(Op0, BB, IP);
2659 unsigned Op1Reg = getReg(Op1, BB, IP);
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002660 unsigned NameIdx = Ty->isUnsigned()*2 + isDiv;
Chris Lattner3e130a22003-01-13 00:32:26 +00002661 MachineInstr *TheCall =
2662 BuildMI(X86::CALLpcrel32, 1).addExternalSymbol(FnName[NameIdx], true);
2663
2664 std::vector<ValueRecord> Args;
Chris Lattnercadff442003-10-23 17:21:43 +00002665 Args.push_back(ValueRecord(Op0Reg, Type::LongTy));
2666 Args.push_back(ValueRecord(Op1Reg, Type::LongTy));
Chris Lattner3e130a22003-01-13 00:32:26 +00002667 doCall(ValueRecord(ResultReg, Type::LongTy), TheCall, Args);
2668 return;
2669 }
2670 case cByte: case cShort: case cInt:
Misha Brukmancf00c4a2003-10-10 17:57:28 +00002671 break; // Small integrals, handled below...
Chris Lattner3e130a22003-01-13 00:32:26 +00002672 default: assert(0 && "Unknown class!");
Chris Lattner94af4142002-12-25 05:13:53 +00002673 }
Chris Lattnerf01729e2002-11-02 20:54:46 +00002674
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002675 static const unsigned MovOpcode[]={ X86::MOV8rr, X86::MOV16rr, X86::MOV32rr };
Chris Lattner2483f672004-10-06 05:01:07 +00002676 static const unsigned NEGOpcode[]={ X86::NEG8r, X86::NEG16r, X86::NEG32r };
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002677 static const unsigned SAROpcode[]={ X86::SAR8ri, X86::SAR16ri, X86::SAR32ri };
2678 static const unsigned SHROpcode[]={ X86::SHR8ri, X86::SHR16ri, X86::SHR32ri };
2679 static const unsigned ADDOpcode[]={ X86::ADD8rr, X86::ADD16rr, X86::ADD32rr };
2680
2681 // Special case signed division by power of 2.
Chris Lattner2483f672004-10-06 05:01:07 +00002682 if (ConstantSInt *CI = dyn_cast<ConstantSInt>(Op1))
2683 if (isDiv) {
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002684 assert(Class != cLong && "This doesn't handle 64-bit divides!");
2685 int V = CI->getValue();
2686
2687 if (V == 1) { // X /s 1 => X
2688 unsigned Op0Reg = getReg(Op0, BB, IP);
2689 BuildMI(*BB, IP, MovOpcode[Class], 1, ResultReg).addReg(Op0Reg);
2690 return;
2691 }
2692
2693 if (V == -1) { // X /s -1 => -X
2694 unsigned Op0Reg = getReg(Op0, BB, IP);
2695 BuildMI(*BB, IP, NEGOpcode[Class], 1, ResultReg).addReg(Op0Reg);
2696 return;
2697 }
2698
Chris Lattner610f1e22004-10-06 04:02:39 +00002699 if (V == 2 || V == -2) { // X /s 2
2700 static const unsigned CMPOpcode[] = {
2701 X86::CMP8ri, X86::CMP16ri, X86::CMP32ri
2702 };
2703 static const unsigned SBBOpcode[] = {
2704 X86::SBB8ri, X86::SBB16ri, X86::SBB32ri
2705 };
2706 unsigned Op0Reg = getReg(Op0, BB, IP);
2707 unsigned SignBit = 1 << (CI->getType()->getPrimitiveSize()*8-1);
2708 BuildMI(*BB, IP, CMPOpcode[Class], 2).addReg(Op0Reg).addImm(SignBit);
2709
2710 unsigned TmpReg = makeAnotherReg(Op0->getType());
2711 BuildMI(*BB, IP, SBBOpcode[Class], 2, TmpReg).addReg(Op0Reg).addImm(-1);
2712
2713 unsigned TmpReg2 = V == 2 ? ResultReg : makeAnotherReg(Op0->getType());
2714 BuildMI(*BB, IP, SAROpcode[Class], 2, TmpReg2).addReg(TmpReg).addImm(1);
2715 if (V == -2) {
2716 BuildMI(*BB, IP, NEGOpcode[Class], 1, ResultReg).addReg(TmpReg2);
2717 }
2718 return;
2719 }
2720
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002721 bool isNeg = false;
2722 if (V < 0) { // Not a positive power of 2?
2723 V = -V;
2724 isNeg = true; // Maybe it's a negative power of 2.
2725 }
2726 if (unsigned Log = ExactLog2(V)) {
2727 --Log;
2728 unsigned Op0Reg = getReg(Op0, BB, IP);
2729 unsigned TmpReg = makeAnotherReg(Op0->getType());
Chris Lattner3ffdff62004-10-06 04:19:43 +00002730 BuildMI(*BB, IP, SAROpcode[Class], 2, TmpReg)
2731 .addReg(Op0Reg).addImm(Log-1);
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002732 unsigned TmpReg2 = makeAnotherReg(Op0->getType());
2733 BuildMI(*BB, IP, SHROpcode[Class], 2, TmpReg2)
2734 .addReg(TmpReg).addImm(32-Log);
2735 unsigned TmpReg3 = makeAnotherReg(Op0->getType());
2736 BuildMI(*BB, IP, ADDOpcode[Class], 2, TmpReg3)
2737 .addReg(Op0Reg).addReg(TmpReg2);
2738
2739 unsigned TmpReg4 = isNeg ? makeAnotherReg(Op0->getType()) : ResultReg;
2740 BuildMI(*BB, IP, SAROpcode[Class], 2, TmpReg4)
Chris Lattner3ffdff62004-10-06 04:19:43 +00002741 .addReg(TmpReg3).addImm(Log);
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002742 if (isNeg)
2743 BuildMI(*BB, IP, NEGOpcode[Class], 1, ResultReg).addReg(TmpReg4);
2744 return;
2745 }
Chris Lattner2483f672004-10-06 05:01:07 +00002746 } else { // X % C
2747 assert(Class != cLong && "This doesn't handle 64-bit remainder!");
2748 int V = CI->getValue();
2749
2750 if (V == 2 || V == -2) { // X % 2, X % -2
Chris Lattner2483f672004-10-06 05:01:07 +00002751 static const unsigned SExtOpcode[] = { X86::CBW, X86::CWD, X86::CDQ };
2752 static const unsigned BaseReg[] = { X86::AL , X86::AX , X86::EAX };
2753 static const unsigned SExtReg[] = { X86::AH , X86::DX , X86::EDX };
2754 static const unsigned ANDOpcode[] = {
2755 X86::AND8ri, X86::AND16ri, X86::AND32ri
2756 };
2757 static const unsigned XOROpcode[] = {
2758 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr
2759 };
2760 static const unsigned SUBOpcode[] = {
2761 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr
2762 };
2763
2764 // Sign extend result into reg of -1 or 0.
2765 unsigned Op0Reg = getReg(Op0, BB, IP);
2766 BuildMI(*BB, IP, MovOpcode[Class], 1, BaseReg[Class]).addReg(Op0Reg);
2767 BuildMI(*BB, IP, SExtOpcode[Class], 0);
2768 unsigned TmpReg0 = makeAnotherReg(Op0->getType());
2769 BuildMI(*BB, IP, MovOpcode[Class], 1, TmpReg0).addReg(SExtReg[Class]);
2770
2771 unsigned TmpReg1 = makeAnotherReg(Op0->getType());
2772 BuildMI(*BB, IP, ANDOpcode[Class], 2, TmpReg1).addReg(Op0Reg).addImm(1);
2773
2774 unsigned TmpReg2 = makeAnotherReg(Op0->getType());
2775 BuildMI(*BB, IP, XOROpcode[Class], 2,
2776 TmpReg2).addReg(TmpReg1).addReg(TmpReg0);
2777 BuildMI(*BB, IP, SUBOpcode[Class], 2,
2778 ResultReg).addReg(TmpReg2).addReg(TmpReg0);
2779 return;
2780 }
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002781 }
2782
2783 static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX };
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002784 static const unsigned ClrOpcode[]={ X86::MOV8ri, X86::MOV16ri, X86::MOV32ri };
Chris Lattnerf01729e2002-11-02 20:54:46 +00002785 static const unsigned ExtRegs[] ={ X86::AH , X86::DX , X86::EDX };
2786
2787 static const unsigned DivOpcode[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002788 { X86::DIV8r , X86::DIV16r , X86::DIV32r , 0 }, // Unsigned division
2789 { X86::IDIV8r, X86::IDIV16r, X86::IDIV32r, 0 }, // Signed division
Chris Lattnerf01729e2002-11-02 20:54:46 +00002790 };
2791
Chris Lattnerf01729e2002-11-02 20:54:46 +00002792 unsigned Reg = Regs[Class];
2793 unsigned ExtReg = ExtRegs[Class];
Chris Lattnerf01729e2002-11-02 20:54:46 +00002794
2795 // Put the first operand into one of the A registers...
Chris Lattner462fa822004-04-11 20:56:28 +00002796 unsigned Op0Reg = getReg(Op0, BB, IP);
2797 unsigned Op1Reg = getReg(Op1, BB, IP);
Chris Lattneree352852004-02-29 07:22:16 +00002798 BuildMI(*BB, IP, MovOpcode[Class], 1, Reg).addReg(Op0Reg);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002799
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002800 if (Ty->isSigned()) {
Chris Lattnerf01729e2002-11-02 20:54:46 +00002801 // Emit a sign extension instruction...
Chris Lattner462fa822004-04-11 20:56:28 +00002802 unsigned ShiftResult = makeAnotherReg(Op0->getType());
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002803 BuildMI(*BB, IP, SAROpcode[Class], 2,ShiftResult).addReg(Op0Reg).addImm(31);
Chris Lattneree352852004-02-29 07:22:16 +00002804 BuildMI(*BB, IP, MovOpcode[Class], 1, ExtReg).addReg(ShiftResult);
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002805
2806 // Emit the appropriate divide or remainder instruction...
2807 BuildMI(*BB, IP, DivOpcode[1][Class], 1).addReg(Op1Reg);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002808 } else {
Alkis Evlogimenosf998a7e2004-01-12 07:22:45 +00002809 // If unsigned, emit a zeroing instruction... (reg = 0)
Chris Lattneree352852004-02-29 07:22:16 +00002810 BuildMI(*BB, IP, ClrOpcode[Class], 2, ExtReg).addImm(0);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002811
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002812 // Emit the appropriate divide or remainder instruction...
2813 BuildMI(*BB, IP, DivOpcode[0][Class], 1).addReg(Op1Reg);
2814 }
Chris Lattner06925362002-11-17 21:56:38 +00002815
Chris Lattnerf01729e2002-11-02 20:54:46 +00002816 // Figure out which register we want to pick the result out of...
Chris Lattnercadff442003-10-23 17:21:43 +00002817 unsigned DestReg = isDiv ? Reg : ExtReg;
Chris Lattnerf01729e2002-11-02 20:54:46 +00002818
Chris Lattnerf01729e2002-11-02 20:54:46 +00002819 // Put the result into the destination register...
Chris Lattneree352852004-02-29 07:22:16 +00002820 BuildMI(*BB, IP, MovOpcode[Class], 1, ResultReg).addReg(DestReg);
Chris Lattnerca9671d2002-11-02 20:28:58 +00002821}
Chris Lattnere2954c82002-11-02 20:04:26 +00002822
Chris Lattner06925362002-11-17 21:56:38 +00002823
Brian Gaekea1719c92002-10-31 23:03:59 +00002824/// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here
2825/// for constant immediate shift values, and for constant immediate
2826/// shift values equal to 1. Even the general case is sort of special,
2827/// because the shift amount has to be in CL, not just any old register.
2828///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002829void X86ISel::visitShiftInst(ShiftInst &I) {
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002830 MachineBasicBlock::iterator IP = BB->end ();
2831 emitShiftOperation (BB, IP, I.getOperand (0), I.getOperand (1),
2832 I.getOpcode () == Instruction::Shl, I.getType (),
2833 getReg (I));
2834}
2835
2836/// emitShiftOperation - Common code shared between visitShiftInst and
2837/// constant expression support.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002838void X86ISel::emitShiftOperation(MachineBasicBlock *MBB,
2839 MachineBasicBlock::iterator IP,
2840 Value *Op, Value *ShiftAmount,
2841 bool isLeftShift, const Type *ResultTy,
2842 unsigned DestReg) {
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002843 unsigned SrcReg = getReg (Op, MBB, IP);
2844 bool isSigned = ResultTy->isSigned ();
2845 unsigned Class = getClass (ResultTy);
Chris Lattner3e130a22003-01-13 00:32:26 +00002846
2847 static const unsigned ConstantOperand[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002848 { X86::SHR8ri, X86::SHR16ri, X86::SHR32ri, X86::SHRD32rri8 }, // SHR
2849 { X86::SAR8ri, X86::SAR16ri, X86::SAR32ri, X86::SHRD32rri8 }, // SAR
2850 { X86::SHL8ri, X86::SHL16ri, X86::SHL32ri, X86::SHLD32rri8 }, // SHL
2851 { X86::SHL8ri, X86::SHL16ri, X86::SHL32ri, X86::SHLD32rri8 }, // SAL = SHL
Chris Lattner3e130a22003-01-13 00:32:26 +00002852 };
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002853
Chris Lattner3e130a22003-01-13 00:32:26 +00002854 static const unsigned NonConstantOperand[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002855 { X86::SHR8rCL, X86::SHR16rCL, X86::SHR32rCL }, // SHR
2856 { X86::SAR8rCL, X86::SAR16rCL, X86::SAR32rCL }, // SAR
2857 { X86::SHL8rCL, X86::SHL16rCL, X86::SHL32rCL }, // SHL
2858 { X86::SHL8rCL, X86::SHL16rCL, X86::SHL32rCL }, // SAL = SHL
Chris Lattner3e130a22003-01-13 00:32:26 +00002859 };
Chris Lattner796df732002-11-02 00:44:25 +00002860
Chris Lattner3e130a22003-01-13 00:32:26 +00002861 // Longs, as usual, are handled specially...
2862 if (Class == cLong) {
2863 // If we have a constant shift, we can generate much more efficient code
2864 // than otherwise...
2865 //
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002866 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002867 unsigned Amount = CUI->getValue();
2868 if (Amount < 32) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002869 const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned];
2870 if (isLeftShift) {
Chris Lattneree352852004-02-29 07:22:16 +00002871 BuildMI(*MBB, IP, Opc[3], 3,
2872 DestReg+1).addReg(SrcReg+1).addReg(SrcReg).addImm(Amount);
2873 BuildMI(*MBB, IP, Opc[2], 2, DestReg).addReg(SrcReg).addImm(Amount);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002874 } else {
Chris Lattneree352852004-02-29 07:22:16 +00002875 BuildMI(*MBB, IP, Opc[3], 3,
2876 DestReg).addReg(SrcReg ).addReg(SrcReg+1).addImm(Amount);
2877 BuildMI(*MBB, IP, Opc[2],2,DestReg+1).addReg(SrcReg+1).addImm(Amount);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002878 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002879 } else { // Shifting more than 32 bits
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002880 Amount -= 32;
2881 if (isLeftShift) {
Chris Lattner722070e2004-04-06 03:42:38 +00002882 if (Amount != 0) {
2883 BuildMI(*MBB, IP, X86::SHL32ri, 2,
2884 DestReg + 1).addReg(SrcReg).addImm(Amount);
2885 } else {
2886 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg+1).addReg(SrcReg);
2887 }
2888 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002889 } else {
Chris Lattner722070e2004-04-06 03:42:38 +00002890 if (Amount != 0) {
2891 BuildMI(*MBB, IP, isSigned ? X86::SAR32ri : X86::SHR32ri, 2,
2892 DestReg).addReg(SrcReg+1).addImm(Amount);
2893 } else {
2894 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg+1);
2895 }
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002896 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002897 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002898 }
2899 } else {
Chris Lattner9171ef52003-06-01 01:56:54 +00002900 unsigned TmpReg = makeAnotherReg(Type::IntTy);
2901
2902 if (!isLeftShift && isSigned) {
2903 // If this is a SHR of a Long, then we need to do funny sign extension
2904 // stuff. TmpReg gets the value to use as the high-part if we are
2905 // shifting more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002906 BuildMI(*MBB, IP, X86::SAR32ri, 2, TmpReg).addReg(SrcReg).addImm(31);
Chris Lattner9171ef52003-06-01 01:56:54 +00002907 } else {
2908 // Other shifts use a fixed zero value if the shift is more than 32
2909 // bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002910 BuildMI(*MBB, IP, X86::MOV32ri, 1, TmpReg).addImm(0);
Chris Lattner9171ef52003-06-01 01:56:54 +00002911 }
2912
2913 // Initialize CL with the shift amount...
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002914 unsigned ShiftAmountReg = getReg(ShiftAmount, MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002915 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002916
2917 unsigned TmpReg2 = makeAnotherReg(Type::IntTy);
2918 unsigned TmpReg3 = makeAnotherReg(Type::IntTy);
2919 if (isLeftShift) {
2920 // TmpReg2 = shld inHi, inLo
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002921 BuildMI(*MBB, IP, X86::SHLD32rrCL,2,TmpReg2).addReg(SrcReg+1)
Chris Lattneree352852004-02-29 07:22:16 +00002922 .addReg(SrcReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002923 // TmpReg3 = shl inLo, CL
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002924 BuildMI(*MBB, IP, X86::SHL32rCL, 1, TmpReg3).addReg(SrcReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002925
2926 // Set the flags to indicate whether the shift was by more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002927 BuildMI(*MBB, IP, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Chris Lattner9171ef52003-06-01 01:56:54 +00002928
2929 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002930 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00002931 DestReg+1).addReg(TmpReg2).addReg(TmpReg3);
2932 // DestLo = (>32) ? TmpReg : TmpReg3;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002933 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002934 DestReg).addReg(TmpReg3).addReg(TmpReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002935 } else {
2936 // TmpReg2 = shrd inLo, inHi
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002937 BuildMI(*MBB, IP, X86::SHRD32rrCL,2,TmpReg2).addReg(SrcReg)
Chris Lattneree352852004-02-29 07:22:16 +00002938 .addReg(SrcReg+1);
Chris Lattner9171ef52003-06-01 01:56:54 +00002939 // TmpReg3 = s[ah]r inHi, CL
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002940 BuildMI(*MBB, IP, isSigned ? X86::SAR32rCL : X86::SHR32rCL, 1, TmpReg3)
Chris Lattner9171ef52003-06-01 01:56:54 +00002941 .addReg(SrcReg+1);
2942
2943 // Set the flags to indicate whether the shift was by more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002944 BuildMI(*MBB, IP, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Chris Lattner9171ef52003-06-01 01:56:54 +00002945
2946 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002947 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00002948 DestReg).addReg(TmpReg2).addReg(TmpReg3);
2949
2950 // DestHi = (>32) ? TmpReg : TmpReg3;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002951 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00002952 DestReg+1).addReg(TmpReg3).addReg(TmpReg);
2953 }
Brian Gaekea1719c92002-10-31 23:03:59 +00002954 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002955 return;
2956 }
Chris Lattnere9913f22002-11-02 01:41:55 +00002957
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002958 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002959 // The shift amount is constant, guaranteed to be a ubyte. Get its value.
2960 assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?");
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002961
Chris Lattner3e130a22003-01-13 00:32:26 +00002962 const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned];
Chris Lattneree352852004-02-29 07:22:16 +00002963 BuildMI(*MBB, IP, Opc[Class], 2,
2964 DestReg).addReg(SrcReg).addImm(CUI->getValue());
Chris Lattner3e130a22003-01-13 00:32:26 +00002965 } else { // The shift amount is non-constant.
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002966 unsigned ShiftAmountReg = getReg (ShiftAmount, MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002967 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002968
Chris Lattner3e130a22003-01-13 00:32:26 +00002969 const unsigned *Opc = NonConstantOperand[isLeftShift*2+isSigned];
Chris Lattneree352852004-02-29 07:22:16 +00002970 BuildMI(*MBB, IP, Opc[Class], 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002971 }
2972}
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002973
Chris Lattner3e130a22003-01-13 00:32:26 +00002974
Chris Lattner6fc3c522002-11-17 21:11:55 +00002975/// visitLoadInst - Implement LLVM load instructions in terms of the x86 'mov'
Chris Lattnere8f0d922002-12-24 00:03:11 +00002976/// instruction. The load and store instructions are the only place where we
2977/// need to worry about the memory layout of the target machine.
Chris Lattner6fc3c522002-11-17 21:11:55 +00002978///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002979void X86ISel::visitLoadInst(LoadInst &I) {
Chris Lattner7dee5da2004-03-08 01:58:35 +00002980 // Check to see if this load instruction is going to be folded into a binary
2981 // instruction, like add. If so, we don't want to emit it. Wouldn't a real
2982 // pattern matching instruction selector be nice?
Chris Lattner95157f72004-04-11 22:05:45 +00002983 unsigned Class = getClassB(I.getType());
Chris Lattnerfeac3e12004-04-11 23:21:26 +00002984 if (I.hasOneUse()) {
Chris Lattner7dee5da2004-03-08 01:58:35 +00002985 Instruction *User = cast<Instruction>(I.use_back());
2986 switch (User->getOpcode()) {
Chris Lattnerfeac3e12004-04-11 23:21:26 +00002987 case Instruction::Cast:
2988 // If this is a cast from a signed-integer type to a floating point type,
2989 // fold the cast here.
John Criswell6b5bd582004-06-09 15:18:51 +00002990 if (getClassB(User->getType()) == cFP &&
Chris Lattnerfeac3e12004-04-11 23:21:26 +00002991 (I.getType() == Type::ShortTy || I.getType() == Type::IntTy ||
2992 I.getType() == Type::LongTy)) {
2993 unsigned DestReg = getReg(User);
2994 static const unsigned Opcode[] = {
2995 0/*BYTE*/, X86::FILD16m, X86::FILD32m, 0/*FP*/, X86::FILD64m
2996 };
Chris Lattner9f1b5312004-05-13 15:12:43 +00002997
2998 if (AllocaInst *AI = dyn_castFixedAlloca(I.getOperand(0))) {
2999 unsigned FI = getFixedSizedAllocaFI(AI);
3000 addFrameReference(BuildMI(BB, Opcode[Class], 4, DestReg), FI);
3001 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00003002 X86AddressMode AM;
3003 getAddressingMode(I.getOperand(0), AM);
3004 addFullAddress(BuildMI(BB, Opcode[Class], 4, DestReg), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00003005 }
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003006 return;
3007 } else {
3008 User = 0;
3009 }
3010 break;
Chris Lattner13c07fe2004-04-12 00:12:04 +00003011
Chris Lattner7dee5da2004-03-08 01:58:35 +00003012 case Instruction::Add:
3013 case Instruction::Sub:
3014 case Instruction::And:
3015 case Instruction::Or:
3016 case Instruction::Xor:
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003017 if (Class == cLong) User = 0;
Chris Lattner7dee5da2004-03-08 01:58:35 +00003018 break;
Chris Lattner95157f72004-04-11 22:05:45 +00003019 case Instruction::Mul:
3020 case Instruction::Div:
Chris Lattner13c07fe2004-04-12 00:12:04 +00003021 if (Class != cFP) User = 0;
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003022 break; // Folding only implemented for floating point.
Chris Lattner95157f72004-04-11 22:05:45 +00003023 default: User = 0; break;
Chris Lattner7dee5da2004-03-08 01:58:35 +00003024 }
3025
3026 if (User) {
3027 // Okay, we found a user. If the load is the first operand and there is
3028 // no second operand load, reverse the operand ordering. Note that this
3029 // can fail for a subtract (ie, no change will be made).
Chris Lattner3dbb5042004-07-21 21:28:26 +00003030 bool Swapped = false;
Chris Lattner7dee5da2004-03-08 01:58:35 +00003031 if (!isa<LoadInst>(User->getOperand(1)))
Chris Lattner3dbb5042004-07-21 21:28:26 +00003032 Swapped = !cast<BinaryOperator>(User)->swapOperands();
Chris Lattner7dee5da2004-03-08 01:58:35 +00003033
3034 // Okay, now that everything is set up, if this load is used by the second
3035 // operand, and if there are no instructions that invalidate the load
3036 // before the binary operator, eliminate the load.
3037 if (User->getOperand(1) == &I &&
3038 isSafeToFoldLoadIntoInstruction(I, *User))
3039 return; // Eliminate the load!
Chris Lattner95157f72004-04-11 22:05:45 +00003040
3041 // If this is a floating point sub or div, we won't be able to swap the
3042 // operands, but we will still be able to eliminate the load.
3043 if (Class == cFP && User->getOperand(0) == &I &&
3044 !isa<LoadInst>(User->getOperand(1)) &&
3045 (User->getOpcode() == Instruction::Sub ||
3046 User->getOpcode() == Instruction::Div) &&
3047 isSafeToFoldLoadIntoInstruction(I, *User))
3048 return; // Eliminate the load!
Chris Lattner3dbb5042004-07-21 21:28:26 +00003049
3050 // If we swapped the operands to the instruction, but couldn't fold the
3051 // load anyway, swap them back. We don't want to break add X, int
3052 // folding.
3053 if (Swapped) cast<BinaryOperator>(User)->swapOperands();
Chris Lattner7dee5da2004-03-08 01:58:35 +00003054 }
3055 }
3056
Chris Lattner6ac1d712003-10-20 04:48:06 +00003057 static const unsigned Opcodes[] = {
Chris Lattner9f1b5312004-05-13 15:12:43 +00003058 X86::MOV8rm, X86::MOV16rm, X86::MOV32rm, X86::FLD32m, X86::MOV32rm
Chris Lattner3e130a22003-01-13 00:32:26 +00003059 };
Chris Lattner6ac1d712003-10-20 04:48:06 +00003060 unsigned Opcode = Opcodes[Class];
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003061 if (I.getType() == Type::DoubleTy) Opcode = X86::FLD64m;
Chris Lattner9f1b5312004-05-13 15:12:43 +00003062
3063 unsigned DestReg = getReg(I);
3064
3065 if (AllocaInst *AI = dyn_castFixedAlloca(I.getOperand(0))) {
3066 unsigned FI = getFixedSizedAllocaFI(AI);
3067 if (Class == cLong) {
3068 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, DestReg), FI);
3069 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, DestReg+1), FI, 4);
3070 } else {
3071 addFrameReference(BuildMI(BB, Opcode, 4, DestReg), FI);
3072 }
3073 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00003074 X86AddressMode AM;
3075 getAddressingMode(I.getOperand(0), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00003076
3077 if (Class == cLong) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003078 addFullAddress(BuildMI(BB, X86::MOV32rm, 4, DestReg), AM);
3079 AM.Disp += 4;
3080 addFullAddress(BuildMI(BB, X86::MOV32rm, 4, DestReg+1), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00003081 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00003082 addFullAddress(BuildMI(BB, Opcode, 4, DestReg), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00003083 }
3084 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003085}
3086
Chris Lattner6fc3c522002-11-17 21:11:55 +00003087/// visitStoreInst - Implement LLVM store instructions in terms of the x86 'mov'
3088/// instruction.
3089///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003090void X86ISel::visitStoreInst(StoreInst &I) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003091 X86AddressMode AM;
3092 getAddressingMode(I.getOperand(1), AM);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003093
Chris Lattner6c09db22003-10-20 04:11:23 +00003094 const Type *ValTy = I.getOperand(0)->getType();
3095 unsigned Class = getClassB(ValTy);
Chris Lattner6ac1d712003-10-20 04:48:06 +00003096
Chris Lattner5a830962004-02-25 02:56:58 +00003097 if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(0))) {
3098 uint64_t Val = CI->getRawValue();
3099 if (Class == cLong) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003100 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(Val & ~0U);
3101 AM.Disp += 4;
3102 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(Val>>32);
Chris Lattner5a830962004-02-25 02:56:58 +00003103 } else {
3104 static const unsigned Opcodes[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003105 X86::MOV8mi, X86::MOV16mi, X86::MOV32mi
Chris Lattner5a830962004-02-25 02:56:58 +00003106 };
3107 unsigned Opcode = Opcodes[Class];
Reid Spencerfc989e12004-08-30 00:13:26 +00003108 addFullAddress(BuildMI(BB, Opcode, 5), AM).addImm(Val);
Chris Lattner5a830962004-02-25 02:56:58 +00003109 }
Chris Lattnerb7cb9ff2004-05-13 15:26:48 +00003110 } else if (isa<ConstantPointerNull>(I.getOperand(0))) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003111 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(0);
Chris Lattner5a830962004-02-25 02:56:58 +00003112 } else if (ConstantBool *CB = dyn_cast<ConstantBool>(I.getOperand(0))) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003113 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CB->getValue());
Chris Lattnere7a31c92004-05-07 21:18:15 +00003114 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0))) {
3115 // Store constant FP values with integer instructions to avoid having to
3116 // load the constants from the constant pool then do a store.
3117 if (CFP->getType() == Type::FloatTy) {
3118 union {
3119 unsigned I;
3120 float F;
3121 } V;
3122 V.F = CFP->getValue();
Reid Spencerfc989e12004-08-30 00:13:26 +00003123 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(V.I);
Chris Lattner5a830962004-02-25 02:56:58 +00003124 } else {
Chris Lattnere7a31c92004-05-07 21:18:15 +00003125 union {
3126 uint64_t I;
3127 double F;
3128 } V;
3129 V.F = CFP->getValue();
Reid Spencerfc989e12004-08-30 00:13:26 +00003130 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm((unsigned)V.I);
3131 AM.Disp += 4;
3132 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(
Chris Lattnere7a31c92004-05-07 21:18:15 +00003133 unsigned(V.I >> 32));
Chris Lattner5a830962004-02-25 02:56:58 +00003134 }
Chris Lattnere7a31c92004-05-07 21:18:15 +00003135
3136 } else if (Class == cLong) {
3137 unsigned ValReg = getReg(I.getOperand(0));
Reid Spencerfc989e12004-08-30 00:13:26 +00003138 addFullAddress(BuildMI(BB, X86::MOV32mr, 5), AM).addReg(ValReg);
3139 AM.Disp += 4;
3140 addFullAddress(BuildMI(BB, X86::MOV32mr, 5), AM).addReg(ValReg+1);
Chris Lattnere7a31c92004-05-07 21:18:15 +00003141 } else {
3142 unsigned ValReg = getReg(I.getOperand(0));
3143 static const unsigned Opcodes[] = {
3144 X86::MOV8mr, X86::MOV16mr, X86::MOV32mr, X86::FST32m
3145 };
3146 unsigned Opcode = Opcodes[Class];
3147 if (ValTy == Type::DoubleTy) Opcode = X86::FST64m;
Chris Lattner9f1b5312004-05-13 15:12:43 +00003148
Reid Spencerfc989e12004-08-30 00:13:26 +00003149 addFullAddress(BuildMI(BB, Opcode, 1+4), AM).addReg(ValReg);
Chris Lattner94af4142002-12-25 05:13:53 +00003150 }
Chris Lattner6fc3c522002-11-17 21:11:55 +00003151}
3152
3153
Misha Brukman538607f2004-03-01 23:53:11 +00003154/// visitCastInst - Here we have various kinds of copying with or without sign
3155/// extension going on.
3156///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003157void X86ISel::visitCastInst(CastInst &CI) {
Chris Lattnerf5854472003-06-21 16:01:24 +00003158 Value *Op = CI.getOperand(0);
Chris Lattner427aeb42004-04-11 19:21:59 +00003159
Chris Lattner99382862004-04-12 00:23:04 +00003160 unsigned SrcClass = getClassB(Op->getType());
3161 unsigned DestClass = getClassB(CI.getType());
3162 // Noop casts are not emitted: getReg will return the source operand as the
3163 // register to use for any uses of the noop cast.
Chris Lattner8b486a12004-06-29 00:14:38 +00003164 if (DestClass == SrcClass) {
3165 // The only detail in this plan is that casts from double -> float are
3166 // truncating operations that we have to codegen through memory (despite
3167 // the fact that the source/dest registers are the same class).
3168 if (CI.getType() != Type::FloatTy || Op->getType() != Type::DoubleTy)
3169 return;
3170 }
Chris Lattner427aeb42004-04-11 19:21:59 +00003171
Chris Lattnerf5854472003-06-21 16:01:24 +00003172 // If this is a cast from a 32-bit integer to a Long type, and the only uses
3173 // of the case are GEP instructions, then the cast does not need to be
3174 // generated explicitly, it will be folded into the GEP.
Chris Lattner99382862004-04-12 00:23:04 +00003175 if (DestClass == cLong && SrcClass == cInt) {
Chris Lattnerf5854472003-06-21 16:01:24 +00003176 bool AllUsesAreGEPs = true;
3177 for (Value::use_iterator I = CI.use_begin(), E = CI.use_end(); I != E; ++I)
3178 if (!isa<GetElementPtrInst>(*I)) {
3179 AllUsesAreGEPs = false;
3180 break;
3181 }
3182
3183 // No need to codegen this cast if all users are getelementptr instrs...
3184 if (AllUsesAreGEPs) return;
3185 }
3186
Chris Lattner99382862004-04-12 00:23:04 +00003187 // If this cast converts a load from a short,int, or long integer to a FP
3188 // value, we will have folded this cast away.
3189 if (DestClass == cFP && isa<LoadInst>(Op) && Op->hasOneUse() &&
3190 (Op->getType() == Type::ShortTy || Op->getType() == Type::IntTy ||
3191 Op->getType() == Type::LongTy))
3192 return;
3193
3194
Chris Lattner548f61d2003-04-23 17:22:12 +00003195 unsigned DestReg = getReg(CI);
3196 MachineBasicBlock::iterator MI = BB->end();
Chris Lattnerf5854472003-06-21 16:01:24 +00003197 emitCastOperation(BB, MI, Op, CI.getType(), DestReg);
Chris Lattner548f61d2003-04-23 17:22:12 +00003198}
3199
Misha Brukman538607f2004-03-01 23:53:11 +00003200/// emitCastOperation - Common code shared between visitCastInst and constant
3201/// expression cast support.
3202///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003203void X86ISel::emitCastOperation(MachineBasicBlock *BB,
3204 MachineBasicBlock::iterator IP,
3205 Value *Src, const Type *DestTy,
3206 unsigned DestReg) {
Chris Lattner3e130a22003-01-13 00:32:26 +00003207 const Type *SrcTy = Src->getType();
3208 unsigned SrcClass = getClassB(SrcTy);
Chris Lattner3e130a22003-01-13 00:32:26 +00003209 unsigned DestClass = getClassB(DestTy);
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003210 unsigned SrcReg = getReg(Src, BB, IP);
3211
Chris Lattner3e130a22003-01-13 00:32:26 +00003212 // Implement casts to bool by using compare on the operand followed by set if
3213 // not zero on the result.
3214 if (DestTy == Type::BoolTy) {
Chris Lattner20772542003-06-01 03:38:24 +00003215 switch (SrcClass) {
3216 case cByte:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003217 BuildMI(*BB, IP, X86::TEST8rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00003218 break;
3219 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003220 BuildMI(*BB, IP, X86::TEST16rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00003221 break;
3222 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003223 BuildMI(*BB, IP, X86::TEST32rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00003224 break;
3225 case cLong: {
3226 unsigned TmpReg = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003227 BuildMI(*BB, IP, X86::OR32rr, 2, TmpReg).addReg(SrcReg).addReg(SrcReg+1);
Chris Lattner20772542003-06-01 03:38:24 +00003228 break;
3229 }
3230 case cFP:
Chris Lattneree352852004-02-29 07:22:16 +00003231 BuildMI(*BB, IP, X86::FTST, 1).addReg(SrcReg);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003232 BuildMI(*BB, IP, X86::FNSTSW8r, 0);
Chris Lattneree352852004-02-29 07:22:16 +00003233 BuildMI(*BB, IP, X86::SAHF, 1);
Chris Lattner311ca2e2004-02-23 03:21:41 +00003234 break;
Chris Lattner20772542003-06-01 03:38:24 +00003235 }
3236
3237 // If the zero flag is not set, then the value is true, set the byte to
3238 // true.
Chris Lattneree352852004-02-29 07:22:16 +00003239 BuildMI(*BB, IP, X86::SETNEr, 1, DestReg);
Chris Lattner94af4142002-12-25 05:13:53 +00003240 return;
3241 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003242
3243 static const unsigned RegRegMove[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003244 X86::MOV8rr, X86::MOV16rr, X86::MOV32rr, X86::FpMOV, X86::MOV32rr
Chris Lattner3e130a22003-01-13 00:32:26 +00003245 };
3246
3247 // Implement casts between values of the same type class (as determined by
3248 // getClass) by using a register-to-register move.
3249 if (SrcClass == DestClass) {
3250 if (SrcClass <= cInt || (SrcClass == cFP && SrcTy == DestTy)) {
Chris Lattneree352852004-02-29 07:22:16 +00003251 BuildMI(*BB, IP, RegRegMove[SrcClass], 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003252 } else if (SrcClass == cFP) {
3253 if (SrcTy == Type::FloatTy) { // double -> float
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003254 assert(DestTy == Type::DoubleTy && "Unknown cFP member!");
Chris Lattneree352852004-02-29 07:22:16 +00003255 BuildMI(*BB, IP, X86::FpMOV, 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003256 } else { // float -> double
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003257 assert(SrcTy == Type::DoubleTy && DestTy == Type::FloatTy &&
3258 "Unknown cFP member!");
3259 // Truncate from double to float by storing to memory as short, then
3260 // reading it back.
3261 unsigned FltAlign = TM.getTargetData().getFloatAlignment();
Chris Lattner3e130a22003-01-13 00:32:26 +00003262 int FrameIdx = F->getFrameInfo()->CreateStackObject(4, FltAlign);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003263 addFrameReference(BuildMI(*BB, IP, X86::FST32m, 5), FrameIdx).addReg(SrcReg);
3264 addFrameReference(BuildMI(*BB, IP, X86::FLD32m, 5, DestReg), FrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003265 }
3266 } else if (SrcClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003267 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg);
3268 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg+1).addReg(SrcReg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00003269 } else {
Chris Lattnerc53544a2003-05-12 20:16:58 +00003270 assert(0 && "Cannot handle this type of cast instruction!");
Chris Lattner548f61d2003-04-23 17:22:12 +00003271 abort();
Brian Gaeked474e9c2002-12-06 10:49:33 +00003272 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003273 return;
3274 }
3275
3276 // Handle cast of SMALLER int to LARGER int using a move with sign extension
3277 // or zero extension, depending on whether the source type was signed.
3278 if (SrcClass <= cInt && (DestClass <= cInt || DestClass == cLong) &&
3279 SrcClass < DestClass) {
3280 bool isLong = DestClass == cLong;
3281 if (isLong) DestClass = cInt;
3282
3283 static const unsigned Opc[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003284 { X86::MOVSX16rr8, X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOV32rr }, // s
3285 { X86::MOVZX16rr8, X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOV32rr } // u
Chris Lattner3e130a22003-01-13 00:32:26 +00003286 };
3287
Chris Lattner96e3b422004-05-09 22:28:45 +00003288 bool isUnsigned = SrcTy->isUnsigned() || SrcTy == Type::BoolTy;
Chris Lattneree352852004-02-29 07:22:16 +00003289 BuildMI(*BB, IP, Opc[isUnsigned][SrcClass + DestClass - 1], 1,
Chris Lattner548f61d2003-04-23 17:22:12 +00003290 DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003291
3292 if (isLong) { // Handle upper 32 bits as appropriate...
3293 if (isUnsigned) // Zero out top bits...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003294 BuildMI(*BB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
Chris Lattner3e130a22003-01-13 00:32:26 +00003295 else // Sign extend bottom half...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003296 BuildMI(*BB, IP, X86::SAR32ri, 2, DestReg+1).addReg(DestReg).addImm(31);
Brian Gaeked474e9c2002-12-06 10:49:33 +00003297 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003298 return;
3299 }
3300
3301 // Special case long -> int ...
3302 if (SrcClass == cLong && DestClass == cInt) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003303 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003304 return;
3305 }
3306
3307 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by a
3308 // move out of AX or AL.
3309 if ((SrcClass <= cInt || SrcClass == cLong) && DestClass <= cInt
3310 && SrcClass > DestClass) {
3311 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX, 0, X86::EAX };
Chris Lattneree352852004-02-29 07:22:16 +00003312 BuildMI(*BB, IP, RegRegMove[SrcClass], 1, AReg[SrcClass]).addReg(SrcReg);
3313 BuildMI(*BB, IP, RegRegMove[DestClass], 1, DestReg).addReg(AReg[DestClass]);
Chris Lattner3e130a22003-01-13 00:32:26 +00003314 return;
3315 }
3316
3317 // Handle casts from integer to floating point now...
3318 if (DestClass == cFP) {
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003319 // Promote the integer to a type supported by FLD. We do this because there
3320 // are no unsigned FLD instructions, so we must promote an unsigned value to
3321 // a larger signed value, then use FLD on the larger value.
3322 //
3323 const Type *PromoteType = 0;
Chris Lattner85aa7092004-04-10 18:32:01 +00003324 unsigned PromoteOpcode = 0;
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003325 unsigned RealDestReg = DestReg;
Chris Lattnerf70c22b2004-06-17 18:19:28 +00003326 switch (SrcTy->getTypeID()) {
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003327 case Type::BoolTyID:
3328 case Type::SByteTyID:
3329 // We don't have the facilities for directly loading byte sized data from
3330 // memory (even signed). Promote it to 16 bits.
3331 PromoteType = Type::ShortTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003332 PromoteOpcode = X86::MOVSX16rr8;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003333 break;
3334 case Type::UByteTyID:
3335 PromoteType = Type::ShortTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003336 PromoteOpcode = X86::MOVZX16rr8;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003337 break;
3338 case Type::UShortTyID:
3339 PromoteType = Type::IntTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003340 PromoteOpcode = X86::MOVZX32rr16;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003341 break;
3342 case Type::UIntTyID: {
3343 // Make a 64 bit temporary... and zero out the top of it...
3344 unsigned TmpReg = makeAnotherReg(Type::LongTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003345 BuildMI(*BB, IP, X86::MOV32rr, 1, TmpReg).addReg(SrcReg);
3346 BuildMI(*BB, IP, X86::MOV32ri, 1, TmpReg+1).addImm(0);
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003347 SrcTy = Type::LongTy;
3348 SrcClass = cLong;
3349 SrcReg = TmpReg;
3350 break;
3351 }
3352 case Type::ULongTyID:
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003353 // Don't fild into the read destination.
3354 DestReg = makeAnotherReg(Type::DoubleTy);
3355 break;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003356 default: // No promotion needed...
3357 break;
3358 }
3359
3360 if (PromoteType) {
3361 unsigned TmpReg = makeAnotherReg(PromoteType);
Chris Lattner7b92de1e2004-04-06 19:29:36 +00003362 BuildMI(*BB, IP, PromoteOpcode, 1, TmpReg).addReg(SrcReg);
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003363 SrcTy = PromoteType;
3364 SrcClass = getClass(PromoteType);
Chris Lattner3e130a22003-01-13 00:32:26 +00003365 SrcReg = TmpReg;
3366 }
3367
3368 // Spill the integer to memory and reload it from there...
Chris Lattnerb6bac512004-02-25 06:13:04 +00003369 int FrameIdx =
3370 F->getFrameInfo()->CreateStackObject(SrcTy, TM.getTargetData());
Chris Lattner3e130a22003-01-13 00:32:26 +00003371
3372 if (SrcClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003373 addFrameReference(BuildMI(*BB, IP, X86::MOV32mr, 5),
Chris Lattneree352852004-02-29 07:22:16 +00003374 FrameIdx).addReg(SrcReg);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003375 addFrameReference(BuildMI(*BB, IP, X86::MOV32mr, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003376 FrameIdx, 4).addReg(SrcReg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00003377 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003378 static const unsigned Op1[] = { X86::MOV8mr, X86::MOV16mr, X86::MOV32mr };
Chris Lattneree352852004-02-29 07:22:16 +00003379 addFrameReference(BuildMI(*BB, IP, Op1[SrcClass], 5),
3380 FrameIdx).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003381 }
3382
3383 static const unsigned Op2[] =
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003384 { 0/*byte*/, X86::FILD16m, X86::FILD32m, 0/*FP*/, X86::FILD64m };
Chris Lattneree352852004-02-29 07:22:16 +00003385 addFrameReference(BuildMI(*BB, IP, Op2[SrcClass], 5, DestReg), FrameIdx);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003386
3387 // We need special handling for unsigned 64-bit integer sources. If the
3388 // input number has the "sign bit" set, then we loaded it incorrectly as a
3389 // negative 64-bit number. In this case, add an offset value.
3390 if (SrcTy == Type::ULongTy) {
3391 // Emit a test instruction to see if the dynamic input value was signed.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003392 BuildMI(*BB, IP, X86::TEST32rr, 2).addReg(SrcReg+1).addReg(SrcReg+1);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003393
Chris Lattnerb6bac512004-02-25 06:13:04 +00003394 // If the sign bit is set, get a pointer to an offset, otherwise get a
3395 // pointer to a zero.
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003396 MachineConstantPool *CP = F->getConstantPool();
3397 unsigned Zero = makeAnotherReg(Type::IntTy);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003398 Constant *Null = Constant::getNullValue(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003399 addConstantPoolReference(BuildMI(*BB, IP, X86::LEA32r, 5, Zero),
Chris Lattnerb6bac512004-02-25 06:13:04 +00003400 CP->getConstantPoolIndex(Null));
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003401 unsigned Offset = makeAnotherReg(Type::IntTy);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003402 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
3403
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003404 addConstantPoolReference(BuildMI(*BB, IP, X86::LEA32r, 5, Offset),
Chris Lattnerb6bac512004-02-25 06:13:04 +00003405 CP->getConstantPoolIndex(OffsetCst));
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003406 unsigned Addr = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003407 BuildMI(*BB, IP, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003408
3409 // Load the constant for an add. FIXME: this could make an 'fadd' that
3410 // reads directly from memory, but we don't support these yet.
3411 unsigned ConstReg = makeAnotherReg(Type::DoubleTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003412 addDirectMem(BuildMI(*BB, IP, X86::FLD32m, 4, ConstReg), Addr);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003413
Chris Lattneree352852004-02-29 07:22:16 +00003414 BuildMI(*BB, IP, X86::FpADD, 2, RealDestReg)
3415 .addReg(ConstReg).addReg(DestReg);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003416 }
3417
Chris Lattner3e130a22003-01-13 00:32:26 +00003418 return;
3419 }
3420
3421 // Handle casts from floating point to integer now...
3422 if (SrcClass == cFP) {
3423 // Change the floating point control register to use "round towards zero"
3424 // mode when truncating to an integer value.
3425 //
3426 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003427 addFrameReference(BuildMI(*BB, IP, X86::FNSTCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003428
3429 // Load the old value of the high byte of the control word...
3430 unsigned HighPartOfCW = makeAnotherReg(Type::UByteTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003431 addFrameReference(BuildMI(*BB, IP, X86::MOV8rm, 4, HighPartOfCW),
Chris Lattneree352852004-02-29 07:22:16 +00003432 CWFrameIdx, 1);
Chris Lattner3e130a22003-01-13 00:32:26 +00003433
3434 // Set the high part to be round to zero...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003435 addFrameReference(BuildMI(*BB, IP, X86::MOV8mi, 5),
Chris Lattneree352852004-02-29 07:22:16 +00003436 CWFrameIdx, 1).addImm(12);
Chris Lattner3e130a22003-01-13 00:32:26 +00003437
3438 // Reload the modified control word now...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003439 addFrameReference(BuildMI(*BB, IP, X86::FLDCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003440
3441 // Restore the memory image of control word to original value
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003442 addFrameReference(BuildMI(*BB, IP, X86::MOV8mr, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003443 CWFrameIdx, 1).addReg(HighPartOfCW);
Chris Lattner3e130a22003-01-13 00:32:26 +00003444
3445 // We don't have the facilities for directly storing byte sized data to
3446 // memory. Promote it to 16 bits. We also must promote unsigned values to
3447 // larger classes because we only have signed FP stores.
3448 unsigned StoreClass = DestClass;
3449 const Type *StoreTy = DestTy;
3450 if (StoreClass == cByte || DestTy->isUnsigned())
3451 switch (StoreClass) {
3452 case cByte: StoreTy = Type::ShortTy; StoreClass = cShort; break;
3453 case cShort: StoreTy = Type::IntTy; StoreClass = cInt; break;
3454 case cInt: StoreTy = Type::LongTy; StoreClass = cLong; break;
Brian Gaeked4615052003-07-18 20:23:43 +00003455 // The following treatment of cLong may not be perfectly right,
3456 // but it survives chains of casts of the form
3457 // double->ulong->double.
3458 case cLong: StoreTy = Type::LongTy; StoreClass = cLong; break;
Chris Lattner3e130a22003-01-13 00:32:26 +00003459 default: assert(0 && "Unknown store class!");
3460 }
3461
3462 // Spill the integer to memory and reload it from there...
3463 int FrameIdx =
3464 F->getFrameInfo()->CreateStackObject(StoreTy, TM.getTargetData());
3465
3466 static const unsigned Op1[] =
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003467 { 0, X86::FIST16m, X86::FIST32m, 0, X86::FISTP64m };
Chris Lattneree352852004-02-29 07:22:16 +00003468 addFrameReference(BuildMI(*BB, IP, Op1[StoreClass], 5),
3469 FrameIdx).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003470
3471 if (DestClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003472 addFrameReference(BuildMI(*BB, IP, X86::MOV32rm, 4, DestReg), FrameIdx);
3473 addFrameReference(BuildMI(*BB, IP, X86::MOV32rm, 4, DestReg+1),
Chris Lattneree352852004-02-29 07:22:16 +00003474 FrameIdx, 4);
Chris Lattner3e130a22003-01-13 00:32:26 +00003475 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003476 static const unsigned Op2[] = { X86::MOV8rm, X86::MOV16rm, X86::MOV32rm };
Chris Lattneree352852004-02-29 07:22:16 +00003477 addFrameReference(BuildMI(*BB, IP, Op2[DestClass], 4, DestReg), FrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003478 }
3479
3480 // Reload the original control word now...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003481 addFrameReference(BuildMI(*BB, IP, X86::FLDCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003482 return;
3483 }
3484
Brian Gaeked474e9c2002-12-06 10:49:33 +00003485 // Anything we haven't handled already, we can't (yet) handle at all.
Chris Lattnerc53544a2003-05-12 20:16:58 +00003486 assert(0 && "Unhandled cast instruction!");
Chris Lattner548f61d2003-04-23 17:22:12 +00003487 abort();
Brian Gaekefa8d5712002-11-22 11:07:01 +00003488}
Brian Gaekea1719c92002-10-31 23:03:59 +00003489
Chris Lattner73815062003-10-18 05:56:40 +00003490/// visitVANextInst - Implement the va_next instruction...
Chris Lattnereca195e2003-05-08 19:44:13 +00003491///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003492void X86ISel::visitVANextInst(VANextInst &I) {
Chris Lattner73815062003-10-18 05:56:40 +00003493 unsigned VAList = getReg(I.getOperand(0));
Chris Lattnereca195e2003-05-08 19:44:13 +00003494 unsigned DestReg = getReg(I);
3495
Chris Lattnereca195e2003-05-08 19:44:13 +00003496 unsigned Size;
Chris Lattnerf70c22b2004-06-17 18:19:28 +00003497 switch (I.getArgType()->getTypeID()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00003498 default:
3499 std::cerr << I;
Chris Lattner73815062003-10-18 05:56:40 +00003500 assert(0 && "Error: bad type for va_next instruction!");
Chris Lattnereca195e2003-05-08 19:44:13 +00003501 return;
3502 case Type::PointerTyID:
3503 case Type::UIntTyID:
3504 case Type::IntTyID:
3505 Size = 4;
Chris Lattnereca195e2003-05-08 19:44:13 +00003506 break;
3507 case Type::ULongTyID:
3508 case Type::LongTyID:
Chris Lattnereca195e2003-05-08 19:44:13 +00003509 case Type::DoubleTyID:
3510 Size = 8;
Chris Lattnereca195e2003-05-08 19:44:13 +00003511 break;
3512 }
3513
3514 // Increment the VAList pointer...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003515 BuildMI(BB, X86::ADD32ri, 2, DestReg).addReg(VAList).addImm(Size);
Chris Lattner73815062003-10-18 05:56:40 +00003516}
Chris Lattnereca195e2003-05-08 19:44:13 +00003517
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003518void X86ISel::visitVAArgInst(VAArgInst &I) {
Chris Lattner73815062003-10-18 05:56:40 +00003519 unsigned VAList = getReg(I.getOperand(0));
3520 unsigned DestReg = getReg(I);
3521
Chris Lattnerf70c22b2004-06-17 18:19:28 +00003522 switch (I.getType()->getTypeID()) {
Chris Lattner73815062003-10-18 05:56:40 +00003523 default:
3524 std::cerr << I;
3525 assert(0 && "Error: bad type for va_next instruction!");
3526 return;
3527 case Type::PointerTyID:
3528 case Type::UIntTyID:
3529 case Type::IntTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003530 addDirectMem(BuildMI(BB, X86::MOV32rm, 4, DestReg), VAList);
Chris Lattner73815062003-10-18 05:56:40 +00003531 break;
3532 case Type::ULongTyID:
3533 case Type::LongTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003534 addDirectMem(BuildMI(BB, X86::MOV32rm, 4, DestReg), VAList);
3535 addRegOffset(BuildMI(BB, X86::MOV32rm, 4, DestReg+1), VAList, 4);
Chris Lattner73815062003-10-18 05:56:40 +00003536 break;
3537 case Type::DoubleTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003538 addDirectMem(BuildMI(BB, X86::FLD64m, 4, DestReg), VAList);
Chris Lattner73815062003-10-18 05:56:40 +00003539 break;
3540 }
Chris Lattnereca195e2003-05-08 19:44:13 +00003541}
3542
Misha Brukman538607f2004-03-01 23:53:11 +00003543/// visitGetElementPtrInst - instruction-select GEP instructions
3544///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003545void X86ISel::visitGetElementPtrInst(GetElementPtrInst &I) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00003546 // If this GEP instruction will be folded into all of its users, we don't need
3547 // to explicitly calculate it!
Reid Spencerfc989e12004-08-30 00:13:26 +00003548 X86AddressMode AM;
3549 if (isGEPFoldable(0, I.getOperand(0), I.op_begin()+1, I.op_end(), AM)) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00003550 // Check all of the users of the instruction to see if they are loads and
3551 // stores.
3552 bool AllWillFold = true;
3553 for (Value::use_iterator UI = I.use_begin(), E = I.use_end(); UI != E; ++UI)
3554 if (cast<Instruction>(*UI)->getOpcode() != Instruction::Load)
3555 if (cast<Instruction>(*UI)->getOpcode() != Instruction::Store ||
3556 cast<Instruction>(*UI)->getOperand(0) == &I) {
3557 AllWillFold = false;
3558 break;
3559 }
3560
3561 // If the instruction is foldable, and will be folded into all users, don't
3562 // emit it!
3563 if (AllWillFold) return;
3564 }
3565
Chris Lattner3e130a22003-01-13 00:32:26 +00003566 unsigned outputReg = getReg(I);
Chris Lattner827832c2004-02-22 17:05:38 +00003567 emitGEPOperation(BB, BB->end(), I.getOperand(0),
Brian Gaeke68b1edc2002-12-16 04:23:29 +00003568 I.op_begin()+1, I.op_end(), outputReg);
Chris Lattnerc0812d82002-12-13 06:56:29 +00003569}
3570
Chris Lattner985fe3d2004-02-25 03:45:50 +00003571/// getGEPIndex - Inspect the getelementptr operands specified with GEPOps and
3572/// GEPTypes (the derived types being stepped through at each level). On return
3573/// from this function, if some indexes of the instruction are representable as
3574/// an X86 lea instruction, the machine operands are put into the Ops
3575/// instruction and the consumed indexes are poped from the GEPOps/GEPTypes
3576/// lists. Otherwise, GEPOps.size() is returned. If this returns a an
3577/// addressing mode that only partially consumes the input, the BaseReg input of
3578/// the addressing mode must be left free.
3579///
3580/// Note that there is one fewer entry in GEPTypes than there is in GEPOps.
3581///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003582void X86ISel::getGEPIndex(MachineBasicBlock *MBB,
3583 MachineBasicBlock::iterator IP,
3584 std::vector<Value*> &GEPOps,
3585 std::vector<const Type*> &GEPTypes,
3586 X86AddressMode &AM) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00003587 const TargetData &TD = TM.getTargetData();
3588
Chris Lattner985fe3d2004-02-25 03:45:50 +00003589 // Clear out the state we are working with...
Reid Spencerfc989e12004-08-30 00:13:26 +00003590 AM.BaseType = X86AddressMode::RegBase;
3591 AM.Base.Reg = 0; // No base register
3592 AM.Scale = 1; // Unit scale
3593 AM.IndexReg = 0; // No index register
3594 AM.Disp = 0; // No displacement
Chris Lattnerb6bac512004-02-25 06:13:04 +00003595
Chris Lattner985fe3d2004-02-25 03:45:50 +00003596 // While there are GEP indexes that can be folded into the current address,
3597 // keep processing them.
3598 while (!GEPTypes.empty()) {
3599 if (const StructType *StTy = dyn_cast<StructType>(GEPTypes.back())) {
3600 // It's a struct access. CUI is the index into the structure,
3601 // which names the field. This index must have unsigned type.
3602 const ConstantUInt *CUI = cast<ConstantUInt>(GEPOps.back());
3603
3604 // Use the TargetData structure to pick out what the layout of the
3605 // structure is in memory. Since the structure index must be constant, we
3606 // can get its value and use it to find the right byte offset from the
3607 // StructLayout class's list of structure member offsets.
Reid Spencerfc989e12004-08-30 00:13:26 +00003608 AM.Disp += TD.getStructLayout(StTy)->MemberOffsets[CUI->getValue()];
Chris Lattner985fe3d2004-02-25 03:45:50 +00003609 GEPOps.pop_back(); // Consume a GEP operand
3610 GEPTypes.pop_back();
3611 } else {
3612 // It's an array or pointer access: [ArraySize x ElementType].
3613 const SequentialType *SqTy = cast<SequentialType>(GEPTypes.back());
3614 Value *idx = GEPOps.back();
3615
3616 // idx is the index into the array. Unlike with structure
3617 // indices, we may not know its actual value at code-generation
3618 // time.
Chris Lattner985fe3d2004-02-25 03:45:50 +00003619
3620 // If idx is a constant, fold it into the offset.
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003621 unsigned TypeSize = TD.getTypeSize(SqTy->getElementType());
Chris Lattner985fe3d2004-02-25 03:45:50 +00003622 if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(idx)) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003623 AM.Disp += TypeSize*CSI->getValue();
Chris Lattner28977af2004-04-05 01:30:19 +00003624 } else if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(idx)) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003625 AM.Disp += TypeSize*CUI->getValue();
Chris Lattner985fe3d2004-02-25 03:45:50 +00003626 } else {
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003627 // If the index reg is already taken, we can't handle this index.
Reid Spencerfc989e12004-08-30 00:13:26 +00003628 if (AM.IndexReg) return;
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003629
3630 // If this is a size that we can handle, then add the index as
3631 switch (TypeSize) {
3632 case 1: case 2: case 4: case 8:
3633 // These are all acceptable scales on X86.
Reid Spencerfc989e12004-08-30 00:13:26 +00003634 AM.Scale = TypeSize;
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003635 break;
3636 default:
3637 // Otherwise, we can't handle this scale
3638 return;
3639 }
3640
3641 if (CastInst *CI = dyn_cast<CastInst>(idx))
3642 if (CI->getOperand(0)->getType() == Type::IntTy ||
3643 CI->getOperand(0)->getType() == Type::UIntTy)
3644 idx = CI->getOperand(0);
3645
Reid Spencerfc989e12004-08-30 00:13:26 +00003646 AM.IndexReg = MBB ? getReg(idx, MBB, IP) : 1;
Chris Lattner985fe3d2004-02-25 03:45:50 +00003647 }
3648
3649 GEPOps.pop_back(); // Consume a GEP operand
3650 GEPTypes.pop_back();
3651 }
3652 }
Chris Lattnerb6bac512004-02-25 06:13:04 +00003653
Chris Lattnerdf040972004-05-23 21:23:12 +00003654 // GEPTypes is empty, which means we have a single operand left. Set it as
3655 // the base register.
Chris Lattnerb6bac512004-02-25 06:13:04 +00003656 //
Reid Spencerfc989e12004-08-30 00:13:26 +00003657 assert(AM.Base.Reg == 0);
Chris Lattnerdf040972004-05-23 21:23:12 +00003658
Reid Spencerfc989e12004-08-30 00:13:26 +00003659 if (AllocaInst *AI = dyn_castFixedAlloca(GEPOps.back())) {
3660 AM.BaseType = X86AddressMode::FrameIndexBase;
3661 AM.Base.FrameIndex = getFixedSizedAllocaFI(AI);
Chris Lattnerdf040972004-05-23 21:23:12 +00003662 GEPOps.pop_back();
3663 return;
Reid Spencerfc989e12004-08-30 00:13:26 +00003664 }
3665
3666#if 0 // FIXME: TODO!
3667 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattnerdf040972004-05-23 21:23:12 +00003668 // FIXME: When addressing modes are more powerful/correct, we could load
3669 // global addresses directly as 32-bit immediates.
3670 }
3671#endif
3672
Reid Spencerfc989e12004-08-30 00:13:26 +00003673 AM.Base.Reg = MBB ? getReg(GEPOps[0], MBB, IP) : 1;
Chris Lattnerb6bac512004-02-25 06:13:04 +00003674 GEPOps.pop_back(); // Consume the last GEP operand
Chris Lattner985fe3d2004-02-25 03:45:50 +00003675}
3676
3677
Chris Lattnerb6bac512004-02-25 06:13:04 +00003678/// isGEPFoldable - Return true if the specified GEP can be completely
3679/// folded into the addressing mode of a load/store or lea instruction.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003680bool X86ISel::isGEPFoldable(MachineBasicBlock *MBB,
3681 Value *Src, User::op_iterator IdxBegin,
3682 User::op_iterator IdxEnd, X86AddressMode &AM) {
Chris Lattner7ca04092004-02-22 17:35:42 +00003683
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003684 std::vector<Value*> GEPOps;
3685 GEPOps.resize(IdxEnd-IdxBegin+1);
3686 GEPOps[0] = Src;
3687 std::copy(IdxBegin, IdxEnd, GEPOps.begin()+1);
3688
Chris Lattnerdf040972004-05-23 21:23:12 +00003689 std::vector<const Type*>
3690 GEPTypes(gep_type_begin(Src->getType(), IdxBegin, IdxEnd),
3691 gep_type_end(Src->getType(), IdxBegin, IdxEnd));
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003692
Chris Lattnerb6bac512004-02-25 06:13:04 +00003693 MachineBasicBlock::iterator IP;
3694 if (MBB) IP = MBB->end();
Reid Spencerfc989e12004-08-30 00:13:26 +00003695 getGEPIndex(MBB, IP, GEPOps, GEPTypes, AM);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003696
3697 // We can fold it away iff the getGEPIndex call eliminated all operands.
3698 return GEPOps.empty();
3699}
3700
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003701void X86ISel::emitGEPOperation(MachineBasicBlock *MBB,
3702 MachineBasicBlock::iterator IP,
3703 Value *Src, User::op_iterator IdxBegin,
3704 User::op_iterator IdxEnd, unsigned TargetReg) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00003705 const TargetData &TD = TM.getTargetData();
Chris Lattnerb6bac512004-02-25 06:13:04 +00003706
Chris Lattnerd2995df2004-07-15 00:58:53 +00003707 // If this is a getelementptr null, with all constant integer indices, just
3708 // replace it with TargetReg = 42.
3709 if (isa<ConstantPointerNull>(Src)) {
3710 User::op_iterator I = IdxBegin;
3711 for (; I != IdxEnd; ++I)
3712 if (!isa<ConstantInt>(*I))
3713 break;
3714 if (I == IdxEnd) { // All constant indices
3715 unsigned Offset = TD.getIndexedOffset(Src->getType(),
3716 std::vector<Value*>(IdxBegin, IdxEnd));
3717 BuildMI(*MBB, IP, X86::MOV32ri, 1, TargetReg).addImm(Offset);
3718 return;
3719 }
3720 }
3721
Chris Lattnerb6bac512004-02-25 06:13:04 +00003722 std::vector<Value*> GEPOps;
3723 GEPOps.resize(IdxEnd-IdxBegin+1);
3724 GEPOps[0] = Src;
3725 std::copy(IdxBegin, IdxEnd, GEPOps.begin()+1);
3726
3727 std::vector<const Type*> GEPTypes;
3728 GEPTypes.assign(gep_type_begin(Src->getType(), IdxBegin, IdxEnd),
3729 gep_type_end(Src->getType(), IdxBegin, IdxEnd));
Chris Lattner985fe3d2004-02-25 03:45:50 +00003730
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003731 // Keep emitting instructions until we consume the entire GEP instruction.
3732 while (!GEPOps.empty()) {
3733 unsigned OldSize = GEPOps.size();
Reid Spencerfc989e12004-08-30 00:13:26 +00003734 X86AddressMode AM;
3735 getGEPIndex(MBB, IP, GEPOps, GEPTypes, AM);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003736
Chris Lattner985fe3d2004-02-25 03:45:50 +00003737 if (GEPOps.size() != OldSize) {
3738 // getGEPIndex consumed some of the input. Build an LEA instruction here.
Chris Lattnerb6bac512004-02-25 06:13:04 +00003739 unsigned NextTarget = 0;
3740 if (!GEPOps.empty()) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003741 assert(AM.Base.Reg == 0 &&
Chris Lattnerb6bac512004-02-25 06:13:04 +00003742 "getGEPIndex should have left the base register open for chaining!");
Reid Spencerfc989e12004-08-30 00:13:26 +00003743 NextTarget = AM.Base.Reg = makeAnotherReg(Type::UIntTy);
Chris Lattner985fe3d2004-02-25 03:45:50 +00003744 }
Chris Lattnerb6bac512004-02-25 06:13:04 +00003745
Reid Spencerfc989e12004-08-30 00:13:26 +00003746 if (AM.BaseType == X86AddressMode::RegBase &&
3747 AM.IndexReg == 0 && AM.Disp == 0)
3748 BuildMI(*MBB, IP, X86::MOV32rr, 1, TargetReg).addReg(AM.Base.Reg);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003749 else
Reid Spencerfc989e12004-08-30 00:13:26 +00003750 addFullAddress(BuildMI(*MBB, IP, X86::LEA32r, 5, TargetReg), AM);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003751 --IP;
3752 TargetReg = NextTarget;
Chris Lattner985fe3d2004-02-25 03:45:50 +00003753 } else if (GEPTypes.empty()) {
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003754 // The getGEPIndex operation didn't want to build an LEA. Check to see if
3755 // all operands are consumed but the base pointer. If so, just load it
3756 // into the register.
Chris Lattner7ca04092004-02-22 17:35:42 +00003757 if (GlobalValue *GV = dyn_cast<GlobalValue>(GEPOps[0])) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003758 BuildMI(*MBB, IP, X86::MOV32ri, 1, TargetReg).addGlobalAddress(GV);
Chris Lattner7ca04092004-02-22 17:35:42 +00003759 } else {
3760 unsigned BaseReg = getReg(GEPOps[0], MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003761 BuildMI(*MBB, IP, X86::MOV32rr, 1, TargetReg).addReg(BaseReg);
Chris Lattner7ca04092004-02-22 17:35:42 +00003762 }
3763 break; // we are now done
Chris Lattnerb6bac512004-02-25 06:13:04 +00003764
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003765 } else {
Brian Gaeke20244b72002-12-12 15:33:40 +00003766 // It's an array or pointer access: [ArraySize x ElementType].
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003767 const SequentialType *SqTy = cast<SequentialType>(GEPTypes.back());
3768 Value *idx = GEPOps.back();
3769 GEPOps.pop_back(); // Consume a GEP operand
3770 GEPTypes.pop_back();
Chris Lattner8a307e82002-12-16 19:32:50 +00003771
Chris Lattner28977af2004-04-05 01:30:19 +00003772 // Many GEP instructions use a [cast (int/uint) to LongTy] as their
Chris Lattnerf5854472003-06-21 16:01:24 +00003773 // operand on X86. Handle this case directly now...
3774 if (CastInst *CI = dyn_cast<CastInst>(idx))
3775 if (CI->getOperand(0)->getType() == Type::IntTy ||
3776 CI->getOperand(0)->getType() == Type::UIntTy)
3777 idx = CI->getOperand(0);
3778
Chris Lattner3e130a22003-01-13 00:32:26 +00003779 // We want to add BaseReg to(idxReg * sizeof ElementType). First, we
Chris Lattner8a307e82002-12-16 19:32:50 +00003780 // must find the size of the pointed-to type (Not coincidentally, the next
3781 // type is the type of the elements in the array).
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003782 const Type *ElTy = SqTy->getElementType();
3783 unsigned elementSize = TD.getTypeSize(ElTy);
Chris Lattner8a307e82002-12-16 19:32:50 +00003784
3785 // If idxReg is a constant, we don't need to perform the multiply!
Chris Lattner28977af2004-04-05 01:30:19 +00003786 if (ConstantInt *CSI = dyn_cast<ConstantInt>(idx)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00003787 if (!CSI->isNullValue()) {
Chris Lattner28977af2004-04-05 01:30:19 +00003788 unsigned Offset = elementSize*CSI->getRawValue();
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003789 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003790 BuildMI(*MBB, IP, X86::ADD32ri, 2, TargetReg)
Chris Lattneree352852004-02-29 07:22:16 +00003791 .addReg(Reg).addImm(Offset);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003792 --IP; // Insert the next instruction before this one.
3793 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00003794 }
3795 } else if (elementSize == 1) {
3796 // If the element size is 1, we don't have to multiply, just add
3797 unsigned idxReg = getReg(idx, MBB, IP);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003798 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003799 BuildMI(*MBB, IP, X86::ADD32rr, 2,TargetReg).addReg(Reg).addReg(idxReg);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003800 --IP; // Insert the next instruction before this one.
3801 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00003802 } else {
3803 unsigned idxReg = getReg(idx, MBB, IP);
3804 unsigned OffsetReg = makeAnotherReg(Type::UIntTy);
Chris Lattnerb2acc512003-10-19 21:09:10 +00003805
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003806 // Make sure we can back the iterator up to point to the first
3807 // instruction emitted.
3808 MachineBasicBlock::iterator BeforeIt = IP;
3809 if (IP == MBB->begin())
3810 BeforeIt = MBB->end();
3811 else
3812 --BeforeIt;
Chris Lattnerb2acc512003-10-19 21:09:10 +00003813 doMultiplyConst(MBB, IP, OffsetReg, Type::IntTy, idxReg, elementSize);
3814
Chris Lattner8a307e82002-12-16 19:32:50 +00003815 // Emit an ADD to add OffsetReg to the basePtr.
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003816 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003817 BuildMI(*MBB, IP, X86::ADD32rr, 2, TargetReg)
Chris Lattneree352852004-02-29 07:22:16 +00003818 .addReg(Reg).addReg(OffsetReg);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003819
3820 // Step to the first instruction of the multiply.
3821 if (BeforeIt == MBB->end())
3822 IP = MBB->begin();
3823 else
3824 IP = ++BeforeIt;
3825
3826 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00003827 }
Brian Gaeke20244b72002-12-12 15:33:40 +00003828 }
Brian Gaeke20244b72002-12-12 15:33:40 +00003829 }
Brian Gaeke20244b72002-12-12 15:33:40 +00003830}
3831
Chris Lattner065faeb2002-12-28 20:24:02 +00003832/// visitAllocaInst - If this is a fixed size alloca, allocate space from the
3833/// frame manager, otherwise do it the hard way.
3834///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003835void X86ISel::visitAllocaInst(AllocaInst &I) {
Chris Lattner9f1b5312004-05-13 15:12:43 +00003836 // If this is a fixed size alloca in the entry block for the function, we
3837 // statically stack allocate the space, so we don't need to do anything here.
3838 //
Chris Lattnercb2fd552004-05-13 07:40:27 +00003839 if (dyn_castFixedAlloca(&I)) return;
Chris Lattner9f1b5312004-05-13 15:12:43 +00003840
Brian Gaekee48ec012002-12-13 06:46:31 +00003841 // Find the data size of the alloca inst's getAllocatedType.
Chris Lattner065faeb2002-12-28 20:24:02 +00003842 const Type *Ty = I.getAllocatedType();
3843 unsigned TySize = TM.getTargetData().getTypeSize(Ty);
3844
Chris Lattner065faeb2002-12-28 20:24:02 +00003845 // Create a register to hold the temporary result of multiplying the type size
3846 // constant by the variable amount.
3847 unsigned TotalSizeReg = makeAnotherReg(Type::UIntTy);
3848 unsigned SrcReg1 = getReg(I.getArraySize());
Chris Lattner065faeb2002-12-28 20:24:02 +00003849
3850 // TotalSizeReg = mul <numelements>, <TypeSize>
3851 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattnerb2acc512003-10-19 21:09:10 +00003852 doMultiplyConst(BB, MBBI, TotalSizeReg, Type::UIntTy, SrcReg1, TySize);
Chris Lattner065faeb2002-12-28 20:24:02 +00003853
3854 // AddedSize = add <TotalSizeReg>, 15
3855 unsigned AddedSizeReg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003856 BuildMI(BB, X86::ADD32ri, 2, AddedSizeReg).addReg(TotalSizeReg).addImm(15);
Chris Lattner065faeb2002-12-28 20:24:02 +00003857
3858 // AlignedSize = and <AddedSize>, ~15
3859 unsigned AlignedSize = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003860 BuildMI(BB, X86::AND32ri, 2, AlignedSize).addReg(AddedSizeReg).addImm(~15);
Chris Lattner065faeb2002-12-28 20:24:02 +00003861
Brian Gaekee48ec012002-12-13 06:46:31 +00003862 // Subtract size from stack pointer, thereby allocating some space.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003863 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(AlignedSize);
Chris Lattner065faeb2002-12-28 20:24:02 +00003864
Brian Gaekee48ec012002-12-13 06:46:31 +00003865 // Put a pointer to the space into the result register, by copying
3866 // the stack pointer.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003867 BuildMI(BB, X86::MOV32rr, 1, getReg(I)).addReg(X86::ESP);
Chris Lattner065faeb2002-12-28 20:24:02 +00003868
Misha Brukman48196b32003-05-03 02:18:17 +00003869 // Inform the Frame Information that we have just allocated a variable-sized
Chris Lattner065faeb2002-12-28 20:24:02 +00003870 // object.
3871 F->getFrameInfo()->CreateVariableSizedObject();
Brian Gaeke20244b72002-12-12 15:33:40 +00003872}
Chris Lattner3e130a22003-01-13 00:32:26 +00003873
3874/// visitMallocInst - Malloc instructions are code generated into direct calls
3875/// to the library malloc.
3876///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003877void X86ISel::visitMallocInst(MallocInst &I) {
Chris Lattner3e130a22003-01-13 00:32:26 +00003878 unsigned AllocSize = TM.getTargetData().getTypeSize(I.getAllocatedType());
3879 unsigned Arg;
3880
3881 if (ConstantUInt *C = dyn_cast<ConstantUInt>(I.getOperand(0))) {
3882 Arg = getReg(ConstantUInt::get(Type::UIntTy, C->getValue() * AllocSize));
3883 } else {
3884 Arg = makeAnotherReg(Type::UIntTy);
Chris Lattnerb2acc512003-10-19 21:09:10 +00003885 unsigned Op0Reg = getReg(I.getOperand(0));
Chris Lattner3e130a22003-01-13 00:32:26 +00003886 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattnerb2acc512003-10-19 21:09:10 +00003887 doMultiplyConst(BB, MBBI, Arg, Type::UIntTy, Op0Reg, AllocSize);
Chris Lattner3e130a22003-01-13 00:32:26 +00003888 }
3889
3890 std::vector<ValueRecord> Args;
3891 Args.push_back(ValueRecord(Arg, Type::UIntTy));
3892 MachineInstr *TheCall = BuildMI(X86::CALLpcrel32,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003893 1).addExternalSymbol("malloc", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00003894 doCall(ValueRecord(getReg(I), I.getType()), TheCall, Args);
3895}
3896
3897
3898/// visitFreeInst - Free instructions are code gen'd to call the free libc
3899/// function.
3900///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003901void X86ISel::visitFreeInst(FreeInst &I) {
Chris Lattner3e130a22003-01-13 00:32:26 +00003902 std::vector<ValueRecord> Args;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00003903 Args.push_back(ValueRecord(I.getOperand(0)));
Chris Lattner3e130a22003-01-13 00:32:26 +00003904 MachineInstr *TheCall = BuildMI(X86::CALLpcrel32,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003905 1).addExternalSymbol("free", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00003906 doCall(ValueRecord(0, Type::VoidTy), TheCall, Args);
3907}
3908
Chris Lattnerd281de22003-07-26 23:49:58 +00003909/// createX86SimpleInstructionSelector - This pass converts an LLVM function
Chris Lattnerb4f68ed2002-10-29 22:37:54 +00003910/// into a machine code representation is a very simple peep-hole fashion. The
Chris Lattner72614082002-10-25 22:55:53 +00003911/// generated code sucks but the implementation is nice and simple.
3912///
Chris Lattnerf70e0c22003-12-28 21:23:38 +00003913FunctionPass *llvm::createX86SimpleInstructionSelector(TargetMachine &TM) {
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003914 return new X86ISel(TM);
Chris Lattner72614082002-10-25 22:55:53 +00003915}