blob: ed93101ecb72a1e78be962ad7bbcb8d356c0c5b3 [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 Lattnerc0354c92004-12-13 17:23:11 +0000118 // If this is main, emit special code.
119 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
120 EmitSpecialCodeForMain();
121
Chris Lattner333b2fa2002-12-13 10:09:43 +0000122 // Instruction select everything except PHI nodes
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000123 visit(Fn);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000124
125 // Select the PHI nodes
126 SelectPHINodes();
127
Chris Lattner986618e2004-02-22 19:47:26 +0000128 // Insert the FP_REG_KILL instructions into blocks that need them.
129 InsertFPRegKills();
130
Chris Lattner72614082002-10-25 22:55:53 +0000131 RegMap.clear();
Chris Lattner333b2fa2002-12-13 10:09:43 +0000132 MBBMap.clear();
Chris Lattnercb2fd552004-05-13 07:40:27 +0000133 AllocaMap.clear();
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000134 F = 0;
Chris Lattner2a865b02003-07-26 23:05:37 +0000135 // We always build a machine code representation for the function
136 return true;
Chris Lattner72614082002-10-25 22:55:53 +0000137 }
138
Chris Lattnerf0eb7be2002-12-15 21:13:40 +0000139 virtual const char *getPassName() const {
140 return "X86 Simple Instruction Selection";
141 }
142
Chris Lattnerc0354c92004-12-13 17:23:11 +0000143 /// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
144 /// the main function.
145 void EmitSpecialCodeForMain();
146
Chris Lattner72614082002-10-25 22:55:53 +0000147 /// visitBasicBlock - This method is called when we are visiting a new basic
Chris Lattner33f53b52002-10-29 20:48:56 +0000148 /// block. This simply creates a new MachineBasicBlock to emit code into
149 /// and adds it to the current MachineFunction. Subsequent visit* for
150 /// instructions will be invoked for all instructions in the basic block.
Chris Lattner72614082002-10-25 22:55:53 +0000151 ///
152 void visitBasicBlock(BasicBlock &LLVM_BB) {
Chris Lattner333b2fa2002-12-13 10:09:43 +0000153 BB = MBBMap[&LLVM_BB];
Chris Lattner72614082002-10-25 22:55:53 +0000154 }
155
Chris Lattner44827152003-12-28 09:47:19 +0000156 /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
157 /// function, lowering any calls to unknown intrinsic functions into the
158 /// equivalent LLVM code.
Misha Brukman538607f2004-03-01 23:53:11 +0000159 ///
Chris Lattner44827152003-12-28 09:47:19 +0000160 void LowerUnknownIntrinsicFunctionCalls(Function &F);
161
Chris Lattner065faeb2002-12-28 20:24:02 +0000162 /// LoadArgumentsToVirtualRegs - Load all of the arguments to this function
163 /// from the stack into virtual registers.
164 ///
165 void LoadArgumentsToVirtualRegs(Function &F);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000166
167 /// SelectPHINodes - Insert machine code to generate phis. This is tricky
168 /// because we have to generate our sources into the source basic blocks,
169 /// not the current one.
170 ///
171 void SelectPHINodes();
172
Chris Lattner986618e2004-02-22 19:47:26 +0000173 /// InsertFPRegKills - Insert FP_REG_KILL instructions into basic blocks
174 /// that need them. This only occurs due to the floating point stackifier
175 /// not being aggressive enough to handle arbitrary global stackification.
176 ///
177 void InsertFPRegKills();
178
Chris Lattner72614082002-10-25 22:55:53 +0000179 // Visitation methods for various instructions. These methods simply emit
180 // fixed X86 code for each instruction.
181 //
Brian Gaekefa8d5712002-11-22 11:07:01 +0000182
183 // Control flow operators
Chris Lattner72614082002-10-25 22:55:53 +0000184 void visitReturnInst(ReturnInst &RI);
Chris Lattner2df035b2002-11-02 19:27:56 +0000185 void visitBranchInst(BranchInst &BI);
Chris Lattner30483b02004-10-16 18:13:05 +0000186 void visitUnreachableInst(UnreachableInst &UI) {}
Chris Lattner3e130a22003-01-13 00:32:26 +0000187
188 struct ValueRecord {
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000189 Value *Val;
Chris Lattner3e130a22003-01-13 00:32:26 +0000190 unsigned Reg;
191 const Type *Ty;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000192 ValueRecord(unsigned R, const Type *T) : Val(0), Reg(R), Ty(T) {}
193 ValueRecord(Value *V) : Val(V), Reg(0), Ty(V->getType()) {}
Chris Lattner3e130a22003-01-13 00:32:26 +0000194 };
195 void doCall(const ValueRecord &Ret, MachineInstr *CallMI,
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000196 const std::vector<ValueRecord> &Args);
Brian Gaekefa8d5712002-11-22 11:07:01 +0000197 void visitCallInst(CallInst &I);
Brian Gaeked0fde302003-11-11 22:41:34 +0000198 void visitIntrinsicCall(Intrinsic::ID ID, CallInst &I);
Chris Lattnere2954c82002-11-02 20:04:26 +0000199
200 // Arithmetic operators
Chris Lattnerf01729e2002-11-02 20:54:46 +0000201 void visitSimpleBinary(BinaryOperator &B, unsigned OpcodeClass);
Chris Lattner68aad932002-11-02 20:13:22 +0000202 void visitAdd(BinaryOperator &B) { visitSimpleBinary(B, 0); }
203 void visitSub(BinaryOperator &B) { visitSimpleBinary(B, 1); }
Chris Lattnerca9671d2002-11-02 20:28:58 +0000204 void visitMul(BinaryOperator &B);
Chris Lattnere2954c82002-11-02 20:04:26 +0000205
Chris Lattnerf01729e2002-11-02 20:54:46 +0000206 void visitDiv(BinaryOperator &B) { visitDivRem(B); }
207 void visitRem(BinaryOperator &B) { visitDivRem(B); }
208 void visitDivRem(BinaryOperator &B);
209
Chris Lattnere2954c82002-11-02 20:04:26 +0000210 // Bitwise operators
Chris Lattner68aad932002-11-02 20:13:22 +0000211 void visitAnd(BinaryOperator &B) { visitSimpleBinary(B, 2); }
212 void visitOr (BinaryOperator &B) { visitSimpleBinary(B, 3); }
213 void visitXor(BinaryOperator &B) { visitSimpleBinary(B, 4); }
Chris Lattnere2954c82002-11-02 20:04:26 +0000214
Chris Lattner6d40c192003-01-16 16:43:00 +0000215 // Comparison operators...
216 void visitSetCondInst(SetCondInst &I);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000217 unsigned EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
218 MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000219 MachineBasicBlock::iterator MBBI);
Chris Lattner12d96a02004-03-30 21:22:00 +0000220 void visitSelectInst(SelectInst &SI);
221
Chris Lattnerb2acc512003-10-19 21:09:10 +0000222
Chris Lattner6fc3c522002-11-17 21:11:55 +0000223 // Memory Instructions
224 void visitLoadInst(LoadInst &I);
225 void visitStoreInst(StoreInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000226 void visitGetElementPtrInst(GetElementPtrInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000227 void visitAllocaInst(AllocaInst &I);
Chris Lattner3e130a22003-01-13 00:32:26 +0000228 void visitMallocInst(MallocInst &I);
229 void visitFreeInst(FreeInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000230
Chris Lattnere2954c82002-11-02 20:04:26 +0000231 // Other operators
Brian Gaekea1719c92002-10-31 23:03:59 +0000232 void visitShiftInst(ShiftInst &I);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000233 void visitPHINode(PHINode &I) {} // PHI nodes handled by second pass
Brian Gaekefa8d5712002-11-22 11:07:01 +0000234 void visitCastInst(CastInst &I);
Chris Lattner73815062003-10-18 05:56:40 +0000235 void visitVANextInst(VANextInst &I);
236 void visitVAArgInst(VAArgInst &I);
Chris Lattner72614082002-10-25 22:55:53 +0000237
238 void visitInstruction(Instruction &I) {
239 std::cerr << "Cannot instruction select: " << I;
240 abort();
241 }
242
Brian Gaeke95780cc2002-12-13 07:56:18 +0000243 /// promote32 - Make a value 32-bits wide, and put it somewhere.
Chris Lattner3e130a22003-01-13 00:32:26 +0000244 ///
245 void promote32(unsigned targetReg, const ValueRecord &VR);
246
Chris Lattner721d2d42004-03-08 01:18:36 +0000247 /// getAddressingMode - Get the addressing mode to use to address the
248 /// specified value. The returned value should be used with addFullAddress.
Reid Spencerfc989e12004-08-30 00:13:26 +0000249 void getAddressingMode(Value *Addr, X86AddressMode &AM);
Chris Lattner721d2d42004-03-08 01:18:36 +0000250
251
252 /// getGEPIndex - This is used to fold GEP instructions into X86 addressing
253 /// expressions.
Chris Lattnerb6bac512004-02-25 06:13:04 +0000254 void getGEPIndex(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
255 std::vector<Value*> &GEPOps,
Reid Spencerfc989e12004-08-30 00:13:26 +0000256 std::vector<const Type*> &GEPTypes,
257 X86AddressMode &AM);
Chris Lattnerb6bac512004-02-25 06:13:04 +0000258
259 /// isGEPFoldable - Return true if the specified GEP can be completely
260 /// folded into the addressing mode of a load/store or lea instruction.
261 bool isGEPFoldable(MachineBasicBlock *MBB,
262 Value *Src, User::op_iterator IdxBegin,
Reid Spencerfc989e12004-08-30 00:13:26 +0000263 User::op_iterator IdxEnd, X86AddressMode &AM);
Chris Lattnerb6bac512004-02-25 06:13:04 +0000264
Chris Lattner3e130a22003-01-13 00:32:26 +0000265 /// emitGEPOperation - Common code shared between visitGetElementPtrInst and
266 /// constant expression GEP support.
267 ///
Chris Lattner827832c2004-02-22 17:05:38 +0000268 void emitGEPOperation(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
Chris Lattner333b2fa2002-12-13 10:09:43 +0000269 Value *Src, User::op_iterator IdxBegin,
Chris Lattnerc0812d82002-12-13 06:56:29 +0000270 User::op_iterator IdxEnd, unsigned TargetReg);
271
Chris Lattner548f61d2003-04-23 17:22:12 +0000272 /// emitCastOperation - Common code shared between visitCastInst and
273 /// constant expression cast support.
Misha Brukman538607f2004-03-01 23:53:11 +0000274 ///
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000275 void emitCastOperation(MachineBasicBlock *BB,MachineBasicBlock::iterator IP,
Chris Lattner548f61d2003-04-23 17:22:12 +0000276 Value *Src, const Type *DestTy, unsigned TargetReg);
277
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000278 /// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
279 /// and constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000280 ///
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000281 void emitSimpleBinaryOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000282 MachineBasicBlock::iterator IP,
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000283 Value *Op0, Value *Op1,
284 unsigned OperatorClass, unsigned TargetReg);
285
Chris Lattner6621ed92004-04-11 21:23:56 +0000286 /// emitBinaryFPOperation - This method handles emission of floating point
287 /// Add (0), Sub (1), Mul (2), and Div (3) operations.
288 void emitBinaryFPOperation(MachineBasicBlock *BB,
289 MachineBasicBlock::iterator IP,
290 Value *Op0, Value *Op1,
291 unsigned OperatorClass, unsigned TargetReg);
292
Chris Lattner462fa822004-04-11 20:56:28 +0000293 void emitMultiply(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
294 Value *Op0, Value *Op1, unsigned TargetReg);
295
296 void doMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI,
297 unsigned DestReg, const Type *DestTy,
298 unsigned Op0Reg, unsigned Op1Reg);
299 void doMultiplyConst(MachineBasicBlock *MBB,
300 MachineBasicBlock::iterator MBBI,
301 unsigned DestReg, const Type *DestTy,
302 unsigned Op0Reg, unsigned Op1Val);
303
Chris Lattnercadff442003-10-23 17:21:43 +0000304 void emitDivRemOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000305 MachineBasicBlock::iterator IP,
Chris Lattner462fa822004-04-11 20:56:28 +0000306 Value *Op0, Value *Op1, bool isDiv,
307 unsigned TargetReg);
Chris Lattnercadff442003-10-23 17:21:43 +0000308
Chris Lattner58c41fe2003-08-24 19:19:47 +0000309 /// emitSetCCOperation - Common code shared between visitSetCondInst and
310 /// constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000311 ///
Chris Lattner58c41fe2003-08-24 19:19:47 +0000312 void emitSetCCOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000313 MachineBasicBlock::iterator IP,
Chris Lattner58c41fe2003-08-24 19:19:47 +0000314 Value *Op0, Value *Op1, unsigned Opcode,
315 unsigned TargetReg);
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000316
317 /// emitShiftOperation - Common code shared between visitShiftInst and
318 /// constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000319 ///
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000320 void emitShiftOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000321 MachineBasicBlock::iterator IP,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000322 Value *Op, Value *ShiftAmount, bool isLeftShift,
323 const Type *ResultTy, unsigned DestReg);
Chris Lattnerce7cafa2004-11-13 20:48:57 +0000324
325 // Emit code for a 'SHLD DestReg, Op0, Op1, Amt' operation, where Amt is a
326 // constant.
327 void doSHLDConst(MachineBasicBlock *MBB,
328 MachineBasicBlock::iterator MBBI,
329 unsigned DestReg, unsigned Op0Reg, unsigned Op1Reg,
330 unsigned Op1Val);
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000331
Chris Lattner12d96a02004-03-30 21:22:00 +0000332 /// emitSelectOperation - Common code shared between visitSelectInst and the
333 /// constant expression support.
334 void emitSelectOperation(MachineBasicBlock *MBB,
335 MachineBasicBlock::iterator IP,
336 Value *Cond, Value *TrueVal, Value *FalseVal,
337 unsigned DestReg);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000338
Chris Lattnerc5291f52002-10-27 21:16:59 +0000339 /// copyConstantToRegister - Output the instructions required to put the
340 /// specified constant into the specified register.
341 ///
Chris Lattner8a307e82002-12-16 19:32:50 +0000342 void copyConstantToRegister(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000343 MachineBasicBlock::iterator MBBI,
Chris Lattner8a307e82002-12-16 19:32:50 +0000344 Constant *C, unsigned Reg);
Chris Lattnerc5291f52002-10-27 21:16:59 +0000345
Chris Lattner01cdb1b2004-06-11 05:33:49 +0000346 void emitUCOMr(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI,
347 unsigned LHS, unsigned RHS);
348
Chris Lattner3e130a22003-01-13 00:32:26 +0000349 /// makeAnotherReg - This method returns the next register number we haven't
350 /// yet used.
351 ///
352 /// Long values are handled somewhat specially. They are always allocated
353 /// as pairs of 32 bit integer values. The register number returned is the
354 /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits
355 /// of the long value.
356 ///
Chris Lattnerc0812d82002-12-13 06:56:29 +0000357 unsigned makeAnotherReg(const Type *Ty) {
Chris Lattner7db1fa92003-07-30 05:33:48 +0000358 assert(dynamic_cast<const X86RegisterInfo*>(TM.getRegisterInfo()) &&
359 "Current target doesn't have X86 reg info??");
360 const X86RegisterInfo *MRI =
361 static_cast<const X86RegisterInfo*>(TM.getRegisterInfo());
Chris Lattner3e130a22003-01-13 00:32:26 +0000362 if (Ty == Type::LongTy || Ty == Type::ULongTy) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000363 const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy);
364 // Create the lower part
365 F->getSSARegMap()->createVirtualRegister(RC);
366 // Create the upper part.
367 return F->getSSARegMap()->createVirtualRegister(RC)-1;
Chris Lattner3e130a22003-01-13 00:32:26 +0000368 }
369
Chris Lattnerc0812d82002-12-13 06:56:29 +0000370 // Add the mapping of regnumber => reg class to MachineFunction
Chris Lattner7db1fa92003-07-30 05:33:48 +0000371 const TargetRegisterClass *RC = MRI->getRegClassForType(Ty);
Chris Lattner3e130a22003-01-13 00:32:26 +0000372 return F->getSSARegMap()->createVirtualRegister(RC);
Brian Gaeke20244b72002-12-12 15:33:40 +0000373 }
374
Chris Lattnercb2fd552004-05-13 07:40:27 +0000375 /// getReg - This method turns an LLVM value into a register number.
Chris Lattner72614082002-10-25 22:55:53 +0000376 ///
377 unsigned getReg(Value &V) { return getReg(&V); } // Allow references
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000378 unsigned getReg(Value *V) {
379 // Just append to the end of the current bb.
380 MachineBasicBlock::iterator It = BB->end();
381 return getReg(V, BB, It);
382 }
Brian Gaeke71794c02002-12-13 11:22:48 +0000383 unsigned getReg(Value *V, MachineBasicBlock *MBB,
Chris Lattnercb2fd552004-05-13 07:40:27 +0000384 MachineBasicBlock::iterator IPt);
Chris Lattner427aeb42004-04-11 19:21:59 +0000385
Chris Lattnercb2fd552004-05-13 07:40:27 +0000386 /// getFixedSizedAllocaFI - Return the frame index for a fixed sized alloca
387 /// that is to be statically allocated with the initial stack frame
388 /// adjustment.
389 unsigned getFixedSizedAllocaFI(AllocaInst *AI);
Chris Lattner72614082002-10-25 22:55:53 +0000390 };
391}
392
Chris Lattnercb2fd552004-05-13 07:40:27 +0000393/// dyn_castFixedAlloca - If the specified value is a fixed size alloca
394/// instruction in the entry block, return it. Otherwise, return a null
395/// pointer.
396static AllocaInst *dyn_castFixedAlloca(Value *V) {
397 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
398 BasicBlock *BB = AI->getParent();
399 if (isa<ConstantUInt>(AI->getArraySize()) && BB ==&BB->getParent()->front())
400 return AI;
401 }
402 return 0;
403}
404
405/// getReg - This method turns an LLVM value into a register number.
406///
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000407unsigned X86ISel::getReg(Value *V, MachineBasicBlock *MBB,
408 MachineBasicBlock::iterator IPt) {
Chris Lattnercb2fd552004-05-13 07:40:27 +0000409 // If this operand is a constant, emit the code to copy the constant into
410 // the register here...
Chris Lattnercb2fd552004-05-13 07:40:27 +0000411 if (Constant *C = dyn_cast<Constant>(V)) {
412 unsigned Reg = makeAnotherReg(V->getType());
413 copyConstantToRegister(MBB, IPt, C, Reg);
414 return Reg;
Chris Lattnercb2fd552004-05-13 07:40:27 +0000415 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
Chris Lattner8b486a12004-06-29 00:14:38 +0000416 // Do not emit noop casts at all, unless it's a double -> float cast.
417 if (getClassB(CI->getType()) == getClassB(CI->getOperand(0)->getType()) &&
418 (CI->getType() != Type::FloatTy ||
419 CI->getOperand(0)->getType() != Type::DoubleTy))
Chris Lattnercb2fd552004-05-13 07:40:27 +0000420 return getReg(CI->getOperand(0), MBB, IPt);
421 } else if (AllocaInst *AI = dyn_castFixedAlloca(V)) {
422 // If the alloca address couldn't be folded into the instruction addressing,
423 // emit an explicit LEA as appropriate.
424 unsigned Reg = makeAnotherReg(V->getType());
425 unsigned FI = getFixedSizedAllocaFI(AI);
426 addFrameReference(BuildMI(*MBB, IPt, X86::LEA32r, 4, Reg), FI);
427 return Reg;
428 }
429
430 unsigned &Reg = RegMap[V];
431 if (Reg == 0) {
432 Reg = makeAnotherReg(V->getType());
433 RegMap[V] = Reg;
434 }
435
436 return Reg;
437}
438
439/// getFixedSizedAllocaFI - Return the frame index for a fixed sized alloca
440/// that is to be statically allocated with the initial stack frame
441/// adjustment.
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000442unsigned X86ISel::getFixedSizedAllocaFI(AllocaInst *AI) {
Chris Lattnercb2fd552004-05-13 07:40:27 +0000443 // Already computed this?
444 std::map<AllocaInst*, unsigned>::iterator I = AllocaMap.lower_bound(AI);
445 if (I != AllocaMap.end() && I->first == AI) return I->second;
446
447 const Type *Ty = AI->getAllocatedType();
448 ConstantUInt *CUI = cast<ConstantUInt>(AI->getArraySize());
449 unsigned TySize = TM.getTargetData().getTypeSize(Ty);
450 TySize *= CUI->getValue(); // Get total allocated size...
451 unsigned Alignment = TM.getTargetData().getTypeAlignment(Ty);
452
453 // Create a new stack object using the frame manager...
454 int FrameIdx = F->getFrameInfo()->CreateStackObject(TySize, Alignment);
455 AllocaMap.insert(I, std::make_pair(AI, FrameIdx));
456 return FrameIdx;
457}
458
459
Chris Lattnerc5291f52002-10-27 21:16:59 +0000460/// copyConstantToRegister - Output the instructions required to put the
461/// specified constant into the specified register.
462///
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000463void X86ISel::copyConstantToRegister(MachineBasicBlock *MBB,
464 MachineBasicBlock::iterator IP,
465 Constant *C, unsigned R) {
Chris Lattner30483b02004-10-16 18:13:05 +0000466 if (isa<UndefValue>(C)) {
467 switch (getClassB(C->getType())) {
468 case cFP:
469 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
470 BuildMI(*MBB, IP, X86::FLD0, 0, R);
471 return;
472 case cLong:
473 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, R+1);
474 // FALL THROUGH
475 default:
476 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, R);
477 return;
478 }
479 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000480 unsigned Class = 0;
481 switch (CE->getOpcode()) {
482 case Instruction::GetElementPtr:
Brian Gaeke68b1edc2002-12-16 04:23:29 +0000483 emitGEPOperation(MBB, IP, CE->getOperand(0),
Chris Lattner333b2fa2002-12-13 10:09:43 +0000484 CE->op_begin()+1, CE->op_end(), R);
Chris Lattnerc0812d82002-12-13 06:56:29 +0000485 return;
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000486 case Instruction::Cast:
Chris Lattner548f61d2003-04-23 17:22:12 +0000487 emitCastOperation(MBB, IP, CE->getOperand(0), CE->getType(), R);
Chris Lattner4b12cde2003-04-21 21:33:44 +0000488 return;
Chris Lattnerc0812d82002-12-13 06:56:29 +0000489
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000490 case Instruction::Xor: ++Class; // FALL THROUGH
491 case Instruction::Or: ++Class; // FALL THROUGH
492 case Instruction::And: ++Class; // FALL THROUGH
493 case Instruction::Sub: ++Class; // FALL THROUGH
494 case Instruction::Add:
495 emitSimpleBinaryOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
496 Class, R);
497 return;
498
Chris Lattner462fa822004-04-11 20:56:28 +0000499 case Instruction::Mul:
500 emitMultiply(MBB, IP, CE->getOperand(0), CE->getOperand(1), R);
Chris Lattnercadff442003-10-23 17:21:43 +0000501 return;
Chris Lattner462fa822004-04-11 20:56:28 +0000502
Chris Lattnercadff442003-10-23 17:21:43 +0000503 case Instruction::Div:
Chris Lattner462fa822004-04-11 20:56:28 +0000504 case Instruction::Rem:
505 emitDivRemOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
506 CE->getOpcode() == Instruction::Div, R);
Chris Lattnercadff442003-10-23 17:21:43 +0000507 return;
Chris Lattnercadff442003-10-23 17:21:43 +0000508
Chris Lattner58c41fe2003-08-24 19:19:47 +0000509 case Instruction::SetNE:
510 case Instruction::SetEQ:
511 case Instruction::SetLT:
512 case Instruction::SetGT:
513 case Instruction::SetLE:
514 case Instruction::SetGE:
515 emitSetCCOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
516 CE->getOpcode(), R);
517 return;
518
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000519 case Instruction::Shl:
520 case Instruction::Shr:
521 emitShiftOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000522 CE->getOpcode() == Instruction::Shl, CE->getType(), R);
523 return;
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000524
Chris Lattner12d96a02004-03-30 21:22:00 +0000525 case Instruction::Select:
526 emitSelectOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
527 CE->getOperand(2), R);
528 return;
529
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000530 default:
Chris Lattner76e2df22004-07-15 02:14:30 +0000531 std::cerr << "Offending expr: " << *C << "\n";
Chris Lattnerb2acc512003-10-19 21:09:10 +0000532 assert(0 && "Constant expression not yet handled!\n");
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000533 }
Brian Gaeke20244b72002-12-12 15:33:40 +0000534 }
Chris Lattnerc5291f52002-10-27 21:16:59 +0000535
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000536 if (C->getType()->isIntegral()) {
Chris Lattner6b993cc2002-12-15 08:02:15 +0000537 unsigned Class = getClassB(C->getType());
Chris Lattner3e130a22003-01-13 00:32:26 +0000538
539 if (Class == cLong) {
540 // Copy the value into the register pair.
Chris Lattnerc07736a2003-07-23 15:22:26 +0000541 uint64_t Val = cast<ConstantInt>(C)->getRawValue();
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000542 BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addImm(Val & 0xFFFFFFFF);
543 BuildMI(*MBB, IP, X86::MOV32ri, 1, R+1).addImm(Val >> 32);
Chris Lattner3e130a22003-01-13 00:32:26 +0000544 return;
545 }
546
Chris Lattner94af4142002-12-25 05:13:53 +0000547 assert(Class <= cInt && "Type not handled yet!");
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000548
549 static const unsigned IntegralOpcodeTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000550 X86::MOV8ri, X86::MOV16ri, X86::MOV32ri
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000551 };
552
Chris Lattner6b993cc2002-12-15 08:02:15 +0000553 if (C->getType() == Type::BoolTy) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000554 BuildMI(*MBB, IP, X86::MOV8ri, 1, R).addImm(C == ConstantBool::True);
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000555 } else {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000556 ConstantInt *CI = cast<ConstantInt>(C);
Chris Lattneree352852004-02-29 07:22:16 +0000557 BuildMI(*MBB, IP, IntegralOpcodeTab[Class],1,R).addImm(CI->getRawValue());
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000558 }
Chris Lattner94af4142002-12-25 05:13:53 +0000559 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
Chris Lattneraf703622004-02-02 18:56:30 +0000560 if (CFP->isExactlyValue(+0.0))
Chris Lattneree352852004-02-29 07:22:16 +0000561 BuildMI(*MBB, IP, X86::FLD0, 0, R);
Chris Lattneraf703622004-02-02 18:56:30 +0000562 else if (CFP->isExactlyValue(+1.0))
Chris Lattneree352852004-02-29 07:22:16 +0000563 BuildMI(*MBB, IP, X86::FLD1, 0, R);
Chris Lattner94af4142002-12-25 05:13:53 +0000564 else {
Chris Lattner3e130a22003-01-13 00:32:26 +0000565 // Otherwise we need to spill the constant to memory...
566 MachineConstantPool *CP = F->getConstantPool();
567 unsigned CPI = CP->getConstantPoolIndex(CFP);
Chris Lattner6c09db22003-10-20 04:11:23 +0000568 const Type *Ty = CFP->getType();
569
570 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000571 unsigned LoadOpcode = Ty == Type::FloatTy ? X86::FLD32m : X86::FLD64m;
Chris Lattneree352852004-02-29 07:22:16 +0000572 addConstantPoolReference(BuildMI(*MBB, IP, LoadOpcode, 4, R), CPI);
Chris Lattner94af4142002-12-25 05:13:53 +0000573 }
574
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000575 } else if (isa<ConstantPointerNull>(C)) {
Brian Gaeke20244b72002-12-12 15:33:40 +0000576 // Copy zero (null pointer) to the register.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000577 BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addImm(0);
Reid Spencer8863f182004-07-18 00:38:32 +0000578 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) {
579 BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addGlobalAddress(GV);
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000580 } else {
Chris Lattner76e2df22004-07-15 02:14:30 +0000581 std::cerr << "Offending constant: " << *C << "\n";
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000582 assert(0 && "Type not handled yet!");
Chris Lattnerc5291f52002-10-27 21:16:59 +0000583 }
584}
585
Chris Lattner065faeb2002-12-28 20:24:02 +0000586/// LoadArgumentsToVirtualRegs - Load all of the arguments to this function from
587/// the stack into virtual registers.
588///
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000589void X86ISel::LoadArgumentsToVirtualRegs(Function &Fn) {
Chris Lattner065faeb2002-12-28 20:24:02 +0000590 // Emit instructions to load the arguments... On entry to a function on the
591 // X86, the stack frame looks like this:
592 //
593 // [ESP] -- return address
Chris Lattner3e130a22003-01-13 00:32:26 +0000594 // [ESP + 4] -- first argument (leftmost lexically)
595 // [ESP + 8] -- second argument, if first argument is four bytes in size
Chris Lattner065faeb2002-12-28 20:24:02 +0000596 // ...
597 //
Chris Lattnerf158da22003-01-16 02:20:12 +0000598 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattneraa09b752002-12-28 21:08:28 +0000599 MachineFrameInfo *MFI = F->getFrameInfo();
Chris Lattner065faeb2002-12-28 20:24:02 +0000600
601 for (Function::aiterator I = Fn.abegin(), E = Fn.aend(); I != E; ++I) {
Chris Lattner427aeb42004-04-11 19:21:59 +0000602 bool ArgLive = !I->use_empty();
603 unsigned Reg = ArgLive ? getReg(*I) : 0;
Chris Lattner065faeb2002-12-28 20:24:02 +0000604 int FI; // Frame object index
Chris Lattner427aeb42004-04-11 19:21:59 +0000605
Chris Lattner065faeb2002-12-28 20:24:02 +0000606 switch (getClassB(I->getType())) {
607 case cByte:
Chris Lattner427aeb42004-04-11 19:21:59 +0000608 if (ArgLive) {
609 FI = MFI->CreateFixedObject(1, ArgOffset);
610 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Reg), FI);
611 }
Chris Lattner065faeb2002-12-28 20:24:02 +0000612 break;
613 case cShort:
Chris Lattner427aeb42004-04-11 19:21:59 +0000614 if (ArgLive) {
615 FI = MFI->CreateFixedObject(2, ArgOffset);
616 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Reg), FI);
617 }
Chris Lattner065faeb2002-12-28 20:24:02 +0000618 break;
619 case cInt:
Chris Lattner427aeb42004-04-11 19:21:59 +0000620 if (ArgLive) {
621 FI = MFI->CreateFixedObject(4, ArgOffset);
622 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Reg), FI);
623 }
Chris Lattner065faeb2002-12-28 20:24:02 +0000624 break;
Chris Lattner3e130a22003-01-13 00:32:26 +0000625 case cLong:
Chris Lattner427aeb42004-04-11 19:21:59 +0000626 if (ArgLive) {
627 FI = MFI->CreateFixedObject(8, ArgOffset);
628 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Reg), FI);
629 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Reg+1), FI, 4);
630 }
Chris Lattner3e130a22003-01-13 00:32:26 +0000631 ArgOffset += 4; // longs require 4 additional bytes
632 break;
Chris Lattner065faeb2002-12-28 20:24:02 +0000633 case cFP:
Chris Lattner427aeb42004-04-11 19:21:59 +0000634 if (ArgLive) {
635 unsigned Opcode;
636 if (I->getType() == Type::FloatTy) {
637 Opcode = X86::FLD32m;
638 FI = MFI->CreateFixedObject(4, ArgOffset);
639 } else {
640 Opcode = X86::FLD64m;
641 FI = MFI->CreateFixedObject(8, ArgOffset);
642 }
643 addFrameReference(BuildMI(BB, Opcode, 4, Reg), FI);
Chris Lattner065faeb2002-12-28 20:24:02 +0000644 }
Chris Lattner427aeb42004-04-11 19:21:59 +0000645 if (I->getType() == Type::DoubleTy)
646 ArgOffset += 4; // doubles require 4 additional bytes
Chris Lattner065faeb2002-12-28 20:24:02 +0000647 break;
648 default:
649 assert(0 && "Unhandled argument type!");
650 }
Chris Lattner3e130a22003-01-13 00:32:26 +0000651 ArgOffset += 4; // Each argument takes at least 4 bytes on the stack...
Chris Lattner065faeb2002-12-28 20:24:02 +0000652 }
Chris Lattnereca195e2003-05-08 19:44:13 +0000653
654 // If the function takes variable number of arguments, add a frame offset for
655 // the start of the first vararg value... this is used to expand
656 // llvm.va_start.
657 if (Fn.getFunctionType()->isVarArg())
658 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner065faeb2002-12-28 20:24:02 +0000659}
660
Chris Lattnerc0354c92004-12-13 17:23:11 +0000661/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
662/// the main function.
663void X86ISel::EmitSpecialCodeForMain() {
664 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
665 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
666 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
667
668 // Set the high part to be 64-bit precision.
669 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
670 CWFrameIdx, 1).addImm(2);
671
672 // Reload the modified control word now.
673 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
674}
Chris Lattner065faeb2002-12-28 20:24:02 +0000675
Chris Lattner333b2fa2002-12-13 10:09:43 +0000676/// SelectPHINodes - Insert machine code to generate phis. This is tricky
677/// because we have to generate our sources into the source basic blocks, not
678/// the current one.
679///
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000680void X86ISel::SelectPHINodes() {
Chris Lattnerd029cd22004-06-02 05:55:25 +0000681 const TargetInstrInfo &TII = *TM.getInstrInfo();
Chris Lattner333b2fa2002-12-13 10:09:43 +0000682 const Function &LF = *F->getFunction(); // The LLVM function...
683 for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) {
684 const BasicBlock *BB = I;
Chris Lattner168aa902004-02-29 07:10:16 +0000685 MachineBasicBlock &MBB = *MBBMap[I];
Chris Lattner333b2fa2002-12-13 10:09:43 +0000686
687 // Loop over all of the PHI nodes in the LLVM basic block...
Chris Lattner168aa902004-02-29 07:10:16 +0000688 MachineBasicBlock::iterator PHIInsertPoint = MBB.begin();
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000689 for (BasicBlock::const_iterator I = BB->begin(); isa<PHINode>(I); ++I) {
690 PHINode *PN = const_cast<PHINode*>(dyn_cast<PHINode>(I));
Chris Lattner3e130a22003-01-13 00:32:26 +0000691
Chris Lattner333b2fa2002-12-13 10:09:43 +0000692 // Create a new machine instr PHI node, and insert it.
Chris Lattner3e130a22003-01-13 00:32:26 +0000693 unsigned PHIReg = getReg(*PN);
Chris Lattner168aa902004-02-29 07:10:16 +0000694 MachineInstr *PhiMI = BuildMI(MBB, PHIInsertPoint,
695 X86::PHI, PN->getNumOperands(), PHIReg);
Chris Lattner3e130a22003-01-13 00:32:26 +0000696
697 MachineInstr *LongPhiMI = 0;
Chris Lattner168aa902004-02-29 07:10:16 +0000698 if (PN->getType() == Type::LongTy || PN->getType() == Type::ULongTy)
699 LongPhiMI = BuildMI(MBB, PHIInsertPoint,
700 X86::PHI, PN->getNumOperands(), PHIReg+1);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000701
Chris Lattnera6e73f12003-05-12 14:22:21 +0000702 // PHIValues - Map of blocks to incoming virtual registers. We use this
703 // so that we only initialize one incoming value for a particular block,
704 // even if the block has multiple entries in the PHI node.
705 //
706 std::map<MachineBasicBlock*, unsigned> PHIValues;
707
Chris Lattner333b2fa2002-12-13 10:09:43 +0000708 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
709 MachineBasicBlock *PredMBB = MBBMap[PN->getIncomingBlock(i)];
Chris Lattnera6e73f12003-05-12 14:22:21 +0000710 unsigned ValReg;
711 std::map<MachineBasicBlock*, unsigned>::iterator EntryIt =
712 PHIValues.lower_bound(PredMBB);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000713
Chris Lattnera6e73f12003-05-12 14:22:21 +0000714 if (EntryIt != PHIValues.end() && EntryIt->first == PredMBB) {
715 // We already inserted an initialization of the register for this
716 // predecessor. Recycle it.
717 ValReg = EntryIt->second;
718
719 } else {
Chris Lattnera81fc682003-10-19 00:26:11 +0000720 // Get the incoming value into a virtual register.
Chris Lattnera6e73f12003-05-12 14:22:21 +0000721 //
Chris Lattnera81fc682003-10-19 00:26:11 +0000722 Value *Val = PN->getIncomingValue(i);
723
724 // If this is a constant or GlobalValue, we may have to insert code
725 // into the basic block to compute it into a virtual register.
Reid Spencer8863f182004-07-18 00:38:32 +0000726 if ((isa<Constant>(Val) && !isa<ConstantExpr>(Val))) {
Chris Lattnercb2fd552004-05-13 07:40:27 +0000727 // Simple constants get emitted at the end of the basic block,
728 // before any terminator instructions. We "know" that the code to
729 // move a constant into a register will never clobber any flags.
730 ValReg = getReg(Val, PredMBB, PredMBB->getFirstTerminator());
Chris Lattnera81fc682003-10-19 00:26:11 +0000731 } else {
Chris Lattnercb2fd552004-05-13 07:40:27 +0000732 // Because we don't want to clobber any values which might be in
733 // physical registers with the computation of this constant (which
734 // might be arbitrarily complex if it is a constant expression),
735 // just insert the computation at the top of the basic block.
736 MachineBasicBlock::iterator PI = PredMBB->begin();
737
738 // Skip over any PHI nodes though!
739 while (PI != PredMBB->end() && PI->getOpcode() == X86::PHI)
740 ++PI;
741
742 ValReg = getReg(Val, PredMBB, PI);
Chris Lattnera81fc682003-10-19 00:26:11 +0000743 }
Chris Lattnera6e73f12003-05-12 14:22:21 +0000744
745 // Remember that we inserted a value for this PHI for this predecessor
746 PHIValues.insert(EntryIt, std::make_pair(PredMBB, ValReg));
747 }
748
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000749 PhiMI->addRegOperand(ValReg);
Chris Lattner3e130a22003-01-13 00:32:26 +0000750 PhiMI->addMachineBasicBlockOperand(PredMBB);
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000751 if (LongPhiMI) {
752 LongPhiMI->addRegOperand(ValReg+1);
753 LongPhiMI->addMachineBasicBlockOperand(PredMBB);
754 }
Chris Lattner333b2fa2002-12-13 10:09:43 +0000755 }
Chris Lattner168aa902004-02-29 07:10:16 +0000756
757 // Now that we emitted all of the incoming values for the PHI node, make
758 // sure to reposition the InsertPoint after the PHI that we just added.
759 // This is needed because we might have inserted a constant into this
760 // block, right after the PHI's which is before the old insert point!
761 PHIInsertPoint = LongPhiMI ? LongPhiMI : PhiMI;
762 ++PHIInsertPoint;
Chris Lattner333b2fa2002-12-13 10:09:43 +0000763 }
764 }
765}
766
Chris Lattner986618e2004-02-22 19:47:26 +0000767/// RequiresFPRegKill - The floating point stackifier pass cannot insert
768/// compensation code on critical edges. As such, it requires that we kill all
769/// FP registers on the exit from any blocks that either ARE critical edges, or
770/// branch to a block that has incoming critical edges.
771///
772/// Note that this kill instruction will eventually be eliminated when
773/// restrictions in the stackifier are relaxed.
774///
Brian Gaeke1afe7732004-04-28 04:45:55 +0000775static bool RequiresFPRegKill(const MachineBasicBlock *MBB) {
Chris Lattner986618e2004-02-22 19:47:26 +0000776#if 0
Brian Gaeke1afe7732004-04-28 04:45:55 +0000777 const BasicBlock *BB = MBB->getBasicBlock ();
Chris Lattner986618e2004-02-22 19:47:26 +0000778 for (succ_const_iterator SI = succ_begin(BB), E = succ_end(BB); SI!=E; ++SI) {
779 const BasicBlock *Succ = *SI;
780 pred_const_iterator PI = pred_begin(Succ), PE = pred_end(Succ);
781 ++PI; // Block have at least one predecessory
782 if (PI != PE) { // If it has exactly one, this isn't crit edge
783 // If this block has more than one predecessor, check all of the
784 // predecessors to see if they have multiple successors. If so, then the
785 // block we are analyzing needs an FPRegKill.
786 for (PI = pred_begin(Succ); PI != PE; ++PI) {
787 const BasicBlock *Pred = *PI;
788 succ_const_iterator SI2 = succ_begin(Pred);
789 ++SI2; // There must be at least one successor of this block.
790 if (SI2 != succ_end(Pred))
791 return true; // Yes, we must insert the kill on this edge.
792 }
793 }
794 }
795 // If we got this far, there is no need to insert the kill instruction.
796 return false;
797#else
798 return true;
799#endif
800}
801
802// InsertFPRegKills - Insert FP_REG_KILL instructions into basic blocks that
803// need them. This only occurs due to the floating point stackifier not being
804// aggressive enough to handle arbitrary global stackification.
805//
806// Currently we insert an FP_REG_KILL instruction into each block that uses or
807// defines a floating point virtual register.
808//
809// When the global register allocators (like linear scan) finally update live
810// variable analysis, we can keep floating point values in registers across
811// portions of the CFG that do not involve critical edges. This will be a big
812// win, but we are waiting on the global allocators before we can do this.
813//
814// With a bit of work, the floating point stackifier pass can be enhanced to
815// break critical edges as needed (to make a place to put compensation code),
816// but this will require some infrastructure improvements as well.
817//
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000818void X86ISel::InsertFPRegKills() {
Chris Lattner986618e2004-02-22 19:47:26 +0000819 SSARegMap &RegMap = *F->getSSARegMap();
Chris Lattner986618e2004-02-22 19:47:26 +0000820
821 for (MachineFunction::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
Chris Lattner986618e2004-02-22 19:47:26 +0000822 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000823 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
824 MachineOperand& MO = I->getOperand(i);
825 if (MO.isRegister() && MO.getReg()) {
826 unsigned Reg = MO.getReg();
Chris Lattner223d4c42004-12-03 05:13:15 +0000827 if (MRegisterInfo::isVirtualRegister(Reg)) {
828 unsigned RegSize = RegMap.getRegClass(Reg)->getSize();
829 if (RegSize == 10 || RegSize == 8)
Chris Lattner65cf42d2004-02-23 07:29:45 +0000830 goto UsesFPReg;
Chris Lattner223d4c42004-12-03 05:13:15 +0000831 }
Chris Lattner986618e2004-02-22 19:47:26 +0000832 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000833 }
Chris Lattner65cf42d2004-02-23 07:29:45 +0000834 // If we haven't found an FP register use or def in this basic block, check
835 // to see if any of our successors has an FP PHI node, which will cause a
836 // copy to be inserted into this block.
Brian Gaeke235aa5e2004-04-28 04:34:16 +0000837 for (MachineBasicBlock::const_succ_iterator SI = BB->succ_begin(),
838 SE = BB->succ_end(); SI != SE; ++SI) {
839 MachineBasicBlock *SBB = *SI;
Chris Lattnerfbc39d52004-02-23 07:42:19 +0000840 for (MachineBasicBlock::iterator I = SBB->begin();
841 I != SBB->end() && I->getOpcode() == X86::PHI; ++I) {
Chris Lattner39869242004-12-02 17:57:21 +0000842 const TargetRegisterClass *RC =
843 RegMap.getRegClass(I->getOperand(0).getReg());
844 if (RC->getSize() == 10 || RC->getSize() == 8)
Chris Lattnerfbc39d52004-02-23 07:42:19 +0000845 goto UsesFPReg;
Chris Lattner986618e2004-02-22 19:47:26 +0000846 }
Chris Lattnerfbc39d52004-02-23 07:42:19 +0000847 }
Chris Lattner65cf42d2004-02-23 07:29:45 +0000848 continue;
849 UsesFPReg:
850 // Okay, this block uses an FP register. If the block has successors (ie,
851 // it's not an unwind/return), insert the FP_REG_KILL instruction.
Brian Gaeke1afe7732004-04-28 04:45:55 +0000852 if (BB->succ_size () && RequiresFPRegKill(BB)) {
Chris Lattneree352852004-02-29 07:22:16 +0000853 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
Chris Lattner65cf42d2004-02-23 07:29:45 +0000854 ++NumFPKill;
Chris Lattner986618e2004-02-22 19:47:26 +0000855 }
856 }
857}
858
859
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000860void X86ISel::getAddressingMode(Value *Addr, X86AddressMode &AM) {
Reid Spencerfc989e12004-08-30 00:13:26 +0000861 AM.BaseType = X86AddressMode::RegBase;
862 AM.Base.Reg = 0; AM.Scale = 1; AM.IndexReg = 0; AM.Disp = 0;
Chris Lattner9f1b5312004-05-13 15:12:43 +0000863 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Addr)) {
864 if (isGEPFoldable(BB, GEP->getOperand(0), GEP->op_begin()+1, GEP->op_end(),
Reid Spencerfc989e12004-08-30 00:13:26 +0000865 AM))
Chris Lattner9f1b5312004-05-13 15:12:43 +0000866 return;
867 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Addr)) {
868 if (CE->getOpcode() == Instruction::GetElementPtr)
869 if (isGEPFoldable(BB, CE->getOperand(0), CE->op_begin()+1, CE->op_end(),
Reid Spencerfc989e12004-08-30 00:13:26 +0000870 AM))
Chris Lattner9f1b5312004-05-13 15:12:43 +0000871 return;
Reid Spencerfc989e12004-08-30 00:13:26 +0000872 } else if (AllocaInst *AI = dyn_castFixedAlloca(Addr)) {
873 AM.BaseType = X86AddressMode::FrameIndexBase;
874 AM.Base.FrameIndex = getFixedSizedAllocaFI(AI);
875 return;
Chris Lattner358a9022004-10-15 05:05:29 +0000876 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(Addr)) {
877 AM.GV = GV;
878 return;
Chris Lattner9f1b5312004-05-13 15:12:43 +0000879 }
880
881 // If it's not foldable, reset addr mode.
Reid Spencerfc989e12004-08-30 00:13:26 +0000882 AM.BaseType = X86AddressMode::RegBase;
883 AM.Base.Reg = getReg(Addr);
884 AM.Scale = 1; AM.IndexReg = 0; AM.Disp = 0;
Chris Lattner9f1b5312004-05-13 15:12:43 +0000885}
886
Chris Lattner307ecba2004-03-30 22:39:09 +0000887// canFoldSetCCIntoBranchOrSelect - Return the setcc instruction if we can fold
888// it into the conditional branch or select instruction which is the only user
889// of the cc instruction. This is the case if the conditional branch is the
Chris Lattnera6f9fe62004-06-18 00:29:22 +0000890// only user of the setcc. We also don't handle long arguments below, so we
891// reject them here as well.
Chris Lattner6d40c192003-01-16 16:43:00 +0000892//
Chris Lattner307ecba2004-03-30 22:39:09 +0000893static SetCondInst *canFoldSetCCIntoBranchOrSelect(Value *V) {
Chris Lattner6d40c192003-01-16 16:43:00 +0000894 if (SetCondInst *SCI = dyn_cast<SetCondInst>(V))
Chris Lattner307ecba2004-03-30 22:39:09 +0000895 if (SCI->hasOneUse()) {
896 Instruction *User = cast<Instruction>(SCI->use_back());
Tanya Lattner9855b842004-12-01 18:27:03 +0000897 if ((isa<BranchInst>(User) || isa<SelectInst>(User)) &&
898 (getClassB(SCI->getOperand(0)->getType()) != cLong ||
899 SCI->getOpcode() == Instruction::SetEQ ||
900 SCI->getOpcode() == Instruction::SetNE) &&
901 (isa<BranchInst>(User) || User->getOperand(0) == V))
Chris Lattner6d40c192003-01-16 16:43:00 +0000902 return SCI;
903 }
904 return 0;
905}
Chris Lattner333b2fa2002-12-13 10:09:43 +0000906
Chris Lattner6d40c192003-01-16 16:43:00 +0000907// Return a fixed numbering for setcc instructions which does not depend on the
908// order of the opcodes.
909//
910static unsigned getSetCCNumber(unsigned Opcode) {
911 switch(Opcode) {
912 default: assert(0 && "Unknown setcc instruction!");
913 case Instruction::SetEQ: return 0;
914 case Instruction::SetNE: return 1;
915 case Instruction::SetLT: return 2;
Chris Lattner55f6fab2003-01-16 18:07:23 +0000916 case Instruction::SetGE: return 3;
917 case Instruction::SetGT: return 4;
918 case Instruction::SetLE: return 5;
Chris Lattner6d40c192003-01-16 16:43:00 +0000919 }
920}
Chris Lattner06925362002-11-17 21:56:38 +0000921
Chris Lattner6d40c192003-01-16 16:43:00 +0000922// LLVM -> X86 signed X86 unsigned
923// ----- ---------- ------------
924// seteq -> sete sete
925// setne -> setne setne
926// setlt -> setl setb
Chris Lattner55f6fab2003-01-16 18:07:23 +0000927// setge -> setge setae
Chris Lattner6d40c192003-01-16 16:43:00 +0000928// setgt -> setg seta
929// setle -> setle setbe
Chris Lattnerb2acc512003-10-19 21:09:10 +0000930// ----
931// sets // Used by comparison with 0 optimization
932// setns
933static const unsigned SetCCOpcodeTab[2][8] = {
934 { X86::SETEr, X86::SETNEr, X86::SETBr, X86::SETAEr, X86::SETAr, X86::SETBEr,
935 0, 0 },
936 { X86::SETEr, X86::SETNEr, X86::SETLr, X86::SETGEr, X86::SETGr, X86::SETLEr,
937 X86::SETSr, X86::SETNSr },
Chris Lattner6d40c192003-01-16 16:43:00 +0000938};
939
Chris Lattner01cdb1b2004-06-11 05:33:49 +0000940/// emitUCOMr - In the future when we support processors before the P6, this
941/// wraps the logic for emitting an FUCOMr vs FUCOMIr.
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000942void X86ISel::emitUCOMr(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
943 unsigned LHS, unsigned RHS) {
Chris Lattner01cdb1b2004-06-11 05:33:49 +0000944 if (0) { // for processors prior to the P6
945 BuildMI(*MBB, IP, X86::FUCOMr, 2).addReg(LHS).addReg(RHS);
946 BuildMI(*MBB, IP, X86::FNSTSW8r, 0);
947 BuildMI(*MBB, IP, X86::SAHF, 1);
948 } else {
949 BuildMI(*MBB, IP, X86::FUCOMIr, 2).addReg(LHS).addReg(RHS);
950 }
951}
952
Chris Lattnerb2acc512003-10-19 21:09:10 +0000953// EmitComparison - This function emits a comparison of the two operands,
954// returning the extended setcc code to use.
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000955unsigned X86ISel::EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
956 MachineBasicBlock *MBB,
957 MachineBasicBlock::iterator IP) {
Brian Gaeke1749d632002-11-07 17:59:21 +0000958 // The arguments are already supposed to be of the same type.
Chris Lattner6d40c192003-01-16 16:43:00 +0000959 const Type *CompTy = Op0->getType();
Chris Lattner3e130a22003-01-13 00:32:26 +0000960 unsigned Class = getClassB(CompTy);
Chris Lattner333864d2003-06-05 19:30:30 +0000961
962 // Special case handling of: cmp R, i
Chris Lattner260195d2004-05-07 19:55:55 +0000963 if (isa<ConstantPointerNull>(Op1)) {
Chris Lattnerde95c9e2004-10-17 06:10:40 +0000964 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattner260195d2004-05-07 19:55:55 +0000965 if (OpNum < 2) // seteq/setne -> test
966 BuildMI(*MBB, IP, X86::TEST32rr, 2).addReg(Op0r).addReg(Op0r);
967 else
968 BuildMI(*MBB, IP, X86::CMP32ri, 2).addReg(Op0r).addImm(0);
969 return OpNum;
970
971 } else if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
Chris Lattnere80e6372004-04-06 16:02:27 +0000972 if (Class == cByte || Class == cShort || Class == cInt) {
973 unsigned Op1v = CI->getRawValue();
Chris Lattnerc07736a2003-07-23 15:22:26 +0000974
Chris Lattner333864d2003-06-05 19:30:30 +0000975 // Mask off any upper bits of the constant, if there are any...
976 Op1v &= (1ULL << (8 << Class)) - 1;
977
Chris Lattnerb2acc512003-10-19 21:09:10 +0000978 // If this is a comparison against zero, emit more efficient code. We
979 // can't handle unsigned comparisons against zero unless they are == or
980 // !=. These should have been strength reduced already anyway.
981 if (Op1v == 0 && (CompTy->isSigned() || OpNum < 2)) {
Chris Lattnerde95c9e2004-10-17 06:10:40 +0000982
983 // If this is a comparison against zero and the LHS is an and of a
984 // register with a constant, use the test to do the and.
985 if (Instruction *Op0I = dyn_cast<Instruction>(Op0))
986 if (Op0I->getOpcode() == Instruction::And && Op0->hasOneUse() &&
987 isa<ConstantInt>(Op0I->getOperand(1))) {
988 static const unsigned TESTTab[] = {
989 X86::TEST8ri, X86::TEST16ri, X86::TEST32ri
990 };
991
992 // Emit test X, i
993 unsigned LHS = getReg(Op0I->getOperand(0), MBB, IP);
994 unsigned Imm =
995 cast<ConstantInt>(Op0I->getOperand(1))->getRawValue();
996 BuildMI(*MBB, IP, TESTTab[Class], 2).addReg(LHS).addImm(Imm);
997
Chris Lattnerde95c9e2004-10-17 06:10:40 +0000998 if (OpNum == 2) return 6; // Map jl -> js
999 if (OpNum == 3) return 7; // Map jg -> jns
1000 return OpNum;
1001 }
1002
1003 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001004 static const unsigned TESTTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001005 X86::TEST8rr, X86::TEST16rr, X86::TEST32rr
Chris Lattnerb2acc512003-10-19 21:09:10 +00001006 };
Chris Lattneree352852004-02-29 07:22:16 +00001007 BuildMI(*MBB, IP, TESTTab[Class], 2).addReg(Op0r).addReg(Op0r);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001008
1009 if (OpNum == 2) return 6; // Map jl -> js
1010 if (OpNum == 3) return 7; // Map jg -> jns
1011 return OpNum;
Chris Lattner333864d2003-06-05 19:30:30 +00001012 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001013
1014 static const unsigned CMPTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001015 X86::CMP8ri, X86::CMP16ri, X86::CMP32ri
Chris Lattnerb2acc512003-10-19 21:09:10 +00001016 };
1017
Chris Lattnerde95c9e2004-10-17 06:10:40 +00001018 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattneree352852004-02-29 07:22:16 +00001019 BuildMI(*MBB, IP, CMPTab[Class], 2).addReg(Op0r).addImm(Op1v);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001020 return OpNum;
Chris Lattnere80e6372004-04-06 16:02:27 +00001021 } else {
Chris Lattnerde95c9e2004-10-17 06:10:40 +00001022 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattnere80e6372004-04-06 16:02:27 +00001023 assert(Class == cLong && "Unknown integer class!");
1024 unsigned LowCst = CI->getRawValue();
1025 unsigned HiCst = CI->getRawValue() >> 32;
1026 if (OpNum < 2) { // seteq, setne
1027 unsigned LoTmp = Op0r;
1028 if (LowCst != 0) {
1029 LoTmp = makeAnotherReg(Type::IntTy);
1030 BuildMI(*MBB, IP, X86::XOR32ri, 2, LoTmp).addReg(Op0r).addImm(LowCst);
1031 }
1032 unsigned HiTmp = Op0r+1;
1033 if (HiCst != 0) {
1034 HiTmp = makeAnotherReg(Type::IntTy);
Chris Lattner48c937e2004-04-06 17:34:50 +00001035 BuildMI(*MBB, IP, X86::XOR32ri, 2,HiTmp).addReg(Op0r+1).addImm(HiCst);
Chris Lattnere80e6372004-04-06 16:02:27 +00001036 }
1037 unsigned FinalTmp = makeAnotherReg(Type::IntTy);
1038 BuildMI(*MBB, IP, X86::OR32rr, 2, FinalTmp).addReg(LoTmp).addReg(HiTmp);
1039 return OpNum;
Chris Lattner48c937e2004-04-06 17:34:50 +00001040 } else {
Tanya Lattner9855b842004-12-01 18:27:03 +00001041 // Emit a sequence of code which compares the high and low parts once
1042 // each, then uses a conditional move to handle the overflow case. For
1043 // example, a setlt for long would generate code like this:
1044 //
1045 // AL = lo(op1) < lo(op2) // Always unsigned comparison
1046 // BL = hi(op1) < hi(op2) // Signedness depends on operands
1047 // dest = hi(op1) == hi(op2) ? BL : AL;
1048 //
1049
1050 // FIXME: This would be much better if we had hierarchical register
1051 // classes! Until then, hardcode registers so that we can deal with
1052 // their aliases (because we don't have conditional byte moves).
1053 //
1054 BuildMI(*MBB, IP, X86::CMP32ri, 2).addReg(Op0r).addImm(LowCst);
1055 BuildMI(*MBB, IP, SetCCOpcodeTab[0][OpNum], 0, X86::AL);
1056 BuildMI(*MBB, IP, X86::CMP32ri, 2).addReg(Op0r+1).addImm(HiCst);
1057 BuildMI(*MBB, IP, SetCCOpcodeTab[CompTy->isSigned()][OpNum], 0,X86::BL);
1058 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::BH);
1059 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::AH);
1060 BuildMI(*MBB, IP, X86::CMOVE16rr, 2, X86::BX).addReg(X86::BX)
1061 .addReg(X86::AX);
1062 // NOTE: visitSetCondInst knows that the value is dumped into the BL
1063 // register at this point for long values...
Chris Lattner48c937e2004-04-06 17:34:50 +00001064 return OpNum;
Chris Lattnere80e6372004-04-06 16:02:27 +00001065 }
Chris Lattner333864d2003-06-05 19:30:30 +00001066 }
Chris Lattnere80e6372004-04-06 16:02:27 +00001067 }
Chris Lattner333864d2003-06-05 19:30:30 +00001068
Chris Lattnerde95c9e2004-10-17 06:10:40 +00001069 unsigned Op0r = getReg(Op0, MBB, IP);
1070
Chris Lattner9f08a922004-02-03 18:54:04 +00001071 // Special case handling of comparison against +/- 0.0
1072 if (ConstantFP *CFP = dyn_cast<ConstantFP>(Op1))
1073 if (CFP->isExactlyValue(+0.0) || CFP->isExactlyValue(-0.0)) {
Chris Lattneree352852004-02-29 07:22:16 +00001074 BuildMI(*MBB, IP, X86::FTST, 1).addReg(Op0r);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001075 BuildMI(*MBB, IP, X86::FNSTSW8r, 0);
Chris Lattneree352852004-02-29 07:22:16 +00001076 BuildMI(*MBB, IP, X86::SAHF, 1);
Chris Lattner9f08a922004-02-03 18:54:04 +00001077 return OpNum;
1078 }
1079
Chris Lattner58c41fe2003-08-24 19:19:47 +00001080 unsigned Op1r = getReg(Op1, MBB, IP);
Chris Lattner3e130a22003-01-13 00:32:26 +00001081 switch (Class) {
1082 default: assert(0 && "Unknown type class!");
1083 // Emit: cmp <var1>, <var2> (do the comparison). We can
1084 // compare 8-bit with 8-bit, 16-bit with 16-bit, 32-bit with
1085 // 32-bit.
1086 case cByte:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001087 BuildMI(*MBB, IP, X86::CMP8rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +00001088 break;
1089 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001090 BuildMI(*MBB, IP, X86::CMP16rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +00001091 break;
1092 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001093 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +00001094 break;
1095 case cFP:
Chris Lattner01cdb1b2004-06-11 05:33:49 +00001096 emitUCOMr(MBB, IP, Op0r, Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +00001097 break;
1098
1099 case cLong:
1100 if (OpNum < 2) { // seteq, setne
1101 unsigned LoTmp = makeAnotherReg(Type::IntTy);
1102 unsigned HiTmp = makeAnotherReg(Type::IntTy);
1103 unsigned FinalTmp = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001104 BuildMI(*MBB, IP, X86::XOR32rr, 2, LoTmp).addReg(Op0r).addReg(Op1r);
1105 BuildMI(*MBB, IP, X86::XOR32rr, 2, HiTmp).addReg(Op0r+1).addReg(Op1r+1);
1106 BuildMI(*MBB, IP, X86::OR32rr, 2, FinalTmp).addReg(LoTmp).addReg(HiTmp);
Chris Lattner3e130a22003-01-13 00:32:26 +00001107 break; // Allow the sete or setne to be generated from flags set by OR
1108 } else {
1109 // Emit a sequence of code which compares the high and low parts once
1110 // each, then uses a conditional move to handle the overflow case. For
1111 // example, a setlt for long would generate code like this:
1112 //
1113 // AL = lo(op1) < lo(op2) // Signedness depends on operands
1114 // BL = hi(op1) < hi(op2) // Always unsigned comparison
Chris Lattner9984fd02004-05-09 23:16:33 +00001115 // dest = hi(op1) == hi(op2) ? BL : AL;
Chris Lattner3e130a22003-01-13 00:32:26 +00001116 //
1117
Chris Lattner6d40c192003-01-16 16:43:00 +00001118 // FIXME: This would be much better if we had hierarchical register
Chris Lattner3e130a22003-01-13 00:32:26 +00001119 // classes! Until then, hardcode registers so that we can deal with their
1120 // aliases (because we don't have conditional byte moves).
1121 //
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001122 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattneree352852004-02-29 07:22:16 +00001123 BuildMI(*MBB, IP, SetCCOpcodeTab[0][OpNum], 0, X86::AL);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001124 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r+1).addReg(Op1r+1);
Chris Lattneree352852004-02-29 07:22:16 +00001125 BuildMI(*MBB, IP, SetCCOpcodeTab[CompTy->isSigned()][OpNum], 0, X86::BL);
1126 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::BH);
1127 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::AH);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001128 BuildMI(*MBB, IP, X86::CMOVE16rr, 2, X86::BX).addReg(X86::BX)
Chris Lattneree352852004-02-29 07:22:16 +00001129 .addReg(X86::AX);
Chris Lattner6d40c192003-01-16 16:43:00 +00001130 // NOTE: visitSetCondInst knows that the value is dumped into the BL
1131 // register at this point for long values...
Chris Lattnerb2acc512003-10-19 21:09:10 +00001132 return OpNum;
Chris Lattner3e130a22003-01-13 00:32:26 +00001133 }
1134 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001135 return OpNum;
Chris Lattner6d40c192003-01-16 16:43:00 +00001136}
Chris Lattner3e130a22003-01-13 00:32:26 +00001137
Chris Lattner6d40c192003-01-16 16:43:00 +00001138/// SetCC instructions - Here we just emit boilerplate code to set a byte-sized
1139/// register, then move it to wherever the result should be.
1140///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001141void X86ISel::visitSetCondInst(SetCondInst &I) {
Chris Lattner307ecba2004-03-30 22:39:09 +00001142 if (canFoldSetCCIntoBranchOrSelect(&I))
1143 return; // Fold this into a branch or select.
Chris Lattner6d40c192003-01-16 16:43:00 +00001144
Chris Lattner6d40c192003-01-16 16:43:00 +00001145 unsigned DestReg = getReg(I);
Chris Lattner58c41fe2003-08-24 19:19:47 +00001146 MachineBasicBlock::iterator MII = BB->end();
1147 emitSetCCOperation(BB, MII, I.getOperand(0), I.getOperand(1), I.getOpcode(),
1148 DestReg);
1149}
Chris Lattner6d40c192003-01-16 16:43:00 +00001150
Chris Lattner58c41fe2003-08-24 19:19:47 +00001151/// emitSetCCOperation - Common code shared between visitSetCondInst and
1152/// constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +00001153///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001154void X86ISel::emitSetCCOperation(MachineBasicBlock *MBB,
1155 MachineBasicBlock::iterator IP,
1156 Value *Op0, Value *Op1, unsigned Opcode,
1157 unsigned TargetReg) {
Chris Lattner58c41fe2003-08-24 19:19:47 +00001158 unsigned OpNum = getSetCCNumber(Opcode);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001159 OpNum = EmitComparison(OpNum, Op0, Op1, MBB, IP);
Chris Lattner58c41fe2003-08-24 19:19:47 +00001160
Chris Lattnerb2acc512003-10-19 21:09:10 +00001161 const Type *CompTy = Op0->getType();
1162 unsigned CompClass = getClassB(CompTy);
1163 bool isSigned = CompTy->isSigned() && CompClass != cFP;
1164
Tanya Lattner9855b842004-12-01 18:27:03 +00001165 if (CompClass != cLong || OpNum < 2) {
1166 // Handle normal comparisons with a setcc instruction...
1167 BuildMI(*MBB, IP, SetCCOpcodeTab[isSigned][OpNum], 0, TargetReg);
1168 } else {
1169 // Handle long comparisons by copying the value which is already in BL into
1170 // the register we want...
1171 BuildMI(*MBB, IP, X86::MOV8rr, 1, TargetReg).addReg(X86::BL);
1172 }
Brian Gaeke1749d632002-11-07 17:59:21 +00001173}
Chris Lattner51b49a92002-11-02 19:45:49 +00001174
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001175void X86ISel::visitSelectInst(SelectInst &SI) {
Chris Lattner12d96a02004-03-30 21:22:00 +00001176 unsigned DestReg = getReg(SI);
1177 MachineBasicBlock::iterator MII = BB->end();
1178 emitSelectOperation(BB, MII, SI.getCondition(), SI.getTrueValue(),
1179 SI.getFalseValue(), DestReg);
1180}
1181
1182/// emitSelect - Common code shared between visitSelectInst and the constant
1183/// expression support.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001184void X86ISel::emitSelectOperation(MachineBasicBlock *MBB,
1185 MachineBasicBlock::iterator IP,
1186 Value *Cond, Value *TrueVal, Value *FalseVal,
1187 unsigned DestReg) {
Chris Lattner12d96a02004-03-30 21:22:00 +00001188 unsigned SelectClass = getClassB(TrueVal->getType());
1189
1190 // We don't support 8-bit conditional moves. If we have incoming constants,
1191 // transform them into 16-bit constants to avoid having a run-time conversion.
1192 if (SelectClass == cByte) {
1193 if (Constant *T = dyn_cast<Constant>(TrueVal))
1194 TrueVal = ConstantExpr::getCast(T, Type::ShortTy);
1195 if (Constant *F = dyn_cast<Constant>(FalseVal))
1196 FalseVal = ConstantExpr::getCast(F, Type::ShortTy);
1197 }
1198
Chris Lattner82c5a992004-04-13 21:56:09 +00001199 unsigned TrueReg = getReg(TrueVal, MBB, IP);
1200 unsigned FalseReg = getReg(FalseVal, MBB, IP);
1201 if (TrueReg == FalseReg) {
1202 static const unsigned Opcode[] = {
1203 X86::MOV8rr, X86::MOV16rr, X86::MOV32rr, X86::FpMOV, X86::MOV32rr
1204 };
1205 BuildMI(*MBB, IP, Opcode[SelectClass], 1, DestReg).addReg(TrueReg);
1206 if (SelectClass == cLong)
1207 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg+1).addReg(TrueReg+1);
1208 return;
1209 }
1210
Chris Lattner307ecba2004-03-30 22:39:09 +00001211 unsigned Opcode;
1212 if (SetCondInst *SCI = canFoldSetCCIntoBranchOrSelect(Cond)) {
1213 // We successfully folded the setcc into the select instruction.
1214
1215 unsigned OpNum = getSetCCNumber(SCI->getOpcode());
1216 OpNum = EmitComparison(OpNum, SCI->getOperand(0), SCI->getOperand(1), MBB,
1217 IP);
1218
1219 const Type *CompTy = SCI->getOperand(0)->getType();
1220 bool isSigned = CompTy->isSigned() && getClassB(CompTy) != cFP;
1221
1222 // LLVM -> X86 signed X86 unsigned
1223 // ----- ---------- ------------
1224 // seteq -> cmovNE cmovNE
1225 // setne -> cmovE cmovE
1226 // setlt -> cmovGE cmovAE
1227 // setge -> cmovL cmovB
1228 // setgt -> cmovLE cmovBE
1229 // setle -> cmovG cmovA
1230 // ----
1231 // cmovNS // Used by comparison with 0 optimization
1232 // cmovS
1233
1234 switch (SelectClass) {
Chris Lattner352eb482004-03-31 22:03:35 +00001235 default: assert(0 && "Unknown value class!");
1236 case cFP: {
1237 // Annoyingly, we don't have a full set of floating point conditional
1238 // moves. :(
1239 static const unsigned OpcodeTab[2][8] = {
1240 { X86::FCMOVNE, X86::FCMOVE, X86::FCMOVAE, X86::FCMOVB,
1241 X86::FCMOVBE, X86::FCMOVA, 0, 0 },
1242 { X86::FCMOVNE, X86::FCMOVE, 0, 0, 0, 0, 0, 0 },
1243 };
1244 Opcode = OpcodeTab[isSigned][OpNum];
1245
1246 // If opcode == 0, we hit a case that we don't support. Output a setcc
1247 // and compare the result against zero.
1248 if (Opcode == 0) {
1249 unsigned CompClass = getClassB(CompTy);
1250 unsigned CondReg;
1251 if (CompClass != cLong || OpNum < 2) {
1252 CondReg = makeAnotherReg(Type::BoolTy);
1253 // Handle normal comparisons with a setcc instruction...
1254 BuildMI(*MBB, IP, SetCCOpcodeTab[isSigned][OpNum], 0, CondReg);
1255 } else {
1256 // Long comparisons end up in the BL register.
1257 CondReg = X86::BL;
1258 }
1259
Chris Lattner68626c22004-03-31 22:22:36 +00001260 BuildMI(*MBB, IP, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
Chris Lattner352eb482004-03-31 22:03:35 +00001261 Opcode = X86::FCMOVE;
1262 }
1263 break;
1264 }
Chris Lattner307ecba2004-03-30 22:39:09 +00001265 case cByte:
1266 case cShort: {
1267 static const unsigned OpcodeTab[2][8] = {
1268 { X86::CMOVNE16rr, X86::CMOVE16rr, X86::CMOVAE16rr, X86::CMOVB16rr,
1269 X86::CMOVBE16rr, X86::CMOVA16rr, 0, 0 },
1270 { X86::CMOVNE16rr, X86::CMOVE16rr, X86::CMOVGE16rr, X86::CMOVL16rr,
1271 X86::CMOVLE16rr, X86::CMOVG16rr, X86::CMOVNS16rr, X86::CMOVS16rr },
1272 };
1273 Opcode = OpcodeTab[isSigned][OpNum];
1274 break;
1275 }
1276 case cInt:
1277 case cLong: {
1278 static const unsigned OpcodeTab[2][8] = {
1279 { X86::CMOVNE32rr, X86::CMOVE32rr, X86::CMOVAE32rr, X86::CMOVB32rr,
1280 X86::CMOVBE32rr, X86::CMOVA32rr, 0, 0 },
1281 { X86::CMOVNE32rr, X86::CMOVE32rr, X86::CMOVGE32rr, X86::CMOVL32rr,
1282 X86::CMOVLE32rr, X86::CMOVG32rr, X86::CMOVNS32rr, X86::CMOVS32rr },
1283 };
1284 Opcode = OpcodeTab[isSigned][OpNum];
1285 break;
1286 }
1287 }
1288 } else {
1289 // Get the value being branched on, and use it to set the condition codes.
1290 unsigned CondReg = getReg(Cond, MBB, IP);
Chris Lattner68626c22004-03-31 22:22:36 +00001291 BuildMI(*MBB, IP, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
Chris Lattner307ecba2004-03-30 22:39:09 +00001292 switch (SelectClass) {
Chris Lattner352eb482004-03-31 22:03:35 +00001293 default: assert(0 && "Unknown value class!");
1294 case cFP: Opcode = X86::FCMOVE; break;
Chris Lattner307ecba2004-03-30 22:39:09 +00001295 case cByte:
Chris Lattner352eb482004-03-31 22:03:35 +00001296 case cShort: Opcode = X86::CMOVE16rr; break;
Chris Lattner307ecba2004-03-30 22:39:09 +00001297 case cInt:
Chris Lattner352eb482004-03-31 22:03:35 +00001298 case cLong: Opcode = X86::CMOVE32rr; break;
Chris Lattner307ecba2004-03-30 22:39:09 +00001299 }
1300 }
Chris Lattner12d96a02004-03-30 21:22:00 +00001301
Chris Lattner12d96a02004-03-30 21:22:00 +00001302 unsigned RealDestReg = DestReg;
Chris Lattner12d96a02004-03-30 21:22:00 +00001303
Chris Lattner12d96a02004-03-30 21:22:00 +00001304
1305 // Annoyingly enough, X86 doesn't HAVE 8-bit conditional moves. Because of
1306 // this, we have to promote the incoming values to 16 bits, perform a 16-bit
1307 // cmove, then truncate the result.
1308 if (SelectClass == cByte) {
1309 DestReg = makeAnotherReg(Type::ShortTy);
1310 if (getClassB(TrueVal->getType()) == cByte) {
1311 // Promote the true value, by storing it into AL, and reading from AX.
1312 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::AL).addReg(TrueReg);
1313 BuildMI(*MBB, IP, X86::MOV8ri, 1, X86::AH).addImm(0);
1314 TrueReg = makeAnotherReg(Type::ShortTy);
1315 BuildMI(*MBB, IP, X86::MOV16rr, 1, TrueReg).addReg(X86::AX);
1316 }
1317 if (getClassB(FalseVal->getType()) == cByte) {
1318 // Promote the true value, by storing it into CL, and reading from CX.
1319 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(FalseReg);
1320 BuildMI(*MBB, IP, X86::MOV8ri, 1, X86::CH).addImm(0);
1321 FalseReg = makeAnotherReg(Type::ShortTy);
1322 BuildMI(*MBB, IP, X86::MOV16rr, 1, FalseReg).addReg(X86::CX);
1323 }
1324 }
1325
1326 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(TrueReg).addReg(FalseReg);
1327
1328 switch (SelectClass) {
1329 case cByte:
1330 // We did the computation with 16-bit registers. Truncate back to our
1331 // result by copying into AX then copying out AL.
1332 BuildMI(*MBB, IP, X86::MOV16rr, 1, X86::AX).addReg(DestReg);
1333 BuildMI(*MBB, IP, X86::MOV8rr, 1, RealDestReg).addReg(X86::AL);
1334 break;
1335 case cLong:
1336 // Move the upper half of the value as well.
1337 BuildMI(*MBB, IP, Opcode, 2,DestReg+1).addReg(TrueReg+1).addReg(FalseReg+1);
1338 break;
1339 }
1340}
Chris Lattner58c41fe2003-08-24 19:19:47 +00001341
1342
1343
Brian Gaekec2505982002-11-30 11:57:28 +00001344/// promote32 - Emit instructions to turn a narrow operand into a 32-bit-wide
1345/// operand, in the specified target register.
Misha Brukman538607f2004-03-01 23:53:11 +00001346///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001347void X86ISel::promote32(unsigned targetReg, const ValueRecord &VR) {
Chris Lattner9984fd02004-05-09 23:16:33 +00001348 bool isUnsigned = VR.Ty->isUnsigned() || VR.Ty == Type::BoolTy;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001349
Chris Lattner29bf0622004-04-06 01:21:00 +00001350 Value *Val = VR.Val;
1351 const Type *Ty = VR.Ty;
Chris Lattner502e36c2004-04-06 01:25:33 +00001352 if (Val) {
Chris Lattner29bf0622004-04-06 01:21:00 +00001353 if (Constant *C = dyn_cast<Constant>(Val)) {
1354 Val = ConstantExpr::getCast(C, Type::IntTy);
1355 Ty = Type::IntTy;
1356 }
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001357
Chris Lattner502e36c2004-04-06 01:25:33 +00001358 // If this is a simple constant, just emit a MOVri directly to avoid the
1359 // copy.
1360 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
1361 int TheVal = CI->getRawValue() & 0xFFFFFFFF;
Chris Lattner2b10b082004-05-12 16:35:04 +00001362 BuildMI(BB, X86::MOV32ri, 1, targetReg).addImm(TheVal);
Chris Lattner502e36c2004-04-06 01:25:33 +00001363 return;
1364 }
1365 }
1366
Chris Lattner29bf0622004-04-06 01:21:00 +00001367 // Make sure we have the register number for this value...
1368 unsigned Reg = Val ? getReg(Val) : VR.Reg;
1369
1370 switch (getClassB(Ty)) {
Chris Lattner94af4142002-12-25 05:13:53 +00001371 case cByte:
1372 // Extend value into target register (8->32)
1373 if (isUnsigned)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001374 BuildMI(BB, X86::MOVZX32rr8, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001375 else
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001376 BuildMI(BB, X86::MOVSX32rr8, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001377 break;
1378 case cShort:
1379 // Extend value into target register (16->32)
1380 if (isUnsigned)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001381 BuildMI(BB, X86::MOVZX32rr16, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001382 else
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001383 BuildMI(BB, X86::MOVSX32rr16, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001384 break;
1385 case cInt:
1386 // Move value into target register (32->32)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001387 BuildMI(BB, X86::MOV32rr, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001388 break;
1389 default:
1390 assert(0 && "Unpromotable operand class in promote32");
1391 }
Brian Gaekec2505982002-11-30 11:57:28 +00001392}
Chris Lattnerc5291f52002-10-27 21:16:59 +00001393
Chris Lattner72614082002-10-25 22:55:53 +00001394/// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such,
1395/// we have the following possibilities:
1396///
1397/// ret void: No return value, simply emit a 'ret' instruction
1398/// ret sbyte, ubyte : Extend value into EAX and return
1399/// ret short, ushort: Extend value into EAX and return
1400/// ret int, uint : Move value into EAX and return
1401/// ret pointer : Move value into EAX and return
Chris Lattner06925362002-11-17 21:56:38 +00001402/// ret long, ulong : Move value into EAX/EDX and return
1403/// ret float/double : Top of FP stack
Chris Lattner72614082002-10-25 22:55:53 +00001404///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001405void X86ISel::visitReturnInst(ReturnInst &I) {
Chris Lattner94af4142002-12-25 05:13:53 +00001406 if (I.getNumOperands() == 0) {
1407 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
1408 return;
1409 }
1410
1411 Value *RetVal = I.getOperand(0);
Chris Lattner3e130a22003-01-13 00:32:26 +00001412 switch (getClassB(RetVal->getType())) {
Chris Lattner94af4142002-12-25 05:13:53 +00001413 case cByte: // integral return values: extend or move into EAX and return
1414 case cShort:
1415 case cInt:
Chris Lattner29bf0622004-04-06 01:21:00 +00001416 promote32(X86::EAX, ValueRecord(RetVal));
Chris Lattnerdbd73722003-05-06 21:32:22 +00001417 // Declare that EAX is live on exit
Chris Lattnerc2489032003-05-07 19:21:28 +00001418 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
Chris Lattner94af4142002-12-25 05:13:53 +00001419 break;
Chris Lattner29bf0622004-04-06 01:21:00 +00001420 case cFP: { // Floats & Doubles: Return in ST(0)
1421 unsigned RetReg = getReg(RetVal);
Chris Lattner3e130a22003-01-13 00:32:26 +00001422 BuildMI(BB, X86::FpSETRESULT, 1).addReg(RetReg);
Chris Lattnerdbd73722003-05-06 21:32:22 +00001423 // Declare that top-of-stack is live on exit
Chris Lattnerc2489032003-05-07 19:21:28 +00001424 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
Chris Lattner94af4142002-12-25 05:13:53 +00001425 break;
Chris Lattner29bf0622004-04-06 01:21:00 +00001426 }
1427 case cLong: {
1428 unsigned RetReg = getReg(RetVal);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001429 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(RetReg);
1430 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RetReg+1);
Chris Lattnerdbd73722003-05-06 21:32:22 +00001431 // Declare that EAX & EDX are live on exit
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001432 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
1433 .addReg(X86::ESP);
Chris Lattner3e130a22003-01-13 00:32:26 +00001434 break;
Chris Lattner29bf0622004-04-06 01:21:00 +00001435 }
Chris Lattner94af4142002-12-25 05:13:53 +00001436 default:
Chris Lattner3e130a22003-01-13 00:32:26 +00001437 visitInstruction(I);
Chris Lattner94af4142002-12-25 05:13:53 +00001438 }
Chris Lattner43189d12002-11-17 20:07:45 +00001439 // Emit a 'ret' instruction
Chris Lattner94af4142002-12-25 05:13:53 +00001440 BuildMI(BB, X86::RET, 0);
Chris Lattner72614082002-10-25 22:55:53 +00001441}
1442
Chris Lattner55f6fab2003-01-16 18:07:23 +00001443// getBlockAfter - Return the basic block which occurs lexically after the
1444// specified one.
1445static inline BasicBlock *getBlockAfter(BasicBlock *BB) {
1446 Function::iterator I = BB; ++I; // Get iterator to next block
1447 return I != BB->getParent()->end() ? &*I : 0;
1448}
1449
Chris Lattner51b49a92002-11-02 19:45:49 +00001450/// visitBranchInst - Handle conditional and unconditional branches here. Note
1451/// that since code layout is frozen at this point, that if we are trying to
1452/// jump to a block that is the immediate successor of the current block, we can
Chris Lattner6d40c192003-01-16 16:43:00 +00001453/// just make a fall-through (but we don't currently).
Chris Lattner51b49a92002-11-02 19:45:49 +00001454///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001455void X86ISel::visitBranchInst(BranchInst &BI) {
Brian Gaekeea9ca672004-04-28 04:19:37 +00001456 // Update machine-CFG edges
1457 BB->addSuccessor (MBBMap[BI.getSuccessor(0)]);
1458 if (BI.isConditional())
1459 BB->addSuccessor (MBBMap[BI.getSuccessor(1)]);
1460
Chris Lattner55f6fab2003-01-16 18:07:23 +00001461 BasicBlock *NextBB = getBlockAfter(BI.getParent()); // BB after current one
1462
1463 if (!BI.isConditional()) { // Unconditional branch?
Chris Lattnercf93cdd2004-01-30 22:13:44 +00001464 if (BI.getSuccessor(0) != NextBB)
Brian Gaeke9f088e42004-05-14 06:54:56 +00001465 BuildMI(BB, X86::JMP, 1).addMBB(MBBMap[BI.getSuccessor(0)]);
Chris Lattner6d40c192003-01-16 16:43:00 +00001466 return;
1467 }
1468
1469 // See if we can fold the setcc into the branch itself...
Chris Lattner307ecba2004-03-30 22:39:09 +00001470 SetCondInst *SCI = canFoldSetCCIntoBranchOrSelect(BI.getCondition());
Chris Lattner6d40c192003-01-16 16:43:00 +00001471 if (SCI == 0) {
1472 // Nope, cannot fold setcc into this branch. Emit a branch on a condition
1473 // computed some other way...
Chris Lattner065faeb2002-12-28 20:24:02 +00001474 unsigned condReg = getReg(BI.getCondition());
Chris Lattner68626c22004-03-31 22:22:36 +00001475 BuildMI(BB, X86::TEST8rr, 2).addReg(condReg).addReg(condReg);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001476 if (BI.getSuccessor(1) == NextBB) {
1477 if (BI.getSuccessor(0) != NextBB)
Brian Gaeke9f088e42004-05-14 06:54:56 +00001478 BuildMI(BB, X86::JNE, 1).addMBB(MBBMap[BI.getSuccessor(0)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001479 } else {
Brian Gaeke9f088e42004-05-14 06:54:56 +00001480 BuildMI(BB, X86::JE, 1).addMBB(MBBMap[BI.getSuccessor(1)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001481
1482 if (BI.getSuccessor(0) != NextBB)
Brian Gaeke9f088e42004-05-14 06:54:56 +00001483 BuildMI(BB, X86::JMP, 1).addMBB(MBBMap[BI.getSuccessor(0)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001484 }
Chris Lattner6d40c192003-01-16 16:43:00 +00001485 return;
Chris Lattner94af4142002-12-25 05:13:53 +00001486 }
Chris Lattner6d40c192003-01-16 16:43:00 +00001487
1488 unsigned OpNum = getSetCCNumber(SCI->getOpcode());
Chris Lattner58c41fe2003-08-24 19:19:47 +00001489 MachineBasicBlock::iterator MII = BB->end();
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001490 OpNum = EmitComparison(OpNum, SCI->getOperand(0), SCI->getOperand(1), BB,MII);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001491
1492 const Type *CompTy = SCI->getOperand(0)->getType();
1493 bool isSigned = CompTy->isSigned() && getClassB(CompTy) != cFP;
Chris Lattner6d40c192003-01-16 16:43:00 +00001494
Chris Lattnerb2acc512003-10-19 21:09:10 +00001495
Chris Lattner6d40c192003-01-16 16:43:00 +00001496 // LLVM -> X86 signed X86 unsigned
1497 // ----- ---------- ------------
1498 // seteq -> je je
1499 // setne -> jne jne
1500 // setlt -> jl jb
Chris Lattner55f6fab2003-01-16 18:07:23 +00001501 // setge -> jge jae
Chris Lattner6d40c192003-01-16 16:43:00 +00001502 // setgt -> jg ja
1503 // setle -> jle jbe
Chris Lattnerb2acc512003-10-19 21:09:10 +00001504 // ----
1505 // js // Used by comparison with 0 optimization
1506 // jns
1507
1508 static const unsigned OpcodeTab[2][8] = {
1509 { X86::JE, X86::JNE, X86::JB, X86::JAE, X86::JA, X86::JBE, 0, 0 },
1510 { X86::JE, X86::JNE, X86::JL, X86::JGE, X86::JG, X86::JLE,
1511 X86::JS, X86::JNS },
Chris Lattner6d40c192003-01-16 16:43:00 +00001512 };
1513
Chris Lattner55f6fab2003-01-16 18:07:23 +00001514 if (BI.getSuccessor(0) != NextBB) {
Brian Gaeke9f088e42004-05-14 06:54:56 +00001515 BuildMI(BB, OpcodeTab[isSigned][OpNum], 1)
1516 .addMBB(MBBMap[BI.getSuccessor(0)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001517 if (BI.getSuccessor(1) != NextBB)
Brian Gaeke9f088e42004-05-14 06:54:56 +00001518 BuildMI(BB, X86::JMP, 1).addMBB(MBBMap[BI.getSuccessor(1)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001519 } else {
1520 // Change to the inverse condition...
1521 if (BI.getSuccessor(1) != NextBB) {
1522 OpNum ^= 1;
Brian Gaeke9f088e42004-05-14 06:54:56 +00001523 BuildMI(BB, OpcodeTab[isSigned][OpNum], 1)
1524 .addMBB(MBBMap[BI.getSuccessor(1)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001525 }
1526 }
Chris Lattner2df035b2002-11-02 19:27:56 +00001527}
1528
Chris Lattner3e130a22003-01-13 00:32:26 +00001529
1530/// doCall - This emits an abstract call instruction, setting up the arguments
1531/// and the return value as appropriate. For the actual function call itself,
1532/// it inserts the specified CallMI instruction into the stream.
1533///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001534void X86ISel::doCall(const ValueRecord &Ret, MachineInstr *CallMI,
1535 const std::vector<ValueRecord> &Args) {
Chris Lattner065faeb2002-12-28 20:24:02 +00001536 // Count how many bytes are to be pushed on the stack...
1537 unsigned NumBytes = 0;
Misha Brukman0d2cf3a2002-12-04 19:22:53 +00001538
Chris Lattner3e130a22003-01-13 00:32:26 +00001539 if (!Args.empty()) {
1540 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1541 switch (getClassB(Args[i].Ty)) {
Chris Lattner065faeb2002-12-28 20:24:02 +00001542 case cByte: case cShort: case cInt:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001543 NumBytes += 4; break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001544 case cLong:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001545 NumBytes += 8; break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001546 case cFP:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001547 NumBytes += Args[i].Ty == Type::FloatTy ? 4 : 8;
1548 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001549 default: assert(0 && "Unknown class!");
1550 }
1551
1552 // Adjust the stack pointer for the new arguments...
Chris Lattneree352852004-02-29 07:22:16 +00001553 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(NumBytes);
Chris Lattner065faeb2002-12-28 20:24:02 +00001554
1555 // Arguments go on the stack in reverse order, as specified by the ABI.
1556 unsigned ArgOffset = 0;
Chris Lattner3e130a22003-01-13 00:32:26 +00001557 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattnerce6096f2004-03-01 02:34:08 +00001558 unsigned ArgReg;
Chris Lattner3e130a22003-01-13 00:32:26 +00001559 switch (getClassB(Args[i].Ty)) {
Chris Lattner065faeb2002-12-28 20:24:02 +00001560 case cByte:
Chris Lattner2b10b082004-05-12 16:35:04 +00001561 if (Args[i].Val && isa<ConstantBool>(Args[i].Val)) {
1562 addRegOffset(BuildMI(BB, X86::MOV32mi, 5), X86::ESP, ArgOffset)
1563 .addImm(Args[i].Val == ConstantBool::True);
1564 break;
1565 }
1566 // FALL THROUGH
Chris Lattner21585222004-03-01 02:42:43 +00001567 case cShort:
1568 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1569 // Zero/Sign extend constant, then stuff into memory.
1570 ConstantInt *Val = cast<ConstantInt>(Args[i].Val);
1571 Val = cast<ConstantInt>(ConstantExpr::getCast(Val, Type::IntTy));
1572 addRegOffset(BuildMI(BB, X86::MOV32mi, 5), X86::ESP, ArgOffset)
1573 .addImm(Val->getRawValue() & 0xFFFFFFFF);
1574 } else {
1575 // Promote arg to 32 bits wide into a temporary register...
1576 ArgReg = makeAnotherReg(Type::UIntTy);
1577 promote32(ArgReg, Args[i]);
1578 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1579 X86::ESP, ArgOffset).addReg(ArgReg);
1580 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001581 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001582 case cInt:
Chris Lattner21585222004-03-01 02:42:43 +00001583 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1584 unsigned Val = cast<ConstantInt>(Args[i].Val)->getRawValue();
1585 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1586 X86::ESP, ArgOffset).addImm(Val);
Chris Lattnerb7cb9ff2004-05-13 15:26:48 +00001587 } else if (Args[i].Val && isa<ConstantPointerNull>(Args[i].Val)) {
1588 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1589 X86::ESP, ArgOffset).addImm(0);
Chris Lattner21585222004-03-01 02:42:43 +00001590 } else {
1591 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1592 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1593 X86::ESP, ArgOffset).addReg(ArgReg);
1594 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001595 break;
Chris Lattner3e130a22003-01-13 00:32:26 +00001596 case cLong:
Chris Lattner92900a62004-04-06 03:23:00 +00001597 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1598 uint64_t Val = cast<ConstantInt>(Args[i].Val)->getRawValue();
1599 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1600 X86::ESP, ArgOffset).addImm(Val & ~0U);
1601 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1602 X86::ESP, ArgOffset+4).addImm(Val >> 32ULL);
1603 } else {
1604 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1605 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1606 X86::ESP, ArgOffset).addReg(ArgReg);
1607 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1608 X86::ESP, ArgOffset+4).addReg(ArgReg+1);
1609 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001610 ArgOffset += 4; // 8 byte entry, not 4.
1611 break;
1612
Chris Lattner065faeb2002-12-28 20:24:02 +00001613 case cFP:
Chris Lattnerce6096f2004-03-01 02:34:08 +00001614 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001615 if (Args[i].Ty == Type::FloatTy) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001616 addRegOffset(BuildMI(BB, X86::FST32m, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001617 X86::ESP, ArgOffset).addReg(ArgReg);
1618 } else {
1619 assert(Args[i].Ty == Type::DoubleTy && "Unknown FP type!");
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001620 addRegOffset(BuildMI(BB, X86::FST64m, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001621 X86::ESP, ArgOffset).addReg(ArgReg);
1622 ArgOffset += 4; // 8 byte entry, not 4.
1623 }
1624 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001625
Chris Lattner3e130a22003-01-13 00:32:26 +00001626 default: assert(0 && "Unknown class!");
Chris Lattner065faeb2002-12-28 20:24:02 +00001627 }
1628 ArgOffset += 4;
Chris Lattner94af4142002-12-25 05:13:53 +00001629 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001630 } else {
Chris Lattneree352852004-02-29 07:22:16 +00001631 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(0);
Chris Lattner94af4142002-12-25 05:13:53 +00001632 }
Chris Lattner6e49a4b2002-12-13 14:13:27 +00001633
Chris Lattner3e130a22003-01-13 00:32:26 +00001634 BB->push_back(CallMI);
Misha Brukman0d2cf3a2002-12-04 19:22:53 +00001635
Chris Lattneree352852004-02-29 07:22:16 +00001636 BuildMI(BB, X86::ADJCALLSTACKUP, 1).addImm(NumBytes);
Chris Lattnera3243642002-12-04 23:45:28 +00001637
1638 // If there is a return value, scavenge the result from the location the call
1639 // leaves it in...
1640 //
Chris Lattner3e130a22003-01-13 00:32:26 +00001641 if (Ret.Ty != Type::VoidTy) {
1642 unsigned DestClass = getClassB(Ret.Ty);
1643 switch (DestClass) {
Brian Gaeke20244b72002-12-12 15:33:40 +00001644 case cByte:
1645 case cShort:
1646 case cInt: {
1647 // Integral results are in %eax, or the appropriate portion
1648 // thereof.
1649 static const unsigned regRegMove[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001650 X86::MOV8rr, X86::MOV16rr, X86::MOV32rr
Brian Gaeke20244b72002-12-12 15:33:40 +00001651 };
1652 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX };
Chris Lattner3e130a22003-01-13 00:32:26 +00001653 BuildMI(BB, regRegMove[DestClass], 1, Ret.Reg).addReg(AReg[DestClass]);
Chris Lattner4fa1acc2002-12-04 23:50:28 +00001654 break;
Brian Gaeke20244b72002-12-12 15:33:40 +00001655 }
Chris Lattner94af4142002-12-25 05:13:53 +00001656 case cFP: // Floating-point return values live in %ST(0)
Chris Lattner3e130a22003-01-13 00:32:26 +00001657 BuildMI(BB, X86::FpGETRESULT, 1, Ret.Reg);
Brian Gaeke20244b72002-12-12 15:33:40 +00001658 break;
Chris Lattner3e130a22003-01-13 00:32:26 +00001659 case cLong: // Long values are left in EDX:EAX
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001660 BuildMI(BB, X86::MOV32rr, 1, Ret.Reg).addReg(X86::EAX);
1661 BuildMI(BB, X86::MOV32rr, 1, Ret.Reg+1).addReg(X86::EDX);
Chris Lattner3e130a22003-01-13 00:32:26 +00001662 break;
1663 default: assert(0 && "Unknown class!");
Chris Lattner4fa1acc2002-12-04 23:50:28 +00001664 }
Chris Lattnera3243642002-12-04 23:45:28 +00001665 }
Brian Gaekefa8d5712002-11-22 11:07:01 +00001666}
Chris Lattner2df035b2002-11-02 19:27:56 +00001667
Chris Lattner3e130a22003-01-13 00:32:26 +00001668
1669/// visitCallInst - Push args on stack and do a procedure call instruction.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001670void X86ISel::visitCallInst(CallInst &CI) {
Chris Lattner3e130a22003-01-13 00:32:26 +00001671 MachineInstr *TheCall;
1672 if (Function *F = CI.getCalledFunction()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001673 // Is it an intrinsic function call?
Brian Gaeked0fde302003-11-11 22:41:34 +00001674 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001675 visitIntrinsicCall(ID, CI); // Special intrinsics are not handled here
1676 return;
1677 }
1678
Chris Lattner3e130a22003-01-13 00:32:26 +00001679 // Emit a CALL instruction with PC-relative displacement.
1680 TheCall = BuildMI(X86::CALLpcrel32, 1).addGlobalAddress(F, true);
1681 } else { // Emit an indirect call...
1682 unsigned Reg = getReg(CI.getCalledValue());
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001683 TheCall = BuildMI(X86::CALL32r, 1).addReg(Reg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001684 }
1685
1686 std::vector<ValueRecord> Args;
1687 for (unsigned i = 1, e = CI.getNumOperands(); i != e; ++i)
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001688 Args.push_back(ValueRecord(CI.getOperand(i)));
Chris Lattner3e130a22003-01-13 00:32:26 +00001689
1690 unsigned DestReg = CI.getType() != Type::VoidTy ? getReg(CI) : 0;
1691 doCall(ValueRecord(DestReg, CI.getType()), TheCall, Args);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001692}
Chris Lattner3e130a22003-01-13 00:32:26 +00001693
Chris Lattner44827152003-12-28 09:47:19 +00001694/// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
1695/// function, lowering any calls to unknown intrinsic functions into the
1696/// equivalent LLVM code.
Misha Brukman538607f2004-03-01 23:53:11 +00001697///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001698void X86ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) {
Chris Lattner44827152003-12-28 09:47:19 +00001699 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1700 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
1701 if (CallInst *CI = dyn_cast<CallInst>(I++))
1702 if (Function *F = CI->getCalledFunction())
1703 switch (F->getIntrinsicID()) {
Chris Lattneraed386e2003-12-28 09:53:23 +00001704 case Intrinsic::not_intrinsic:
Chris Lattner317201d2004-03-13 00:24:00 +00001705 case Intrinsic::vastart:
1706 case Intrinsic::vacopy:
1707 case Intrinsic::vaend:
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001708 case Intrinsic::returnaddress:
1709 case Intrinsic::frameaddress:
Chris Lattner915e5e52004-02-12 17:53:22 +00001710 case Intrinsic::memcpy:
Chris Lattner2a0f2242004-02-14 04:46:05 +00001711 case Intrinsic::memset:
Chris Lattnerdc572442004-06-15 21:36:44 +00001712 case Intrinsic::isunordered:
John Criswell4ffff9e2004-04-08 20:31:47 +00001713 case Intrinsic::readport:
1714 case Intrinsic::writeport:
Chris Lattner44827152003-12-28 09:47:19 +00001715 // We directly implement these intrinsics
1716 break;
John Criswelle5a4c152004-04-13 22:13:14 +00001717 case Intrinsic::readio: {
1718 // On X86, memory operations are in-order. Lower this intrinsic
1719 // into a volatile load.
1720 Instruction *Before = CI->getPrev();
Chris Lattnerb4fe76c2004-06-11 04:31:10 +00001721 LoadInst * LI = new LoadInst(CI->getOperand(1), "", true, CI);
1722 CI->replaceAllUsesWith(LI);
1723 BB->getInstList().erase(CI);
John Criswelle5a4c152004-04-13 22:13:14 +00001724 break;
1725 }
1726 case Intrinsic::writeio: {
1727 // On X86, memory operations are in-order. Lower this intrinsic
1728 // into a volatile store.
1729 Instruction *Before = CI->getPrev();
Chris Lattnerb4fe76c2004-06-11 04:31:10 +00001730 StoreInst *LI = new StoreInst(CI->getOperand(1),
1731 CI->getOperand(2), true, CI);
1732 CI->replaceAllUsesWith(LI);
1733 BB->getInstList().erase(CI);
John Criswelle5a4c152004-04-13 22:13:14 +00001734 break;
1735 }
Chris Lattner44827152003-12-28 09:47:19 +00001736 default:
1737 // All other intrinsic calls we must lower.
1738 Instruction *Before = CI->getPrev();
Chris Lattnerf70e0c22003-12-28 21:23:38 +00001739 TM.getIntrinsicLowering().LowerIntrinsicCall(CI);
Chris Lattner44827152003-12-28 09:47:19 +00001740 if (Before) { // Move iterator to instruction after call
Chris Lattnerb4fe76c2004-06-11 04:31:10 +00001741 I = Before; ++I;
Chris Lattner44827152003-12-28 09:47:19 +00001742 } else {
1743 I = BB->begin();
1744 }
1745 }
Chris Lattner44827152003-12-28 09:47:19 +00001746}
1747
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001748void X86ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001749 unsigned TmpReg1, TmpReg2;
1750 switch (ID) {
Chris Lattner5634b9f2004-03-13 00:24:52 +00001751 case Intrinsic::vastart:
Chris Lattnereca195e2003-05-08 19:44:13 +00001752 // Get the address of the first vararg value...
Chris Lattner73815062003-10-18 05:56:40 +00001753 TmpReg1 = getReg(CI);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001754 addFrameReference(BuildMI(BB, X86::LEA32r, 5, TmpReg1), VarArgsFrameIndex);
Chris Lattnereca195e2003-05-08 19:44:13 +00001755 return;
1756
Chris Lattner5634b9f2004-03-13 00:24:52 +00001757 case Intrinsic::vacopy:
Chris Lattner73815062003-10-18 05:56:40 +00001758 TmpReg1 = getReg(CI);
1759 TmpReg2 = getReg(CI.getOperand(1));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001760 BuildMI(BB, X86::MOV32rr, 1, TmpReg1).addReg(TmpReg2);
Chris Lattnereca195e2003-05-08 19:44:13 +00001761 return;
Chris Lattner5634b9f2004-03-13 00:24:52 +00001762 case Intrinsic::vaend: return; // Noop on X86
Chris Lattnereca195e2003-05-08 19:44:13 +00001763
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001764 case Intrinsic::returnaddress:
1765 case Intrinsic::frameaddress:
1766 TmpReg1 = getReg(CI);
1767 if (cast<Constant>(CI.getOperand(1))->isNullValue()) {
1768 if (ID == Intrinsic::returnaddress) {
1769 // Just load the return address
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001770 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, TmpReg1),
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001771 ReturnAddressIndex);
1772 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001773 addFrameReference(BuildMI(BB, X86::LEA32r, 4, TmpReg1),
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001774 ReturnAddressIndex, -4);
1775 }
1776 } else {
1777 // Values other than zero are not implemented yet.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001778 BuildMI(BB, X86::MOV32ri, 1, TmpReg1).addImm(0);
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001779 }
1780 return;
1781
Chris Lattnerdc572442004-06-15 21:36:44 +00001782 case Intrinsic::isunordered:
1783 TmpReg1 = getReg(CI.getOperand(1));
1784 TmpReg2 = getReg(CI.getOperand(2));
1785 emitUCOMr(BB, BB->end(), TmpReg2, TmpReg1);
1786 TmpReg2 = getReg(CI);
1787 BuildMI(BB, X86::SETPr, 0, TmpReg2);
1788 return;
1789
Chris Lattner915e5e52004-02-12 17:53:22 +00001790 case Intrinsic::memcpy: {
1791 assert(CI.getNumOperands() == 5 && "Illegal llvm.memcpy call!");
1792 unsigned Align = 1;
1793 if (ConstantInt *AlignC = dyn_cast<ConstantInt>(CI.getOperand(4))) {
1794 Align = AlignC->getRawValue();
1795 if (Align == 0) Align = 1;
1796 }
1797
1798 // Turn the byte code into # iterations
Chris Lattner915e5e52004-02-12 17:53:22 +00001799 unsigned CountReg;
Chris Lattner2a0f2242004-02-14 04:46:05 +00001800 unsigned Opcode;
Chris Lattner915e5e52004-02-12 17:53:22 +00001801 switch (Align & 3) {
1802 case 2: // WORD aligned
Chris Lattner07122832004-02-13 23:36:47 +00001803 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
1804 CountReg = getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/2));
1805 } else {
1806 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001807 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001808 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
Chris Lattner07122832004-02-13 23:36:47 +00001809 }
Chris Lattner2a0f2242004-02-14 04:46:05 +00001810 Opcode = X86::REP_MOVSW;
Chris Lattner915e5e52004-02-12 17:53:22 +00001811 break;
1812 case 0: // DWORD aligned
Chris Lattner07122832004-02-13 23:36:47 +00001813 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
1814 CountReg = getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/4));
1815 } else {
1816 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001817 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001818 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
Chris Lattner07122832004-02-13 23:36:47 +00001819 }
Chris Lattner2a0f2242004-02-14 04:46:05 +00001820 Opcode = X86::REP_MOVSD;
Chris Lattner915e5e52004-02-12 17:53:22 +00001821 break;
Chris Lattner8dd8d262004-02-26 01:20:02 +00001822 default: // BYTE aligned
Chris Lattner07122832004-02-13 23:36:47 +00001823 CountReg = getReg(CI.getOperand(3));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001824 Opcode = X86::REP_MOVSB;
Chris Lattner915e5e52004-02-12 17:53:22 +00001825 break;
1826 }
1827
1828 // No matter what the alignment is, we put the source in ESI, the
1829 // destination in EDI, and the count in ECX.
1830 TmpReg1 = getReg(CI.getOperand(1));
1831 TmpReg2 = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001832 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
1833 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
1834 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001835 BuildMI(BB, Opcode, 0);
1836 return;
1837 }
1838 case Intrinsic::memset: {
1839 assert(CI.getNumOperands() == 5 && "Illegal llvm.memset call!");
1840 unsigned Align = 1;
1841 if (ConstantInt *AlignC = dyn_cast<ConstantInt>(CI.getOperand(4))) {
1842 Align = AlignC->getRawValue();
1843 if (Align == 0) Align = 1;
Chris Lattner915e5e52004-02-12 17:53:22 +00001844 }
1845
Chris Lattner2a0f2242004-02-14 04:46:05 +00001846 // Turn the byte code into # iterations
Chris Lattner2a0f2242004-02-14 04:46:05 +00001847 unsigned CountReg;
1848 unsigned Opcode;
1849 if (ConstantInt *ValC = dyn_cast<ConstantInt>(CI.getOperand(2))) {
1850 unsigned Val = ValC->getRawValue() & 255;
1851
1852 // If the value is a constant, then we can potentially use larger copies.
1853 switch (Align & 3) {
1854 case 2: // WORD aligned
1855 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
Chris Lattner300d0ed2004-02-14 06:00:36 +00001856 CountReg =getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/2));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001857 } else {
1858 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001859 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001860 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001861 }
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001862 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001863 Opcode = X86::REP_STOSW;
1864 break;
1865 case 0: // DWORD aligned
1866 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
Chris Lattner300d0ed2004-02-14 06:00:36 +00001867 CountReg =getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/4));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001868 } else {
1869 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001870 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001871 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001872 }
1873 Val = (Val << 8) | Val;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001874 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001875 Opcode = X86::REP_STOSD;
1876 break;
Chris Lattner8dd8d262004-02-26 01:20:02 +00001877 default: // BYTE aligned
Chris Lattner2a0f2242004-02-14 04:46:05 +00001878 CountReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001879 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001880 Opcode = X86::REP_STOSB;
1881 break;
1882 }
1883 } else {
1884 // If it's not a constant value we are storing, just fall back. We could
1885 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
1886 unsigned ValReg = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001887 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001888 CountReg = getReg(CI.getOperand(3));
1889 Opcode = X86::REP_STOSB;
1890 }
1891
1892 // No matter what the alignment is, we put the source in ESI, the
1893 // destination in EDI, and the count in ECX.
1894 TmpReg1 = getReg(CI.getOperand(1));
1895 //TmpReg2 = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001896 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
1897 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001898 BuildMI(BB, Opcode, 0);
Chris Lattner915e5e52004-02-12 17:53:22 +00001899 return;
1900 }
1901
Chris Lattner87e18de2004-04-13 17:20:37 +00001902 case Intrinsic::readport: {
1903 // First, determine that the size of the operand falls within the acceptable
1904 // range for this architecture.
John Criswell4ffff9e2004-04-08 20:31:47 +00001905 //
Chris Lattner87e18de2004-04-13 17:20:37 +00001906 if (getClassB(CI.getOperand(1)->getType()) != cShort) {
John Criswellca6ea0f2004-04-08 22:39:13 +00001907 std::cerr << "llvm.readport: Address size is not 16 bits\n";
Chris Lattner87e18de2004-04-13 17:20:37 +00001908 exit(1);
John Criswellca6ea0f2004-04-08 22:39:13 +00001909 }
John Criswell4ffff9e2004-04-08 20:31:47 +00001910
John Criswell4ffff9e2004-04-08 20:31:47 +00001911 // Now, move the I/O port address into the DX register and use the IN
1912 // instruction to get the input data.
1913 //
Chris Lattner87e18de2004-04-13 17:20:37 +00001914 unsigned Class = getClass(CI.getCalledFunction()->getReturnType());
1915 unsigned DestReg = getReg(CI);
John Criswell4ffff9e2004-04-08 20:31:47 +00001916
Chris Lattner87e18de2004-04-13 17:20:37 +00001917 // If the port is a single-byte constant, use the immediate form.
1918 if (ConstantInt *C = dyn_cast<ConstantInt>(CI.getOperand(1)))
1919 if ((C->getRawValue() & 255) == C->getRawValue()) {
1920 switch (Class) {
1921 case cByte:
1922 BuildMI(BB, X86::IN8ri, 1).addImm((unsigned char)C->getRawValue());
1923 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AL);
1924 return;
1925 case cShort:
1926 BuildMI(BB, X86::IN16ri, 1).addImm((unsigned char)C->getRawValue());
1927 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AX);
1928 return;
1929 case cInt:
1930 BuildMI(BB, X86::IN32ri, 1).addImm((unsigned char)C->getRawValue());
1931 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::EAX);
1932 return;
1933 }
1934 }
1935
1936 unsigned Reg = getReg(CI.getOperand(1));
1937 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
1938 switch (Class) {
1939 case cByte:
1940 BuildMI(BB, X86::IN8rr, 0);
1941 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AL);
1942 break;
1943 case cShort:
1944 BuildMI(BB, X86::IN16rr, 0);
1945 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AX);
1946 break;
1947 case cInt:
1948 BuildMI(BB, X86::IN32rr, 0);
1949 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::EAX);
1950 break;
1951 default:
1952 std::cerr << "Cannot do input on this data type";
John Criswellca6ea0f2004-04-08 22:39:13 +00001953 exit (1);
1954 }
John Criswell4ffff9e2004-04-08 20:31:47 +00001955 return;
Chris Lattner87e18de2004-04-13 17:20:37 +00001956 }
John Criswell4ffff9e2004-04-08 20:31:47 +00001957
Chris Lattner87e18de2004-04-13 17:20:37 +00001958 case Intrinsic::writeport: {
1959 // First, determine that the size of the operand falls within the
1960 // acceptable range for this architecture.
1961 if (getClass(CI.getOperand(2)->getType()) != cShort) {
1962 std::cerr << "llvm.writeport: Address size is not 16 bits\n";
1963 exit(1);
1964 }
1965
1966 unsigned Class = getClassB(CI.getOperand(1)->getType());
1967 unsigned ValReg = getReg(CI.getOperand(1));
1968 switch (Class) {
1969 case cByte:
1970 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
1971 break;
1972 case cShort:
1973 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(ValReg);
1974 break;
1975 case cInt:
1976 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(ValReg);
1977 break;
1978 default:
1979 std::cerr << "llvm.writeport: invalid data type for X86 target";
1980 exit(1);
1981 }
1982
1983
1984 // If the port is a single-byte constant, use the immediate form.
1985 if (ConstantInt *C = dyn_cast<ConstantInt>(CI.getOperand(2)))
1986 if ((C->getRawValue() & 255) == C->getRawValue()) {
1987 static const unsigned O[] = { X86::OUT8ir, X86::OUT16ir, X86::OUT32ir };
1988 BuildMI(BB, O[Class], 1).addImm((unsigned char)C->getRawValue());
1989 return;
1990 }
1991
1992 // Otherwise, move the I/O port address into the DX register and the value
1993 // to write into the AL/AX/EAX register.
1994 static const unsigned Opc[] = { X86::OUT8rr, X86::OUT16rr, X86::OUT32rr };
1995 unsigned Reg = getReg(CI.getOperand(2));
1996 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
1997 BuildMI(BB, Opc[Class], 0);
1998 return;
1999 }
2000
Chris Lattner44827152003-12-28 09:47:19 +00002001 default: assert(0 && "Error: unknown intrinsics should have been lowered!");
Chris Lattnereca195e2003-05-08 19:44:13 +00002002 }
2003}
2004
Chris Lattner7dee5da2004-03-08 01:58:35 +00002005static bool isSafeToFoldLoadIntoInstruction(LoadInst &LI, Instruction &User) {
2006 if (LI.getParent() != User.getParent())
2007 return false;
2008 BasicBlock::iterator It = &LI;
2009 // Check all of the instructions between the load and the user. We should
2010 // really use alias analysis here, but for now we just do something simple.
2011 for (++It; It != BasicBlock::iterator(&User); ++It) {
2012 switch (It->getOpcode()) {
Chris Lattner85c84e72004-03-18 06:29:54 +00002013 case Instruction::Free:
Chris Lattner7dee5da2004-03-08 01:58:35 +00002014 case Instruction::Store:
2015 case Instruction::Call:
2016 case Instruction::Invoke:
2017 return false;
Chris Lattner133dbb12004-04-12 03:02:48 +00002018 case Instruction::Load:
2019 if (cast<LoadInst>(It)->isVolatile() && LI.isVolatile())
2020 return false;
2021 break;
Chris Lattner7dee5da2004-03-08 01:58:35 +00002022 }
2023 }
2024 return true;
2025}
2026
Chris Lattnerb515f6d2003-05-08 20:49:25 +00002027/// visitSimpleBinary - Implement simple binary operators for integral types...
2028/// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or, 4 for
2029/// Xor.
Misha Brukman538607f2004-03-01 23:53:11 +00002030///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002031void X86ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +00002032 unsigned DestReg = getReg(B);
2033 MachineBasicBlock::iterator MI = BB->end();
Chris Lattner721d2d42004-03-08 01:18:36 +00002034 Value *Op0 = B.getOperand(0), *Op1 = B.getOperand(1);
Chris Lattnerc81e6ba2004-05-10 15:15:55 +00002035 unsigned Class = getClassB(B.getType());
Chris Lattner721d2d42004-03-08 01:18:36 +00002036
Chris Lattnerde95c9e2004-10-17 06:10:40 +00002037 // If this is AND X, C, and it is only used by a setcc instruction, it will
2038 // be folded. There is no need to emit this instruction.
2039 if (B.hasOneUse() && OperatorClass == 2 && isa<ConstantInt>(Op1))
2040 if (Class == cByte || Class == cShort || Class == cInt) {
2041 Instruction *Use = cast<Instruction>(B.use_back());
2042 if (isa<SetCondInst>(Use) &&
2043 Use->getOperand(1) == Constant::getNullValue(B.getType())) {
2044 switch (getSetCCNumber(Use->getOpcode())) {
2045 case 0:
2046 case 1:
2047 return;
2048 default:
2049 if (B.getType()->isSigned()) return;
2050 }
2051 }
2052 }
2053
Chris Lattner7dee5da2004-03-08 01:58:35 +00002054 // Special case: op Reg, load [mem]
Chris Lattnerc81e6ba2004-05-10 15:15:55 +00002055 if (isa<LoadInst>(Op0) && !isa<LoadInst>(Op1) && Class != cLong &&
Chris Lattnerccd97962004-06-17 22:15:25 +00002056 Op0->hasOneUse() &&
Chris Lattnerc81e6ba2004-05-10 15:15:55 +00002057 isSafeToFoldLoadIntoInstruction(*cast<LoadInst>(Op0), B))
Chris Lattner7dee5da2004-03-08 01:58:35 +00002058 if (!B.swapOperands())
2059 std::swap(Op0, Op1); // Make sure any loads are in the RHS.
2060
Chris Lattnerccd97962004-06-17 22:15:25 +00002061 if (isa<LoadInst>(Op1) && Class != cLong && Op1->hasOneUse() &&
Chris Lattner7dee5da2004-03-08 01:58:35 +00002062 isSafeToFoldLoadIntoInstruction(*cast<LoadInst>(Op1), B)) {
2063
Chris Lattner95157f72004-04-11 22:05:45 +00002064 unsigned Opcode;
2065 if (Class != cFP) {
2066 static const unsigned OpcodeTab[][3] = {
2067 // Arithmetic operators
2068 { X86::ADD8rm, X86::ADD16rm, X86::ADD32rm }, // ADD
2069 { X86::SUB8rm, X86::SUB16rm, X86::SUB32rm }, // SUB
2070
2071 // Bitwise operators
2072 { X86::AND8rm, X86::AND16rm, X86::AND32rm }, // AND
2073 { X86:: OR8rm, X86:: OR16rm, X86:: OR32rm }, // OR
2074 { X86::XOR8rm, X86::XOR16rm, X86::XOR32rm }, // XOR
2075 };
2076 Opcode = OpcodeTab[OperatorClass][Class];
2077 } else {
2078 static const unsigned OpcodeTab[][2] = {
2079 { X86::FADD32m, X86::FADD64m }, // ADD
2080 { X86::FSUB32m, X86::FSUB64m }, // SUB
2081 };
2082 const Type *Ty = Op0->getType();
2083 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
2084 Opcode = OpcodeTab[OperatorClass][Ty == Type::DoubleTy];
2085 }
Chris Lattner7dee5da2004-03-08 01:58:35 +00002086
Chris Lattner7dee5da2004-03-08 01:58:35 +00002087 unsigned Op0r = getReg(Op0);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002088 if (AllocaInst *AI =
2089 dyn_castFixedAlloca(cast<LoadInst>(Op1)->getOperand(0))) {
2090 unsigned FI = getFixedSizedAllocaFI(AI);
2091 addFrameReference(BuildMI(BB, Opcode, 5, DestReg).addReg(Op0r), FI);
2092
2093 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002094 X86AddressMode AM;
2095 getAddressingMode(cast<LoadInst>(Op1)->getOperand(0), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002096
Reid Spencerfc989e12004-08-30 00:13:26 +00002097 addFullAddress(BuildMI(BB, Opcode, 5, DestReg).addReg(Op0r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002098 }
Chris Lattner7dee5da2004-03-08 01:58:35 +00002099 return;
2100 }
2101
Chris Lattner95157f72004-04-11 22:05:45 +00002102 // If this is a floating point subtract, check to see if we can fold the first
2103 // operand in.
2104 if (Class == cFP && OperatorClass == 1 &&
2105 isa<LoadInst>(Op0) &&
2106 isSafeToFoldLoadIntoInstruction(*cast<LoadInst>(Op0), B)) {
2107 const Type *Ty = Op0->getType();
2108 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
2109 unsigned Opcode = Ty == Type::FloatTy ? X86::FSUBR32m : X86::FSUBR64m;
2110
Chris Lattner95157f72004-04-11 22:05:45 +00002111 unsigned Op1r = getReg(Op1);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002112 if (AllocaInst *AI =
2113 dyn_castFixedAlloca(cast<LoadInst>(Op0)->getOperand(0))) {
2114 unsigned FI = getFixedSizedAllocaFI(AI);
2115 addFrameReference(BuildMI(BB, Opcode, 5, DestReg).addReg(Op1r), FI);
2116 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002117 X86AddressMode AM;
2118 getAddressingMode(cast<LoadInst>(Op0)->getOperand(0), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002119
Reid Spencerfc989e12004-08-30 00:13:26 +00002120 addFullAddress(BuildMI(BB, Opcode, 5, DestReg).addReg(Op1r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002121 }
Chris Lattner95157f72004-04-11 22:05:45 +00002122 return;
2123 }
2124
Chris Lattner721d2d42004-03-08 01:18:36 +00002125 emitSimpleBinaryOperation(BB, MI, Op0, Op1, OperatorClass, DestReg);
Chris Lattnerb515f6d2003-05-08 20:49:25 +00002126}
Chris Lattner3e130a22003-01-13 00:32:26 +00002127
Chris Lattner6621ed92004-04-11 21:23:56 +00002128
2129/// emitBinaryFPOperation - This method handles emission of floating point
2130/// Add (0), Sub (1), Mul (2), and Div (3) operations.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002131void X86ISel::emitBinaryFPOperation(MachineBasicBlock *BB,
2132 MachineBasicBlock::iterator IP,
2133 Value *Op0, Value *Op1,
2134 unsigned OperatorClass, unsigned DestReg) {
Chris Lattner6621ed92004-04-11 21:23:56 +00002135 // Special case: op Reg, <const fp>
2136 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1))
2137 if (!Op1C->isExactlyValue(+0.0) && !Op1C->isExactlyValue(+1.0)) {
2138 // Create a constant pool entry for this constant.
2139 MachineConstantPool *CP = F->getConstantPool();
2140 unsigned CPI = CP->getConstantPoolIndex(Op1C);
2141 const Type *Ty = Op1->getType();
2142
2143 static const unsigned OpcodeTab[][4] = {
2144 { X86::FADD32m, X86::FSUB32m, X86::FMUL32m, X86::FDIV32m }, // Float
2145 { X86::FADD64m, X86::FSUB64m, X86::FMUL64m, X86::FDIV64m }, // Double
2146 };
2147
2148 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
2149 unsigned Opcode = OpcodeTab[Ty != Type::FloatTy][OperatorClass];
2150 unsigned Op0r = getReg(Op0, BB, IP);
2151 addConstantPoolReference(BuildMI(*BB, IP, Opcode, 5,
2152 DestReg).addReg(Op0r), CPI);
2153 return;
2154 }
2155
Chris Lattner13c07fe2004-04-12 00:12:04 +00002156 // Special case: R1 = op <const fp>, R2
Chris Lattner6621ed92004-04-11 21:23:56 +00002157 if (ConstantFP *CFP = dyn_cast<ConstantFP>(Op0))
2158 if (CFP->isExactlyValue(-0.0) && OperatorClass == 1) {
2159 // -0.0 - X === -X
2160 unsigned op1Reg = getReg(Op1, BB, IP);
2161 BuildMI(*BB, IP, X86::FCHS, 1, DestReg).addReg(op1Reg);
2162 return;
2163 } else if (!CFP->isExactlyValue(+0.0) && !CFP->isExactlyValue(+1.0)) {
Chris Lattner13c07fe2004-04-12 00:12:04 +00002164 // R1 = op CST, R2 --> R1 = opr R2, CST
Chris Lattner6621ed92004-04-11 21:23:56 +00002165
2166 // Create a constant pool entry for this constant.
2167 MachineConstantPool *CP = F->getConstantPool();
2168 unsigned CPI = CP->getConstantPoolIndex(CFP);
2169 const Type *Ty = CFP->getType();
2170
2171 static const unsigned OpcodeTab[][4] = {
2172 { X86::FADD32m, X86::FSUBR32m, X86::FMUL32m, X86::FDIVR32m }, // Float
2173 { X86::FADD64m, X86::FSUBR64m, X86::FMUL64m, X86::FDIVR64m }, // Double
2174 };
2175
2176 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2177 unsigned Opcode = OpcodeTab[Ty != Type::FloatTy][OperatorClass];
2178 unsigned Op1r = getReg(Op1, BB, IP);
2179 addConstantPoolReference(BuildMI(*BB, IP, Opcode, 5,
2180 DestReg).addReg(Op1r), CPI);
2181 return;
2182 }
2183
2184 // General case.
2185 static const unsigned OpcodeTab[4] = {
2186 X86::FpADD, X86::FpSUB, X86::FpMUL, X86::FpDIV
2187 };
2188
2189 unsigned Opcode = OpcodeTab[OperatorClass];
2190 unsigned Op0r = getReg(Op0, BB, IP);
2191 unsigned Op1r = getReg(Op1, BB, IP);
2192 BuildMI(*BB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r);
2193}
2194
Chris Lattnerb2acc512003-10-19 21:09:10 +00002195/// emitSimpleBinaryOperation - Implement simple binary operators for integral
2196/// types... OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for
2197/// Or, 4 for Xor.
Chris Lattner68aad932002-11-02 20:13:22 +00002198///
Chris Lattnerb515f6d2003-05-08 20:49:25 +00002199/// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
2200/// and constant expression support.
Chris Lattnerb2acc512003-10-19 21:09:10 +00002201///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002202void X86ISel::emitSimpleBinaryOperation(MachineBasicBlock *MBB,
2203 MachineBasicBlock::iterator IP,
2204 Value *Op0, Value *Op1,
2205 unsigned OperatorClass,
2206 unsigned DestReg) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +00002207 unsigned Class = getClassB(Op0->getType());
Chris Lattnerb2acc512003-10-19 21:09:10 +00002208
Chris Lattner6621ed92004-04-11 21:23:56 +00002209 if (Class == cFP) {
2210 assert(OperatorClass < 2 && "No logical ops for FP!");
2211 emitBinaryFPOperation(MBB, IP, Op0, Op1, OperatorClass, DestReg);
2212 return;
2213 }
2214
Chris Lattner48b0c972004-04-11 20:26:20 +00002215 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0))
Chris Lattner667ea022004-06-18 00:50:37 +00002216 if (OperatorClass == 1) {
Chris Lattner48b0c972004-04-11 20:26:20 +00002217 static unsigned const NEGTab[] = {
2218 X86::NEG8r, X86::NEG16r, X86::NEG32r, 0, X86::NEG32r
2219 };
Chris Lattner667ea022004-06-18 00:50:37 +00002220
2221 // sub 0, X -> neg X
2222 if (CI->isNullValue()) {
2223 unsigned op1Reg = getReg(Op1, MBB, IP);
2224 BuildMI(*MBB, IP, NEGTab[Class], 1, DestReg).addReg(op1Reg);
Chris Lattner48b0c972004-04-11 20:26:20 +00002225
Chris Lattner667ea022004-06-18 00:50:37 +00002226 if (Class == cLong) {
2227 // We just emitted: Dl = neg Sl
2228 // Now emit : T = addc Sh, 0
2229 // : Dh = neg T
2230 unsigned T = makeAnotherReg(Type::IntTy);
2231 BuildMI(*MBB, IP, X86::ADC32ri, 2, T).addReg(op1Reg+1).addImm(0);
2232 BuildMI(*MBB, IP, X86::NEG32r, 1, DestReg+1).addReg(T);
2233 }
2234 return;
2235 } else if (Op1->hasOneUse() && Class != cLong) {
2236 // sub C, X -> tmp = neg X; DestReg = add tmp, C. This is better
2237 // than copying C into a temporary register, because of register
2238 // pressure (tmp and destreg can share a register.
2239 static unsigned const ADDRITab[] = {
2240 X86::ADD8ri, X86::ADD16ri, X86::ADD32ri, 0, X86::ADD32ri
2241 };
2242 unsigned op1Reg = getReg(Op1, MBB, IP);
2243 unsigned Tmp = makeAnotherReg(Op0->getType());
2244 BuildMI(*MBB, IP, NEGTab[Class], 1, Tmp).addReg(op1Reg);
Chris Lattner30483732004-06-20 07:49:54 +00002245 BuildMI(*MBB, IP, ADDRITab[Class], 2,
2246 DestReg).addReg(Tmp).addImm(CI->getRawValue());
Chris Lattner667ea022004-06-18 00:50:37 +00002247 return;
Chris Lattnerb2acc512003-10-19 21:09:10 +00002248 }
Chris Lattner48b0c972004-04-11 20:26:20 +00002249 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002250
Chris Lattner48b0c972004-04-11 20:26:20 +00002251 // Special case: op Reg, <const int>
2252 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner721d2d42004-03-08 01:18:36 +00002253 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002254
Chris Lattner721d2d42004-03-08 01:18:36 +00002255 // xor X, -1 -> not X
2256 if (OperatorClass == 4 && Op1C->isAllOnesValue()) {
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002257 static unsigned const NOTTab[] = {
2258 X86::NOT8r, X86::NOT16r, X86::NOT32r, 0, X86::NOT32r
2259 };
Chris Lattner721d2d42004-03-08 01:18:36 +00002260 BuildMI(*MBB, IP, NOTTab[Class], 1, DestReg).addReg(Op0r);
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002261 if (Class == cLong) // Invert the top part too
2262 BuildMI(*MBB, IP, X86::NOT32r, 1, DestReg+1).addReg(Op0r+1);
Chris Lattner721d2d42004-03-08 01:18:36 +00002263 return;
2264 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002265
Chris Lattner721d2d42004-03-08 01:18:36 +00002266 // add X, -1 -> dec X
Chris Lattner06521672004-04-06 03:36:57 +00002267 if (OperatorClass == 0 && Op1C->isAllOnesValue() && Class != cLong) {
2268 // Note that we can't use dec for 64-bit decrements, because it does not
2269 // set the carry flag!
2270 static unsigned const DECTab[] = { X86::DEC8r, X86::DEC16r, X86::DEC32r };
Chris Lattner721d2d42004-03-08 01:18:36 +00002271 BuildMI(*MBB, IP, DECTab[Class], 1, DestReg).addReg(Op0r);
2272 return;
2273 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002274
Chris Lattner721d2d42004-03-08 01:18:36 +00002275 // add X, 1 -> inc X
Chris Lattner06521672004-04-06 03:36:57 +00002276 if (OperatorClass == 0 && Op1C->equalsInt(1) && Class != cLong) {
2277 // Note that we can't use inc for 64-bit increments, because it does not
2278 // set the carry flag!
2279 static unsigned const INCTab[] = { X86::INC8r, X86::INC16r, X86::INC32r };
Alkis Evlogimenosbee8a092004-04-02 18:11:32 +00002280 BuildMI(*MBB, IP, INCTab[Class], 1, DestReg).addReg(Op0r);
Chris Lattner721d2d42004-03-08 01:18:36 +00002281 return;
2282 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002283
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002284 static const unsigned OpcodeTab[][5] = {
Chris Lattner721d2d42004-03-08 01:18:36 +00002285 // Arithmetic operators
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002286 { X86::ADD8ri, X86::ADD16ri, X86::ADD32ri, 0, X86::ADD32ri }, // ADD
2287 { X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, X86::SUB32ri }, // SUB
Chris Lattnerb2acc512003-10-19 21:09:10 +00002288
Chris Lattner721d2d42004-03-08 01:18:36 +00002289 // Bitwise operators
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002290 { X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, X86::AND32ri }, // AND
2291 { X86:: OR8ri, X86:: OR16ri, X86:: OR32ri, 0, X86::OR32ri }, // OR
2292 { X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, X86::XOR32ri }, // XOR
Chris Lattner721d2d42004-03-08 01:18:36 +00002293 };
2294
Chris Lattner721d2d42004-03-08 01:18:36 +00002295 unsigned Opcode = OpcodeTab[OperatorClass][Class];
Chris Lattner33f7fa32004-04-06 03:15:53 +00002296 unsigned Op1l = cast<ConstantInt>(Op1C)->getRawValue();
Chris Lattner721d2d42004-03-08 01:18:36 +00002297
Chris Lattner33f7fa32004-04-06 03:15:53 +00002298 if (Class != cLong) {
2299 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addImm(Op1l);
2300 return;
Chris Lattner6621ed92004-04-11 21:23:56 +00002301 }
2302
2303 // If this is a long value and the high or low bits have a special
2304 // property, emit some special cases.
2305 unsigned Op1h = cast<ConstantInt>(Op1C)->getRawValue() >> 32LL;
2306
2307 // If the constant is zero in the low 32-bits, just copy the low part
2308 // across and apply the normal 32-bit operation to the high parts. There
2309 // will be no carry or borrow into the top.
2310 if (Op1l == 0) {
2311 if (OperatorClass != 2) // All but and...
2312 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg).addReg(Op0r);
2313 else
2314 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
2315 BuildMI(*MBB, IP, OpcodeTab[OperatorClass][cLong], 2, DestReg+1)
2316 .addReg(Op0r+1).addImm(Op1h);
Chris Lattner33f7fa32004-04-06 03:15:53 +00002317 return;
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002318 }
Chris Lattner6621ed92004-04-11 21:23:56 +00002319
2320 // If this is a logical operation and the top 32-bits are zero, just
2321 // operate on the lower 32.
2322 if (Op1h == 0 && OperatorClass > 1) {
2323 BuildMI(*MBB, IP, OpcodeTab[OperatorClass][cLong], 2, DestReg)
2324 .addReg(Op0r).addImm(Op1l);
2325 if (OperatorClass != 2) // All but and
2326 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg+1).addReg(Op0r+1);
2327 else
2328 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
2329 return;
2330 }
2331
2332 // TODO: We could handle lots of other special cases here, such as AND'ing
2333 // with 0xFFFFFFFF00000000 -> noop, etc.
2334
2335 // Otherwise, code generate the full operation with a constant.
2336 static const unsigned TopTab[] = {
2337 X86::ADC32ri, X86::SBB32ri, X86::AND32ri, X86::OR32ri, X86::XOR32ri
2338 };
2339
2340 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addImm(Op1l);
2341 BuildMI(*MBB, IP, TopTab[OperatorClass], 2, DestReg+1)
2342 .addReg(Op0r+1).addImm(Op1h);
2343 return;
Chris Lattner721d2d42004-03-08 01:18:36 +00002344 }
2345
2346 // Finally, handle the general case now.
Chris Lattner7ba92302004-04-06 02:13:25 +00002347 static const unsigned OpcodeTab[][5] = {
Chris Lattner721d2d42004-03-08 01:18:36 +00002348 // Arithmetic operators
Chris Lattner6621ed92004-04-11 21:23:56 +00002349 { X86::ADD8rr, X86::ADD16rr, X86::ADD32rr, 0, X86::ADD32rr }, // ADD
2350 { X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, 0, X86::SUB32rr }, // SUB
Chris Lattner721d2d42004-03-08 01:18:36 +00002351
Chris Lattnerb2acc512003-10-19 21:09:10 +00002352 // Bitwise operators
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002353 { X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, X86::AND32rr }, // AND
2354 { X86:: OR8rr, X86:: OR16rr, X86:: OR32rr, 0, X86:: OR32rr }, // OR
2355 { X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, X86::XOR32rr }, // XOR
Chris Lattnerb2acc512003-10-19 21:09:10 +00002356 };
Chris Lattner721d2d42004-03-08 01:18:36 +00002357
Chris Lattnerb2acc512003-10-19 21:09:10 +00002358 unsigned Opcode = OpcodeTab[OperatorClass][Class];
Chris Lattner721d2d42004-03-08 01:18:36 +00002359 unsigned Op0r = getReg(Op0, MBB, IP);
2360 unsigned Op1r = getReg(Op1, MBB, IP);
2361 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r);
2362
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002363 if (Class == cLong) { // Handle the upper 32 bits of long values...
Chris Lattner721d2d42004-03-08 01:18:36 +00002364 static const unsigned TopTab[] = {
2365 X86::ADC32rr, X86::SBB32rr, X86::AND32rr, X86::OR32rr, X86::XOR32rr
2366 };
2367 BuildMI(*MBB, IP, TopTab[OperatorClass], 2,
2368 DestReg+1).addReg(Op0r+1).addReg(Op1r+1);
2369 }
Chris Lattnere2954c82002-11-02 20:04:26 +00002370}
2371
Chris Lattner3e130a22003-01-13 00:32:26 +00002372/// doMultiply - Emit appropriate instructions to multiply together the
2373/// registers op0Reg and op1Reg, and put the result in DestReg. The type of the
2374/// result should be given as DestTy.
2375///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002376void X86ISel::doMultiply(MachineBasicBlock *MBB,
2377 MachineBasicBlock::iterator MBBI,
2378 unsigned DestReg, const Type *DestTy,
2379 unsigned op0Reg, unsigned op1Reg) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002380 unsigned Class = getClass(DestTy);
Chris Lattner94af4142002-12-25 05:13:53 +00002381 switch (Class) {
Chris Lattner0f1c4612003-06-21 17:16:58 +00002382 case cInt:
2383 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002384 BuildMI(*MBB, MBBI, Class == cInt ? X86::IMUL32rr:X86::IMUL16rr, 2, DestReg)
Chris Lattner0f1c4612003-06-21 17:16:58 +00002385 .addReg(op0Reg).addReg(op1Reg);
2386 return;
2387 case cByte:
2388 // Must use the MUL instruction, which forces use of AL...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002389 BuildMI(*MBB, MBBI, X86::MOV8rr, 1, X86::AL).addReg(op0Reg);
2390 BuildMI(*MBB, MBBI, X86::MUL8r, 1).addReg(op1Reg);
2391 BuildMI(*MBB, MBBI, X86::MOV8rr, 1, DestReg).addReg(X86::AL);
Chris Lattner0f1c4612003-06-21 17:16:58 +00002392 return;
Chris Lattner94af4142002-12-25 05:13:53 +00002393 default:
Chris Lattner3e130a22003-01-13 00:32:26 +00002394 case cLong: assert(0 && "doMultiply cannot operate on LONG values!");
Chris Lattner94af4142002-12-25 05:13:53 +00002395 }
Brian Gaeke20244b72002-12-12 15:33:40 +00002396}
2397
Chris Lattnerb2acc512003-10-19 21:09:10 +00002398// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
2399// returns zero when the input is not exactly a power of two.
2400static unsigned ExactLog2(unsigned Val) {
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002401 if (Val == 0 || (Val & (Val-1))) return 0;
Chris Lattnerb2acc512003-10-19 21:09:10 +00002402 unsigned Count = 0;
2403 while (Val != 1) {
Chris Lattnerb2acc512003-10-19 21:09:10 +00002404 Val >>= 1;
2405 ++Count;
2406 }
2407 return Count+1;
2408}
2409
Chris Lattner462fa822004-04-11 20:56:28 +00002410
2411/// doMultiplyConst - This function is specialized to efficiently codegen an 8,
2412/// 16, or 32-bit integer multiply by a constant.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002413void X86ISel::doMultiplyConst(MachineBasicBlock *MBB,
2414 MachineBasicBlock::iterator IP,
2415 unsigned DestReg, const Type *DestTy,
2416 unsigned op0Reg, unsigned ConstRHS) {
Chris Lattner6ab06d52004-04-06 04:55:43 +00002417 static const unsigned MOVrrTab[] = {X86::MOV8rr, X86::MOV16rr, X86::MOV32rr};
2418 static const unsigned MOVriTab[] = {X86::MOV8ri, X86::MOV16ri, X86::MOV32ri};
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002419 static const unsigned ADDrrTab[] = {X86::ADD8rr, X86::ADD16rr, X86::ADD32rr};
Chris Lattner596b97f2004-07-19 23:47:21 +00002420 static const unsigned NEGrTab[] = {X86::NEG8r , X86::NEG16r , X86::NEG32r };
Chris Lattner6ab06d52004-04-06 04:55:43 +00002421
Chris Lattnerb2acc512003-10-19 21:09:10 +00002422 unsigned Class = getClass(DestTy);
Chris Lattner596b97f2004-07-19 23:47:21 +00002423 unsigned TmpReg;
Chris Lattnerb2acc512003-10-19 21:09:10 +00002424
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002425 // Handle special cases here.
2426 switch (ConstRHS) {
Chris Lattner596b97f2004-07-19 23:47:21 +00002427 case -2:
2428 TmpReg = makeAnotherReg(DestTy);
2429 BuildMI(*MBB, IP, NEGrTab[Class], 1, TmpReg).addReg(op0Reg);
2430 BuildMI(*MBB, IP, ADDrrTab[Class], 1,DestReg).addReg(TmpReg).addReg(TmpReg);
2431 return;
2432 case -1:
2433 BuildMI(*MBB, IP, NEGrTab[Class], 1, DestReg).addReg(op0Reg);
2434 return;
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002435 case 0:
Chris Lattner6ab06d52004-04-06 04:55:43 +00002436 BuildMI(*MBB, IP, MOVriTab[Class], 1, DestReg).addImm(0);
2437 return;
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002438 case 1:
Chris Lattner6ab06d52004-04-06 04:55:43 +00002439 BuildMI(*MBB, IP, MOVrrTab[Class], 1, DestReg).addReg(op0Reg);
2440 return;
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002441 case 2:
2442 BuildMI(*MBB, IP, ADDrrTab[Class], 1,DestReg).addReg(op0Reg).addReg(op0Reg);
2443 return;
2444 case 3:
2445 case 5:
2446 case 9:
2447 if (Class == cInt) {
Reid Spencerfc989e12004-08-30 00:13:26 +00002448 X86AddressMode AM;
2449 AM.BaseType = X86AddressMode::RegBase;
2450 AM.Base.Reg = op0Reg;
2451 AM.Scale = ConstRHS-1;
2452 AM.IndexReg = op0Reg;
2453 AM.Disp = 0;
2454 addFullAddress(BuildMI(*MBB, IP, X86::LEA32r, 5, DestReg), AM);
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002455 return;
2456 }
Chris Lattner596b97f2004-07-19 23:47:21 +00002457 case -3:
2458 case -5:
2459 case -9:
2460 if (Class == cInt) {
2461 TmpReg = makeAnotherReg(DestTy);
Reid Spencerfc989e12004-08-30 00:13:26 +00002462 X86AddressMode AM;
2463 AM.BaseType = X86AddressMode::RegBase;
2464 AM.Base.Reg = op0Reg;
2465 AM.Scale = -ConstRHS-1;
2466 AM.IndexReg = op0Reg;
2467 AM.Disp = 0;
2468 addFullAddress(BuildMI(*MBB, IP, X86::LEA32r, 5, TmpReg), AM);
Chris Lattner596b97f2004-07-19 23:47:21 +00002469 BuildMI(*MBB, IP, NEGrTab[Class], 1, DestReg).addReg(TmpReg);
2470 return;
2471 }
Chris Lattner6ab06d52004-04-06 04:55:43 +00002472 }
2473
Chris Lattnerb2acc512003-10-19 21:09:10 +00002474 // If the element size is exactly a power of 2, use a shift to get it.
2475 if (unsigned Shift = ExactLog2(ConstRHS)) {
2476 switch (Class) {
2477 default: assert(0 && "Unknown class for this function!");
2478 case cByte:
Chris Lattner596b97f2004-07-19 23:47:21 +00002479 BuildMI(*MBB, IP, X86::SHL8ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002480 return;
2481 case cShort:
Chris Lattner596b97f2004-07-19 23:47:21 +00002482 BuildMI(*MBB, IP, X86::SHL16ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002483 return;
2484 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002485 BuildMI(*MBB, IP, X86::SHL32ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002486 return;
2487 }
2488 }
Chris Lattner596b97f2004-07-19 23:47:21 +00002489
2490 // If the element size is a negative power of 2, use a shift/neg to get it.
2491 if (unsigned Shift = ExactLog2(-ConstRHS)) {
2492 TmpReg = makeAnotherReg(DestTy);
2493 BuildMI(*MBB, IP, NEGrTab[Class], 1, TmpReg).addReg(op0Reg);
2494 switch (Class) {
2495 default: assert(0 && "Unknown class for this function!");
2496 case cByte:
2497 BuildMI(*MBB, IP, X86::SHL8ri,2, DestReg).addReg(TmpReg).addImm(Shift-1);
2498 return;
2499 case cShort:
2500 BuildMI(*MBB, IP, X86::SHL16ri,2, DestReg).addReg(TmpReg).addImm(Shift-1);
2501 return;
2502 case cInt:
2503 BuildMI(*MBB, IP, X86::SHL32ri,2, DestReg).addReg(TmpReg).addImm(Shift-1);
2504 return;
2505 }
2506 }
Chris Lattnerc01d1232003-10-20 03:42:58 +00002507
2508 if (Class == cShort) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002509 BuildMI(*MBB, IP, X86::IMUL16rri,2,DestReg).addReg(op0Reg).addImm(ConstRHS);
Chris Lattnerc01d1232003-10-20 03:42:58 +00002510 return;
2511 } else if (Class == cInt) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002512 BuildMI(*MBB, IP, X86::IMUL32rri,2,DestReg).addReg(op0Reg).addImm(ConstRHS);
Chris Lattnerc01d1232003-10-20 03:42:58 +00002513 return;
2514 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002515
2516 // Most general case, emit a normal multiply...
Chris Lattner596b97f2004-07-19 23:47:21 +00002517 TmpReg = makeAnotherReg(DestTy);
Chris Lattneree352852004-02-29 07:22:16 +00002518 BuildMI(*MBB, IP, MOVriTab[Class], 1, TmpReg).addImm(ConstRHS);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002519
2520 // Emit a MUL to multiply the register holding the index by
2521 // elementSize, putting the result in OffsetReg.
2522 doMultiply(MBB, IP, DestReg, DestTy, op0Reg, TmpReg);
2523}
2524
Chris Lattnerca9671d2002-11-02 20:28:58 +00002525/// visitMul - Multiplies are not simple binary operators because they must deal
2526/// with the EAX register explicitly.
2527///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002528void X86ISel::visitMul(BinaryOperator &I) {
Chris Lattner462fa822004-04-11 20:56:28 +00002529 unsigned ResultReg = getReg(I);
2530
Chris Lattner95157f72004-04-11 22:05:45 +00002531 Value *Op0 = I.getOperand(0);
2532 Value *Op1 = I.getOperand(1);
2533
2534 // Fold loads into floating point multiplies.
2535 if (getClass(Op0->getType()) == cFP) {
2536 if (isa<LoadInst>(Op0) && !isa<LoadInst>(Op1))
2537 if (!I.swapOperands())
2538 std::swap(Op0, Op1); // Make sure any loads are in the RHS.
2539 if (LoadInst *LI = dyn_cast<LoadInst>(Op1))
2540 if (isSafeToFoldLoadIntoInstruction(*LI, I)) {
2541 const Type *Ty = Op0->getType();
2542 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2543 unsigned Opcode = Ty == Type::FloatTy ? X86::FMUL32m : X86::FMUL64m;
2544
Chris Lattner95157f72004-04-11 22:05:45 +00002545 unsigned Op0r = getReg(Op0);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002546 if (AllocaInst *AI = dyn_castFixedAlloca(LI->getOperand(0))) {
2547 unsigned FI = getFixedSizedAllocaFI(AI);
2548 addFrameReference(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op0r), FI);
2549 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002550 X86AddressMode AM;
2551 getAddressingMode(LI->getOperand(0), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002552
Reid Spencerfc989e12004-08-30 00:13:26 +00002553 addFullAddress(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op0r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002554 }
Chris Lattner95157f72004-04-11 22:05:45 +00002555 return;
2556 }
2557 }
2558
Chris Lattner462fa822004-04-11 20:56:28 +00002559 MachineBasicBlock::iterator IP = BB->end();
Chris Lattner95157f72004-04-11 22:05:45 +00002560 emitMultiply(BB, IP, Op0, Op1, ResultReg);
Chris Lattner462fa822004-04-11 20:56:28 +00002561}
2562
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002563void X86ISel::emitMultiply(MachineBasicBlock *MBB,
2564 MachineBasicBlock::iterator IP,
2565 Value *Op0, Value *Op1, unsigned DestReg) {
Chris Lattner462fa822004-04-11 20:56:28 +00002566 MachineBasicBlock &BB = *MBB;
2567 TypeClass Class = getClass(Op0->getType());
Chris Lattner3e130a22003-01-13 00:32:26 +00002568
2569 // Simple scalar multiply?
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002570 unsigned Op0Reg = getReg(Op0, &BB, IP);
Chris Lattner462fa822004-04-11 20:56:28 +00002571 switch (Class) {
2572 case cByte:
2573 case cShort:
2574 case cInt:
2575 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner462fa822004-04-11 20:56:28 +00002576 unsigned Val = (unsigned)CI->getRawValue(); // Isn't a 64-bit constant
2577 doMultiplyConst(&BB, IP, DestReg, Op0->getType(), Op0Reg, Val);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002578 } else {
Chris Lattner462fa822004-04-11 20:56:28 +00002579 unsigned Op1Reg = getReg(Op1, &BB, IP);
2580 doMultiply(&BB, IP, DestReg, Op1->getType(), Op0Reg, Op1Reg);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002581 }
Chris Lattner462fa822004-04-11 20:56:28 +00002582 return;
2583 case cFP:
Chris Lattner6621ed92004-04-11 21:23:56 +00002584 emitBinaryFPOperation(MBB, IP, Op0, Op1, 2, DestReg);
2585 return;
Chris Lattner462fa822004-04-11 20:56:28 +00002586 case cLong:
2587 break;
2588 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002589
Chris Lattner462fa822004-04-11 20:56:28 +00002590 // Long value. We have to do things the hard way...
2591 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
2592 unsigned CLow = CI->getRawValue();
2593 unsigned CHi = CI->getRawValue() >> 32;
2594
2595 if (CLow == 0) {
2596 // If the low part of the constant is all zeros, things are simple.
2597 BuildMI(BB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
2598 doMultiplyConst(&BB, IP, DestReg+1, Type::UIntTy, Op0Reg, CHi);
2599 return;
2600 }
2601
2602 // Multiply the two low parts... capturing carry into EDX
2603 unsigned OverflowReg = 0;
2604 if (CLow == 1) {
2605 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg).addReg(Op0Reg);
Chris Lattner028adc42004-04-06 04:29:36 +00002606 } else {
Chris Lattner462fa822004-04-11 20:56:28 +00002607 unsigned Op1RegL = makeAnotherReg(Type::UIntTy);
2608 OverflowReg = makeAnotherReg(Type::UIntTy);
2609 BuildMI(BB, IP, X86::MOV32ri, 1, Op1RegL).addImm(CLow);
2610 BuildMI(BB, IP, X86::MOV32rr, 1, X86::EAX).addReg(Op0Reg);
2611 BuildMI(BB, IP, X86::MUL32r, 1).addReg(Op1RegL); // AL*BL
Chris Lattner028adc42004-04-06 04:29:36 +00002612
Chris Lattner462fa822004-04-11 20:56:28 +00002613 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg).addReg(X86::EAX); // AL*BL
2614 BuildMI(BB, IP, X86::MOV32rr, 1,
2615 OverflowReg).addReg(X86::EDX); // AL*BL >> 32
2616 }
2617
2618 unsigned AHBLReg = makeAnotherReg(Type::UIntTy); // AH*BL
2619 doMultiplyConst(&BB, IP, AHBLReg, Type::UIntTy, Op0Reg+1, CLow);
2620
2621 unsigned AHBLplusOverflowReg;
2622 if (OverflowReg) {
2623 AHBLplusOverflowReg = makeAnotherReg(Type::UIntTy);
2624 BuildMI(BB, IP, X86::ADD32rr, 2, // AH*BL+(AL*BL >> 32)
Chris Lattner028adc42004-04-06 04:29:36 +00002625 AHBLplusOverflowReg).addReg(AHBLReg).addReg(OverflowReg);
Chris Lattner462fa822004-04-11 20:56:28 +00002626 } else {
2627 AHBLplusOverflowReg = AHBLReg;
2628 }
2629
2630 if (CHi == 0) {
2631 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg+1).addReg(AHBLplusOverflowReg);
2632 } else {
Chris Lattner028adc42004-04-06 04:29:36 +00002633 unsigned ALBHReg = makeAnotherReg(Type::UIntTy); // AL*BH
Chris Lattner462fa822004-04-11 20:56:28 +00002634 doMultiplyConst(&BB, IP, ALBHReg, Type::UIntTy, Op0Reg, CHi);
Chris Lattner028adc42004-04-06 04:29:36 +00002635
Chris Lattner462fa822004-04-11 20:56:28 +00002636 BuildMI(BB, IP, X86::ADD32rr, 2, // AL*BH + AH*BL + (AL*BL >> 32)
Chris Lattner028adc42004-04-06 04:29:36 +00002637 DestReg+1).addReg(AHBLplusOverflowReg).addReg(ALBHReg);
2638 }
Chris Lattner462fa822004-04-11 20:56:28 +00002639 return;
Chris Lattner3e130a22003-01-13 00:32:26 +00002640 }
Chris Lattner462fa822004-04-11 20:56:28 +00002641
2642 // General 64x64 multiply
2643
2644 unsigned Op1Reg = getReg(Op1, &BB, IP);
2645 // Multiply the two low parts... capturing carry into EDX
2646 BuildMI(BB, IP, X86::MOV32rr, 1, X86::EAX).addReg(Op0Reg);
2647 BuildMI(BB, IP, X86::MUL32r, 1).addReg(Op1Reg); // AL*BL
2648
2649 unsigned OverflowReg = makeAnotherReg(Type::UIntTy);
2650 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg).addReg(X86::EAX); // AL*BL
2651 BuildMI(BB, IP, X86::MOV32rr, 1,
2652 OverflowReg).addReg(X86::EDX); // AL*BL >> 32
2653
2654 unsigned AHBLReg = makeAnotherReg(Type::UIntTy); // AH*BL
2655 BuildMI(BB, IP, X86::IMUL32rr, 2,
2656 AHBLReg).addReg(Op0Reg+1).addReg(Op1Reg);
2657
2658 unsigned AHBLplusOverflowReg = makeAnotherReg(Type::UIntTy);
2659 BuildMI(BB, IP, X86::ADD32rr, 2, // AH*BL+(AL*BL >> 32)
2660 AHBLplusOverflowReg).addReg(AHBLReg).addReg(OverflowReg);
2661
2662 unsigned ALBHReg = makeAnotherReg(Type::UIntTy); // AL*BH
2663 BuildMI(BB, IP, X86::IMUL32rr, 2,
2664 ALBHReg).addReg(Op0Reg).addReg(Op1Reg+1);
2665
2666 BuildMI(BB, IP, X86::ADD32rr, 2, // AL*BH + AH*BL + (AL*BL >> 32)
2667 DestReg+1).addReg(AHBLplusOverflowReg).addReg(ALBHReg);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002668}
Chris Lattnerca9671d2002-11-02 20:28:58 +00002669
Chris Lattner06925362002-11-17 21:56:38 +00002670
Chris Lattnerf01729e2002-11-02 20:54:46 +00002671/// visitDivRem - Handle division and remainder instructions... these
2672/// instruction both require the same instructions to be generated, they just
2673/// select the result from a different register. Note that both of these
2674/// instructions work differently for signed and unsigned operands.
2675///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002676void X86ISel::visitDivRem(BinaryOperator &I) {
Chris Lattnercadff442003-10-23 17:21:43 +00002677 unsigned ResultReg = getReg(I);
Chris Lattner95157f72004-04-11 22:05:45 +00002678 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2679
2680 // Fold loads into floating point divides.
2681 if (getClass(Op0->getType()) == cFP) {
2682 if (LoadInst *LI = dyn_cast<LoadInst>(Op1))
2683 if (isSafeToFoldLoadIntoInstruction(*LI, I)) {
2684 const Type *Ty = Op0->getType();
2685 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2686 unsigned Opcode = Ty == Type::FloatTy ? X86::FDIV32m : X86::FDIV64m;
2687
Chris Lattner95157f72004-04-11 22:05:45 +00002688 unsigned Op0r = getReg(Op0);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002689 if (AllocaInst *AI = dyn_castFixedAlloca(LI->getOperand(0))) {
2690 unsigned FI = getFixedSizedAllocaFI(AI);
2691 addFrameReference(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op0r), FI);
2692 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002693 X86AddressMode AM;
2694 getAddressingMode(LI->getOperand(0), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002695
Reid Spencerfc989e12004-08-30 00:13:26 +00002696 addFullAddress(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op0r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002697 }
Chris Lattner95157f72004-04-11 22:05:45 +00002698 return;
2699 }
2700
2701 if (LoadInst *LI = dyn_cast<LoadInst>(Op0))
2702 if (isSafeToFoldLoadIntoInstruction(*LI, I)) {
2703 const Type *Ty = Op0->getType();
2704 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2705 unsigned Opcode = Ty == Type::FloatTy ? X86::FDIVR32m : X86::FDIVR64m;
2706
Chris Lattner95157f72004-04-11 22:05:45 +00002707 unsigned Op1r = getReg(Op1);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002708 if (AllocaInst *AI = dyn_castFixedAlloca(LI->getOperand(0))) {
2709 unsigned FI = getFixedSizedAllocaFI(AI);
2710 addFrameReference(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op1r), FI);
2711 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002712 X86AddressMode AM;
2713 getAddressingMode(LI->getOperand(0), AM);
2714 addFullAddress(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op1r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002715 }
Chris Lattner95157f72004-04-11 22:05:45 +00002716 return;
2717 }
2718 }
2719
Chris Lattner94af4142002-12-25 05:13:53 +00002720
Chris Lattnercadff442003-10-23 17:21:43 +00002721 MachineBasicBlock::iterator IP = BB->end();
Chris Lattner95157f72004-04-11 22:05:45 +00002722 emitDivRemOperation(BB, IP, Op0, Op1,
Chris Lattner462fa822004-04-11 20:56:28 +00002723 I.getOpcode() == Instruction::Div, ResultReg);
Chris Lattnercadff442003-10-23 17:21:43 +00002724}
2725
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002726void X86ISel::emitDivRemOperation(MachineBasicBlock *BB,
2727 MachineBasicBlock::iterator IP,
2728 Value *Op0, Value *Op1, bool isDiv,
2729 unsigned ResultReg) {
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002730 const Type *Ty = Op0->getType();
2731 unsigned Class = getClass(Ty);
Chris Lattner94af4142002-12-25 05:13:53 +00002732 switch (Class) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002733 case cFP: // Floating point divide
Chris Lattnercadff442003-10-23 17:21:43 +00002734 if (isDiv) {
Chris Lattner6621ed92004-04-11 21:23:56 +00002735 emitBinaryFPOperation(BB, IP, Op0, Op1, 3, ResultReg);
2736 return;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00002737 } else { // Floating point remainder...
Chris Lattner462fa822004-04-11 20:56:28 +00002738 unsigned Op0Reg = getReg(Op0, BB, IP);
2739 unsigned Op1Reg = getReg(Op1, BB, IP);
Chris Lattner3e130a22003-01-13 00:32:26 +00002740 MachineInstr *TheCall =
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002741 BuildMI(X86::CALLpcrel32, 1).addExternalSymbol("fmod", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00002742 std::vector<ValueRecord> Args;
Chris Lattnercadff442003-10-23 17:21:43 +00002743 Args.push_back(ValueRecord(Op0Reg, Type::DoubleTy));
2744 Args.push_back(ValueRecord(Op1Reg, Type::DoubleTy));
Chris Lattner3e130a22003-01-13 00:32:26 +00002745 doCall(ValueRecord(ResultReg, Type::DoubleTy), TheCall, Args);
2746 }
Chris Lattner94af4142002-12-25 05:13:53 +00002747 return;
Chris Lattner3e130a22003-01-13 00:32:26 +00002748 case cLong: {
2749 static const char *FnName[] =
2750 { "__moddi3", "__divdi3", "__umoddi3", "__udivdi3" };
Chris Lattner462fa822004-04-11 20:56:28 +00002751 unsigned Op0Reg = getReg(Op0, BB, IP);
2752 unsigned Op1Reg = getReg(Op1, BB, IP);
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002753 unsigned NameIdx = Ty->isUnsigned()*2 + isDiv;
Chris Lattner3e130a22003-01-13 00:32:26 +00002754 MachineInstr *TheCall =
2755 BuildMI(X86::CALLpcrel32, 1).addExternalSymbol(FnName[NameIdx], true);
2756
2757 std::vector<ValueRecord> Args;
Chris Lattnercadff442003-10-23 17:21:43 +00002758 Args.push_back(ValueRecord(Op0Reg, Type::LongTy));
2759 Args.push_back(ValueRecord(Op1Reg, Type::LongTy));
Chris Lattner3e130a22003-01-13 00:32:26 +00002760 doCall(ValueRecord(ResultReg, Type::LongTy), TheCall, Args);
2761 return;
2762 }
2763 case cByte: case cShort: case cInt:
Misha Brukmancf00c4a2003-10-10 17:57:28 +00002764 break; // Small integrals, handled below...
Chris Lattner3e130a22003-01-13 00:32:26 +00002765 default: assert(0 && "Unknown class!");
Chris Lattner94af4142002-12-25 05:13:53 +00002766 }
Chris Lattnerf01729e2002-11-02 20:54:46 +00002767
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002768 static const unsigned MovOpcode[]={ X86::MOV8rr, X86::MOV16rr, X86::MOV32rr };
Chris Lattner2483f672004-10-06 05:01:07 +00002769 static const unsigned NEGOpcode[]={ X86::NEG8r, X86::NEG16r, X86::NEG32r };
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002770 static const unsigned SAROpcode[]={ X86::SAR8ri, X86::SAR16ri, X86::SAR32ri };
2771 static const unsigned SHROpcode[]={ X86::SHR8ri, X86::SHR16ri, X86::SHR32ri };
2772 static const unsigned ADDOpcode[]={ X86::ADD8rr, X86::ADD16rr, X86::ADD32rr };
2773
2774 // Special case signed division by power of 2.
Chris Lattner2483f672004-10-06 05:01:07 +00002775 if (ConstantSInt *CI = dyn_cast<ConstantSInt>(Op1))
2776 if (isDiv) {
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002777 assert(Class != cLong && "This doesn't handle 64-bit divides!");
2778 int V = CI->getValue();
2779
2780 if (V == 1) { // X /s 1 => X
2781 unsigned Op0Reg = getReg(Op0, BB, IP);
2782 BuildMI(*BB, IP, MovOpcode[Class], 1, ResultReg).addReg(Op0Reg);
2783 return;
2784 }
2785
2786 if (V == -1) { // X /s -1 => -X
2787 unsigned Op0Reg = getReg(Op0, BB, IP);
2788 BuildMI(*BB, IP, NEGOpcode[Class], 1, ResultReg).addReg(Op0Reg);
2789 return;
2790 }
2791
Chris Lattner610f1e22004-10-06 04:02:39 +00002792 if (V == 2 || V == -2) { // X /s 2
2793 static const unsigned CMPOpcode[] = {
2794 X86::CMP8ri, X86::CMP16ri, X86::CMP32ri
2795 };
2796 static const unsigned SBBOpcode[] = {
2797 X86::SBB8ri, X86::SBB16ri, X86::SBB32ri
2798 };
2799 unsigned Op0Reg = getReg(Op0, BB, IP);
2800 unsigned SignBit = 1 << (CI->getType()->getPrimitiveSize()*8-1);
2801 BuildMI(*BB, IP, CMPOpcode[Class], 2).addReg(Op0Reg).addImm(SignBit);
2802
2803 unsigned TmpReg = makeAnotherReg(Op0->getType());
2804 BuildMI(*BB, IP, SBBOpcode[Class], 2, TmpReg).addReg(Op0Reg).addImm(-1);
2805
2806 unsigned TmpReg2 = V == 2 ? ResultReg : makeAnotherReg(Op0->getType());
2807 BuildMI(*BB, IP, SAROpcode[Class], 2, TmpReg2).addReg(TmpReg).addImm(1);
2808 if (V == -2) {
2809 BuildMI(*BB, IP, NEGOpcode[Class], 1, ResultReg).addReg(TmpReg2);
2810 }
2811 return;
2812 }
2813
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002814 bool isNeg = false;
2815 if (V < 0) { // Not a positive power of 2?
2816 V = -V;
2817 isNeg = true; // Maybe it's a negative power of 2.
2818 }
2819 if (unsigned Log = ExactLog2(V)) {
2820 --Log;
2821 unsigned Op0Reg = getReg(Op0, BB, IP);
2822 unsigned TmpReg = makeAnotherReg(Op0->getType());
Chris Lattner3ffdff62004-10-06 04:19:43 +00002823 BuildMI(*BB, IP, SAROpcode[Class], 2, TmpReg)
2824 .addReg(Op0Reg).addImm(Log-1);
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002825 unsigned TmpReg2 = makeAnotherReg(Op0->getType());
2826 BuildMI(*BB, IP, SHROpcode[Class], 2, TmpReg2)
2827 .addReg(TmpReg).addImm(32-Log);
2828 unsigned TmpReg3 = makeAnotherReg(Op0->getType());
2829 BuildMI(*BB, IP, ADDOpcode[Class], 2, TmpReg3)
2830 .addReg(Op0Reg).addReg(TmpReg2);
2831
2832 unsigned TmpReg4 = isNeg ? makeAnotherReg(Op0->getType()) : ResultReg;
2833 BuildMI(*BB, IP, SAROpcode[Class], 2, TmpReg4)
Chris Lattner3ffdff62004-10-06 04:19:43 +00002834 .addReg(TmpReg3).addImm(Log);
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002835 if (isNeg)
2836 BuildMI(*BB, IP, NEGOpcode[Class], 1, ResultReg).addReg(TmpReg4);
2837 return;
2838 }
Chris Lattner2483f672004-10-06 05:01:07 +00002839 } else { // X % C
2840 assert(Class != cLong && "This doesn't handle 64-bit remainder!");
2841 int V = CI->getValue();
2842
2843 if (V == 2 || V == -2) { // X % 2, X % -2
Chris Lattner2483f672004-10-06 05:01:07 +00002844 static const unsigned SExtOpcode[] = { X86::CBW, X86::CWD, X86::CDQ };
2845 static const unsigned BaseReg[] = { X86::AL , X86::AX , X86::EAX };
2846 static const unsigned SExtReg[] = { X86::AH , X86::DX , X86::EDX };
2847 static const unsigned ANDOpcode[] = {
2848 X86::AND8ri, X86::AND16ri, X86::AND32ri
2849 };
2850 static const unsigned XOROpcode[] = {
2851 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr
2852 };
2853 static const unsigned SUBOpcode[] = {
2854 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr
2855 };
2856
2857 // Sign extend result into reg of -1 or 0.
2858 unsigned Op0Reg = getReg(Op0, BB, IP);
2859 BuildMI(*BB, IP, MovOpcode[Class], 1, BaseReg[Class]).addReg(Op0Reg);
2860 BuildMI(*BB, IP, SExtOpcode[Class], 0);
2861 unsigned TmpReg0 = makeAnotherReg(Op0->getType());
2862 BuildMI(*BB, IP, MovOpcode[Class], 1, TmpReg0).addReg(SExtReg[Class]);
2863
2864 unsigned TmpReg1 = makeAnotherReg(Op0->getType());
2865 BuildMI(*BB, IP, ANDOpcode[Class], 2, TmpReg1).addReg(Op0Reg).addImm(1);
2866
2867 unsigned TmpReg2 = makeAnotherReg(Op0->getType());
2868 BuildMI(*BB, IP, XOROpcode[Class], 2,
2869 TmpReg2).addReg(TmpReg1).addReg(TmpReg0);
2870 BuildMI(*BB, IP, SUBOpcode[Class], 2,
2871 ResultReg).addReg(TmpReg2).addReg(TmpReg0);
2872 return;
2873 }
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002874 }
2875
2876 static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX };
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002877 static const unsigned ClrOpcode[]={ X86::MOV8ri, X86::MOV16ri, X86::MOV32ri };
Chris Lattnerf01729e2002-11-02 20:54:46 +00002878 static const unsigned ExtRegs[] ={ X86::AH , X86::DX , X86::EDX };
2879
2880 static const unsigned DivOpcode[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002881 { X86::DIV8r , X86::DIV16r , X86::DIV32r , 0 }, // Unsigned division
2882 { X86::IDIV8r, X86::IDIV16r, X86::IDIV32r, 0 }, // Signed division
Chris Lattnerf01729e2002-11-02 20:54:46 +00002883 };
2884
Chris Lattnerf01729e2002-11-02 20:54:46 +00002885 unsigned Reg = Regs[Class];
2886 unsigned ExtReg = ExtRegs[Class];
Chris Lattnerf01729e2002-11-02 20:54:46 +00002887
2888 // Put the first operand into one of the A registers...
Chris Lattner462fa822004-04-11 20:56:28 +00002889 unsigned Op0Reg = getReg(Op0, BB, IP);
2890 unsigned Op1Reg = getReg(Op1, BB, IP);
Chris Lattneree352852004-02-29 07:22:16 +00002891 BuildMI(*BB, IP, MovOpcode[Class], 1, Reg).addReg(Op0Reg);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002892
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002893 if (Ty->isSigned()) {
Chris Lattnerf01729e2002-11-02 20:54:46 +00002894 // Emit a sign extension instruction...
Chris Lattner462fa822004-04-11 20:56:28 +00002895 unsigned ShiftResult = makeAnotherReg(Op0->getType());
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002896 BuildMI(*BB, IP, SAROpcode[Class], 2,ShiftResult).addReg(Op0Reg).addImm(31);
Chris Lattneree352852004-02-29 07:22:16 +00002897 BuildMI(*BB, IP, MovOpcode[Class], 1, ExtReg).addReg(ShiftResult);
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002898
2899 // Emit the appropriate divide or remainder instruction...
2900 BuildMI(*BB, IP, DivOpcode[1][Class], 1).addReg(Op1Reg);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002901 } else {
Alkis Evlogimenosf998a7e2004-01-12 07:22:45 +00002902 // If unsigned, emit a zeroing instruction... (reg = 0)
Chris Lattneree352852004-02-29 07:22:16 +00002903 BuildMI(*BB, IP, ClrOpcode[Class], 2, ExtReg).addImm(0);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002904
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002905 // Emit the appropriate divide or remainder instruction...
2906 BuildMI(*BB, IP, DivOpcode[0][Class], 1).addReg(Op1Reg);
2907 }
Chris Lattner06925362002-11-17 21:56:38 +00002908
Chris Lattnerf01729e2002-11-02 20:54:46 +00002909 // Figure out which register we want to pick the result out of...
Chris Lattnercadff442003-10-23 17:21:43 +00002910 unsigned DestReg = isDiv ? Reg : ExtReg;
Chris Lattnerf01729e2002-11-02 20:54:46 +00002911
Chris Lattnerf01729e2002-11-02 20:54:46 +00002912 // Put the result into the destination register...
Chris Lattneree352852004-02-29 07:22:16 +00002913 BuildMI(*BB, IP, MovOpcode[Class], 1, ResultReg).addReg(DestReg);
Chris Lattnerca9671d2002-11-02 20:28:58 +00002914}
Chris Lattnere2954c82002-11-02 20:04:26 +00002915
Chris Lattner06925362002-11-17 21:56:38 +00002916
Brian Gaekea1719c92002-10-31 23:03:59 +00002917/// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here
2918/// for constant immediate shift values, and for constant immediate
2919/// shift values equal to 1. Even the general case is sort of special,
2920/// because the shift amount has to be in CL, not just any old register.
2921///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002922void X86ISel::visitShiftInst(ShiftInst &I) {
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002923 MachineBasicBlock::iterator IP = BB->end ();
2924 emitShiftOperation (BB, IP, I.getOperand (0), I.getOperand (1),
2925 I.getOpcode () == Instruction::Shl, I.getType (),
2926 getReg (I));
2927}
2928
Chris Lattnerce7cafa2004-11-13 20:48:57 +00002929/// Emit code for a 'SHLD DestReg, Op0, Op1, Amt' operation, where Amt is a
2930/// constant.
2931void X86ISel::doSHLDConst(MachineBasicBlock *MBB,
2932 MachineBasicBlock::iterator IP,
2933 unsigned DestReg, unsigned Op0Reg, unsigned Op1Reg,
2934 unsigned Amt) {
2935 // SHLD is a very inefficient operation on every processor, try to do
2936 // somethign simpler for common values of 'Amt'.
2937 if (Amt == 0) {
2938 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg).addReg(Op0Reg);
2939 } else if (Amt == 1) {
2940 unsigned Tmp = makeAnotherReg(Type::UIntTy);
2941 BuildMI(*MBB, IP, X86::ADD32rr, 2, Tmp).addReg(Op1Reg).addReg(Op1Reg);
2942 BuildMI(*MBB, IP, X86::ADC32rr, 2, DestReg).addReg(Op0Reg).addReg(Op0Reg);
2943 } else if (Amt == 2 || Amt == 3) {
2944 // On the P4 and Athlon it is cheaper to replace shld ..., 2|3 with a
2945 // shift/lea pair. NOTE: This should not be done on the P6 family!
2946 unsigned Tmp = makeAnotherReg(Type::UIntTy);
2947 BuildMI(*MBB, IP, X86::SHR32ri, 2, Tmp).addReg(Op1Reg).addImm(32-Amt);
2948 X86AddressMode AM;
2949 AM.BaseType = X86AddressMode::RegBase;
2950 AM.Base.Reg = Tmp;
2951 AM.Scale = 1 << Amt;
2952 AM.IndexReg = Op0Reg;
2953 AM.Disp = 0;
2954 addFullAddress(BuildMI(*MBB, IP, X86::LEA32r, 4, DestReg), AM);
2955 } else {
2956 // NOTE: It is always cheaper on the P4 to emit SHLD as two shifts and an OR
2957 // than it is to emit a real SHLD.
2958
2959 BuildMI(*MBB, IP, X86::SHLD32rri8, 3,
2960 DestReg).addReg(Op0Reg).addReg(Op1Reg).addImm(Amt);
2961 }
2962}
2963
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002964/// emitShiftOperation - Common code shared between visitShiftInst and
2965/// constant expression support.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002966void X86ISel::emitShiftOperation(MachineBasicBlock *MBB,
2967 MachineBasicBlock::iterator IP,
2968 Value *Op, Value *ShiftAmount,
2969 bool isLeftShift, const Type *ResultTy,
2970 unsigned DestReg) {
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002971 unsigned SrcReg = getReg (Op, MBB, IP);
2972 bool isSigned = ResultTy->isSigned ();
2973 unsigned Class = getClass (ResultTy);
Chris Lattnerde95c9e2004-10-17 06:10:40 +00002974
Chris Lattnerce7cafa2004-11-13 20:48:57 +00002975 static const unsigned ConstantOperand[][3] = {
2976 { X86::SHR8ri, X86::SHR16ri, X86::SHR32ri }, // SHR
2977 { X86::SAR8ri, X86::SAR16ri, X86::SAR32ri }, // SAR
2978 { X86::SHL8ri, X86::SHL16ri, X86::SHL32ri }, // SHL
2979 { X86::SHL8ri, X86::SHL16ri, X86::SHL32ri }, // SAL = SHL
Chris Lattner3e130a22003-01-13 00:32:26 +00002980 };
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002981
Chris Lattnerce7cafa2004-11-13 20:48:57 +00002982 static const unsigned NonConstantOperand[][3] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002983 { X86::SHR8rCL, X86::SHR16rCL, X86::SHR32rCL }, // SHR
2984 { X86::SAR8rCL, X86::SAR16rCL, X86::SAR32rCL }, // SAR
2985 { X86::SHL8rCL, X86::SHL16rCL, X86::SHL32rCL }, // SHL
2986 { X86::SHL8rCL, X86::SHL16rCL, X86::SHL32rCL }, // SAL = SHL
Chris Lattner3e130a22003-01-13 00:32:26 +00002987 };
Chris Lattner796df732002-11-02 00:44:25 +00002988
Chris Lattnerce7cafa2004-11-13 20:48:57 +00002989 // Longs, as usual, are handled specially.
Chris Lattner3e130a22003-01-13 00:32:26 +00002990 if (Class == cLong) {
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002991 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002992 unsigned Amount = CUI->getValue();
Chris Lattner62f5a942004-11-13 20:04:38 +00002993 if (Amount == 1 && isLeftShift) { // X << 1 == X+X
Chris Lattner44205ca2004-11-13 20:03:48 +00002994 BuildMI(*MBB, IP, X86::ADD32rr, 2,
2995 DestReg).addReg(SrcReg).addReg(SrcReg);
2996 BuildMI(*MBB, IP, X86::ADC32rr, 2,
2997 DestReg+1).addReg(SrcReg+1).addReg(SrcReg+1);
2998 } else if (Amount < 32) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002999 const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned];
3000 if (isLeftShift) {
Chris Lattnerce7cafa2004-11-13 20:48:57 +00003001 doSHLDConst(MBB, IP, DestReg+1, SrcReg+1, SrcReg, Amount);
Chris Lattneree352852004-02-29 07:22:16 +00003002 BuildMI(*MBB, IP, Opc[2], 2, DestReg).addReg(SrcReg).addImm(Amount);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003003 } else {
Chris Lattnerce7cafa2004-11-13 20:48:57 +00003004 BuildMI(*MBB, IP, X86::SHRD32rri8, 3,
3005 DestReg).addReg(SrcReg ).addReg(SrcReg+1).addImm(Amount);
Chris Lattneree352852004-02-29 07:22:16 +00003006 BuildMI(*MBB, IP, Opc[2],2,DestReg+1).addReg(SrcReg+1).addImm(Amount);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003007 }
Chris Lattner36c625d2004-11-15 23:16:34 +00003008 } else if (Amount == 32) {
3009 if (isLeftShift) {
3010 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg+1).addReg(SrcReg);
3011 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
3012 } else {
Chris Lattner39a83dc2004-11-16 18:40:52 +00003013 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg+1);
Chris Lattner36c625d2004-11-15 23:16:34 +00003014 if (!isSigned) {
3015 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
3016 } else {
3017 BuildMI(*MBB, IP, X86::SAR32ri, 2,
3018 DestReg+1).addReg(SrcReg).addImm(31);
3019 }
3020 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003021 } else { // Shifting more than 32 bits
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003022 Amount -= 32;
3023 if (isLeftShift) {
Chris Lattner36c625d2004-11-15 23:16:34 +00003024 BuildMI(*MBB, IP, X86::SHL32ri, 2,
3025 DestReg + 1).addReg(SrcReg).addImm(Amount);
Chris Lattner722070e2004-04-06 03:42:38 +00003026 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003027 } else {
Chris Lattner36c625d2004-11-15 23:16:34 +00003028 BuildMI(*MBB, IP, isSigned ? X86::SAR32ri : X86::SHR32ri, 2,
3029 DestReg).addReg(SrcReg+1).addImm(Amount);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003030 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003031 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003032 }
3033 } else {
Chris Lattner9171ef52003-06-01 01:56:54 +00003034 unsigned TmpReg = makeAnotherReg(Type::IntTy);
Chris Lattner9171ef52003-06-01 01:56:54 +00003035 if (!isLeftShift && isSigned) {
3036 // If this is a SHR of a Long, then we need to do funny sign extension
3037 // stuff. TmpReg gets the value to use as the high-part if we are
3038 // shifting more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003039 BuildMI(*MBB, IP, X86::SAR32ri, 2, TmpReg).addReg(SrcReg).addImm(31);
Chris Lattner9171ef52003-06-01 01:56:54 +00003040 } else {
3041 // Other shifts use a fixed zero value if the shift is more than 32
3042 // bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003043 BuildMI(*MBB, IP, X86::MOV32ri, 1, TmpReg).addImm(0);
Chris Lattner9171ef52003-06-01 01:56:54 +00003044 }
3045
3046 // Initialize CL with the shift amount...
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00003047 unsigned ShiftAmountReg = getReg(ShiftAmount, MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003048 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00003049
3050 unsigned TmpReg2 = makeAnotherReg(Type::IntTy);
3051 unsigned TmpReg3 = makeAnotherReg(Type::IntTy);
3052 if (isLeftShift) {
3053 // TmpReg2 = shld inHi, inLo
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003054 BuildMI(*MBB, IP, X86::SHLD32rrCL,2,TmpReg2).addReg(SrcReg+1)
Chris Lattneree352852004-02-29 07:22:16 +00003055 .addReg(SrcReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00003056 // TmpReg3 = shl inLo, CL
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003057 BuildMI(*MBB, IP, X86::SHL32rCL, 1, TmpReg3).addReg(SrcReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00003058
3059 // Set the flags to indicate whether the shift was by more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003060 BuildMI(*MBB, IP, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Chris Lattner9171ef52003-06-01 01:56:54 +00003061
3062 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003063 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00003064 DestReg+1).addReg(TmpReg2).addReg(TmpReg3);
3065 // DestLo = (>32) ? TmpReg : TmpReg3;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003066 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00003067 DestReg).addReg(TmpReg3).addReg(TmpReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00003068 } else {
3069 // TmpReg2 = shrd inLo, inHi
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003070 BuildMI(*MBB, IP, X86::SHRD32rrCL,2,TmpReg2).addReg(SrcReg)
Chris Lattneree352852004-02-29 07:22:16 +00003071 .addReg(SrcReg+1);
Chris Lattner9171ef52003-06-01 01:56:54 +00003072 // TmpReg3 = s[ah]r inHi, CL
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003073 BuildMI(*MBB, IP, isSigned ? X86::SAR32rCL : X86::SHR32rCL, 1, TmpReg3)
Chris Lattner9171ef52003-06-01 01:56:54 +00003074 .addReg(SrcReg+1);
3075
3076 // Set the flags to indicate whether the shift was by more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003077 BuildMI(*MBB, IP, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Chris Lattner9171ef52003-06-01 01:56:54 +00003078
3079 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003080 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00003081 DestReg).addReg(TmpReg2).addReg(TmpReg3);
3082
3083 // DestHi = (>32) ? TmpReg : TmpReg3;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003084 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00003085 DestReg+1).addReg(TmpReg3).addReg(TmpReg);
3086 }
Brian Gaekea1719c92002-10-31 23:03:59 +00003087 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003088 return;
3089 }
Chris Lattnere9913f22002-11-02 01:41:55 +00003090
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00003091 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00003092 // The shift amount is constant, guaranteed to be a ubyte. Get its value.
3093 assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?");
Chris Lattnerb1761fc2002-11-02 01:15:18 +00003094
Chris Lattner44205ca2004-11-13 20:03:48 +00003095 if (CUI->getValue() == 1 && isLeftShift) { // X << 1 -> X+X
3096 static const int AddOpC[] = { X86::ADD8rr, X86::ADD16rr, X86::ADD32rr };
3097 BuildMI(*MBB, IP, AddOpC[Class], 2,DestReg).addReg(SrcReg).addReg(SrcReg);
3098 } else {
3099 const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned];
3100 BuildMI(*MBB, IP, Opc[Class], 2,
3101 DestReg).addReg(SrcReg).addImm(CUI->getValue());
3102 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003103 } else { // The shift amount is non-constant.
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00003104 unsigned ShiftAmountReg = getReg (ShiftAmount, MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003105 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
Chris Lattnerb1761fc2002-11-02 01:15:18 +00003106
Chris Lattner3e130a22003-01-13 00:32:26 +00003107 const unsigned *Opc = NonConstantOperand[isLeftShift*2+isSigned];
Chris Lattneree352852004-02-29 07:22:16 +00003108 BuildMI(*MBB, IP, Opc[Class], 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003109 }
3110}
Chris Lattnerb1761fc2002-11-02 01:15:18 +00003111
Chris Lattner3e130a22003-01-13 00:32:26 +00003112
Chris Lattner6fc3c522002-11-17 21:11:55 +00003113/// visitLoadInst - Implement LLVM load instructions in terms of the x86 'mov'
Chris Lattnere8f0d922002-12-24 00:03:11 +00003114/// instruction. The load and store instructions are the only place where we
3115/// need to worry about the memory layout of the target machine.
Chris Lattner6fc3c522002-11-17 21:11:55 +00003116///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003117void X86ISel::visitLoadInst(LoadInst &I) {
Chris Lattner7dee5da2004-03-08 01:58:35 +00003118 // Check to see if this load instruction is going to be folded into a binary
3119 // instruction, like add. If so, we don't want to emit it. Wouldn't a real
3120 // pattern matching instruction selector be nice?
Chris Lattner95157f72004-04-11 22:05:45 +00003121 unsigned Class = getClassB(I.getType());
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003122 if (I.hasOneUse()) {
Chris Lattner7dee5da2004-03-08 01:58:35 +00003123 Instruction *User = cast<Instruction>(I.use_back());
3124 switch (User->getOpcode()) {
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003125 case Instruction::Cast:
3126 // If this is a cast from a signed-integer type to a floating point type,
3127 // fold the cast here.
John Criswell6b5bd582004-06-09 15:18:51 +00003128 if (getClassB(User->getType()) == cFP &&
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003129 (I.getType() == Type::ShortTy || I.getType() == Type::IntTy ||
3130 I.getType() == Type::LongTy)) {
3131 unsigned DestReg = getReg(User);
3132 static const unsigned Opcode[] = {
3133 0/*BYTE*/, X86::FILD16m, X86::FILD32m, 0/*FP*/, X86::FILD64m
3134 };
Chris Lattner9f1b5312004-05-13 15:12:43 +00003135
3136 if (AllocaInst *AI = dyn_castFixedAlloca(I.getOperand(0))) {
3137 unsigned FI = getFixedSizedAllocaFI(AI);
3138 addFrameReference(BuildMI(BB, Opcode[Class], 4, DestReg), FI);
3139 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00003140 X86AddressMode AM;
3141 getAddressingMode(I.getOperand(0), AM);
3142 addFullAddress(BuildMI(BB, Opcode[Class], 4, DestReg), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00003143 }
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003144 return;
3145 } else {
3146 User = 0;
3147 }
3148 break;
Chris Lattner13c07fe2004-04-12 00:12:04 +00003149
Chris Lattner7dee5da2004-03-08 01:58:35 +00003150 case Instruction::Add:
3151 case Instruction::Sub:
3152 case Instruction::And:
3153 case Instruction::Or:
3154 case Instruction::Xor:
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003155 if (Class == cLong) User = 0;
Chris Lattner7dee5da2004-03-08 01:58:35 +00003156 break;
Chris Lattner95157f72004-04-11 22:05:45 +00003157 case Instruction::Mul:
3158 case Instruction::Div:
Chris Lattner13c07fe2004-04-12 00:12:04 +00003159 if (Class != cFP) User = 0;
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003160 break; // Folding only implemented for floating point.
Chris Lattner95157f72004-04-11 22:05:45 +00003161 default: User = 0; break;
Chris Lattner7dee5da2004-03-08 01:58:35 +00003162 }
3163
3164 if (User) {
3165 // Okay, we found a user. If the load is the first operand and there is
3166 // no second operand load, reverse the operand ordering. Note that this
3167 // can fail for a subtract (ie, no change will be made).
Chris Lattner3dbb5042004-07-21 21:28:26 +00003168 bool Swapped = false;
Chris Lattner7dee5da2004-03-08 01:58:35 +00003169 if (!isa<LoadInst>(User->getOperand(1)))
Chris Lattner3dbb5042004-07-21 21:28:26 +00003170 Swapped = !cast<BinaryOperator>(User)->swapOperands();
Chris Lattner7dee5da2004-03-08 01:58:35 +00003171
3172 // Okay, now that everything is set up, if this load is used by the second
3173 // operand, and if there are no instructions that invalidate the load
3174 // before the binary operator, eliminate the load.
3175 if (User->getOperand(1) == &I &&
3176 isSafeToFoldLoadIntoInstruction(I, *User))
3177 return; // Eliminate the load!
Chris Lattner95157f72004-04-11 22:05:45 +00003178
3179 // If this is a floating point sub or div, we won't be able to swap the
3180 // operands, but we will still be able to eliminate the load.
3181 if (Class == cFP && User->getOperand(0) == &I &&
3182 !isa<LoadInst>(User->getOperand(1)) &&
3183 (User->getOpcode() == Instruction::Sub ||
3184 User->getOpcode() == Instruction::Div) &&
3185 isSafeToFoldLoadIntoInstruction(I, *User))
3186 return; // Eliminate the load!
Chris Lattner3dbb5042004-07-21 21:28:26 +00003187
3188 // If we swapped the operands to the instruction, but couldn't fold the
3189 // load anyway, swap them back. We don't want to break add X, int
3190 // folding.
3191 if (Swapped) cast<BinaryOperator>(User)->swapOperands();
Chris Lattner7dee5da2004-03-08 01:58:35 +00003192 }
3193 }
3194
Chris Lattner6ac1d712003-10-20 04:48:06 +00003195 static const unsigned Opcodes[] = {
Chris Lattner9f1b5312004-05-13 15:12:43 +00003196 X86::MOV8rm, X86::MOV16rm, X86::MOV32rm, X86::FLD32m, X86::MOV32rm
Chris Lattner3e130a22003-01-13 00:32:26 +00003197 };
Chris Lattner6ac1d712003-10-20 04:48:06 +00003198 unsigned Opcode = Opcodes[Class];
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003199 if (I.getType() == Type::DoubleTy) Opcode = X86::FLD64m;
Chris Lattner9f1b5312004-05-13 15:12:43 +00003200
3201 unsigned DestReg = getReg(I);
3202
3203 if (AllocaInst *AI = dyn_castFixedAlloca(I.getOperand(0))) {
3204 unsigned FI = getFixedSizedAllocaFI(AI);
3205 if (Class == cLong) {
3206 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, DestReg), FI);
3207 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, DestReg+1), FI, 4);
3208 } else {
3209 addFrameReference(BuildMI(BB, Opcode, 4, DestReg), FI);
3210 }
3211 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00003212 X86AddressMode AM;
3213 getAddressingMode(I.getOperand(0), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00003214
3215 if (Class == cLong) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003216 addFullAddress(BuildMI(BB, X86::MOV32rm, 4, DestReg), AM);
3217 AM.Disp += 4;
3218 addFullAddress(BuildMI(BB, X86::MOV32rm, 4, DestReg+1), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00003219 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00003220 addFullAddress(BuildMI(BB, Opcode, 4, DestReg), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00003221 }
3222 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003223}
3224
Chris Lattner6fc3c522002-11-17 21:11:55 +00003225/// visitStoreInst - Implement LLVM store instructions in terms of the x86 'mov'
3226/// instruction.
3227///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003228void X86ISel::visitStoreInst(StoreInst &I) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003229 X86AddressMode AM;
3230 getAddressingMode(I.getOperand(1), AM);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003231
Chris Lattner6c09db22003-10-20 04:11:23 +00003232 const Type *ValTy = I.getOperand(0)->getType();
3233 unsigned Class = getClassB(ValTy);
Chris Lattner6ac1d712003-10-20 04:48:06 +00003234
Chris Lattner5a830962004-02-25 02:56:58 +00003235 if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(0))) {
3236 uint64_t Val = CI->getRawValue();
3237 if (Class == cLong) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003238 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(Val & ~0U);
3239 AM.Disp += 4;
3240 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(Val>>32);
Chris Lattner5a830962004-02-25 02:56:58 +00003241 } else {
3242 static const unsigned Opcodes[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003243 X86::MOV8mi, X86::MOV16mi, X86::MOV32mi
Chris Lattner5a830962004-02-25 02:56:58 +00003244 };
3245 unsigned Opcode = Opcodes[Class];
Reid Spencerfc989e12004-08-30 00:13:26 +00003246 addFullAddress(BuildMI(BB, Opcode, 5), AM).addImm(Val);
Chris Lattner5a830962004-02-25 02:56:58 +00003247 }
Chris Lattnerb7cb9ff2004-05-13 15:26:48 +00003248 } else if (isa<ConstantPointerNull>(I.getOperand(0))) {
Chris Lattner358a9022004-10-15 05:05:29 +00003249 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(0);
Chris Lattner5a830962004-02-25 02:56:58 +00003250 } else if (ConstantBool *CB = dyn_cast<ConstantBool>(I.getOperand(0))) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003251 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CB->getValue());
Chris Lattnere7a31c92004-05-07 21:18:15 +00003252 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0))) {
3253 // Store constant FP values with integer instructions to avoid having to
3254 // load the constants from the constant pool then do a store.
3255 if (CFP->getType() == Type::FloatTy) {
3256 union {
3257 unsigned I;
3258 float F;
3259 } V;
3260 V.F = CFP->getValue();
Reid Spencerfc989e12004-08-30 00:13:26 +00003261 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(V.I);
Chris Lattner5a830962004-02-25 02:56:58 +00003262 } else {
Chris Lattnere7a31c92004-05-07 21:18:15 +00003263 union {
3264 uint64_t I;
3265 double F;
3266 } V;
3267 V.F = CFP->getValue();
Reid Spencerfc989e12004-08-30 00:13:26 +00003268 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm((unsigned)V.I);
3269 AM.Disp += 4;
3270 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(
Chris Lattnere7a31c92004-05-07 21:18:15 +00003271 unsigned(V.I >> 32));
Chris Lattner5a830962004-02-25 02:56:58 +00003272 }
Chris Lattnere7a31c92004-05-07 21:18:15 +00003273
3274 } else if (Class == cLong) {
3275 unsigned ValReg = getReg(I.getOperand(0));
Reid Spencerfc989e12004-08-30 00:13:26 +00003276 addFullAddress(BuildMI(BB, X86::MOV32mr, 5), AM).addReg(ValReg);
3277 AM.Disp += 4;
3278 addFullAddress(BuildMI(BB, X86::MOV32mr, 5), AM).addReg(ValReg+1);
Chris Lattnere7a31c92004-05-07 21:18:15 +00003279 } else {
Chris Lattner358a9022004-10-15 05:05:29 +00003280 // FIXME: stop emitting these two instructions:
3281 // movl $global,%eax
3282 // movl %eax,(%ebx)
3283 // when one instruction will suffice. That includes when the global
3284 // has an offset applied to it.
Chris Lattnere7a31c92004-05-07 21:18:15 +00003285 unsigned ValReg = getReg(I.getOperand(0));
3286 static const unsigned Opcodes[] = {
3287 X86::MOV8mr, X86::MOV16mr, X86::MOV32mr, X86::FST32m
3288 };
3289 unsigned Opcode = Opcodes[Class];
3290 if (ValTy == Type::DoubleTy) Opcode = X86::FST64m;
Chris Lattner9f1b5312004-05-13 15:12:43 +00003291
Reid Spencerfc989e12004-08-30 00:13:26 +00003292 addFullAddress(BuildMI(BB, Opcode, 1+4), AM).addReg(ValReg);
Chris Lattner94af4142002-12-25 05:13:53 +00003293 }
Chris Lattner6fc3c522002-11-17 21:11:55 +00003294}
3295
3296
Misha Brukman538607f2004-03-01 23:53:11 +00003297/// visitCastInst - Here we have various kinds of copying with or without sign
3298/// extension going on.
3299///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003300void X86ISel::visitCastInst(CastInst &CI) {
Chris Lattnerf5854472003-06-21 16:01:24 +00003301 Value *Op = CI.getOperand(0);
Chris Lattner427aeb42004-04-11 19:21:59 +00003302
Chris Lattner99382862004-04-12 00:23:04 +00003303 unsigned SrcClass = getClassB(Op->getType());
3304 unsigned DestClass = getClassB(CI.getType());
3305 // Noop casts are not emitted: getReg will return the source operand as the
3306 // register to use for any uses of the noop cast.
Chris Lattner8b486a12004-06-29 00:14:38 +00003307 if (DestClass == SrcClass) {
3308 // The only detail in this plan is that casts from double -> float are
3309 // truncating operations that we have to codegen through memory (despite
3310 // the fact that the source/dest registers are the same class).
3311 if (CI.getType() != Type::FloatTy || Op->getType() != Type::DoubleTy)
3312 return;
3313 }
Chris Lattner427aeb42004-04-11 19:21:59 +00003314
Chris Lattnerf5854472003-06-21 16:01:24 +00003315 // If this is a cast from a 32-bit integer to a Long type, and the only uses
3316 // of the case are GEP instructions, then the cast does not need to be
3317 // generated explicitly, it will be folded into the GEP.
Chris Lattner99382862004-04-12 00:23:04 +00003318 if (DestClass == cLong && SrcClass == cInt) {
Chris Lattnerf5854472003-06-21 16:01:24 +00003319 bool AllUsesAreGEPs = true;
3320 for (Value::use_iterator I = CI.use_begin(), E = CI.use_end(); I != E; ++I)
3321 if (!isa<GetElementPtrInst>(*I)) {
3322 AllUsesAreGEPs = false;
3323 break;
3324 }
3325
3326 // No need to codegen this cast if all users are getelementptr instrs...
3327 if (AllUsesAreGEPs) return;
3328 }
3329
Chris Lattner99382862004-04-12 00:23:04 +00003330 // If this cast converts a load from a short,int, or long integer to a FP
3331 // value, we will have folded this cast away.
3332 if (DestClass == cFP && isa<LoadInst>(Op) && Op->hasOneUse() &&
3333 (Op->getType() == Type::ShortTy || Op->getType() == Type::IntTy ||
3334 Op->getType() == Type::LongTy))
3335 return;
3336
3337
Chris Lattner548f61d2003-04-23 17:22:12 +00003338 unsigned DestReg = getReg(CI);
3339 MachineBasicBlock::iterator MI = BB->end();
Chris Lattnerf5854472003-06-21 16:01:24 +00003340 emitCastOperation(BB, MI, Op, CI.getType(), DestReg);
Chris Lattner548f61d2003-04-23 17:22:12 +00003341}
3342
Misha Brukman538607f2004-03-01 23:53:11 +00003343/// emitCastOperation - Common code shared between visitCastInst and constant
3344/// expression cast support.
3345///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003346void X86ISel::emitCastOperation(MachineBasicBlock *BB,
3347 MachineBasicBlock::iterator IP,
3348 Value *Src, const Type *DestTy,
3349 unsigned DestReg) {
Chris Lattner3e130a22003-01-13 00:32:26 +00003350 const Type *SrcTy = Src->getType();
3351 unsigned SrcClass = getClassB(SrcTy);
Chris Lattner3e130a22003-01-13 00:32:26 +00003352 unsigned DestClass = getClassB(DestTy);
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003353 unsigned SrcReg = getReg(Src, BB, IP);
3354
Chris Lattner3e130a22003-01-13 00:32:26 +00003355 // Implement casts to bool by using compare on the operand followed by set if
3356 // not zero on the result.
3357 if (DestTy == Type::BoolTy) {
Chris Lattner20772542003-06-01 03:38:24 +00003358 switch (SrcClass) {
3359 case cByte:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003360 BuildMI(*BB, IP, X86::TEST8rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00003361 break;
3362 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003363 BuildMI(*BB, IP, X86::TEST16rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00003364 break;
3365 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003366 BuildMI(*BB, IP, X86::TEST32rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00003367 break;
3368 case cLong: {
3369 unsigned TmpReg = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003370 BuildMI(*BB, IP, X86::OR32rr, 2, TmpReg).addReg(SrcReg).addReg(SrcReg+1);
Chris Lattner20772542003-06-01 03:38:24 +00003371 break;
3372 }
3373 case cFP:
Chris Lattneree352852004-02-29 07:22:16 +00003374 BuildMI(*BB, IP, X86::FTST, 1).addReg(SrcReg);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003375 BuildMI(*BB, IP, X86::FNSTSW8r, 0);
Chris Lattneree352852004-02-29 07:22:16 +00003376 BuildMI(*BB, IP, X86::SAHF, 1);
Chris Lattner311ca2e2004-02-23 03:21:41 +00003377 break;
Chris Lattner20772542003-06-01 03:38:24 +00003378 }
3379
3380 // If the zero flag is not set, then the value is true, set the byte to
3381 // true.
Chris Lattneree352852004-02-29 07:22:16 +00003382 BuildMI(*BB, IP, X86::SETNEr, 1, DestReg);
Chris Lattner94af4142002-12-25 05:13:53 +00003383 return;
3384 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003385
3386 static const unsigned RegRegMove[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003387 X86::MOV8rr, X86::MOV16rr, X86::MOV32rr, X86::FpMOV, X86::MOV32rr
Chris Lattner3e130a22003-01-13 00:32:26 +00003388 };
3389
3390 // Implement casts between values of the same type class (as determined by
3391 // getClass) by using a register-to-register move.
3392 if (SrcClass == DestClass) {
3393 if (SrcClass <= cInt || (SrcClass == cFP && SrcTy == DestTy)) {
Chris Lattneree352852004-02-29 07:22:16 +00003394 BuildMI(*BB, IP, RegRegMove[SrcClass], 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003395 } else if (SrcClass == cFP) {
3396 if (SrcTy == Type::FloatTy) { // double -> float
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003397 assert(DestTy == Type::DoubleTy && "Unknown cFP member!");
Chris Lattneree352852004-02-29 07:22:16 +00003398 BuildMI(*BB, IP, X86::FpMOV, 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003399 } else { // float -> double
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003400 assert(SrcTy == Type::DoubleTy && DestTy == Type::FloatTy &&
3401 "Unknown cFP member!");
3402 // Truncate from double to float by storing to memory as short, then
3403 // reading it back.
3404 unsigned FltAlign = TM.getTargetData().getFloatAlignment();
Chris Lattner3e130a22003-01-13 00:32:26 +00003405 int FrameIdx = F->getFrameInfo()->CreateStackObject(4, FltAlign);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003406 addFrameReference(BuildMI(*BB, IP, X86::FST32m, 5), FrameIdx).addReg(SrcReg);
3407 addFrameReference(BuildMI(*BB, IP, X86::FLD32m, 5, DestReg), FrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003408 }
3409 } else if (SrcClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003410 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg);
3411 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg+1).addReg(SrcReg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00003412 } else {
Chris Lattnerc53544a2003-05-12 20:16:58 +00003413 assert(0 && "Cannot handle this type of cast instruction!");
Chris Lattner548f61d2003-04-23 17:22:12 +00003414 abort();
Brian Gaeked474e9c2002-12-06 10:49:33 +00003415 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003416 return;
3417 }
3418
3419 // Handle cast of SMALLER int to LARGER int using a move with sign extension
3420 // or zero extension, depending on whether the source type was signed.
3421 if (SrcClass <= cInt && (DestClass <= cInt || DestClass == cLong) &&
3422 SrcClass < DestClass) {
3423 bool isLong = DestClass == cLong;
3424 if (isLong) DestClass = cInt;
3425
3426 static const unsigned Opc[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003427 { X86::MOVSX16rr8, X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOV32rr }, // s
3428 { X86::MOVZX16rr8, X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOV32rr } // u
Chris Lattner3e130a22003-01-13 00:32:26 +00003429 };
3430
Chris Lattner96e3b422004-05-09 22:28:45 +00003431 bool isUnsigned = SrcTy->isUnsigned() || SrcTy == Type::BoolTy;
Chris Lattneree352852004-02-29 07:22:16 +00003432 BuildMI(*BB, IP, Opc[isUnsigned][SrcClass + DestClass - 1], 1,
Chris Lattner548f61d2003-04-23 17:22:12 +00003433 DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003434
3435 if (isLong) { // Handle upper 32 bits as appropriate...
3436 if (isUnsigned) // Zero out top bits...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003437 BuildMI(*BB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
Chris Lattner3e130a22003-01-13 00:32:26 +00003438 else // Sign extend bottom half...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003439 BuildMI(*BB, IP, X86::SAR32ri, 2, DestReg+1).addReg(DestReg).addImm(31);
Brian Gaeked474e9c2002-12-06 10:49:33 +00003440 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003441 return;
3442 }
3443
3444 // Special case long -> int ...
3445 if (SrcClass == cLong && DestClass == cInt) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003446 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003447 return;
3448 }
3449
3450 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by a
3451 // move out of AX or AL.
3452 if ((SrcClass <= cInt || SrcClass == cLong) && DestClass <= cInt
3453 && SrcClass > DestClass) {
3454 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX, 0, X86::EAX };
Chris Lattneree352852004-02-29 07:22:16 +00003455 BuildMI(*BB, IP, RegRegMove[SrcClass], 1, AReg[SrcClass]).addReg(SrcReg);
3456 BuildMI(*BB, IP, RegRegMove[DestClass], 1, DestReg).addReg(AReg[DestClass]);
Chris Lattner3e130a22003-01-13 00:32:26 +00003457 return;
3458 }
3459
3460 // Handle casts from integer to floating point now...
3461 if (DestClass == cFP) {
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003462 // Promote the integer to a type supported by FLD. We do this because there
3463 // are no unsigned FLD instructions, so we must promote an unsigned value to
3464 // a larger signed value, then use FLD on the larger value.
3465 //
3466 const Type *PromoteType = 0;
Chris Lattner85aa7092004-04-10 18:32:01 +00003467 unsigned PromoteOpcode = 0;
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003468 unsigned RealDestReg = DestReg;
Chris Lattnerf70c22b2004-06-17 18:19:28 +00003469 switch (SrcTy->getTypeID()) {
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003470 case Type::BoolTyID:
3471 case Type::SByteTyID:
3472 // We don't have the facilities for directly loading byte sized data from
3473 // memory (even signed). Promote it to 16 bits.
3474 PromoteType = Type::ShortTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003475 PromoteOpcode = X86::MOVSX16rr8;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003476 break;
3477 case Type::UByteTyID:
3478 PromoteType = Type::ShortTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003479 PromoteOpcode = X86::MOVZX16rr8;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003480 break;
3481 case Type::UShortTyID:
3482 PromoteType = Type::IntTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003483 PromoteOpcode = X86::MOVZX32rr16;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003484 break;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003485 case Type::ULongTyID:
Chris Lattner56a31c62004-10-17 08:01:28 +00003486 case Type::UIntTyID:
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003487 // Don't fild into the read destination.
3488 DestReg = makeAnotherReg(Type::DoubleTy);
3489 break;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003490 default: // No promotion needed...
3491 break;
3492 }
3493
3494 if (PromoteType) {
3495 unsigned TmpReg = makeAnotherReg(PromoteType);
Chris Lattner7b92de1e2004-04-06 19:29:36 +00003496 BuildMI(*BB, IP, PromoteOpcode, 1, TmpReg).addReg(SrcReg);
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003497 SrcTy = PromoteType;
3498 SrcClass = getClass(PromoteType);
Chris Lattner3e130a22003-01-13 00:32:26 +00003499 SrcReg = TmpReg;
3500 }
3501
3502 // Spill the integer to memory and reload it from there...
Chris Lattnerb6bac512004-02-25 06:13:04 +00003503 int FrameIdx =
3504 F->getFrameInfo()->CreateStackObject(SrcTy, TM.getTargetData());
Chris Lattner3e130a22003-01-13 00:32:26 +00003505
3506 if (SrcClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003507 addFrameReference(BuildMI(*BB, IP, X86::MOV32mr, 5),
Chris Lattneree352852004-02-29 07:22:16 +00003508 FrameIdx).addReg(SrcReg);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003509 addFrameReference(BuildMI(*BB, IP, X86::MOV32mr, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003510 FrameIdx, 4).addReg(SrcReg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00003511 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003512 static const unsigned Op1[] = { X86::MOV8mr, X86::MOV16mr, X86::MOV32mr };
Chris Lattneree352852004-02-29 07:22:16 +00003513 addFrameReference(BuildMI(*BB, IP, Op1[SrcClass], 5),
3514 FrameIdx).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003515 }
3516
3517 static const unsigned Op2[] =
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003518 { 0/*byte*/, X86::FILD16m, X86::FILD32m, 0/*FP*/, X86::FILD64m };
Chris Lattneree352852004-02-29 07:22:16 +00003519 addFrameReference(BuildMI(*BB, IP, Op2[SrcClass], 5, DestReg), FrameIdx);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003520
Chris Lattner56a31c62004-10-17 08:01:28 +00003521 if (SrcTy == Type::UIntTy) {
3522 // If this is a cast from uint -> double, we need to be careful about if
3523 // the "sign" bit is set. If so, we don't want to make a negative number,
3524 // we want to make a positive number. Emit code to add an offset if the
3525 // sign bit is set.
3526
3527 // Compute whether the sign bit is set by shifting the reg right 31 bits.
3528 unsigned IsNeg = makeAnotherReg(Type::IntTy);
3529 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(SrcReg).addImm(31);
3530
3531 // Create a CP value that has the offset in one word and 0 in the other.
3532 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
3533 0x4f80000000000000ULL);
3534 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
3535 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(DestReg)
3536 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
3537
3538 } else if (SrcTy == Type::ULongTy) {
3539 // We need special handling for unsigned 64-bit integer sources. If the
3540 // input number has the "sign bit" set, then we loaded it incorrectly as a
3541 // negative 64-bit number. In this case, add an offset value.
3542
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003543 // Emit a test instruction to see if the dynamic input value was signed.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003544 BuildMI(*BB, IP, X86::TEST32rr, 2).addReg(SrcReg+1).addReg(SrcReg+1);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003545
Chris Lattnerb6bac512004-02-25 06:13:04 +00003546 // If the sign bit is set, get a pointer to an offset, otherwise get a
3547 // pointer to a zero.
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003548 MachineConstantPool *CP = F->getConstantPool();
3549 unsigned Zero = makeAnotherReg(Type::IntTy);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003550 Constant *Null = Constant::getNullValue(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003551 addConstantPoolReference(BuildMI(*BB, IP, X86::LEA32r, 5, Zero),
Chris Lattnerb6bac512004-02-25 06:13:04 +00003552 CP->getConstantPoolIndex(Null));
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003553 unsigned Offset = makeAnotherReg(Type::IntTy);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003554 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
3555
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003556 addConstantPoolReference(BuildMI(*BB, IP, X86::LEA32r, 5, Offset),
Chris Lattnerb6bac512004-02-25 06:13:04 +00003557 CP->getConstantPoolIndex(OffsetCst));
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003558 unsigned Addr = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003559 BuildMI(*BB, IP, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003560
3561 // Load the constant for an add. FIXME: this could make an 'fadd' that
3562 // reads directly from memory, but we don't support these yet.
3563 unsigned ConstReg = makeAnotherReg(Type::DoubleTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003564 addDirectMem(BuildMI(*BB, IP, X86::FLD32m, 4, ConstReg), Addr);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003565
Chris Lattneree352852004-02-29 07:22:16 +00003566 BuildMI(*BB, IP, X86::FpADD, 2, RealDestReg)
3567 .addReg(ConstReg).addReg(DestReg);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003568 }
3569
Chris Lattner3e130a22003-01-13 00:32:26 +00003570 return;
3571 }
3572
3573 // Handle casts from floating point to integer now...
3574 if (SrcClass == cFP) {
3575 // Change the floating point control register to use "round towards zero"
3576 // mode when truncating to an integer value.
3577 //
3578 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003579 addFrameReference(BuildMI(*BB, IP, X86::FNSTCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003580
3581 // Load the old value of the high byte of the control word...
3582 unsigned HighPartOfCW = makeAnotherReg(Type::UByteTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003583 addFrameReference(BuildMI(*BB, IP, X86::MOV8rm, 4, HighPartOfCW),
Chris Lattneree352852004-02-29 07:22:16 +00003584 CWFrameIdx, 1);
Chris Lattner3e130a22003-01-13 00:32:26 +00003585
3586 // Set the high part to be round to zero...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003587 addFrameReference(BuildMI(*BB, IP, X86::MOV8mi, 5),
Chris Lattneree352852004-02-29 07:22:16 +00003588 CWFrameIdx, 1).addImm(12);
Chris Lattner3e130a22003-01-13 00:32:26 +00003589
3590 // Reload the modified control word now...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003591 addFrameReference(BuildMI(*BB, IP, X86::FLDCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003592
3593 // Restore the memory image of control word to original value
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003594 addFrameReference(BuildMI(*BB, IP, X86::MOV8mr, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003595 CWFrameIdx, 1).addReg(HighPartOfCW);
Chris Lattner3e130a22003-01-13 00:32:26 +00003596
3597 // We don't have the facilities for directly storing byte sized data to
3598 // memory. Promote it to 16 bits. We also must promote unsigned values to
3599 // larger classes because we only have signed FP stores.
3600 unsigned StoreClass = DestClass;
3601 const Type *StoreTy = DestTy;
3602 if (StoreClass == cByte || DestTy->isUnsigned())
3603 switch (StoreClass) {
3604 case cByte: StoreTy = Type::ShortTy; StoreClass = cShort; break;
3605 case cShort: StoreTy = Type::IntTy; StoreClass = cInt; break;
3606 case cInt: StoreTy = Type::LongTy; StoreClass = cLong; break;
Brian Gaeked4615052003-07-18 20:23:43 +00003607 // The following treatment of cLong may not be perfectly right,
3608 // but it survives chains of casts of the form
3609 // double->ulong->double.
3610 case cLong: StoreTy = Type::LongTy; StoreClass = cLong; break;
Chris Lattner3e130a22003-01-13 00:32:26 +00003611 default: assert(0 && "Unknown store class!");
3612 }
3613
3614 // Spill the integer to memory and reload it from there...
3615 int FrameIdx =
3616 F->getFrameInfo()->CreateStackObject(StoreTy, TM.getTargetData());
3617
3618 static const unsigned Op1[] =
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003619 { 0, X86::FIST16m, X86::FIST32m, 0, X86::FISTP64m };
Chris Lattneree352852004-02-29 07:22:16 +00003620 addFrameReference(BuildMI(*BB, IP, Op1[StoreClass], 5),
3621 FrameIdx).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003622
3623 if (DestClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003624 addFrameReference(BuildMI(*BB, IP, X86::MOV32rm, 4, DestReg), FrameIdx);
3625 addFrameReference(BuildMI(*BB, IP, X86::MOV32rm, 4, DestReg+1),
Chris Lattneree352852004-02-29 07:22:16 +00003626 FrameIdx, 4);
Chris Lattner3e130a22003-01-13 00:32:26 +00003627 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003628 static const unsigned Op2[] = { X86::MOV8rm, X86::MOV16rm, X86::MOV32rm };
Chris Lattneree352852004-02-29 07:22:16 +00003629 addFrameReference(BuildMI(*BB, IP, Op2[DestClass], 4, DestReg), FrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003630 }
3631
3632 // Reload the original control word now...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003633 addFrameReference(BuildMI(*BB, IP, X86::FLDCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003634 return;
3635 }
3636
Brian Gaeked474e9c2002-12-06 10:49:33 +00003637 // Anything we haven't handled already, we can't (yet) handle at all.
Chris Lattnerc53544a2003-05-12 20:16:58 +00003638 assert(0 && "Unhandled cast instruction!");
Chris Lattner548f61d2003-04-23 17:22:12 +00003639 abort();
Brian Gaekefa8d5712002-11-22 11:07:01 +00003640}
Brian Gaekea1719c92002-10-31 23:03:59 +00003641
Chris Lattner73815062003-10-18 05:56:40 +00003642/// visitVANextInst - Implement the va_next instruction...
Chris Lattnereca195e2003-05-08 19:44:13 +00003643///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003644void X86ISel::visitVANextInst(VANextInst &I) {
Chris Lattner73815062003-10-18 05:56:40 +00003645 unsigned VAList = getReg(I.getOperand(0));
Chris Lattnereca195e2003-05-08 19:44:13 +00003646 unsigned DestReg = getReg(I);
3647
Chris Lattnereca195e2003-05-08 19:44:13 +00003648 unsigned Size;
Chris Lattnerf70c22b2004-06-17 18:19:28 +00003649 switch (I.getArgType()->getTypeID()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00003650 default:
3651 std::cerr << I;
Chris Lattner73815062003-10-18 05:56:40 +00003652 assert(0 && "Error: bad type for va_next instruction!");
Chris Lattnereca195e2003-05-08 19:44:13 +00003653 return;
3654 case Type::PointerTyID:
3655 case Type::UIntTyID:
3656 case Type::IntTyID:
3657 Size = 4;
Chris Lattnereca195e2003-05-08 19:44:13 +00003658 break;
3659 case Type::ULongTyID:
3660 case Type::LongTyID:
Chris Lattnereca195e2003-05-08 19:44:13 +00003661 case Type::DoubleTyID:
3662 Size = 8;
Chris Lattnereca195e2003-05-08 19:44:13 +00003663 break;
3664 }
3665
3666 // Increment the VAList pointer...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003667 BuildMI(BB, X86::ADD32ri, 2, DestReg).addReg(VAList).addImm(Size);
Chris Lattner73815062003-10-18 05:56:40 +00003668}
Chris Lattnereca195e2003-05-08 19:44:13 +00003669
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003670void X86ISel::visitVAArgInst(VAArgInst &I) {
Chris Lattner73815062003-10-18 05:56:40 +00003671 unsigned VAList = getReg(I.getOperand(0));
3672 unsigned DestReg = getReg(I);
3673
Chris Lattnerf70c22b2004-06-17 18:19:28 +00003674 switch (I.getType()->getTypeID()) {
Chris Lattner73815062003-10-18 05:56:40 +00003675 default:
3676 std::cerr << I;
3677 assert(0 && "Error: bad type for va_next instruction!");
3678 return;
3679 case Type::PointerTyID:
3680 case Type::UIntTyID:
3681 case Type::IntTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003682 addDirectMem(BuildMI(BB, X86::MOV32rm, 4, DestReg), VAList);
Chris Lattner73815062003-10-18 05:56:40 +00003683 break;
3684 case Type::ULongTyID:
3685 case Type::LongTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003686 addDirectMem(BuildMI(BB, X86::MOV32rm, 4, DestReg), VAList);
3687 addRegOffset(BuildMI(BB, X86::MOV32rm, 4, DestReg+1), VAList, 4);
Chris Lattner73815062003-10-18 05:56:40 +00003688 break;
3689 case Type::DoubleTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003690 addDirectMem(BuildMI(BB, X86::FLD64m, 4, DestReg), VAList);
Chris Lattner73815062003-10-18 05:56:40 +00003691 break;
3692 }
Chris Lattnereca195e2003-05-08 19:44:13 +00003693}
3694
Misha Brukman538607f2004-03-01 23:53:11 +00003695/// visitGetElementPtrInst - instruction-select GEP instructions
3696///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003697void X86ISel::visitGetElementPtrInst(GetElementPtrInst &I) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00003698 // If this GEP instruction will be folded into all of its users, we don't need
3699 // to explicitly calculate it!
Reid Spencerfc989e12004-08-30 00:13:26 +00003700 X86AddressMode AM;
3701 if (isGEPFoldable(0, I.getOperand(0), I.op_begin()+1, I.op_end(), AM)) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00003702 // Check all of the users of the instruction to see if they are loads and
3703 // stores.
3704 bool AllWillFold = true;
3705 for (Value::use_iterator UI = I.use_begin(), E = I.use_end(); UI != E; ++UI)
3706 if (cast<Instruction>(*UI)->getOpcode() != Instruction::Load)
3707 if (cast<Instruction>(*UI)->getOpcode() != Instruction::Store ||
3708 cast<Instruction>(*UI)->getOperand(0) == &I) {
3709 AllWillFold = false;
3710 break;
3711 }
3712
3713 // If the instruction is foldable, and will be folded into all users, don't
3714 // emit it!
3715 if (AllWillFold) return;
3716 }
3717
Chris Lattner3e130a22003-01-13 00:32:26 +00003718 unsigned outputReg = getReg(I);
Chris Lattner827832c2004-02-22 17:05:38 +00003719 emitGEPOperation(BB, BB->end(), I.getOperand(0),
Brian Gaeke68b1edc2002-12-16 04:23:29 +00003720 I.op_begin()+1, I.op_end(), outputReg);
Chris Lattnerc0812d82002-12-13 06:56:29 +00003721}
3722
Chris Lattner985fe3d2004-02-25 03:45:50 +00003723/// getGEPIndex - Inspect the getelementptr operands specified with GEPOps and
3724/// GEPTypes (the derived types being stepped through at each level). On return
3725/// from this function, if some indexes of the instruction are representable as
3726/// an X86 lea instruction, the machine operands are put into the Ops
3727/// instruction and the consumed indexes are poped from the GEPOps/GEPTypes
3728/// lists. Otherwise, GEPOps.size() is returned. If this returns a an
3729/// addressing mode that only partially consumes the input, the BaseReg input of
3730/// the addressing mode must be left free.
3731///
3732/// Note that there is one fewer entry in GEPTypes than there is in GEPOps.
3733///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003734void X86ISel::getGEPIndex(MachineBasicBlock *MBB,
3735 MachineBasicBlock::iterator IP,
3736 std::vector<Value*> &GEPOps,
3737 std::vector<const Type*> &GEPTypes,
3738 X86AddressMode &AM) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00003739 const TargetData &TD = TM.getTargetData();
3740
Chris Lattner985fe3d2004-02-25 03:45:50 +00003741 // Clear out the state we are working with...
Reid Spencerfc989e12004-08-30 00:13:26 +00003742 AM.BaseType = X86AddressMode::RegBase;
3743 AM.Base.Reg = 0; // No base register
3744 AM.Scale = 1; // Unit scale
3745 AM.IndexReg = 0; // No index register
3746 AM.Disp = 0; // No displacement
Chris Lattnerb6bac512004-02-25 06:13:04 +00003747
Chris Lattner985fe3d2004-02-25 03:45:50 +00003748 // While there are GEP indexes that can be folded into the current address,
3749 // keep processing them.
3750 while (!GEPTypes.empty()) {
3751 if (const StructType *StTy = dyn_cast<StructType>(GEPTypes.back())) {
3752 // It's a struct access. CUI is the index into the structure,
3753 // which names the field. This index must have unsigned type.
3754 const ConstantUInt *CUI = cast<ConstantUInt>(GEPOps.back());
3755
3756 // Use the TargetData structure to pick out what the layout of the
3757 // structure is in memory. Since the structure index must be constant, we
3758 // can get its value and use it to find the right byte offset from the
3759 // StructLayout class's list of structure member offsets.
Reid Spencerfc989e12004-08-30 00:13:26 +00003760 AM.Disp += TD.getStructLayout(StTy)->MemberOffsets[CUI->getValue()];
Chris Lattner985fe3d2004-02-25 03:45:50 +00003761 GEPOps.pop_back(); // Consume a GEP operand
3762 GEPTypes.pop_back();
3763 } else {
3764 // It's an array or pointer access: [ArraySize x ElementType].
3765 const SequentialType *SqTy = cast<SequentialType>(GEPTypes.back());
3766 Value *idx = GEPOps.back();
3767
3768 // idx is the index into the array. Unlike with structure
3769 // indices, we may not know its actual value at code-generation
3770 // time.
Chris Lattner985fe3d2004-02-25 03:45:50 +00003771
3772 // If idx is a constant, fold it into the offset.
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003773 unsigned TypeSize = TD.getTypeSize(SqTy->getElementType());
Chris Lattner985fe3d2004-02-25 03:45:50 +00003774 if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(idx)) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003775 AM.Disp += TypeSize*CSI->getValue();
Chris Lattner28977af2004-04-05 01:30:19 +00003776 } else if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(idx)) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003777 AM.Disp += TypeSize*CUI->getValue();
Chris Lattner985fe3d2004-02-25 03:45:50 +00003778 } else {
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003779 // If the index reg is already taken, we can't handle this index.
Reid Spencerfc989e12004-08-30 00:13:26 +00003780 if (AM.IndexReg) return;
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003781
3782 // If this is a size that we can handle, then add the index as
3783 switch (TypeSize) {
3784 case 1: case 2: case 4: case 8:
3785 // These are all acceptable scales on X86.
Reid Spencerfc989e12004-08-30 00:13:26 +00003786 AM.Scale = TypeSize;
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003787 break;
3788 default:
3789 // Otherwise, we can't handle this scale
3790 return;
3791 }
3792
3793 if (CastInst *CI = dyn_cast<CastInst>(idx))
3794 if (CI->getOperand(0)->getType() == Type::IntTy ||
3795 CI->getOperand(0)->getType() == Type::UIntTy)
3796 idx = CI->getOperand(0);
3797
Reid Spencerfc989e12004-08-30 00:13:26 +00003798 AM.IndexReg = MBB ? getReg(idx, MBB, IP) : 1;
Chris Lattner985fe3d2004-02-25 03:45:50 +00003799 }
3800
3801 GEPOps.pop_back(); // Consume a GEP operand
3802 GEPTypes.pop_back();
3803 }
3804 }
Chris Lattnerb6bac512004-02-25 06:13:04 +00003805
Chris Lattnerdf040972004-05-23 21:23:12 +00003806 // GEPTypes is empty, which means we have a single operand left. Set it as
3807 // the base register.
Chris Lattnerb6bac512004-02-25 06:13:04 +00003808 //
Reid Spencerfc989e12004-08-30 00:13:26 +00003809 assert(AM.Base.Reg == 0);
Chris Lattnerdf040972004-05-23 21:23:12 +00003810
Reid Spencerfc989e12004-08-30 00:13:26 +00003811 if (AllocaInst *AI = dyn_castFixedAlloca(GEPOps.back())) {
3812 AM.BaseType = X86AddressMode::FrameIndexBase;
3813 AM.Base.FrameIndex = getFixedSizedAllocaFI(AI);
Chris Lattnerdf040972004-05-23 21:23:12 +00003814 GEPOps.pop_back();
3815 return;
Reid Spencerfc989e12004-08-30 00:13:26 +00003816 }
3817
Chris Lattner358a9022004-10-15 05:05:29 +00003818 if (GlobalValue *GV = dyn_cast<GlobalValue>(GEPOps.back())) {
3819 AM.GV = GV;
3820 GEPOps.pop_back();
3821 return;
Chris Lattnerdf040972004-05-23 21:23:12 +00003822 }
Chris Lattnerdf040972004-05-23 21:23:12 +00003823
Reid Spencerfc989e12004-08-30 00:13:26 +00003824 AM.Base.Reg = MBB ? getReg(GEPOps[0], MBB, IP) : 1;
Chris Lattnerb6bac512004-02-25 06:13:04 +00003825 GEPOps.pop_back(); // Consume the last GEP operand
Chris Lattner985fe3d2004-02-25 03:45:50 +00003826}
3827
3828
Chris Lattnerb6bac512004-02-25 06:13:04 +00003829/// isGEPFoldable - Return true if the specified GEP can be completely
3830/// folded into the addressing mode of a load/store or lea instruction.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003831bool X86ISel::isGEPFoldable(MachineBasicBlock *MBB,
3832 Value *Src, User::op_iterator IdxBegin,
3833 User::op_iterator IdxEnd, X86AddressMode &AM) {
Chris Lattner7ca04092004-02-22 17:35:42 +00003834
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003835 std::vector<Value*> GEPOps;
3836 GEPOps.resize(IdxEnd-IdxBegin+1);
3837 GEPOps[0] = Src;
3838 std::copy(IdxBegin, IdxEnd, GEPOps.begin()+1);
3839
Chris Lattnerdf040972004-05-23 21:23:12 +00003840 std::vector<const Type*>
3841 GEPTypes(gep_type_begin(Src->getType(), IdxBegin, IdxEnd),
3842 gep_type_end(Src->getType(), IdxBegin, IdxEnd));
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003843
Chris Lattnerb6bac512004-02-25 06:13:04 +00003844 MachineBasicBlock::iterator IP;
3845 if (MBB) IP = MBB->end();
Reid Spencerfc989e12004-08-30 00:13:26 +00003846 getGEPIndex(MBB, IP, GEPOps, GEPTypes, AM);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003847
3848 // We can fold it away iff the getGEPIndex call eliminated all operands.
3849 return GEPOps.empty();
3850}
3851
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003852void X86ISel::emitGEPOperation(MachineBasicBlock *MBB,
3853 MachineBasicBlock::iterator IP,
3854 Value *Src, User::op_iterator IdxBegin,
3855 User::op_iterator IdxEnd, unsigned TargetReg) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00003856 const TargetData &TD = TM.getTargetData();
Chris Lattnerb6bac512004-02-25 06:13:04 +00003857
Chris Lattnerd2995df2004-07-15 00:58:53 +00003858 // If this is a getelementptr null, with all constant integer indices, just
3859 // replace it with TargetReg = 42.
3860 if (isa<ConstantPointerNull>(Src)) {
3861 User::op_iterator I = IdxBegin;
3862 for (; I != IdxEnd; ++I)
3863 if (!isa<ConstantInt>(*I))
3864 break;
3865 if (I == IdxEnd) { // All constant indices
3866 unsigned Offset = TD.getIndexedOffset(Src->getType(),
3867 std::vector<Value*>(IdxBegin, IdxEnd));
3868 BuildMI(*MBB, IP, X86::MOV32ri, 1, TargetReg).addImm(Offset);
3869 return;
3870 }
3871 }
3872
Chris Lattnerb6bac512004-02-25 06:13:04 +00003873 std::vector<Value*> GEPOps;
3874 GEPOps.resize(IdxEnd-IdxBegin+1);
3875 GEPOps[0] = Src;
3876 std::copy(IdxBegin, IdxEnd, GEPOps.begin()+1);
3877
3878 std::vector<const Type*> GEPTypes;
3879 GEPTypes.assign(gep_type_begin(Src->getType(), IdxBegin, IdxEnd),
3880 gep_type_end(Src->getType(), IdxBegin, IdxEnd));
Chris Lattner985fe3d2004-02-25 03:45:50 +00003881
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003882 // Keep emitting instructions until we consume the entire GEP instruction.
3883 while (!GEPOps.empty()) {
3884 unsigned OldSize = GEPOps.size();
Reid Spencerfc989e12004-08-30 00:13:26 +00003885 X86AddressMode AM;
3886 getGEPIndex(MBB, IP, GEPOps, GEPTypes, AM);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003887
Chris Lattner985fe3d2004-02-25 03:45:50 +00003888 if (GEPOps.size() != OldSize) {
3889 // getGEPIndex consumed some of the input. Build an LEA instruction here.
Chris Lattnerb6bac512004-02-25 06:13:04 +00003890 unsigned NextTarget = 0;
3891 if (!GEPOps.empty()) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003892 assert(AM.Base.Reg == 0 &&
Chris Lattnerb6bac512004-02-25 06:13:04 +00003893 "getGEPIndex should have left the base register open for chaining!");
Reid Spencerfc989e12004-08-30 00:13:26 +00003894 NextTarget = AM.Base.Reg = makeAnotherReg(Type::UIntTy);
Chris Lattner985fe3d2004-02-25 03:45:50 +00003895 }
Chris Lattnerb6bac512004-02-25 06:13:04 +00003896
Reid Spencerfc989e12004-08-30 00:13:26 +00003897 if (AM.BaseType == X86AddressMode::RegBase &&
Chris Lattner358a9022004-10-15 05:05:29 +00003898 AM.IndexReg == 0 && AM.Disp == 0 && !AM.GV)
Reid Spencerfc989e12004-08-30 00:13:26 +00003899 BuildMI(*MBB, IP, X86::MOV32rr, 1, TargetReg).addReg(AM.Base.Reg);
Chris Lattner358a9022004-10-15 05:05:29 +00003900 else if (AM.BaseType == X86AddressMode::RegBase && AM.Base.Reg == 0 &&
3901 AM.IndexReg == 0 && AM.Disp == 0)
3902 BuildMI(*MBB, IP, X86::MOV32ri, 1, TargetReg).addGlobalAddress(AM.GV);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003903 else
Reid Spencerfc989e12004-08-30 00:13:26 +00003904 addFullAddress(BuildMI(*MBB, IP, X86::LEA32r, 5, TargetReg), AM);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003905 --IP;
3906 TargetReg = NextTarget;
Chris Lattner985fe3d2004-02-25 03:45:50 +00003907 } else if (GEPTypes.empty()) {
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003908 // The getGEPIndex operation didn't want to build an LEA. Check to see if
3909 // all operands are consumed but the base pointer. If so, just load it
3910 // into the register.
Chris Lattner7ca04092004-02-22 17:35:42 +00003911 if (GlobalValue *GV = dyn_cast<GlobalValue>(GEPOps[0])) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003912 BuildMI(*MBB, IP, X86::MOV32ri, 1, TargetReg).addGlobalAddress(GV);
Chris Lattner7ca04092004-02-22 17:35:42 +00003913 } else {
3914 unsigned BaseReg = getReg(GEPOps[0], MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003915 BuildMI(*MBB, IP, X86::MOV32rr, 1, TargetReg).addReg(BaseReg);
Chris Lattner7ca04092004-02-22 17:35:42 +00003916 }
3917 break; // we are now done
Chris Lattnerb6bac512004-02-25 06:13:04 +00003918
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003919 } else {
Brian Gaeke20244b72002-12-12 15:33:40 +00003920 // It's an array or pointer access: [ArraySize x ElementType].
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003921 const SequentialType *SqTy = cast<SequentialType>(GEPTypes.back());
3922 Value *idx = GEPOps.back();
3923 GEPOps.pop_back(); // Consume a GEP operand
3924 GEPTypes.pop_back();
Chris Lattner8a307e82002-12-16 19:32:50 +00003925
Chris Lattner28977af2004-04-05 01:30:19 +00003926 // Many GEP instructions use a [cast (int/uint) to LongTy] as their
Chris Lattnerf5854472003-06-21 16:01:24 +00003927 // operand on X86. Handle this case directly now...
3928 if (CastInst *CI = dyn_cast<CastInst>(idx))
3929 if (CI->getOperand(0)->getType() == Type::IntTy ||
3930 CI->getOperand(0)->getType() == Type::UIntTy)
3931 idx = CI->getOperand(0);
3932
Chris Lattner3e130a22003-01-13 00:32:26 +00003933 // We want to add BaseReg to(idxReg * sizeof ElementType). First, we
Chris Lattner8a307e82002-12-16 19:32:50 +00003934 // must find the size of the pointed-to type (Not coincidentally, the next
3935 // type is the type of the elements in the array).
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003936 const Type *ElTy = SqTy->getElementType();
3937 unsigned elementSize = TD.getTypeSize(ElTy);
Chris Lattner8a307e82002-12-16 19:32:50 +00003938
3939 // If idxReg is a constant, we don't need to perform the multiply!
Chris Lattner28977af2004-04-05 01:30:19 +00003940 if (ConstantInt *CSI = dyn_cast<ConstantInt>(idx)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00003941 if (!CSI->isNullValue()) {
Chris Lattner28977af2004-04-05 01:30:19 +00003942 unsigned Offset = elementSize*CSI->getRawValue();
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003943 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003944 BuildMI(*MBB, IP, X86::ADD32ri, 2, TargetReg)
Chris Lattneree352852004-02-29 07:22:16 +00003945 .addReg(Reg).addImm(Offset);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003946 --IP; // Insert the next instruction before this one.
3947 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00003948 }
3949 } else if (elementSize == 1) {
3950 // If the element size is 1, we don't have to multiply, just add
3951 unsigned idxReg = getReg(idx, MBB, IP);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003952 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003953 BuildMI(*MBB, IP, X86::ADD32rr, 2,TargetReg).addReg(Reg).addReg(idxReg);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003954 --IP; // Insert the next instruction before this one.
3955 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00003956 } else {
3957 unsigned idxReg = getReg(idx, MBB, IP);
3958 unsigned OffsetReg = makeAnotherReg(Type::UIntTy);
Chris Lattnerb2acc512003-10-19 21:09:10 +00003959
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003960 // Make sure we can back the iterator up to point to the first
3961 // instruction emitted.
3962 MachineBasicBlock::iterator BeforeIt = IP;
3963 if (IP == MBB->begin())
3964 BeforeIt = MBB->end();
3965 else
3966 --BeforeIt;
Chris Lattnerb2acc512003-10-19 21:09:10 +00003967 doMultiplyConst(MBB, IP, OffsetReg, Type::IntTy, idxReg, elementSize);
3968
Chris Lattner8a307e82002-12-16 19:32:50 +00003969 // Emit an ADD to add OffsetReg to the basePtr.
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003970 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003971 BuildMI(*MBB, IP, X86::ADD32rr, 2, TargetReg)
Chris Lattneree352852004-02-29 07:22:16 +00003972 .addReg(Reg).addReg(OffsetReg);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003973
3974 // Step to the first instruction of the multiply.
3975 if (BeforeIt == MBB->end())
3976 IP = MBB->begin();
3977 else
3978 IP = ++BeforeIt;
3979
3980 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00003981 }
Brian Gaeke20244b72002-12-12 15:33:40 +00003982 }
Brian Gaeke20244b72002-12-12 15:33:40 +00003983 }
Brian Gaeke20244b72002-12-12 15:33:40 +00003984}
3985
Chris Lattner065faeb2002-12-28 20:24:02 +00003986/// visitAllocaInst - If this is a fixed size alloca, allocate space from the
3987/// frame manager, otherwise do it the hard way.
3988///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003989void X86ISel::visitAllocaInst(AllocaInst &I) {
Chris Lattner9f1b5312004-05-13 15:12:43 +00003990 // If this is a fixed size alloca in the entry block for the function, we
3991 // statically stack allocate the space, so we don't need to do anything here.
3992 //
Chris Lattnercb2fd552004-05-13 07:40:27 +00003993 if (dyn_castFixedAlloca(&I)) return;
Chris Lattner9f1b5312004-05-13 15:12:43 +00003994
Brian Gaekee48ec012002-12-13 06:46:31 +00003995 // Find the data size of the alloca inst's getAllocatedType.
Chris Lattner065faeb2002-12-28 20:24:02 +00003996 const Type *Ty = I.getAllocatedType();
3997 unsigned TySize = TM.getTargetData().getTypeSize(Ty);
3998
Chris Lattner065faeb2002-12-28 20:24:02 +00003999 // Create a register to hold the temporary result of multiplying the type size
4000 // constant by the variable amount.
4001 unsigned TotalSizeReg = makeAnotherReg(Type::UIntTy);
4002 unsigned SrcReg1 = getReg(I.getArraySize());
Chris Lattner065faeb2002-12-28 20:24:02 +00004003
4004 // TotalSizeReg = mul <numelements>, <TypeSize>
4005 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattnerb2acc512003-10-19 21:09:10 +00004006 doMultiplyConst(BB, MBBI, TotalSizeReg, Type::UIntTy, SrcReg1, TySize);
Chris Lattner065faeb2002-12-28 20:24:02 +00004007
4008 // AddedSize = add <TotalSizeReg>, 15
4009 unsigned AddedSizeReg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00004010 BuildMI(BB, X86::ADD32ri, 2, AddedSizeReg).addReg(TotalSizeReg).addImm(15);
Chris Lattner065faeb2002-12-28 20:24:02 +00004011
4012 // AlignedSize = and <AddedSize>, ~15
4013 unsigned AlignedSize = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00004014 BuildMI(BB, X86::AND32ri, 2, AlignedSize).addReg(AddedSizeReg).addImm(~15);
Chris Lattner065faeb2002-12-28 20:24:02 +00004015
Brian Gaekee48ec012002-12-13 06:46:31 +00004016 // Subtract size from stack pointer, thereby allocating some space.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00004017 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(AlignedSize);
Chris Lattner065faeb2002-12-28 20:24:02 +00004018
Brian Gaekee48ec012002-12-13 06:46:31 +00004019 // Put a pointer to the space into the result register, by copying
4020 // the stack pointer.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00004021 BuildMI(BB, X86::MOV32rr, 1, getReg(I)).addReg(X86::ESP);
Chris Lattner065faeb2002-12-28 20:24:02 +00004022
Misha Brukman48196b32003-05-03 02:18:17 +00004023 // Inform the Frame Information that we have just allocated a variable-sized
Chris Lattner065faeb2002-12-28 20:24:02 +00004024 // object.
4025 F->getFrameInfo()->CreateVariableSizedObject();
Brian Gaeke20244b72002-12-12 15:33:40 +00004026}
Chris Lattner3e130a22003-01-13 00:32:26 +00004027
4028/// visitMallocInst - Malloc instructions are code generated into direct calls
4029/// to the library malloc.
4030///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00004031void X86ISel::visitMallocInst(MallocInst &I) {
Chris Lattner3e130a22003-01-13 00:32:26 +00004032 unsigned AllocSize = TM.getTargetData().getTypeSize(I.getAllocatedType());
4033 unsigned Arg;
4034
4035 if (ConstantUInt *C = dyn_cast<ConstantUInt>(I.getOperand(0))) {
4036 Arg = getReg(ConstantUInt::get(Type::UIntTy, C->getValue() * AllocSize));
4037 } else {
4038 Arg = makeAnotherReg(Type::UIntTy);
Chris Lattnerb2acc512003-10-19 21:09:10 +00004039 unsigned Op0Reg = getReg(I.getOperand(0));
Chris Lattner3e130a22003-01-13 00:32:26 +00004040 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattnerb2acc512003-10-19 21:09:10 +00004041 doMultiplyConst(BB, MBBI, Arg, Type::UIntTy, Op0Reg, AllocSize);
Chris Lattner3e130a22003-01-13 00:32:26 +00004042 }
4043
4044 std::vector<ValueRecord> Args;
4045 Args.push_back(ValueRecord(Arg, Type::UIntTy));
4046 MachineInstr *TheCall = BuildMI(X86::CALLpcrel32,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00004047 1).addExternalSymbol("malloc", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00004048 doCall(ValueRecord(getReg(I), I.getType()), TheCall, Args);
4049}
4050
4051
4052/// visitFreeInst - Free instructions are code gen'd to call the free libc
4053/// function.
4054///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00004055void X86ISel::visitFreeInst(FreeInst &I) {
Chris Lattner3e130a22003-01-13 00:32:26 +00004056 std::vector<ValueRecord> Args;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00004057 Args.push_back(ValueRecord(I.getOperand(0)));
Chris Lattner3e130a22003-01-13 00:32:26 +00004058 MachineInstr *TheCall = BuildMI(X86::CALLpcrel32,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00004059 1).addExternalSymbol("free", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00004060 doCall(ValueRecord(0, Type::VoidTy), TheCall, Args);
4061}
4062
Chris Lattnerd281de22003-07-26 23:49:58 +00004063/// createX86SimpleInstructionSelector - This pass converts an LLVM function
Chris Lattnerb4f68ed2002-10-29 22:37:54 +00004064/// into a machine code representation is a very simple peep-hole fashion. The
Chris Lattner72614082002-10-25 22:55:53 +00004065/// generated code sucks but the implementation is nice and simple.
4066///
Chris Lattnerf70e0c22003-12-28 21:23:38 +00004067FunctionPass *llvm::createX86SimpleInstructionSelector(TargetMachine &TM) {
Misha Brukmaneae1bf12004-09-21 18:21:21 +00004068 return new X86ISel(TM);
Chris Lattner72614082002-10-25 22:55:53 +00004069}