blob: 19c61080db62d96017401f90a7d27c12d5ec3554 [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"
Chris Lattner986618e2004-02-22 19:47:26 +000031#include "Support/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 {
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000077 struct ISel : public FunctionPass, InstVisitor<ISel> {
78 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
Chris Lattnerf70e0c22003-12-28 21:23:38 +000093 ISel(TargetMachine &tm) : TM(tm), F(0), BB(0) {}
Chris Lattner72614082002-10-25 22:55:53 +000094
95 /// runOnFunction - Top level implementation of instruction selection for
96 /// the entire function.
97 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000098 bool runOnFunction(Function &Fn) {
Chris Lattner44827152003-12-28 09:47:19 +000099 // First pass over the function, lower any unknown intrinsic functions
100 // with the IntrinsicLowering class.
101 LowerUnknownIntrinsicFunctionCalls(Fn);
102
Chris Lattner36b36032002-10-29 23:40:58 +0000103 F = &MachineFunction::construct(&Fn, TM);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000104
Chris Lattner065faeb2002-12-28 20:24:02 +0000105 // Create all of the machine basic blocks for the function...
Chris Lattner333b2fa2002-12-13 10:09:43 +0000106 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
107 F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I));
108
Chris Lattner14aa7fe2002-12-16 22:54:46 +0000109 BB = &F->front();
Chris Lattnerdbd73722003-05-06 21:32:22 +0000110
Chris Lattner0e5b79c2004-02-15 01:04:03 +0000111 // Set up a frame object for the return address. This is used by the
112 // llvm.returnaddress & llvm.frameaddress intrinisics.
113 ReturnAddressIndex = F->getFrameInfo()->CreateFixedObject(4, -4);
114
Chris Lattnerdbd73722003-05-06 21:32:22 +0000115 // Copy incoming arguments off of the stack...
Chris Lattner065faeb2002-12-28 20:24:02 +0000116 LoadArgumentsToVirtualRegs(Fn);
Chris Lattner14aa7fe2002-12-16 22:54:46 +0000117
Chris Lattner333b2fa2002-12-13 10:09:43 +0000118 // Instruction select everything except PHI nodes
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000119 visit(Fn);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000120
121 // Select the PHI nodes
122 SelectPHINodes();
123
Chris Lattner986618e2004-02-22 19:47:26 +0000124 // Insert the FP_REG_KILL instructions into blocks that need them.
125 InsertFPRegKills();
126
Chris Lattner72614082002-10-25 22:55:53 +0000127 RegMap.clear();
Chris Lattner333b2fa2002-12-13 10:09:43 +0000128 MBBMap.clear();
Chris Lattnercb2fd552004-05-13 07:40:27 +0000129 AllocaMap.clear();
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000130 F = 0;
Chris Lattner2a865b02003-07-26 23:05:37 +0000131 // We always build a machine code representation for the function
132 return true;
Chris Lattner72614082002-10-25 22:55:53 +0000133 }
134
Chris Lattnerf0eb7be2002-12-15 21:13:40 +0000135 virtual const char *getPassName() const {
136 return "X86 Simple Instruction Selection";
137 }
138
Chris Lattner72614082002-10-25 22:55:53 +0000139 /// visitBasicBlock - This method is called when we are visiting a new basic
Chris Lattner33f53b52002-10-29 20:48:56 +0000140 /// block. This simply creates a new MachineBasicBlock to emit code into
141 /// and adds it to the current MachineFunction. Subsequent visit* for
142 /// instructions will be invoked for all instructions in the basic block.
Chris Lattner72614082002-10-25 22:55:53 +0000143 ///
144 void visitBasicBlock(BasicBlock &LLVM_BB) {
Chris Lattner333b2fa2002-12-13 10:09:43 +0000145 BB = MBBMap[&LLVM_BB];
Chris Lattner72614082002-10-25 22:55:53 +0000146 }
147
Chris Lattner44827152003-12-28 09:47:19 +0000148 /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
149 /// function, lowering any calls to unknown intrinsic functions into the
150 /// equivalent LLVM code.
Misha Brukman538607f2004-03-01 23:53:11 +0000151 ///
Chris Lattner44827152003-12-28 09:47:19 +0000152 void LowerUnknownIntrinsicFunctionCalls(Function &F);
153
Chris Lattner065faeb2002-12-28 20:24:02 +0000154 /// LoadArgumentsToVirtualRegs - Load all of the arguments to this function
155 /// from the stack into virtual registers.
156 ///
157 void LoadArgumentsToVirtualRegs(Function &F);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000158
159 /// SelectPHINodes - Insert machine code to generate phis. This is tricky
160 /// because we have to generate our sources into the source basic blocks,
161 /// not the current one.
162 ///
163 void SelectPHINodes();
164
Chris Lattner986618e2004-02-22 19:47:26 +0000165 /// InsertFPRegKills - Insert FP_REG_KILL instructions into basic blocks
166 /// that need them. This only occurs due to the floating point stackifier
167 /// not being aggressive enough to handle arbitrary global stackification.
168 ///
169 void InsertFPRegKills();
170
Chris Lattner72614082002-10-25 22:55:53 +0000171 // Visitation methods for various instructions. These methods simply emit
172 // fixed X86 code for each instruction.
173 //
Brian Gaekefa8d5712002-11-22 11:07:01 +0000174
175 // Control flow operators
Chris Lattner72614082002-10-25 22:55:53 +0000176 void visitReturnInst(ReturnInst &RI);
Chris Lattner2df035b2002-11-02 19:27:56 +0000177 void visitBranchInst(BranchInst &BI);
Chris Lattner3e130a22003-01-13 00:32:26 +0000178
179 struct ValueRecord {
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000180 Value *Val;
Chris Lattner3e130a22003-01-13 00:32:26 +0000181 unsigned Reg;
182 const Type *Ty;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000183 ValueRecord(unsigned R, const Type *T) : Val(0), Reg(R), Ty(T) {}
184 ValueRecord(Value *V) : Val(V), Reg(0), Ty(V->getType()) {}
Chris Lattner3e130a22003-01-13 00:32:26 +0000185 };
186 void doCall(const ValueRecord &Ret, MachineInstr *CallMI,
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000187 const std::vector<ValueRecord> &Args);
Brian Gaekefa8d5712002-11-22 11:07:01 +0000188 void visitCallInst(CallInst &I);
Brian Gaeked0fde302003-11-11 22:41:34 +0000189 void visitIntrinsicCall(Intrinsic::ID ID, CallInst &I);
Chris Lattnere2954c82002-11-02 20:04:26 +0000190
191 // Arithmetic operators
Chris Lattnerf01729e2002-11-02 20:54:46 +0000192 void visitSimpleBinary(BinaryOperator &B, unsigned OpcodeClass);
Chris Lattner68aad932002-11-02 20:13:22 +0000193 void visitAdd(BinaryOperator &B) { visitSimpleBinary(B, 0); }
194 void visitSub(BinaryOperator &B) { visitSimpleBinary(B, 1); }
Chris Lattnerca9671d2002-11-02 20:28:58 +0000195 void visitMul(BinaryOperator &B);
Chris Lattnere2954c82002-11-02 20:04:26 +0000196
Chris Lattnerf01729e2002-11-02 20:54:46 +0000197 void visitDiv(BinaryOperator &B) { visitDivRem(B); }
198 void visitRem(BinaryOperator &B) { visitDivRem(B); }
199 void visitDivRem(BinaryOperator &B);
200
Chris Lattnere2954c82002-11-02 20:04:26 +0000201 // Bitwise operators
Chris Lattner68aad932002-11-02 20:13:22 +0000202 void visitAnd(BinaryOperator &B) { visitSimpleBinary(B, 2); }
203 void visitOr (BinaryOperator &B) { visitSimpleBinary(B, 3); }
204 void visitXor(BinaryOperator &B) { visitSimpleBinary(B, 4); }
Chris Lattnere2954c82002-11-02 20:04:26 +0000205
Chris Lattner6d40c192003-01-16 16:43:00 +0000206 // Comparison operators...
207 void visitSetCondInst(SetCondInst &I);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000208 unsigned EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
209 MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000210 MachineBasicBlock::iterator MBBI);
Chris Lattner12d96a02004-03-30 21:22:00 +0000211 void visitSelectInst(SelectInst &SI);
212
Chris Lattnerb2acc512003-10-19 21:09:10 +0000213
Chris Lattner6fc3c522002-11-17 21:11:55 +0000214 // Memory Instructions
215 void visitLoadInst(LoadInst &I);
216 void visitStoreInst(StoreInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000217 void visitGetElementPtrInst(GetElementPtrInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000218 void visitAllocaInst(AllocaInst &I);
Chris Lattner3e130a22003-01-13 00:32:26 +0000219 void visitMallocInst(MallocInst &I);
220 void visitFreeInst(FreeInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000221
Chris Lattnere2954c82002-11-02 20:04:26 +0000222 // Other operators
Brian Gaekea1719c92002-10-31 23:03:59 +0000223 void visitShiftInst(ShiftInst &I);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000224 void visitPHINode(PHINode &I) {} // PHI nodes handled by second pass
Brian Gaekefa8d5712002-11-22 11:07:01 +0000225 void visitCastInst(CastInst &I);
Chris Lattner73815062003-10-18 05:56:40 +0000226 void visitVANextInst(VANextInst &I);
227 void visitVAArgInst(VAArgInst &I);
Chris Lattner72614082002-10-25 22:55:53 +0000228
229 void visitInstruction(Instruction &I) {
230 std::cerr << "Cannot instruction select: " << I;
231 abort();
232 }
233
Brian Gaeke95780cc2002-12-13 07:56:18 +0000234 /// promote32 - Make a value 32-bits wide, and put it somewhere.
Chris Lattner3e130a22003-01-13 00:32:26 +0000235 ///
236 void promote32(unsigned targetReg, const ValueRecord &VR);
237
Chris Lattner721d2d42004-03-08 01:18:36 +0000238 /// getAddressingMode - Get the addressing mode to use to address the
239 /// specified value. The returned value should be used with addFullAddress.
Reid Spencerfc989e12004-08-30 00:13:26 +0000240 void getAddressingMode(Value *Addr, X86AddressMode &AM);
Chris Lattner721d2d42004-03-08 01:18:36 +0000241
242
243 /// getGEPIndex - This is used to fold GEP instructions into X86 addressing
244 /// expressions.
Chris Lattnerb6bac512004-02-25 06:13:04 +0000245 void getGEPIndex(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
246 std::vector<Value*> &GEPOps,
Reid Spencerfc989e12004-08-30 00:13:26 +0000247 std::vector<const Type*> &GEPTypes,
248 X86AddressMode &AM);
Chris Lattnerb6bac512004-02-25 06:13:04 +0000249
250 /// isGEPFoldable - Return true if the specified GEP can be completely
251 /// folded into the addressing mode of a load/store or lea instruction.
252 bool isGEPFoldable(MachineBasicBlock *MBB,
253 Value *Src, User::op_iterator IdxBegin,
Reid Spencerfc989e12004-08-30 00:13:26 +0000254 User::op_iterator IdxEnd, X86AddressMode &AM);
Chris Lattnerb6bac512004-02-25 06:13:04 +0000255
Chris Lattner3e130a22003-01-13 00:32:26 +0000256 /// emitGEPOperation - Common code shared between visitGetElementPtrInst and
257 /// constant expression GEP support.
258 ///
Chris Lattner827832c2004-02-22 17:05:38 +0000259 void emitGEPOperation(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
Chris Lattner333b2fa2002-12-13 10:09:43 +0000260 Value *Src, User::op_iterator IdxBegin,
Chris Lattnerc0812d82002-12-13 06:56:29 +0000261 User::op_iterator IdxEnd, unsigned TargetReg);
262
Chris Lattner548f61d2003-04-23 17:22:12 +0000263 /// emitCastOperation - Common code shared between visitCastInst and
264 /// constant expression cast support.
Misha Brukman538607f2004-03-01 23:53:11 +0000265 ///
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000266 void emitCastOperation(MachineBasicBlock *BB,MachineBasicBlock::iterator IP,
Chris Lattner548f61d2003-04-23 17:22:12 +0000267 Value *Src, const Type *DestTy, unsigned TargetReg);
268
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000269 /// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
270 /// and constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000271 ///
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000272 void emitSimpleBinaryOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000273 MachineBasicBlock::iterator IP,
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000274 Value *Op0, Value *Op1,
275 unsigned OperatorClass, unsigned TargetReg);
276
Chris Lattner6621ed92004-04-11 21:23:56 +0000277 /// emitBinaryFPOperation - This method handles emission of floating point
278 /// Add (0), Sub (1), Mul (2), and Div (3) operations.
279 void emitBinaryFPOperation(MachineBasicBlock *BB,
280 MachineBasicBlock::iterator IP,
281 Value *Op0, Value *Op1,
282 unsigned OperatorClass, unsigned TargetReg);
283
Chris Lattner462fa822004-04-11 20:56:28 +0000284 void emitMultiply(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
285 Value *Op0, Value *Op1, unsigned TargetReg);
286
287 void doMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI,
288 unsigned DestReg, const Type *DestTy,
289 unsigned Op0Reg, unsigned Op1Reg);
290 void doMultiplyConst(MachineBasicBlock *MBB,
291 MachineBasicBlock::iterator MBBI,
292 unsigned DestReg, const Type *DestTy,
293 unsigned Op0Reg, unsigned Op1Val);
294
Chris Lattnercadff442003-10-23 17:21:43 +0000295 void emitDivRemOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000296 MachineBasicBlock::iterator IP,
Chris Lattner462fa822004-04-11 20:56:28 +0000297 Value *Op0, Value *Op1, bool isDiv,
298 unsigned TargetReg);
Chris Lattnercadff442003-10-23 17:21:43 +0000299
Chris Lattner58c41fe2003-08-24 19:19:47 +0000300 /// emitSetCCOperation - Common code shared between visitSetCondInst and
301 /// constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000302 ///
Chris Lattner58c41fe2003-08-24 19:19:47 +0000303 void emitSetCCOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000304 MachineBasicBlock::iterator IP,
Chris Lattner58c41fe2003-08-24 19:19:47 +0000305 Value *Op0, Value *Op1, unsigned Opcode,
306 unsigned TargetReg);
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000307
308 /// emitShiftOperation - Common code shared between visitShiftInst and
309 /// constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000310 ///
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000311 void emitShiftOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000312 MachineBasicBlock::iterator IP,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000313 Value *Op, Value *ShiftAmount, bool isLeftShift,
314 const Type *ResultTy, unsigned DestReg);
315
Chris Lattner12d96a02004-03-30 21:22:00 +0000316 /// emitSelectOperation - Common code shared between visitSelectInst and the
317 /// constant expression support.
318 void emitSelectOperation(MachineBasicBlock *MBB,
319 MachineBasicBlock::iterator IP,
320 Value *Cond, Value *TrueVal, Value *FalseVal,
321 unsigned DestReg);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000322
Chris Lattnerc5291f52002-10-27 21:16:59 +0000323 /// copyConstantToRegister - Output the instructions required to put the
324 /// specified constant into the specified register.
325 ///
Chris Lattner8a307e82002-12-16 19:32:50 +0000326 void copyConstantToRegister(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000327 MachineBasicBlock::iterator MBBI,
Chris Lattner8a307e82002-12-16 19:32:50 +0000328 Constant *C, unsigned Reg);
Chris Lattnerc5291f52002-10-27 21:16:59 +0000329
Chris Lattner01cdb1b2004-06-11 05:33:49 +0000330 void emitUCOMr(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI,
331 unsigned LHS, unsigned RHS);
332
Chris Lattner3e130a22003-01-13 00:32:26 +0000333 /// makeAnotherReg - This method returns the next register number we haven't
334 /// yet used.
335 ///
336 /// Long values are handled somewhat specially. They are always allocated
337 /// as pairs of 32 bit integer values. The register number returned is the
338 /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits
339 /// of the long value.
340 ///
Chris Lattnerc0812d82002-12-13 06:56:29 +0000341 unsigned makeAnotherReg(const Type *Ty) {
Chris Lattner7db1fa92003-07-30 05:33:48 +0000342 assert(dynamic_cast<const X86RegisterInfo*>(TM.getRegisterInfo()) &&
343 "Current target doesn't have X86 reg info??");
344 const X86RegisterInfo *MRI =
345 static_cast<const X86RegisterInfo*>(TM.getRegisterInfo());
Chris Lattner3e130a22003-01-13 00:32:26 +0000346 if (Ty == Type::LongTy || Ty == Type::ULongTy) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000347 const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy);
348 // Create the lower part
349 F->getSSARegMap()->createVirtualRegister(RC);
350 // Create the upper part.
351 return F->getSSARegMap()->createVirtualRegister(RC)-1;
Chris Lattner3e130a22003-01-13 00:32:26 +0000352 }
353
Chris Lattnerc0812d82002-12-13 06:56:29 +0000354 // Add the mapping of regnumber => reg class to MachineFunction
Chris Lattner7db1fa92003-07-30 05:33:48 +0000355 const TargetRegisterClass *RC = MRI->getRegClassForType(Ty);
Chris Lattner3e130a22003-01-13 00:32:26 +0000356 return F->getSSARegMap()->createVirtualRegister(RC);
Brian Gaeke20244b72002-12-12 15:33:40 +0000357 }
358
Chris Lattnercb2fd552004-05-13 07:40:27 +0000359 /// getReg - This method turns an LLVM value into a register number.
Chris Lattner72614082002-10-25 22:55:53 +0000360 ///
361 unsigned getReg(Value &V) { return getReg(&V); } // Allow references
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000362 unsigned getReg(Value *V) {
363 // Just append to the end of the current bb.
364 MachineBasicBlock::iterator It = BB->end();
365 return getReg(V, BB, It);
366 }
Brian Gaeke71794c02002-12-13 11:22:48 +0000367 unsigned getReg(Value *V, MachineBasicBlock *MBB,
Chris Lattnercb2fd552004-05-13 07:40:27 +0000368 MachineBasicBlock::iterator IPt);
Chris Lattner427aeb42004-04-11 19:21:59 +0000369
Chris Lattnercb2fd552004-05-13 07:40:27 +0000370 /// getFixedSizedAllocaFI - Return the frame index for a fixed sized alloca
371 /// that is to be statically allocated with the initial stack frame
372 /// adjustment.
373 unsigned getFixedSizedAllocaFI(AllocaInst *AI);
Chris Lattner72614082002-10-25 22:55:53 +0000374 };
375}
376
Chris Lattnercb2fd552004-05-13 07:40:27 +0000377/// dyn_castFixedAlloca - If the specified value is a fixed size alloca
378/// instruction in the entry block, return it. Otherwise, return a null
379/// pointer.
380static AllocaInst *dyn_castFixedAlloca(Value *V) {
381 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
382 BasicBlock *BB = AI->getParent();
383 if (isa<ConstantUInt>(AI->getArraySize()) && BB ==&BB->getParent()->front())
384 return AI;
385 }
386 return 0;
387}
388
389/// getReg - This method turns an LLVM value into a register number.
390///
391unsigned ISel::getReg(Value *V, MachineBasicBlock *MBB,
392 MachineBasicBlock::iterator IPt) {
393 // If this operand is a constant, emit the code to copy the constant into
394 // the register here...
Chris Lattnercb2fd552004-05-13 07:40:27 +0000395 if (Constant *C = dyn_cast<Constant>(V)) {
396 unsigned Reg = makeAnotherReg(V->getType());
397 copyConstantToRegister(MBB, IPt, C, Reg);
398 return Reg;
Chris Lattnercb2fd552004-05-13 07:40:27 +0000399 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
Chris Lattner8b486a12004-06-29 00:14:38 +0000400 // Do not emit noop casts at all, unless it's a double -> float cast.
401 if (getClassB(CI->getType()) == getClassB(CI->getOperand(0)->getType()) &&
402 (CI->getType() != Type::FloatTy ||
403 CI->getOperand(0)->getType() != Type::DoubleTy))
Chris Lattnercb2fd552004-05-13 07:40:27 +0000404 return getReg(CI->getOperand(0), MBB, IPt);
405 } else if (AllocaInst *AI = dyn_castFixedAlloca(V)) {
406 // If the alloca address couldn't be folded into the instruction addressing,
407 // emit an explicit LEA as appropriate.
408 unsigned Reg = makeAnotherReg(V->getType());
409 unsigned FI = getFixedSizedAllocaFI(AI);
410 addFrameReference(BuildMI(*MBB, IPt, X86::LEA32r, 4, Reg), FI);
411 return Reg;
412 }
413
414 unsigned &Reg = RegMap[V];
415 if (Reg == 0) {
416 Reg = makeAnotherReg(V->getType());
417 RegMap[V] = Reg;
418 }
419
420 return Reg;
421}
422
423/// getFixedSizedAllocaFI - Return the frame index for a fixed sized alloca
424/// that is to be statically allocated with the initial stack frame
425/// adjustment.
426unsigned ISel::getFixedSizedAllocaFI(AllocaInst *AI) {
427 // Already computed this?
428 std::map<AllocaInst*, unsigned>::iterator I = AllocaMap.lower_bound(AI);
429 if (I != AllocaMap.end() && I->first == AI) return I->second;
430
431 const Type *Ty = AI->getAllocatedType();
432 ConstantUInt *CUI = cast<ConstantUInt>(AI->getArraySize());
433 unsigned TySize = TM.getTargetData().getTypeSize(Ty);
434 TySize *= CUI->getValue(); // Get total allocated size...
435 unsigned Alignment = TM.getTargetData().getTypeAlignment(Ty);
436
437 // Create a new stack object using the frame manager...
438 int FrameIdx = F->getFrameInfo()->CreateStackObject(TySize, Alignment);
439 AllocaMap.insert(I, std::make_pair(AI, FrameIdx));
440 return FrameIdx;
441}
442
443
Chris Lattnerc5291f52002-10-27 21:16:59 +0000444/// copyConstantToRegister - Output the instructions required to put the
445/// specified constant into the specified register.
446///
Chris Lattner8a307e82002-12-16 19:32:50 +0000447void ISel::copyConstantToRegister(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000448 MachineBasicBlock::iterator IP,
Chris Lattner8a307e82002-12-16 19:32:50 +0000449 Constant *C, unsigned R) {
Chris Lattnerc0812d82002-12-13 06:56:29 +0000450 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000451 unsigned Class = 0;
452 switch (CE->getOpcode()) {
453 case Instruction::GetElementPtr:
Brian Gaeke68b1edc2002-12-16 04:23:29 +0000454 emitGEPOperation(MBB, IP, CE->getOperand(0),
Chris Lattner333b2fa2002-12-13 10:09:43 +0000455 CE->op_begin()+1, CE->op_end(), R);
Chris Lattnerc0812d82002-12-13 06:56:29 +0000456 return;
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000457 case Instruction::Cast:
Chris Lattner548f61d2003-04-23 17:22:12 +0000458 emitCastOperation(MBB, IP, CE->getOperand(0), CE->getType(), R);
Chris Lattner4b12cde2003-04-21 21:33:44 +0000459 return;
Chris Lattnerc0812d82002-12-13 06:56:29 +0000460
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000461 case Instruction::Xor: ++Class; // FALL THROUGH
462 case Instruction::Or: ++Class; // FALL THROUGH
463 case Instruction::And: ++Class; // FALL THROUGH
464 case Instruction::Sub: ++Class; // FALL THROUGH
465 case Instruction::Add:
466 emitSimpleBinaryOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
467 Class, R);
468 return;
469
Chris Lattner462fa822004-04-11 20:56:28 +0000470 case Instruction::Mul:
471 emitMultiply(MBB, IP, CE->getOperand(0), CE->getOperand(1), R);
Chris Lattnercadff442003-10-23 17:21:43 +0000472 return;
Chris Lattner462fa822004-04-11 20:56:28 +0000473
Chris Lattnercadff442003-10-23 17:21:43 +0000474 case Instruction::Div:
Chris Lattner462fa822004-04-11 20:56:28 +0000475 case Instruction::Rem:
476 emitDivRemOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
477 CE->getOpcode() == Instruction::Div, R);
Chris Lattnercadff442003-10-23 17:21:43 +0000478 return;
Chris Lattnercadff442003-10-23 17:21:43 +0000479
Chris Lattner58c41fe2003-08-24 19:19:47 +0000480 case Instruction::SetNE:
481 case Instruction::SetEQ:
482 case Instruction::SetLT:
483 case Instruction::SetGT:
484 case Instruction::SetLE:
485 case Instruction::SetGE:
486 emitSetCCOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
487 CE->getOpcode(), R);
488 return;
489
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000490 case Instruction::Shl:
491 case Instruction::Shr:
492 emitShiftOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000493 CE->getOpcode() == Instruction::Shl, CE->getType(), R);
494 return;
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000495
Chris Lattner12d96a02004-03-30 21:22:00 +0000496 case Instruction::Select:
497 emitSelectOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
498 CE->getOperand(2), R);
499 return;
500
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000501 default:
Chris Lattner76e2df22004-07-15 02:14:30 +0000502 std::cerr << "Offending expr: " << *C << "\n";
Chris Lattnerb2acc512003-10-19 21:09:10 +0000503 assert(0 && "Constant expression not yet handled!\n");
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000504 }
Brian Gaeke20244b72002-12-12 15:33:40 +0000505 }
Chris Lattnerc5291f52002-10-27 21:16:59 +0000506
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000507 if (C->getType()->isIntegral()) {
Chris Lattner6b993cc2002-12-15 08:02:15 +0000508 unsigned Class = getClassB(C->getType());
Chris Lattner3e130a22003-01-13 00:32:26 +0000509
510 if (Class == cLong) {
511 // Copy the value into the register pair.
Chris Lattnerc07736a2003-07-23 15:22:26 +0000512 uint64_t Val = cast<ConstantInt>(C)->getRawValue();
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000513 BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addImm(Val & 0xFFFFFFFF);
514 BuildMI(*MBB, IP, X86::MOV32ri, 1, R+1).addImm(Val >> 32);
Chris Lattner3e130a22003-01-13 00:32:26 +0000515 return;
516 }
517
Chris Lattner94af4142002-12-25 05:13:53 +0000518 assert(Class <= cInt && "Type not handled yet!");
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000519
520 static const unsigned IntegralOpcodeTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000521 X86::MOV8ri, X86::MOV16ri, X86::MOV32ri
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000522 };
523
Chris Lattner6b993cc2002-12-15 08:02:15 +0000524 if (C->getType() == Type::BoolTy) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000525 BuildMI(*MBB, IP, X86::MOV8ri, 1, R).addImm(C == ConstantBool::True);
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000526 } else {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000527 ConstantInt *CI = cast<ConstantInt>(C);
Chris Lattneree352852004-02-29 07:22:16 +0000528 BuildMI(*MBB, IP, IntegralOpcodeTab[Class],1,R).addImm(CI->getRawValue());
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000529 }
Chris Lattner94af4142002-12-25 05:13:53 +0000530 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
Chris Lattneraf703622004-02-02 18:56:30 +0000531 if (CFP->isExactlyValue(+0.0))
Chris Lattneree352852004-02-29 07:22:16 +0000532 BuildMI(*MBB, IP, X86::FLD0, 0, R);
Chris Lattneraf703622004-02-02 18:56:30 +0000533 else if (CFP->isExactlyValue(+1.0))
Chris Lattneree352852004-02-29 07:22:16 +0000534 BuildMI(*MBB, IP, X86::FLD1, 0, R);
Chris Lattner94af4142002-12-25 05:13:53 +0000535 else {
Chris Lattner3e130a22003-01-13 00:32:26 +0000536 // Otherwise we need to spill the constant to memory...
537 MachineConstantPool *CP = F->getConstantPool();
538 unsigned CPI = CP->getConstantPoolIndex(CFP);
Chris Lattner6c09db22003-10-20 04:11:23 +0000539 const Type *Ty = CFP->getType();
540
541 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000542 unsigned LoadOpcode = Ty == Type::FloatTy ? X86::FLD32m : X86::FLD64m;
Chris Lattneree352852004-02-29 07:22:16 +0000543 addConstantPoolReference(BuildMI(*MBB, IP, LoadOpcode, 4, R), CPI);
Chris Lattner94af4142002-12-25 05:13:53 +0000544 }
545
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000546 } else if (isa<ConstantPointerNull>(C)) {
Brian Gaeke20244b72002-12-12 15:33:40 +0000547 // Copy zero (null pointer) to the register.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000548 BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addImm(0);
Reid Spencer8863f182004-07-18 00:38:32 +0000549 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) {
550 BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addGlobalAddress(GV);
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000551 } else {
Chris Lattner76e2df22004-07-15 02:14:30 +0000552 std::cerr << "Offending constant: " << *C << "\n";
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000553 assert(0 && "Type not handled yet!");
Chris Lattnerc5291f52002-10-27 21:16:59 +0000554 }
555}
556
Chris Lattner065faeb2002-12-28 20:24:02 +0000557/// LoadArgumentsToVirtualRegs - Load all of the arguments to this function from
558/// the stack into virtual registers.
559///
560void ISel::LoadArgumentsToVirtualRegs(Function &Fn) {
561 // Emit instructions to load the arguments... On entry to a function on the
562 // X86, the stack frame looks like this:
563 //
564 // [ESP] -- return address
Chris Lattner3e130a22003-01-13 00:32:26 +0000565 // [ESP + 4] -- first argument (leftmost lexically)
566 // [ESP + 8] -- second argument, if first argument is four bytes in size
Chris Lattner065faeb2002-12-28 20:24:02 +0000567 // ...
568 //
Chris Lattnerf158da22003-01-16 02:20:12 +0000569 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattneraa09b752002-12-28 21:08:28 +0000570 MachineFrameInfo *MFI = F->getFrameInfo();
Chris Lattner065faeb2002-12-28 20:24:02 +0000571
572 for (Function::aiterator I = Fn.abegin(), E = Fn.aend(); I != E; ++I) {
Chris Lattner427aeb42004-04-11 19:21:59 +0000573 bool ArgLive = !I->use_empty();
574 unsigned Reg = ArgLive ? getReg(*I) : 0;
Chris Lattner065faeb2002-12-28 20:24:02 +0000575 int FI; // Frame object index
Chris Lattner427aeb42004-04-11 19:21:59 +0000576
Chris Lattner065faeb2002-12-28 20:24:02 +0000577 switch (getClassB(I->getType())) {
578 case cByte:
Chris Lattner427aeb42004-04-11 19:21:59 +0000579 if (ArgLive) {
580 FI = MFI->CreateFixedObject(1, ArgOffset);
581 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Reg), FI);
582 }
Chris Lattner065faeb2002-12-28 20:24:02 +0000583 break;
584 case cShort:
Chris Lattner427aeb42004-04-11 19:21:59 +0000585 if (ArgLive) {
586 FI = MFI->CreateFixedObject(2, ArgOffset);
587 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Reg), FI);
588 }
Chris Lattner065faeb2002-12-28 20:24:02 +0000589 break;
590 case cInt:
Chris Lattner427aeb42004-04-11 19:21:59 +0000591 if (ArgLive) {
592 FI = MFI->CreateFixedObject(4, ArgOffset);
593 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Reg), FI);
594 }
Chris Lattner065faeb2002-12-28 20:24:02 +0000595 break;
Chris Lattner3e130a22003-01-13 00:32:26 +0000596 case cLong:
Chris Lattner427aeb42004-04-11 19:21:59 +0000597 if (ArgLive) {
598 FI = MFI->CreateFixedObject(8, ArgOffset);
599 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Reg), FI);
600 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Reg+1), FI, 4);
601 }
Chris Lattner3e130a22003-01-13 00:32:26 +0000602 ArgOffset += 4; // longs require 4 additional bytes
603 break;
Chris Lattner065faeb2002-12-28 20:24:02 +0000604 case cFP:
Chris Lattner427aeb42004-04-11 19:21:59 +0000605 if (ArgLive) {
606 unsigned Opcode;
607 if (I->getType() == Type::FloatTy) {
608 Opcode = X86::FLD32m;
609 FI = MFI->CreateFixedObject(4, ArgOffset);
610 } else {
611 Opcode = X86::FLD64m;
612 FI = MFI->CreateFixedObject(8, ArgOffset);
613 }
614 addFrameReference(BuildMI(BB, Opcode, 4, Reg), FI);
Chris Lattner065faeb2002-12-28 20:24:02 +0000615 }
Chris Lattner427aeb42004-04-11 19:21:59 +0000616 if (I->getType() == Type::DoubleTy)
617 ArgOffset += 4; // doubles require 4 additional bytes
Chris Lattner065faeb2002-12-28 20:24:02 +0000618 break;
619 default:
620 assert(0 && "Unhandled argument type!");
621 }
Chris Lattner3e130a22003-01-13 00:32:26 +0000622 ArgOffset += 4; // Each argument takes at least 4 bytes on the stack...
Chris Lattner065faeb2002-12-28 20:24:02 +0000623 }
Chris Lattnereca195e2003-05-08 19:44:13 +0000624
625 // If the function takes variable number of arguments, add a frame offset for
626 // the start of the first vararg value... this is used to expand
627 // llvm.va_start.
628 if (Fn.getFunctionType()->isVarArg())
629 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner065faeb2002-12-28 20:24:02 +0000630}
631
632
Chris Lattner333b2fa2002-12-13 10:09:43 +0000633/// SelectPHINodes - Insert machine code to generate phis. This is tricky
634/// because we have to generate our sources into the source basic blocks, not
635/// the current one.
636///
637void ISel::SelectPHINodes() {
Chris Lattnerd029cd22004-06-02 05:55:25 +0000638 const TargetInstrInfo &TII = *TM.getInstrInfo();
Chris Lattner333b2fa2002-12-13 10:09:43 +0000639 const Function &LF = *F->getFunction(); // The LLVM function...
640 for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) {
641 const BasicBlock *BB = I;
Chris Lattner168aa902004-02-29 07:10:16 +0000642 MachineBasicBlock &MBB = *MBBMap[I];
Chris Lattner333b2fa2002-12-13 10:09:43 +0000643
644 // Loop over all of the PHI nodes in the LLVM basic block...
Chris Lattner168aa902004-02-29 07:10:16 +0000645 MachineBasicBlock::iterator PHIInsertPoint = MBB.begin();
Chris Lattner333b2fa2002-12-13 10:09:43 +0000646 for (BasicBlock::const_iterator I = BB->begin();
Chris Lattnera81fc682003-10-19 00:26:11 +0000647 PHINode *PN = const_cast<PHINode*>(dyn_cast<PHINode>(I)); ++I) {
Chris Lattner3e130a22003-01-13 00:32:26 +0000648
Chris Lattner333b2fa2002-12-13 10:09:43 +0000649 // Create a new machine instr PHI node, and insert it.
Chris Lattner3e130a22003-01-13 00:32:26 +0000650 unsigned PHIReg = getReg(*PN);
Chris Lattner168aa902004-02-29 07:10:16 +0000651 MachineInstr *PhiMI = BuildMI(MBB, PHIInsertPoint,
652 X86::PHI, PN->getNumOperands(), PHIReg);
Chris Lattner3e130a22003-01-13 00:32:26 +0000653
654 MachineInstr *LongPhiMI = 0;
Chris Lattner168aa902004-02-29 07:10:16 +0000655 if (PN->getType() == Type::LongTy || PN->getType() == Type::ULongTy)
656 LongPhiMI = BuildMI(MBB, PHIInsertPoint,
657 X86::PHI, PN->getNumOperands(), PHIReg+1);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000658
Chris Lattnera6e73f12003-05-12 14:22:21 +0000659 // PHIValues - Map of blocks to incoming virtual registers. We use this
660 // so that we only initialize one incoming value for a particular block,
661 // even if the block has multiple entries in the PHI node.
662 //
663 std::map<MachineBasicBlock*, unsigned> PHIValues;
664
Chris Lattner333b2fa2002-12-13 10:09:43 +0000665 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
666 MachineBasicBlock *PredMBB = MBBMap[PN->getIncomingBlock(i)];
Chris Lattnera6e73f12003-05-12 14:22:21 +0000667 unsigned ValReg;
668 std::map<MachineBasicBlock*, unsigned>::iterator EntryIt =
669 PHIValues.lower_bound(PredMBB);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000670
Chris Lattnera6e73f12003-05-12 14:22:21 +0000671 if (EntryIt != PHIValues.end() && EntryIt->first == PredMBB) {
672 // We already inserted an initialization of the register for this
673 // predecessor. Recycle it.
674 ValReg = EntryIt->second;
675
676 } else {
Chris Lattnera81fc682003-10-19 00:26:11 +0000677 // Get the incoming value into a virtual register.
Chris Lattnera6e73f12003-05-12 14:22:21 +0000678 //
Chris Lattnera81fc682003-10-19 00:26:11 +0000679 Value *Val = PN->getIncomingValue(i);
680
681 // If this is a constant or GlobalValue, we may have to insert code
682 // into the basic block to compute it into a virtual register.
Reid Spencer8863f182004-07-18 00:38:32 +0000683 if ((isa<Constant>(Val) && !isa<ConstantExpr>(Val))) {
Chris Lattnercb2fd552004-05-13 07:40:27 +0000684 // Simple constants get emitted at the end of the basic block,
685 // before any terminator instructions. We "know" that the code to
686 // move a constant into a register will never clobber any flags.
687 ValReg = getReg(Val, PredMBB, PredMBB->getFirstTerminator());
Chris Lattnera81fc682003-10-19 00:26:11 +0000688 } else {
Chris Lattnercb2fd552004-05-13 07:40:27 +0000689 // Because we don't want to clobber any values which might be in
690 // physical registers with the computation of this constant (which
691 // might be arbitrarily complex if it is a constant expression),
692 // just insert the computation at the top of the basic block.
693 MachineBasicBlock::iterator PI = PredMBB->begin();
694
695 // Skip over any PHI nodes though!
696 while (PI != PredMBB->end() && PI->getOpcode() == X86::PHI)
697 ++PI;
698
699 ValReg = getReg(Val, PredMBB, PI);
Chris Lattnera81fc682003-10-19 00:26:11 +0000700 }
Chris Lattnera6e73f12003-05-12 14:22:21 +0000701
702 // Remember that we inserted a value for this PHI for this predecessor
703 PHIValues.insert(EntryIt, std::make_pair(PredMBB, ValReg));
704 }
705
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000706 PhiMI->addRegOperand(ValReg);
Chris Lattner3e130a22003-01-13 00:32:26 +0000707 PhiMI->addMachineBasicBlockOperand(PredMBB);
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000708 if (LongPhiMI) {
709 LongPhiMI->addRegOperand(ValReg+1);
710 LongPhiMI->addMachineBasicBlockOperand(PredMBB);
711 }
Chris Lattner333b2fa2002-12-13 10:09:43 +0000712 }
Chris Lattner168aa902004-02-29 07:10:16 +0000713
714 // Now that we emitted all of the incoming values for the PHI node, make
715 // sure to reposition the InsertPoint after the PHI that we just added.
716 // This is needed because we might have inserted a constant into this
717 // block, right after the PHI's which is before the old insert point!
718 PHIInsertPoint = LongPhiMI ? LongPhiMI : PhiMI;
719 ++PHIInsertPoint;
Chris Lattner333b2fa2002-12-13 10:09:43 +0000720 }
721 }
722}
723
Chris Lattner986618e2004-02-22 19:47:26 +0000724/// RequiresFPRegKill - The floating point stackifier pass cannot insert
725/// compensation code on critical edges. As such, it requires that we kill all
726/// FP registers on the exit from any blocks that either ARE critical edges, or
727/// branch to a block that has incoming critical edges.
728///
729/// Note that this kill instruction will eventually be eliminated when
730/// restrictions in the stackifier are relaxed.
731///
Brian Gaeke1afe7732004-04-28 04:45:55 +0000732static bool RequiresFPRegKill(const MachineBasicBlock *MBB) {
Chris Lattner986618e2004-02-22 19:47:26 +0000733#if 0
Brian Gaeke1afe7732004-04-28 04:45:55 +0000734 const BasicBlock *BB = MBB->getBasicBlock ();
Chris Lattner986618e2004-02-22 19:47:26 +0000735 for (succ_const_iterator SI = succ_begin(BB), E = succ_end(BB); SI!=E; ++SI) {
736 const BasicBlock *Succ = *SI;
737 pred_const_iterator PI = pred_begin(Succ), PE = pred_end(Succ);
738 ++PI; // Block have at least one predecessory
739 if (PI != PE) { // If it has exactly one, this isn't crit edge
740 // If this block has more than one predecessor, check all of the
741 // predecessors to see if they have multiple successors. If so, then the
742 // block we are analyzing needs an FPRegKill.
743 for (PI = pred_begin(Succ); PI != PE; ++PI) {
744 const BasicBlock *Pred = *PI;
745 succ_const_iterator SI2 = succ_begin(Pred);
746 ++SI2; // There must be at least one successor of this block.
747 if (SI2 != succ_end(Pred))
748 return true; // Yes, we must insert the kill on this edge.
749 }
750 }
751 }
752 // If we got this far, there is no need to insert the kill instruction.
753 return false;
754#else
755 return true;
756#endif
757}
758
759// InsertFPRegKills - Insert FP_REG_KILL instructions into basic blocks that
760// need them. This only occurs due to the floating point stackifier not being
761// aggressive enough to handle arbitrary global stackification.
762//
763// Currently we insert an FP_REG_KILL instruction into each block that uses or
764// defines a floating point virtual register.
765//
766// When the global register allocators (like linear scan) finally update live
767// variable analysis, we can keep floating point values in registers across
768// portions of the CFG that do not involve critical edges. This will be a big
769// win, but we are waiting on the global allocators before we can do this.
770//
771// With a bit of work, the floating point stackifier pass can be enhanced to
772// break critical edges as needed (to make a place to put compensation code),
773// but this will require some infrastructure improvements as well.
774//
775void ISel::InsertFPRegKills() {
776 SSARegMap &RegMap = *F->getSSARegMap();
Chris Lattner986618e2004-02-22 19:47:26 +0000777
778 for (MachineFunction::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
Chris Lattner986618e2004-02-22 19:47:26 +0000779 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000780 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
781 MachineOperand& MO = I->getOperand(i);
782 if (MO.isRegister() && MO.getReg()) {
783 unsigned Reg = MO.getReg();
Chris Lattner986618e2004-02-22 19:47:26 +0000784 if (MRegisterInfo::isVirtualRegister(Reg))
Chris Lattner65cf42d2004-02-23 07:29:45 +0000785 if (RegMap.getRegClass(Reg)->getSize() == 10)
786 goto UsesFPReg;
Chris Lattner986618e2004-02-22 19:47:26 +0000787 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000788 }
Chris Lattner65cf42d2004-02-23 07:29:45 +0000789 // If we haven't found an FP register use or def in this basic block, check
790 // to see if any of our successors has an FP PHI node, which will cause a
791 // copy to be inserted into this block.
Brian Gaeke235aa5e2004-04-28 04:34:16 +0000792 for (MachineBasicBlock::const_succ_iterator SI = BB->succ_begin(),
793 SE = BB->succ_end(); SI != SE; ++SI) {
794 MachineBasicBlock *SBB = *SI;
Chris Lattnerfbc39d52004-02-23 07:42:19 +0000795 for (MachineBasicBlock::iterator I = SBB->begin();
796 I != SBB->end() && I->getOpcode() == X86::PHI; ++I) {
797 if (RegMap.getRegClass(I->getOperand(0).getReg())->getSize() == 10)
798 goto UsesFPReg;
Chris Lattner986618e2004-02-22 19:47:26 +0000799 }
Chris Lattnerfbc39d52004-02-23 07:42:19 +0000800 }
Chris Lattner65cf42d2004-02-23 07:29:45 +0000801 continue;
802 UsesFPReg:
803 // Okay, this block uses an FP register. If the block has successors (ie,
804 // it's not an unwind/return), insert the FP_REG_KILL instruction.
Brian Gaeke1afe7732004-04-28 04:45:55 +0000805 if (BB->succ_size () && RequiresFPRegKill(BB)) {
Chris Lattneree352852004-02-29 07:22:16 +0000806 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
Chris Lattner65cf42d2004-02-23 07:29:45 +0000807 ++NumFPKill;
Chris Lattner986618e2004-02-22 19:47:26 +0000808 }
809 }
810}
811
812
Reid Spencerfc989e12004-08-30 00:13:26 +0000813void ISel::getAddressingMode(Value *Addr, X86AddressMode &AM) {
814 AM.BaseType = X86AddressMode::RegBase;
815 AM.Base.Reg = 0; AM.Scale = 1; AM.IndexReg = 0; AM.Disp = 0;
Chris Lattner9f1b5312004-05-13 15:12:43 +0000816 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Addr)) {
817 if (isGEPFoldable(BB, GEP->getOperand(0), GEP->op_begin()+1, GEP->op_end(),
Reid Spencerfc989e12004-08-30 00:13:26 +0000818 AM))
Chris Lattner9f1b5312004-05-13 15:12:43 +0000819 return;
820 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Addr)) {
821 if (CE->getOpcode() == Instruction::GetElementPtr)
822 if (isGEPFoldable(BB, CE->getOperand(0), CE->op_begin()+1, CE->op_end(),
Reid Spencerfc989e12004-08-30 00:13:26 +0000823 AM))
Chris Lattner9f1b5312004-05-13 15:12:43 +0000824 return;
Reid Spencerfc989e12004-08-30 00:13:26 +0000825 } else if (AllocaInst *AI = dyn_castFixedAlloca(Addr)) {
826 AM.BaseType = X86AddressMode::FrameIndexBase;
827 AM.Base.FrameIndex = getFixedSizedAllocaFI(AI);
828 return;
Chris Lattner9f1b5312004-05-13 15:12:43 +0000829 }
830
831 // If it's not foldable, reset addr mode.
Reid Spencerfc989e12004-08-30 00:13:26 +0000832 AM.BaseType = X86AddressMode::RegBase;
833 AM.Base.Reg = getReg(Addr);
834 AM.Scale = 1; AM.IndexReg = 0; AM.Disp = 0;
Chris Lattner9f1b5312004-05-13 15:12:43 +0000835}
836
Chris Lattner307ecba2004-03-30 22:39:09 +0000837// canFoldSetCCIntoBranchOrSelect - Return the setcc instruction if we can fold
838// it into the conditional branch or select instruction which is the only user
839// of the cc instruction. This is the case if the conditional branch is the
Chris Lattnera6f9fe62004-06-18 00:29:22 +0000840// only user of the setcc. We also don't handle long arguments below, so we
841// reject them here as well.
Chris Lattner6d40c192003-01-16 16:43:00 +0000842//
Chris Lattner307ecba2004-03-30 22:39:09 +0000843static SetCondInst *canFoldSetCCIntoBranchOrSelect(Value *V) {
Chris Lattner6d40c192003-01-16 16:43:00 +0000844 if (SetCondInst *SCI = dyn_cast<SetCondInst>(V))
Chris Lattner307ecba2004-03-30 22:39:09 +0000845 if (SCI->hasOneUse()) {
846 Instruction *User = cast<Instruction>(SCI->use_back());
847 if ((isa<BranchInst>(User) || isa<SelectInst>(User)) &&
Chris Lattner48c937e2004-04-06 17:34:50 +0000848 (getClassB(SCI->getOperand(0)->getType()) != cLong ||
849 SCI->getOpcode() == Instruction::SetEQ ||
850 SCI->getOpcode() == Instruction::SetNE))
Chris Lattner6d40c192003-01-16 16:43:00 +0000851 return SCI;
852 }
853 return 0;
854}
Chris Lattner333b2fa2002-12-13 10:09:43 +0000855
Chris Lattner6d40c192003-01-16 16:43:00 +0000856// Return a fixed numbering for setcc instructions which does not depend on the
857// order of the opcodes.
858//
859static unsigned getSetCCNumber(unsigned Opcode) {
860 switch(Opcode) {
861 default: assert(0 && "Unknown setcc instruction!");
862 case Instruction::SetEQ: return 0;
863 case Instruction::SetNE: return 1;
864 case Instruction::SetLT: return 2;
Chris Lattner55f6fab2003-01-16 18:07:23 +0000865 case Instruction::SetGE: return 3;
866 case Instruction::SetGT: return 4;
867 case Instruction::SetLE: return 5;
Chris Lattner6d40c192003-01-16 16:43:00 +0000868 }
869}
Chris Lattner06925362002-11-17 21:56:38 +0000870
Chris Lattner6d40c192003-01-16 16:43:00 +0000871// LLVM -> X86 signed X86 unsigned
872// ----- ---------- ------------
873// seteq -> sete sete
874// setne -> setne setne
875// setlt -> setl setb
Chris Lattner55f6fab2003-01-16 18:07:23 +0000876// setge -> setge setae
Chris Lattner6d40c192003-01-16 16:43:00 +0000877// setgt -> setg seta
878// setle -> setle setbe
Chris Lattnerb2acc512003-10-19 21:09:10 +0000879// ----
880// sets // Used by comparison with 0 optimization
881// setns
882static const unsigned SetCCOpcodeTab[2][8] = {
883 { X86::SETEr, X86::SETNEr, X86::SETBr, X86::SETAEr, X86::SETAr, X86::SETBEr,
884 0, 0 },
885 { X86::SETEr, X86::SETNEr, X86::SETLr, X86::SETGEr, X86::SETGr, X86::SETLEr,
886 X86::SETSr, X86::SETNSr },
Chris Lattner6d40c192003-01-16 16:43:00 +0000887};
888
Chris Lattner01cdb1b2004-06-11 05:33:49 +0000889/// emitUCOMr - In the future when we support processors before the P6, this
890/// wraps the logic for emitting an FUCOMr vs FUCOMIr.
891void ISel::emitUCOMr(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
892 unsigned LHS, unsigned RHS) {
893 if (0) { // for processors prior to the P6
894 BuildMI(*MBB, IP, X86::FUCOMr, 2).addReg(LHS).addReg(RHS);
895 BuildMI(*MBB, IP, X86::FNSTSW8r, 0);
896 BuildMI(*MBB, IP, X86::SAHF, 1);
897 } else {
898 BuildMI(*MBB, IP, X86::FUCOMIr, 2).addReg(LHS).addReg(RHS);
899 }
900}
901
Chris Lattnerb2acc512003-10-19 21:09:10 +0000902// EmitComparison - This function emits a comparison of the two operands,
903// returning the extended setcc code to use.
904unsigned ISel::EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
905 MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000906 MachineBasicBlock::iterator IP) {
Brian Gaeke1749d632002-11-07 17:59:21 +0000907 // The arguments are already supposed to be of the same type.
Chris Lattner6d40c192003-01-16 16:43:00 +0000908 const Type *CompTy = Op0->getType();
Chris Lattner3e130a22003-01-13 00:32:26 +0000909 unsigned Class = getClassB(CompTy);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000910 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattner333864d2003-06-05 19:30:30 +0000911
912 // Special case handling of: cmp R, i
Chris Lattner260195d2004-05-07 19:55:55 +0000913 if (isa<ConstantPointerNull>(Op1)) {
914 if (OpNum < 2) // seteq/setne -> test
915 BuildMI(*MBB, IP, X86::TEST32rr, 2).addReg(Op0r).addReg(Op0r);
916 else
917 BuildMI(*MBB, IP, X86::CMP32ri, 2).addReg(Op0r).addImm(0);
918 return OpNum;
919
920 } else if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
Chris Lattnere80e6372004-04-06 16:02:27 +0000921 if (Class == cByte || Class == cShort || Class == cInt) {
922 unsigned Op1v = CI->getRawValue();
Chris Lattnerc07736a2003-07-23 15:22:26 +0000923
Chris Lattner333864d2003-06-05 19:30:30 +0000924 // Mask off any upper bits of the constant, if there are any...
925 Op1v &= (1ULL << (8 << Class)) - 1;
926
Chris Lattnerb2acc512003-10-19 21:09:10 +0000927 // If this is a comparison against zero, emit more efficient code. We
928 // can't handle unsigned comparisons against zero unless they are == or
929 // !=. These should have been strength reduced already anyway.
930 if (Op1v == 0 && (CompTy->isSigned() || OpNum < 2)) {
931 static const unsigned TESTTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000932 X86::TEST8rr, X86::TEST16rr, X86::TEST32rr
Chris Lattnerb2acc512003-10-19 21:09:10 +0000933 };
Chris Lattneree352852004-02-29 07:22:16 +0000934 BuildMI(*MBB, IP, TESTTab[Class], 2).addReg(Op0r).addReg(Op0r);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000935
936 if (OpNum == 2) return 6; // Map jl -> js
937 if (OpNum == 3) return 7; // Map jg -> jns
938 return OpNum;
Chris Lattner333864d2003-06-05 19:30:30 +0000939 }
Chris Lattnerb2acc512003-10-19 21:09:10 +0000940
941 static const unsigned CMPTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000942 X86::CMP8ri, X86::CMP16ri, X86::CMP32ri
Chris Lattnerb2acc512003-10-19 21:09:10 +0000943 };
944
Chris Lattneree352852004-02-29 07:22:16 +0000945 BuildMI(*MBB, IP, CMPTab[Class], 2).addReg(Op0r).addImm(Op1v);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000946 return OpNum;
Chris Lattnere80e6372004-04-06 16:02:27 +0000947 } else {
948 assert(Class == cLong && "Unknown integer class!");
949 unsigned LowCst = CI->getRawValue();
950 unsigned HiCst = CI->getRawValue() >> 32;
951 if (OpNum < 2) { // seteq, setne
952 unsigned LoTmp = Op0r;
953 if (LowCst != 0) {
954 LoTmp = makeAnotherReg(Type::IntTy);
955 BuildMI(*MBB, IP, X86::XOR32ri, 2, LoTmp).addReg(Op0r).addImm(LowCst);
956 }
957 unsigned HiTmp = Op0r+1;
958 if (HiCst != 0) {
959 HiTmp = makeAnotherReg(Type::IntTy);
Chris Lattner48c937e2004-04-06 17:34:50 +0000960 BuildMI(*MBB, IP, X86::XOR32ri, 2,HiTmp).addReg(Op0r+1).addImm(HiCst);
Chris Lattnere80e6372004-04-06 16:02:27 +0000961 }
962 unsigned FinalTmp = makeAnotherReg(Type::IntTy);
963 BuildMI(*MBB, IP, X86::OR32rr, 2, FinalTmp).addReg(LoTmp).addReg(HiTmp);
964 return OpNum;
Chris Lattner48c937e2004-04-06 17:34:50 +0000965 } else {
966 // Emit a sequence of code which compares the high and low parts once
967 // each, then uses a conditional move to handle the overflow case. For
968 // example, a setlt for long would generate code like this:
969 //
Chris Lattner9984fd02004-05-09 23:16:33 +0000970 // AL = lo(op1) < lo(op2) // Always unsigned comparison
971 // BL = hi(op1) < hi(op2) // Signedness depends on operands
972 // dest = hi(op1) == hi(op2) ? BL : AL;
Chris Lattner48c937e2004-04-06 17:34:50 +0000973 //
974
975 // FIXME: This would be much better if we had hierarchical register
976 // classes! Until then, hardcode registers so that we can deal with
977 // their aliases (because we don't have conditional byte moves).
978 //
979 BuildMI(*MBB, IP, X86::CMP32ri, 2).addReg(Op0r).addImm(LowCst);
980 BuildMI(*MBB, IP, SetCCOpcodeTab[0][OpNum], 0, X86::AL);
981 BuildMI(*MBB, IP, X86::CMP32ri, 2).addReg(Op0r+1).addImm(HiCst);
982 BuildMI(*MBB, IP, SetCCOpcodeTab[CompTy->isSigned()][OpNum], 0,X86::BL);
983 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::BH);
984 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::AH);
985 BuildMI(*MBB, IP, X86::CMOVE16rr, 2, X86::BX).addReg(X86::BX)
986 .addReg(X86::AX);
987 // NOTE: visitSetCondInst knows that the value is dumped into the BL
988 // register at this point for long values...
989 return OpNum;
Chris Lattnere80e6372004-04-06 16:02:27 +0000990 }
Chris Lattner333864d2003-06-05 19:30:30 +0000991 }
Chris Lattnere80e6372004-04-06 16:02:27 +0000992 }
Chris Lattner333864d2003-06-05 19:30:30 +0000993
Chris Lattner9f08a922004-02-03 18:54:04 +0000994 // Special case handling of comparison against +/- 0.0
995 if (ConstantFP *CFP = dyn_cast<ConstantFP>(Op1))
996 if (CFP->isExactlyValue(+0.0) || CFP->isExactlyValue(-0.0)) {
Chris Lattneree352852004-02-29 07:22:16 +0000997 BuildMI(*MBB, IP, X86::FTST, 1).addReg(Op0r);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000998 BuildMI(*MBB, IP, X86::FNSTSW8r, 0);
Chris Lattneree352852004-02-29 07:22:16 +0000999 BuildMI(*MBB, IP, X86::SAHF, 1);
Chris Lattner9f08a922004-02-03 18:54:04 +00001000 return OpNum;
1001 }
1002
Chris Lattner58c41fe2003-08-24 19:19:47 +00001003 unsigned Op1r = getReg(Op1, MBB, IP);
Chris Lattner3e130a22003-01-13 00:32:26 +00001004 switch (Class) {
1005 default: assert(0 && "Unknown type class!");
1006 // Emit: cmp <var1>, <var2> (do the comparison). We can
1007 // compare 8-bit with 8-bit, 16-bit with 16-bit, 32-bit with
1008 // 32-bit.
1009 case cByte:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001010 BuildMI(*MBB, IP, X86::CMP8rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +00001011 break;
1012 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001013 BuildMI(*MBB, IP, X86::CMP16rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +00001014 break;
1015 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001016 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +00001017 break;
1018 case cFP:
Chris Lattner01cdb1b2004-06-11 05:33:49 +00001019 emitUCOMr(MBB, IP, Op0r, Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +00001020 break;
1021
1022 case cLong:
1023 if (OpNum < 2) { // seteq, setne
1024 unsigned LoTmp = makeAnotherReg(Type::IntTy);
1025 unsigned HiTmp = makeAnotherReg(Type::IntTy);
1026 unsigned FinalTmp = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001027 BuildMI(*MBB, IP, X86::XOR32rr, 2, LoTmp).addReg(Op0r).addReg(Op1r);
1028 BuildMI(*MBB, IP, X86::XOR32rr, 2, HiTmp).addReg(Op0r+1).addReg(Op1r+1);
1029 BuildMI(*MBB, IP, X86::OR32rr, 2, FinalTmp).addReg(LoTmp).addReg(HiTmp);
Chris Lattner3e130a22003-01-13 00:32:26 +00001030 break; // Allow the sete or setne to be generated from flags set by OR
1031 } else {
1032 // Emit a sequence of code which compares the high and low parts once
1033 // each, then uses a conditional move to handle the overflow case. For
1034 // example, a setlt for long would generate code like this:
1035 //
1036 // AL = lo(op1) < lo(op2) // Signedness depends on operands
1037 // BL = hi(op1) < hi(op2) // Always unsigned comparison
Chris Lattner9984fd02004-05-09 23:16:33 +00001038 // dest = hi(op1) == hi(op2) ? BL : AL;
Chris Lattner3e130a22003-01-13 00:32:26 +00001039 //
1040
Chris Lattner6d40c192003-01-16 16:43:00 +00001041 // FIXME: This would be much better if we had hierarchical register
Chris Lattner3e130a22003-01-13 00:32:26 +00001042 // classes! Until then, hardcode registers so that we can deal with their
1043 // aliases (because we don't have conditional byte moves).
1044 //
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001045 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattneree352852004-02-29 07:22:16 +00001046 BuildMI(*MBB, IP, SetCCOpcodeTab[0][OpNum], 0, X86::AL);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001047 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r+1).addReg(Op1r+1);
Chris Lattneree352852004-02-29 07:22:16 +00001048 BuildMI(*MBB, IP, SetCCOpcodeTab[CompTy->isSigned()][OpNum], 0, X86::BL);
1049 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::BH);
1050 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::AH);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001051 BuildMI(*MBB, IP, X86::CMOVE16rr, 2, X86::BX).addReg(X86::BX)
Chris Lattneree352852004-02-29 07:22:16 +00001052 .addReg(X86::AX);
Chris Lattner6d40c192003-01-16 16:43:00 +00001053 // NOTE: visitSetCondInst knows that the value is dumped into the BL
1054 // register at this point for long values...
Chris Lattnerb2acc512003-10-19 21:09:10 +00001055 return OpNum;
Chris Lattner3e130a22003-01-13 00:32:26 +00001056 }
1057 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001058 return OpNum;
Chris Lattner6d40c192003-01-16 16:43:00 +00001059}
Chris Lattner3e130a22003-01-13 00:32:26 +00001060
Chris Lattner6d40c192003-01-16 16:43:00 +00001061/// SetCC instructions - Here we just emit boilerplate code to set a byte-sized
1062/// register, then move it to wherever the result should be.
1063///
1064void ISel::visitSetCondInst(SetCondInst &I) {
Chris Lattner307ecba2004-03-30 22:39:09 +00001065 if (canFoldSetCCIntoBranchOrSelect(&I))
1066 return; // Fold this into a branch or select.
Chris Lattner6d40c192003-01-16 16:43:00 +00001067
Chris Lattner6d40c192003-01-16 16:43:00 +00001068 unsigned DestReg = getReg(I);
Chris Lattner58c41fe2003-08-24 19:19:47 +00001069 MachineBasicBlock::iterator MII = BB->end();
1070 emitSetCCOperation(BB, MII, I.getOperand(0), I.getOperand(1), I.getOpcode(),
1071 DestReg);
1072}
Chris Lattner6d40c192003-01-16 16:43:00 +00001073
Chris Lattner58c41fe2003-08-24 19:19:47 +00001074/// emitSetCCOperation - Common code shared between visitSetCondInst and
1075/// constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +00001076///
Chris Lattner58c41fe2003-08-24 19:19:47 +00001077void ISel::emitSetCCOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00001078 MachineBasicBlock::iterator IP,
Chris Lattner58c41fe2003-08-24 19:19:47 +00001079 Value *Op0, Value *Op1, unsigned Opcode,
1080 unsigned TargetReg) {
1081 unsigned OpNum = getSetCCNumber(Opcode);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001082 OpNum = EmitComparison(OpNum, Op0, Op1, MBB, IP);
Chris Lattner58c41fe2003-08-24 19:19:47 +00001083
Chris Lattnerb2acc512003-10-19 21:09:10 +00001084 const Type *CompTy = Op0->getType();
1085 unsigned CompClass = getClassB(CompTy);
1086 bool isSigned = CompTy->isSigned() && CompClass != cFP;
1087
1088 if (CompClass != cLong || OpNum < 2) {
Chris Lattner6d40c192003-01-16 16:43:00 +00001089 // Handle normal comparisons with a setcc instruction...
Chris Lattneree352852004-02-29 07:22:16 +00001090 BuildMI(*MBB, IP, SetCCOpcodeTab[isSigned][OpNum], 0, TargetReg);
Chris Lattner6d40c192003-01-16 16:43:00 +00001091 } else {
1092 // Handle long comparisons by copying the value which is already in BL into
1093 // the register we want...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001094 BuildMI(*MBB, IP, X86::MOV8rr, 1, TargetReg).addReg(X86::BL);
Chris Lattner6d40c192003-01-16 16:43:00 +00001095 }
Brian Gaeke1749d632002-11-07 17:59:21 +00001096}
Chris Lattner51b49a92002-11-02 19:45:49 +00001097
Chris Lattner12d96a02004-03-30 21:22:00 +00001098void ISel::visitSelectInst(SelectInst &SI) {
1099 unsigned DestReg = getReg(SI);
1100 MachineBasicBlock::iterator MII = BB->end();
1101 emitSelectOperation(BB, MII, SI.getCondition(), SI.getTrueValue(),
1102 SI.getFalseValue(), DestReg);
1103}
1104
1105/// emitSelect - Common code shared between visitSelectInst and the constant
1106/// expression support.
1107void ISel::emitSelectOperation(MachineBasicBlock *MBB,
1108 MachineBasicBlock::iterator IP,
1109 Value *Cond, Value *TrueVal, Value *FalseVal,
1110 unsigned DestReg) {
1111 unsigned SelectClass = getClassB(TrueVal->getType());
1112
1113 // We don't support 8-bit conditional moves. If we have incoming constants,
1114 // transform them into 16-bit constants to avoid having a run-time conversion.
1115 if (SelectClass == cByte) {
1116 if (Constant *T = dyn_cast<Constant>(TrueVal))
1117 TrueVal = ConstantExpr::getCast(T, Type::ShortTy);
1118 if (Constant *F = dyn_cast<Constant>(FalseVal))
1119 FalseVal = ConstantExpr::getCast(F, Type::ShortTy);
1120 }
1121
Chris Lattner82c5a992004-04-13 21:56:09 +00001122 unsigned TrueReg = getReg(TrueVal, MBB, IP);
1123 unsigned FalseReg = getReg(FalseVal, MBB, IP);
1124 if (TrueReg == FalseReg) {
1125 static const unsigned Opcode[] = {
1126 X86::MOV8rr, X86::MOV16rr, X86::MOV32rr, X86::FpMOV, X86::MOV32rr
1127 };
1128 BuildMI(*MBB, IP, Opcode[SelectClass], 1, DestReg).addReg(TrueReg);
1129 if (SelectClass == cLong)
1130 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg+1).addReg(TrueReg+1);
1131 return;
1132 }
1133
Chris Lattner307ecba2004-03-30 22:39:09 +00001134 unsigned Opcode;
1135 if (SetCondInst *SCI = canFoldSetCCIntoBranchOrSelect(Cond)) {
1136 // We successfully folded the setcc into the select instruction.
1137
1138 unsigned OpNum = getSetCCNumber(SCI->getOpcode());
1139 OpNum = EmitComparison(OpNum, SCI->getOperand(0), SCI->getOperand(1), MBB,
1140 IP);
1141
1142 const Type *CompTy = SCI->getOperand(0)->getType();
1143 bool isSigned = CompTy->isSigned() && getClassB(CompTy) != cFP;
1144
1145 // LLVM -> X86 signed X86 unsigned
1146 // ----- ---------- ------------
1147 // seteq -> cmovNE cmovNE
1148 // setne -> cmovE cmovE
1149 // setlt -> cmovGE cmovAE
1150 // setge -> cmovL cmovB
1151 // setgt -> cmovLE cmovBE
1152 // setle -> cmovG cmovA
1153 // ----
1154 // cmovNS // Used by comparison with 0 optimization
1155 // cmovS
1156
1157 switch (SelectClass) {
Chris Lattner352eb482004-03-31 22:03:35 +00001158 default: assert(0 && "Unknown value class!");
1159 case cFP: {
1160 // Annoyingly, we don't have a full set of floating point conditional
1161 // moves. :(
1162 static const unsigned OpcodeTab[2][8] = {
1163 { X86::FCMOVNE, X86::FCMOVE, X86::FCMOVAE, X86::FCMOVB,
1164 X86::FCMOVBE, X86::FCMOVA, 0, 0 },
1165 { X86::FCMOVNE, X86::FCMOVE, 0, 0, 0, 0, 0, 0 },
1166 };
1167 Opcode = OpcodeTab[isSigned][OpNum];
1168
1169 // If opcode == 0, we hit a case that we don't support. Output a setcc
1170 // and compare the result against zero.
1171 if (Opcode == 0) {
1172 unsigned CompClass = getClassB(CompTy);
1173 unsigned CondReg;
1174 if (CompClass != cLong || OpNum < 2) {
1175 CondReg = makeAnotherReg(Type::BoolTy);
1176 // Handle normal comparisons with a setcc instruction...
1177 BuildMI(*MBB, IP, SetCCOpcodeTab[isSigned][OpNum], 0, CondReg);
1178 } else {
1179 // Long comparisons end up in the BL register.
1180 CondReg = X86::BL;
1181 }
1182
Chris Lattner68626c22004-03-31 22:22:36 +00001183 BuildMI(*MBB, IP, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
Chris Lattner352eb482004-03-31 22:03:35 +00001184 Opcode = X86::FCMOVE;
1185 }
1186 break;
1187 }
Chris Lattner307ecba2004-03-30 22:39:09 +00001188 case cByte:
1189 case cShort: {
1190 static const unsigned OpcodeTab[2][8] = {
1191 { X86::CMOVNE16rr, X86::CMOVE16rr, X86::CMOVAE16rr, X86::CMOVB16rr,
1192 X86::CMOVBE16rr, X86::CMOVA16rr, 0, 0 },
1193 { X86::CMOVNE16rr, X86::CMOVE16rr, X86::CMOVGE16rr, X86::CMOVL16rr,
1194 X86::CMOVLE16rr, X86::CMOVG16rr, X86::CMOVNS16rr, X86::CMOVS16rr },
1195 };
1196 Opcode = OpcodeTab[isSigned][OpNum];
1197 break;
1198 }
1199 case cInt:
1200 case cLong: {
1201 static const unsigned OpcodeTab[2][8] = {
1202 { X86::CMOVNE32rr, X86::CMOVE32rr, X86::CMOVAE32rr, X86::CMOVB32rr,
1203 X86::CMOVBE32rr, X86::CMOVA32rr, 0, 0 },
1204 { X86::CMOVNE32rr, X86::CMOVE32rr, X86::CMOVGE32rr, X86::CMOVL32rr,
1205 X86::CMOVLE32rr, X86::CMOVG32rr, X86::CMOVNS32rr, X86::CMOVS32rr },
1206 };
1207 Opcode = OpcodeTab[isSigned][OpNum];
1208 break;
1209 }
1210 }
1211 } else {
1212 // Get the value being branched on, and use it to set the condition codes.
1213 unsigned CondReg = getReg(Cond, MBB, IP);
Chris Lattner68626c22004-03-31 22:22:36 +00001214 BuildMI(*MBB, IP, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
Chris Lattner307ecba2004-03-30 22:39:09 +00001215 switch (SelectClass) {
Chris Lattner352eb482004-03-31 22:03:35 +00001216 default: assert(0 && "Unknown value class!");
1217 case cFP: Opcode = X86::FCMOVE; break;
Chris Lattner307ecba2004-03-30 22:39:09 +00001218 case cByte:
Chris Lattner352eb482004-03-31 22:03:35 +00001219 case cShort: Opcode = X86::CMOVE16rr; break;
Chris Lattner307ecba2004-03-30 22:39:09 +00001220 case cInt:
Chris Lattner352eb482004-03-31 22:03:35 +00001221 case cLong: Opcode = X86::CMOVE32rr; break;
Chris Lattner307ecba2004-03-30 22:39:09 +00001222 }
1223 }
Chris Lattner12d96a02004-03-30 21:22:00 +00001224
Chris Lattner12d96a02004-03-30 21:22:00 +00001225 unsigned RealDestReg = DestReg;
Chris Lattner12d96a02004-03-30 21:22:00 +00001226
Chris Lattner12d96a02004-03-30 21:22:00 +00001227
1228 // Annoyingly enough, X86 doesn't HAVE 8-bit conditional moves. Because of
1229 // this, we have to promote the incoming values to 16 bits, perform a 16-bit
1230 // cmove, then truncate the result.
1231 if (SelectClass == cByte) {
1232 DestReg = makeAnotherReg(Type::ShortTy);
1233 if (getClassB(TrueVal->getType()) == cByte) {
1234 // Promote the true value, by storing it into AL, and reading from AX.
1235 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::AL).addReg(TrueReg);
1236 BuildMI(*MBB, IP, X86::MOV8ri, 1, X86::AH).addImm(0);
1237 TrueReg = makeAnotherReg(Type::ShortTy);
1238 BuildMI(*MBB, IP, X86::MOV16rr, 1, TrueReg).addReg(X86::AX);
1239 }
1240 if (getClassB(FalseVal->getType()) == cByte) {
1241 // Promote the true value, by storing it into CL, and reading from CX.
1242 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(FalseReg);
1243 BuildMI(*MBB, IP, X86::MOV8ri, 1, X86::CH).addImm(0);
1244 FalseReg = makeAnotherReg(Type::ShortTy);
1245 BuildMI(*MBB, IP, X86::MOV16rr, 1, FalseReg).addReg(X86::CX);
1246 }
1247 }
1248
1249 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(TrueReg).addReg(FalseReg);
1250
1251 switch (SelectClass) {
1252 case cByte:
1253 // We did the computation with 16-bit registers. Truncate back to our
1254 // result by copying into AX then copying out AL.
1255 BuildMI(*MBB, IP, X86::MOV16rr, 1, X86::AX).addReg(DestReg);
1256 BuildMI(*MBB, IP, X86::MOV8rr, 1, RealDestReg).addReg(X86::AL);
1257 break;
1258 case cLong:
1259 // Move the upper half of the value as well.
1260 BuildMI(*MBB, IP, Opcode, 2,DestReg+1).addReg(TrueReg+1).addReg(FalseReg+1);
1261 break;
1262 }
1263}
Chris Lattner58c41fe2003-08-24 19:19:47 +00001264
1265
1266
Brian Gaekec2505982002-11-30 11:57:28 +00001267/// promote32 - Emit instructions to turn a narrow operand into a 32-bit-wide
1268/// operand, in the specified target register.
Misha Brukman538607f2004-03-01 23:53:11 +00001269///
Chris Lattner3e130a22003-01-13 00:32:26 +00001270void ISel::promote32(unsigned targetReg, const ValueRecord &VR) {
Chris Lattner9984fd02004-05-09 23:16:33 +00001271 bool isUnsigned = VR.Ty->isUnsigned() || VR.Ty == Type::BoolTy;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001272
Chris Lattner29bf0622004-04-06 01:21:00 +00001273 Value *Val = VR.Val;
1274 const Type *Ty = VR.Ty;
Chris Lattner502e36c2004-04-06 01:25:33 +00001275 if (Val) {
Chris Lattner29bf0622004-04-06 01:21:00 +00001276 if (Constant *C = dyn_cast<Constant>(Val)) {
1277 Val = ConstantExpr::getCast(C, Type::IntTy);
1278 Ty = Type::IntTy;
1279 }
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001280
Chris Lattner502e36c2004-04-06 01:25:33 +00001281 // If this is a simple constant, just emit a MOVri directly to avoid the
1282 // copy.
1283 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
1284 int TheVal = CI->getRawValue() & 0xFFFFFFFF;
Chris Lattner2b10b082004-05-12 16:35:04 +00001285 BuildMI(BB, X86::MOV32ri, 1, targetReg).addImm(TheVal);
Chris Lattner502e36c2004-04-06 01:25:33 +00001286 return;
1287 }
1288 }
1289
Chris Lattner29bf0622004-04-06 01:21:00 +00001290 // Make sure we have the register number for this value...
1291 unsigned Reg = Val ? getReg(Val) : VR.Reg;
1292
1293 switch (getClassB(Ty)) {
Chris Lattner94af4142002-12-25 05:13:53 +00001294 case cByte:
1295 // Extend value into target register (8->32)
1296 if (isUnsigned)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001297 BuildMI(BB, X86::MOVZX32rr8, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001298 else
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001299 BuildMI(BB, X86::MOVSX32rr8, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001300 break;
1301 case cShort:
1302 // Extend value into target register (16->32)
1303 if (isUnsigned)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001304 BuildMI(BB, X86::MOVZX32rr16, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001305 else
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001306 BuildMI(BB, X86::MOVSX32rr16, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001307 break;
1308 case cInt:
1309 // Move value into target register (32->32)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001310 BuildMI(BB, X86::MOV32rr, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001311 break;
1312 default:
1313 assert(0 && "Unpromotable operand class in promote32");
1314 }
Brian Gaekec2505982002-11-30 11:57:28 +00001315}
Chris Lattnerc5291f52002-10-27 21:16:59 +00001316
Chris Lattner72614082002-10-25 22:55:53 +00001317/// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such,
1318/// we have the following possibilities:
1319///
1320/// ret void: No return value, simply emit a 'ret' instruction
1321/// ret sbyte, ubyte : Extend value into EAX and return
1322/// ret short, ushort: Extend value into EAX and return
1323/// ret int, uint : Move value into EAX and return
1324/// ret pointer : Move value into EAX and return
Chris Lattner06925362002-11-17 21:56:38 +00001325/// ret long, ulong : Move value into EAX/EDX and return
1326/// ret float/double : Top of FP stack
Chris Lattner72614082002-10-25 22:55:53 +00001327///
Chris Lattner3e130a22003-01-13 00:32:26 +00001328void ISel::visitReturnInst(ReturnInst &I) {
Chris Lattner94af4142002-12-25 05:13:53 +00001329 if (I.getNumOperands() == 0) {
1330 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
1331 return;
1332 }
1333
1334 Value *RetVal = I.getOperand(0);
Chris Lattner3e130a22003-01-13 00:32:26 +00001335 switch (getClassB(RetVal->getType())) {
Chris Lattner94af4142002-12-25 05:13:53 +00001336 case cByte: // integral return values: extend or move into EAX and return
1337 case cShort:
1338 case cInt:
Chris Lattner29bf0622004-04-06 01:21:00 +00001339 promote32(X86::EAX, ValueRecord(RetVal));
Chris Lattnerdbd73722003-05-06 21:32:22 +00001340 // Declare that EAX is live on exit
Chris Lattnerc2489032003-05-07 19:21:28 +00001341 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
Chris Lattner94af4142002-12-25 05:13:53 +00001342 break;
Chris Lattner29bf0622004-04-06 01:21:00 +00001343 case cFP: { // Floats & Doubles: Return in ST(0)
1344 unsigned RetReg = getReg(RetVal);
Chris Lattner3e130a22003-01-13 00:32:26 +00001345 BuildMI(BB, X86::FpSETRESULT, 1).addReg(RetReg);
Chris Lattnerdbd73722003-05-06 21:32:22 +00001346 // Declare that top-of-stack is live on exit
Chris Lattnerc2489032003-05-07 19:21:28 +00001347 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
Chris Lattner94af4142002-12-25 05:13:53 +00001348 break;
Chris Lattner29bf0622004-04-06 01:21:00 +00001349 }
1350 case cLong: {
1351 unsigned RetReg = getReg(RetVal);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001352 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(RetReg);
1353 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RetReg+1);
Chris Lattnerdbd73722003-05-06 21:32:22 +00001354 // Declare that EAX & EDX are live on exit
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001355 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
1356 .addReg(X86::ESP);
Chris Lattner3e130a22003-01-13 00:32:26 +00001357 break;
Chris Lattner29bf0622004-04-06 01:21:00 +00001358 }
Chris Lattner94af4142002-12-25 05:13:53 +00001359 default:
Chris Lattner3e130a22003-01-13 00:32:26 +00001360 visitInstruction(I);
Chris Lattner94af4142002-12-25 05:13:53 +00001361 }
Chris Lattner43189d12002-11-17 20:07:45 +00001362 // Emit a 'ret' instruction
Chris Lattner94af4142002-12-25 05:13:53 +00001363 BuildMI(BB, X86::RET, 0);
Chris Lattner72614082002-10-25 22:55:53 +00001364}
1365
Chris Lattner55f6fab2003-01-16 18:07:23 +00001366// getBlockAfter - Return the basic block which occurs lexically after the
1367// specified one.
1368static inline BasicBlock *getBlockAfter(BasicBlock *BB) {
1369 Function::iterator I = BB; ++I; // Get iterator to next block
1370 return I != BB->getParent()->end() ? &*I : 0;
1371}
1372
Chris Lattner51b49a92002-11-02 19:45:49 +00001373/// visitBranchInst - Handle conditional and unconditional branches here. Note
1374/// that since code layout is frozen at this point, that if we are trying to
1375/// jump to a block that is the immediate successor of the current block, we can
Chris Lattner6d40c192003-01-16 16:43:00 +00001376/// just make a fall-through (but we don't currently).
Chris Lattner51b49a92002-11-02 19:45:49 +00001377///
Chris Lattner94af4142002-12-25 05:13:53 +00001378void ISel::visitBranchInst(BranchInst &BI) {
Brian Gaekeea9ca672004-04-28 04:19:37 +00001379 // Update machine-CFG edges
1380 BB->addSuccessor (MBBMap[BI.getSuccessor(0)]);
1381 if (BI.isConditional())
1382 BB->addSuccessor (MBBMap[BI.getSuccessor(1)]);
1383
Chris Lattner55f6fab2003-01-16 18:07:23 +00001384 BasicBlock *NextBB = getBlockAfter(BI.getParent()); // BB after current one
1385
1386 if (!BI.isConditional()) { // Unconditional branch?
Chris Lattnercf93cdd2004-01-30 22:13:44 +00001387 if (BI.getSuccessor(0) != NextBB)
Brian Gaeke9f088e42004-05-14 06:54:56 +00001388 BuildMI(BB, X86::JMP, 1).addMBB(MBBMap[BI.getSuccessor(0)]);
Chris Lattner6d40c192003-01-16 16:43:00 +00001389 return;
1390 }
1391
1392 // See if we can fold the setcc into the branch itself...
Chris Lattner307ecba2004-03-30 22:39:09 +00001393 SetCondInst *SCI = canFoldSetCCIntoBranchOrSelect(BI.getCondition());
Chris Lattner6d40c192003-01-16 16:43:00 +00001394 if (SCI == 0) {
1395 // Nope, cannot fold setcc into this branch. Emit a branch on a condition
1396 // computed some other way...
Chris Lattner065faeb2002-12-28 20:24:02 +00001397 unsigned condReg = getReg(BI.getCondition());
Chris Lattner68626c22004-03-31 22:22:36 +00001398 BuildMI(BB, X86::TEST8rr, 2).addReg(condReg).addReg(condReg);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001399 if (BI.getSuccessor(1) == NextBB) {
1400 if (BI.getSuccessor(0) != NextBB)
Brian Gaeke9f088e42004-05-14 06:54:56 +00001401 BuildMI(BB, X86::JNE, 1).addMBB(MBBMap[BI.getSuccessor(0)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001402 } else {
Brian Gaeke9f088e42004-05-14 06:54:56 +00001403 BuildMI(BB, X86::JE, 1).addMBB(MBBMap[BI.getSuccessor(1)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001404
1405 if (BI.getSuccessor(0) != NextBB)
Brian Gaeke9f088e42004-05-14 06:54:56 +00001406 BuildMI(BB, X86::JMP, 1).addMBB(MBBMap[BI.getSuccessor(0)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001407 }
Chris Lattner6d40c192003-01-16 16:43:00 +00001408 return;
Chris Lattner94af4142002-12-25 05:13:53 +00001409 }
Chris Lattner6d40c192003-01-16 16:43:00 +00001410
1411 unsigned OpNum = getSetCCNumber(SCI->getOpcode());
Chris Lattner58c41fe2003-08-24 19:19:47 +00001412 MachineBasicBlock::iterator MII = BB->end();
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001413 OpNum = EmitComparison(OpNum, SCI->getOperand(0), SCI->getOperand(1), BB,MII);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001414
1415 const Type *CompTy = SCI->getOperand(0)->getType();
1416 bool isSigned = CompTy->isSigned() && getClassB(CompTy) != cFP;
Chris Lattner6d40c192003-01-16 16:43:00 +00001417
Chris Lattnerb2acc512003-10-19 21:09:10 +00001418
Chris Lattner6d40c192003-01-16 16:43:00 +00001419 // LLVM -> X86 signed X86 unsigned
1420 // ----- ---------- ------------
1421 // seteq -> je je
1422 // setne -> jne jne
1423 // setlt -> jl jb
Chris Lattner55f6fab2003-01-16 18:07:23 +00001424 // setge -> jge jae
Chris Lattner6d40c192003-01-16 16:43:00 +00001425 // setgt -> jg ja
1426 // setle -> jle jbe
Chris Lattnerb2acc512003-10-19 21:09:10 +00001427 // ----
1428 // js // Used by comparison with 0 optimization
1429 // jns
1430
1431 static const unsigned OpcodeTab[2][8] = {
1432 { X86::JE, X86::JNE, X86::JB, X86::JAE, X86::JA, X86::JBE, 0, 0 },
1433 { X86::JE, X86::JNE, X86::JL, X86::JGE, X86::JG, X86::JLE,
1434 X86::JS, X86::JNS },
Chris Lattner6d40c192003-01-16 16:43:00 +00001435 };
1436
Chris Lattner55f6fab2003-01-16 18:07:23 +00001437 if (BI.getSuccessor(0) != NextBB) {
Brian Gaeke9f088e42004-05-14 06:54:56 +00001438 BuildMI(BB, OpcodeTab[isSigned][OpNum], 1)
1439 .addMBB(MBBMap[BI.getSuccessor(0)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001440 if (BI.getSuccessor(1) != NextBB)
Brian Gaeke9f088e42004-05-14 06:54:56 +00001441 BuildMI(BB, X86::JMP, 1).addMBB(MBBMap[BI.getSuccessor(1)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001442 } else {
1443 // Change to the inverse condition...
1444 if (BI.getSuccessor(1) != NextBB) {
1445 OpNum ^= 1;
Brian Gaeke9f088e42004-05-14 06:54:56 +00001446 BuildMI(BB, OpcodeTab[isSigned][OpNum], 1)
1447 .addMBB(MBBMap[BI.getSuccessor(1)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001448 }
1449 }
Chris Lattner2df035b2002-11-02 19:27:56 +00001450}
1451
Chris Lattner3e130a22003-01-13 00:32:26 +00001452
1453/// doCall - This emits an abstract call instruction, setting up the arguments
1454/// and the return value as appropriate. For the actual function call itself,
1455/// it inserts the specified CallMI instruction into the stream.
1456///
1457void ISel::doCall(const ValueRecord &Ret, MachineInstr *CallMI,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001458 const std::vector<ValueRecord> &Args) {
Chris Lattner3e130a22003-01-13 00:32:26 +00001459
Chris Lattner065faeb2002-12-28 20:24:02 +00001460 // Count how many bytes are to be pushed on the stack...
1461 unsigned NumBytes = 0;
Misha Brukman0d2cf3a2002-12-04 19:22:53 +00001462
Chris Lattner3e130a22003-01-13 00:32:26 +00001463 if (!Args.empty()) {
1464 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1465 switch (getClassB(Args[i].Ty)) {
Chris Lattner065faeb2002-12-28 20:24:02 +00001466 case cByte: case cShort: case cInt:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001467 NumBytes += 4; break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001468 case cLong:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001469 NumBytes += 8; break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001470 case cFP:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001471 NumBytes += Args[i].Ty == Type::FloatTy ? 4 : 8;
1472 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001473 default: assert(0 && "Unknown class!");
1474 }
1475
1476 // Adjust the stack pointer for the new arguments...
Chris Lattneree352852004-02-29 07:22:16 +00001477 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(NumBytes);
Chris Lattner065faeb2002-12-28 20:24:02 +00001478
1479 // Arguments go on the stack in reverse order, as specified by the ABI.
1480 unsigned ArgOffset = 0;
Chris Lattner3e130a22003-01-13 00:32:26 +00001481 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattnerce6096f2004-03-01 02:34:08 +00001482 unsigned ArgReg;
Chris Lattner3e130a22003-01-13 00:32:26 +00001483 switch (getClassB(Args[i].Ty)) {
Chris Lattner065faeb2002-12-28 20:24:02 +00001484 case cByte:
Chris Lattner2b10b082004-05-12 16:35:04 +00001485 if (Args[i].Val && isa<ConstantBool>(Args[i].Val)) {
1486 addRegOffset(BuildMI(BB, X86::MOV32mi, 5), X86::ESP, ArgOffset)
1487 .addImm(Args[i].Val == ConstantBool::True);
1488 break;
1489 }
1490 // FALL THROUGH
Chris Lattner21585222004-03-01 02:42:43 +00001491 case cShort:
1492 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1493 // Zero/Sign extend constant, then stuff into memory.
1494 ConstantInt *Val = cast<ConstantInt>(Args[i].Val);
1495 Val = cast<ConstantInt>(ConstantExpr::getCast(Val, Type::IntTy));
1496 addRegOffset(BuildMI(BB, X86::MOV32mi, 5), X86::ESP, ArgOffset)
1497 .addImm(Val->getRawValue() & 0xFFFFFFFF);
1498 } else {
1499 // Promote arg to 32 bits wide into a temporary register...
1500 ArgReg = makeAnotherReg(Type::UIntTy);
1501 promote32(ArgReg, Args[i]);
1502 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1503 X86::ESP, ArgOffset).addReg(ArgReg);
1504 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001505 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001506 case cInt:
Chris Lattner21585222004-03-01 02:42:43 +00001507 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1508 unsigned Val = cast<ConstantInt>(Args[i].Val)->getRawValue();
1509 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1510 X86::ESP, ArgOffset).addImm(Val);
Chris Lattnerb7cb9ff2004-05-13 15:26:48 +00001511 } else if (Args[i].Val && isa<ConstantPointerNull>(Args[i].Val)) {
1512 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1513 X86::ESP, ArgOffset).addImm(0);
Chris Lattner21585222004-03-01 02:42:43 +00001514 } else {
1515 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1516 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1517 X86::ESP, ArgOffset).addReg(ArgReg);
1518 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001519 break;
Chris Lattner3e130a22003-01-13 00:32:26 +00001520 case cLong:
Chris Lattner92900a62004-04-06 03:23:00 +00001521 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1522 uint64_t Val = cast<ConstantInt>(Args[i].Val)->getRawValue();
1523 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1524 X86::ESP, ArgOffset).addImm(Val & ~0U);
1525 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1526 X86::ESP, ArgOffset+4).addImm(Val >> 32ULL);
1527 } else {
1528 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1529 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1530 X86::ESP, ArgOffset).addReg(ArgReg);
1531 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1532 X86::ESP, ArgOffset+4).addReg(ArgReg+1);
1533 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001534 ArgOffset += 4; // 8 byte entry, not 4.
1535 break;
1536
Chris Lattner065faeb2002-12-28 20:24:02 +00001537 case cFP:
Chris Lattnerce6096f2004-03-01 02:34:08 +00001538 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001539 if (Args[i].Ty == Type::FloatTy) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001540 addRegOffset(BuildMI(BB, X86::FST32m, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001541 X86::ESP, ArgOffset).addReg(ArgReg);
1542 } else {
1543 assert(Args[i].Ty == Type::DoubleTy && "Unknown FP type!");
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001544 addRegOffset(BuildMI(BB, X86::FST64m, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001545 X86::ESP, ArgOffset).addReg(ArgReg);
1546 ArgOffset += 4; // 8 byte entry, not 4.
1547 }
1548 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001549
Chris Lattner3e130a22003-01-13 00:32:26 +00001550 default: assert(0 && "Unknown class!");
Chris Lattner065faeb2002-12-28 20:24:02 +00001551 }
1552 ArgOffset += 4;
Chris Lattner94af4142002-12-25 05:13:53 +00001553 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001554 } else {
Chris Lattneree352852004-02-29 07:22:16 +00001555 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(0);
Chris Lattner94af4142002-12-25 05:13:53 +00001556 }
Chris Lattner6e49a4b2002-12-13 14:13:27 +00001557
Chris Lattner3e130a22003-01-13 00:32:26 +00001558 BB->push_back(CallMI);
Misha Brukman0d2cf3a2002-12-04 19:22:53 +00001559
Chris Lattneree352852004-02-29 07:22:16 +00001560 BuildMI(BB, X86::ADJCALLSTACKUP, 1).addImm(NumBytes);
Chris Lattnera3243642002-12-04 23:45:28 +00001561
1562 // If there is a return value, scavenge the result from the location the call
1563 // leaves it in...
1564 //
Chris Lattner3e130a22003-01-13 00:32:26 +00001565 if (Ret.Ty != Type::VoidTy) {
1566 unsigned DestClass = getClassB(Ret.Ty);
1567 switch (DestClass) {
Brian Gaeke20244b72002-12-12 15:33:40 +00001568 case cByte:
1569 case cShort:
1570 case cInt: {
1571 // Integral results are in %eax, or the appropriate portion
1572 // thereof.
1573 static const unsigned regRegMove[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001574 X86::MOV8rr, X86::MOV16rr, X86::MOV32rr
Brian Gaeke20244b72002-12-12 15:33:40 +00001575 };
1576 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX };
Chris Lattner3e130a22003-01-13 00:32:26 +00001577 BuildMI(BB, regRegMove[DestClass], 1, Ret.Reg).addReg(AReg[DestClass]);
Chris Lattner4fa1acc2002-12-04 23:50:28 +00001578 break;
Brian Gaeke20244b72002-12-12 15:33:40 +00001579 }
Chris Lattner94af4142002-12-25 05:13:53 +00001580 case cFP: // Floating-point return values live in %ST(0)
Chris Lattner3e130a22003-01-13 00:32:26 +00001581 BuildMI(BB, X86::FpGETRESULT, 1, Ret.Reg);
Brian Gaeke20244b72002-12-12 15:33:40 +00001582 break;
Chris Lattner3e130a22003-01-13 00:32:26 +00001583 case cLong: // Long values are left in EDX:EAX
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001584 BuildMI(BB, X86::MOV32rr, 1, Ret.Reg).addReg(X86::EAX);
1585 BuildMI(BB, X86::MOV32rr, 1, Ret.Reg+1).addReg(X86::EDX);
Chris Lattner3e130a22003-01-13 00:32:26 +00001586 break;
1587 default: assert(0 && "Unknown class!");
Chris Lattner4fa1acc2002-12-04 23:50:28 +00001588 }
Chris Lattnera3243642002-12-04 23:45:28 +00001589 }
Brian Gaekefa8d5712002-11-22 11:07:01 +00001590}
Chris Lattner2df035b2002-11-02 19:27:56 +00001591
Chris Lattner3e130a22003-01-13 00:32:26 +00001592
1593/// visitCallInst - Push args on stack and do a procedure call instruction.
1594void ISel::visitCallInst(CallInst &CI) {
1595 MachineInstr *TheCall;
1596 if (Function *F = CI.getCalledFunction()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001597 // Is it an intrinsic function call?
Brian Gaeked0fde302003-11-11 22:41:34 +00001598 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001599 visitIntrinsicCall(ID, CI); // Special intrinsics are not handled here
1600 return;
1601 }
1602
Chris Lattner3e130a22003-01-13 00:32:26 +00001603 // Emit a CALL instruction with PC-relative displacement.
1604 TheCall = BuildMI(X86::CALLpcrel32, 1).addGlobalAddress(F, true);
1605 } else { // Emit an indirect call...
1606 unsigned Reg = getReg(CI.getCalledValue());
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001607 TheCall = BuildMI(X86::CALL32r, 1).addReg(Reg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001608 }
1609
1610 std::vector<ValueRecord> Args;
1611 for (unsigned i = 1, e = CI.getNumOperands(); i != e; ++i)
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001612 Args.push_back(ValueRecord(CI.getOperand(i)));
Chris Lattner3e130a22003-01-13 00:32:26 +00001613
1614 unsigned DestReg = CI.getType() != Type::VoidTy ? getReg(CI) : 0;
1615 doCall(ValueRecord(DestReg, CI.getType()), TheCall, Args);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001616}
Chris Lattner3e130a22003-01-13 00:32:26 +00001617
Chris Lattner44827152003-12-28 09:47:19 +00001618/// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
1619/// function, lowering any calls to unknown intrinsic functions into the
1620/// equivalent LLVM code.
Misha Brukman538607f2004-03-01 23:53:11 +00001621///
Chris Lattner44827152003-12-28 09:47:19 +00001622void ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) {
1623 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1624 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
1625 if (CallInst *CI = dyn_cast<CallInst>(I++))
1626 if (Function *F = CI->getCalledFunction())
1627 switch (F->getIntrinsicID()) {
Chris Lattneraed386e2003-12-28 09:53:23 +00001628 case Intrinsic::not_intrinsic:
Chris Lattner317201d2004-03-13 00:24:00 +00001629 case Intrinsic::vastart:
1630 case Intrinsic::vacopy:
1631 case Intrinsic::vaend:
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001632 case Intrinsic::returnaddress:
1633 case Intrinsic::frameaddress:
Chris Lattner915e5e52004-02-12 17:53:22 +00001634 case Intrinsic::memcpy:
Chris Lattner2a0f2242004-02-14 04:46:05 +00001635 case Intrinsic::memset:
Chris Lattnerdc572442004-06-15 21:36:44 +00001636 case Intrinsic::isunordered:
John Criswell4ffff9e2004-04-08 20:31:47 +00001637 case Intrinsic::readport:
1638 case Intrinsic::writeport:
Chris Lattner44827152003-12-28 09:47:19 +00001639 // We directly implement these intrinsics
1640 break;
John Criswelle5a4c152004-04-13 22:13:14 +00001641 case Intrinsic::readio: {
1642 // On X86, memory operations are in-order. Lower this intrinsic
1643 // into a volatile load.
1644 Instruction *Before = CI->getPrev();
Chris Lattnerb4fe76c2004-06-11 04:31:10 +00001645 LoadInst * LI = new LoadInst(CI->getOperand(1), "", true, CI);
1646 CI->replaceAllUsesWith(LI);
1647 BB->getInstList().erase(CI);
John Criswelle5a4c152004-04-13 22:13:14 +00001648 break;
1649 }
1650 case Intrinsic::writeio: {
1651 // On X86, memory operations are in-order. Lower this intrinsic
1652 // into a volatile store.
1653 Instruction *Before = CI->getPrev();
Chris Lattnerb4fe76c2004-06-11 04:31:10 +00001654 StoreInst *LI = new StoreInst(CI->getOperand(1),
1655 CI->getOperand(2), true, CI);
1656 CI->replaceAllUsesWith(LI);
1657 BB->getInstList().erase(CI);
John Criswelle5a4c152004-04-13 22:13:14 +00001658 break;
1659 }
Chris Lattner44827152003-12-28 09:47:19 +00001660 default:
1661 // All other intrinsic calls we must lower.
1662 Instruction *Before = CI->getPrev();
Chris Lattnerf70e0c22003-12-28 21:23:38 +00001663 TM.getIntrinsicLowering().LowerIntrinsicCall(CI);
Chris Lattner44827152003-12-28 09:47:19 +00001664 if (Before) { // Move iterator to instruction after call
Chris Lattnerb4fe76c2004-06-11 04:31:10 +00001665 I = Before; ++I;
Chris Lattner44827152003-12-28 09:47:19 +00001666 } else {
1667 I = BB->begin();
1668 }
1669 }
Chris Lattner44827152003-12-28 09:47:19 +00001670}
1671
Brian Gaeked0fde302003-11-11 22:41:34 +00001672void ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001673 unsigned TmpReg1, TmpReg2;
1674 switch (ID) {
Chris Lattner5634b9f2004-03-13 00:24:52 +00001675 case Intrinsic::vastart:
Chris Lattnereca195e2003-05-08 19:44:13 +00001676 // Get the address of the first vararg value...
Chris Lattner73815062003-10-18 05:56:40 +00001677 TmpReg1 = getReg(CI);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001678 addFrameReference(BuildMI(BB, X86::LEA32r, 5, TmpReg1), VarArgsFrameIndex);
Chris Lattnereca195e2003-05-08 19:44:13 +00001679 return;
1680
Chris Lattner5634b9f2004-03-13 00:24:52 +00001681 case Intrinsic::vacopy:
Chris Lattner73815062003-10-18 05:56:40 +00001682 TmpReg1 = getReg(CI);
1683 TmpReg2 = getReg(CI.getOperand(1));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001684 BuildMI(BB, X86::MOV32rr, 1, TmpReg1).addReg(TmpReg2);
Chris Lattnereca195e2003-05-08 19:44:13 +00001685 return;
Chris Lattner5634b9f2004-03-13 00:24:52 +00001686 case Intrinsic::vaend: return; // Noop on X86
Chris Lattnereca195e2003-05-08 19:44:13 +00001687
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001688 case Intrinsic::returnaddress:
1689 case Intrinsic::frameaddress:
1690 TmpReg1 = getReg(CI);
1691 if (cast<Constant>(CI.getOperand(1))->isNullValue()) {
1692 if (ID == Intrinsic::returnaddress) {
1693 // Just load the return address
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001694 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, TmpReg1),
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001695 ReturnAddressIndex);
1696 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001697 addFrameReference(BuildMI(BB, X86::LEA32r, 4, TmpReg1),
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001698 ReturnAddressIndex, -4);
1699 }
1700 } else {
1701 // Values other than zero are not implemented yet.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001702 BuildMI(BB, X86::MOV32ri, 1, TmpReg1).addImm(0);
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001703 }
1704 return;
1705
Chris Lattnerdc572442004-06-15 21:36:44 +00001706 case Intrinsic::isunordered:
1707 TmpReg1 = getReg(CI.getOperand(1));
1708 TmpReg2 = getReg(CI.getOperand(2));
1709 emitUCOMr(BB, BB->end(), TmpReg2, TmpReg1);
1710 TmpReg2 = getReg(CI);
1711 BuildMI(BB, X86::SETPr, 0, TmpReg2);
1712 return;
1713
Chris Lattner915e5e52004-02-12 17:53:22 +00001714 case Intrinsic::memcpy: {
1715 assert(CI.getNumOperands() == 5 && "Illegal llvm.memcpy call!");
1716 unsigned Align = 1;
1717 if (ConstantInt *AlignC = dyn_cast<ConstantInt>(CI.getOperand(4))) {
1718 Align = AlignC->getRawValue();
1719 if (Align == 0) Align = 1;
1720 }
1721
1722 // Turn the byte code into # iterations
Chris Lattner915e5e52004-02-12 17:53:22 +00001723 unsigned CountReg;
Chris Lattner2a0f2242004-02-14 04:46:05 +00001724 unsigned Opcode;
Chris Lattner915e5e52004-02-12 17:53:22 +00001725 switch (Align & 3) {
1726 case 2: // WORD aligned
Chris Lattner07122832004-02-13 23:36:47 +00001727 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
1728 CountReg = getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/2));
1729 } else {
1730 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001731 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001732 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
Chris Lattner07122832004-02-13 23:36:47 +00001733 }
Chris Lattner2a0f2242004-02-14 04:46:05 +00001734 Opcode = X86::REP_MOVSW;
Chris Lattner915e5e52004-02-12 17:53:22 +00001735 break;
1736 case 0: // DWORD aligned
Chris Lattner07122832004-02-13 23:36:47 +00001737 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
1738 CountReg = getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/4));
1739 } else {
1740 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001741 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001742 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
Chris Lattner07122832004-02-13 23:36:47 +00001743 }
Chris Lattner2a0f2242004-02-14 04:46:05 +00001744 Opcode = X86::REP_MOVSD;
Chris Lattner915e5e52004-02-12 17:53:22 +00001745 break;
Chris Lattner8dd8d262004-02-26 01:20:02 +00001746 default: // BYTE aligned
Chris Lattner07122832004-02-13 23:36:47 +00001747 CountReg = getReg(CI.getOperand(3));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001748 Opcode = X86::REP_MOVSB;
Chris Lattner915e5e52004-02-12 17:53:22 +00001749 break;
1750 }
1751
1752 // No matter what the alignment is, we put the source in ESI, the
1753 // destination in EDI, and the count in ECX.
1754 TmpReg1 = getReg(CI.getOperand(1));
1755 TmpReg2 = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001756 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
1757 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
1758 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001759 BuildMI(BB, Opcode, 0);
1760 return;
1761 }
1762 case Intrinsic::memset: {
1763 assert(CI.getNumOperands() == 5 && "Illegal llvm.memset call!");
1764 unsigned Align = 1;
1765 if (ConstantInt *AlignC = dyn_cast<ConstantInt>(CI.getOperand(4))) {
1766 Align = AlignC->getRawValue();
1767 if (Align == 0) Align = 1;
Chris Lattner915e5e52004-02-12 17:53:22 +00001768 }
1769
Chris Lattner2a0f2242004-02-14 04:46:05 +00001770 // Turn the byte code into # iterations
Chris Lattner2a0f2242004-02-14 04:46:05 +00001771 unsigned CountReg;
1772 unsigned Opcode;
1773 if (ConstantInt *ValC = dyn_cast<ConstantInt>(CI.getOperand(2))) {
1774 unsigned Val = ValC->getRawValue() & 255;
1775
1776 // If the value is a constant, then we can potentially use larger copies.
1777 switch (Align & 3) {
1778 case 2: // WORD aligned
1779 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
Chris Lattner300d0ed2004-02-14 06:00:36 +00001780 CountReg =getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/2));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001781 } else {
1782 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001783 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001784 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001785 }
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001786 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001787 Opcode = X86::REP_STOSW;
1788 break;
1789 case 0: // DWORD aligned
1790 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
Chris Lattner300d0ed2004-02-14 06:00:36 +00001791 CountReg =getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/4));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001792 } else {
1793 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001794 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001795 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001796 }
1797 Val = (Val << 8) | Val;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001798 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001799 Opcode = X86::REP_STOSD;
1800 break;
Chris Lattner8dd8d262004-02-26 01:20:02 +00001801 default: // BYTE aligned
Chris Lattner2a0f2242004-02-14 04:46:05 +00001802 CountReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001803 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001804 Opcode = X86::REP_STOSB;
1805 break;
1806 }
1807 } else {
1808 // If it's not a constant value we are storing, just fall back. We could
1809 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
1810 unsigned ValReg = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001811 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001812 CountReg = getReg(CI.getOperand(3));
1813 Opcode = X86::REP_STOSB;
1814 }
1815
1816 // No matter what the alignment is, we put the source in ESI, the
1817 // destination in EDI, and the count in ECX.
1818 TmpReg1 = getReg(CI.getOperand(1));
1819 //TmpReg2 = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001820 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
1821 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001822 BuildMI(BB, Opcode, 0);
Chris Lattner915e5e52004-02-12 17:53:22 +00001823 return;
1824 }
1825
Chris Lattner87e18de2004-04-13 17:20:37 +00001826 case Intrinsic::readport: {
1827 // First, determine that the size of the operand falls within the acceptable
1828 // range for this architecture.
John Criswell4ffff9e2004-04-08 20:31:47 +00001829 //
Chris Lattner87e18de2004-04-13 17:20:37 +00001830 if (getClassB(CI.getOperand(1)->getType()) != cShort) {
John Criswellca6ea0f2004-04-08 22:39:13 +00001831 std::cerr << "llvm.readport: Address size is not 16 bits\n";
Chris Lattner87e18de2004-04-13 17:20:37 +00001832 exit(1);
John Criswellca6ea0f2004-04-08 22:39:13 +00001833 }
John Criswell4ffff9e2004-04-08 20:31:47 +00001834
John Criswell4ffff9e2004-04-08 20:31:47 +00001835 // Now, move the I/O port address into the DX register and use the IN
1836 // instruction to get the input data.
1837 //
Chris Lattner87e18de2004-04-13 17:20:37 +00001838 unsigned Class = getClass(CI.getCalledFunction()->getReturnType());
1839 unsigned DestReg = getReg(CI);
John Criswell4ffff9e2004-04-08 20:31:47 +00001840
Chris Lattner87e18de2004-04-13 17:20:37 +00001841 // If the port is a single-byte constant, use the immediate form.
1842 if (ConstantInt *C = dyn_cast<ConstantInt>(CI.getOperand(1)))
1843 if ((C->getRawValue() & 255) == C->getRawValue()) {
1844 switch (Class) {
1845 case cByte:
1846 BuildMI(BB, X86::IN8ri, 1).addImm((unsigned char)C->getRawValue());
1847 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AL);
1848 return;
1849 case cShort:
1850 BuildMI(BB, X86::IN16ri, 1).addImm((unsigned char)C->getRawValue());
1851 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AX);
1852 return;
1853 case cInt:
1854 BuildMI(BB, X86::IN32ri, 1).addImm((unsigned char)C->getRawValue());
1855 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::EAX);
1856 return;
1857 }
1858 }
1859
1860 unsigned Reg = getReg(CI.getOperand(1));
1861 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
1862 switch (Class) {
1863 case cByte:
1864 BuildMI(BB, X86::IN8rr, 0);
1865 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AL);
1866 break;
1867 case cShort:
1868 BuildMI(BB, X86::IN16rr, 0);
1869 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AX);
1870 break;
1871 case cInt:
1872 BuildMI(BB, X86::IN32rr, 0);
1873 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::EAX);
1874 break;
1875 default:
1876 std::cerr << "Cannot do input on this data type";
John Criswellca6ea0f2004-04-08 22:39:13 +00001877 exit (1);
1878 }
John Criswell4ffff9e2004-04-08 20:31:47 +00001879 return;
Chris Lattner87e18de2004-04-13 17:20:37 +00001880 }
John Criswell4ffff9e2004-04-08 20:31:47 +00001881
Chris Lattner87e18de2004-04-13 17:20:37 +00001882 case Intrinsic::writeport: {
1883 // First, determine that the size of the operand falls within the
1884 // acceptable range for this architecture.
1885 if (getClass(CI.getOperand(2)->getType()) != cShort) {
1886 std::cerr << "llvm.writeport: Address size is not 16 bits\n";
1887 exit(1);
1888 }
1889
1890 unsigned Class = getClassB(CI.getOperand(1)->getType());
1891 unsigned ValReg = getReg(CI.getOperand(1));
1892 switch (Class) {
1893 case cByte:
1894 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
1895 break;
1896 case cShort:
1897 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(ValReg);
1898 break;
1899 case cInt:
1900 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(ValReg);
1901 break;
1902 default:
1903 std::cerr << "llvm.writeport: invalid data type for X86 target";
1904 exit(1);
1905 }
1906
1907
1908 // If the port is a single-byte constant, use the immediate form.
1909 if (ConstantInt *C = dyn_cast<ConstantInt>(CI.getOperand(2)))
1910 if ((C->getRawValue() & 255) == C->getRawValue()) {
1911 static const unsigned O[] = { X86::OUT8ir, X86::OUT16ir, X86::OUT32ir };
1912 BuildMI(BB, O[Class], 1).addImm((unsigned char)C->getRawValue());
1913 return;
1914 }
1915
1916 // Otherwise, move the I/O port address into the DX register and the value
1917 // to write into the AL/AX/EAX register.
1918 static const unsigned Opc[] = { X86::OUT8rr, X86::OUT16rr, X86::OUT32rr };
1919 unsigned Reg = getReg(CI.getOperand(2));
1920 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
1921 BuildMI(BB, Opc[Class], 0);
1922 return;
1923 }
1924
Chris Lattner44827152003-12-28 09:47:19 +00001925 default: assert(0 && "Error: unknown intrinsics should have been lowered!");
Chris Lattnereca195e2003-05-08 19:44:13 +00001926 }
1927}
1928
Chris Lattner7dee5da2004-03-08 01:58:35 +00001929static bool isSafeToFoldLoadIntoInstruction(LoadInst &LI, Instruction &User) {
1930 if (LI.getParent() != User.getParent())
1931 return false;
1932 BasicBlock::iterator It = &LI;
1933 // Check all of the instructions between the load and the user. We should
1934 // really use alias analysis here, but for now we just do something simple.
1935 for (++It; It != BasicBlock::iterator(&User); ++It) {
1936 switch (It->getOpcode()) {
Chris Lattner85c84e72004-03-18 06:29:54 +00001937 case Instruction::Free:
Chris Lattner7dee5da2004-03-08 01:58:35 +00001938 case Instruction::Store:
1939 case Instruction::Call:
1940 case Instruction::Invoke:
1941 return false;
Chris Lattner133dbb12004-04-12 03:02:48 +00001942 case Instruction::Load:
1943 if (cast<LoadInst>(It)->isVolatile() && LI.isVolatile())
1944 return false;
1945 break;
Chris Lattner7dee5da2004-03-08 01:58:35 +00001946 }
1947 }
1948 return true;
1949}
1950
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001951/// visitSimpleBinary - Implement simple binary operators for integral types...
1952/// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or, 4 for
1953/// Xor.
Misha Brukman538607f2004-03-01 23:53:11 +00001954///
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001955void ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) {
1956 unsigned DestReg = getReg(B);
1957 MachineBasicBlock::iterator MI = BB->end();
Chris Lattner721d2d42004-03-08 01:18:36 +00001958 Value *Op0 = B.getOperand(0), *Op1 = B.getOperand(1);
Chris Lattnerc81e6ba2004-05-10 15:15:55 +00001959 unsigned Class = getClassB(B.getType());
Chris Lattner721d2d42004-03-08 01:18:36 +00001960
Chris Lattner7dee5da2004-03-08 01:58:35 +00001961 // Special case: op Reg, load [mem]
Chris Lattnerc81e6ba2004-05-10 15:15:55 +00001962 if (isa<LoadInst>(Op0) && !isa<LoadInst>(Op1) && Class != cLong &&
Chris Lattnerccd97962004-06-17 22:15:25 +00001963 Op0->hasOneUse() &&
Chris Lattnerc81e6ba2004-05-10 15:15:55 +00001964 isSafeToFoldLoadIntoInstruction(*cast<LoadInst>(Op0), B))
Chris Lattner7dee5da2004-03-08 01:58:35 +00001965 if (!B.swapOperands())
1966 std::swap(Op0, Op1); // Make sure any loads are in the RHS.
1967
Chris Lattnerccd97962004-06-17 22:15:25 +00001968 if (isa<LoadInst>(Op1) && Class != cLong && Op1->hasOneUse() &&
Chris Lattner7dee5da2004-03-08 01:58:35 +00001969 isSafeToFoldLoadIntoInstruction(*cast<LoadInst>(Op1), B)) {
1970
Chris Lattner95157f72004-04-11 22:05:45 +00001971 unsigned Opcode;
1972 if (Class != cFP) {
1973 static const unsigned OpcodeTab[][3] = {
1974 // Arithmetic operators
1975 { X86::ADD8rm, X86::ADD16rm, X86::ADD32rm }, // ADD
1976 { X86::SUB8rm, X86::SUB16rm, X86::SUB32rm }, // SUB
1977
1978 // Bitwise operators
1979 { X86::AND8rm, X86::AND16rm, X86::AND32rm }, // AND
1980 { X86:: OR8rm, X86:: OR16rm, X86:: OR32rm }, // OR
1981 { X86::XOR8rm, X86::XOR16rm, X86::XOR32rm }, // XOR
1982 };
1983 Opcode = OpcodeTab[OperatorClass][Class];
1984 } else {
1985 static const unsigned OpcodeTab[][2] = {
1986 { X86::FADD32m, X86::FADD64m }, // ADD
1987 { X86::FSUB32m, X86::FSUB64m }, // SUB
1988 };
1989 const Type *Ty = Op0->getType();
1990 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
1991 Opcode = OpcodeTab[OperatorClass][Ty == Type::DoubleTy];
1992 }
Chris Lattner7dee5da2004-03-08 01:58:35 +00001993
Chris Lattner7dee5da2004-03-08 01:58:35 +00001994 unsigned Op0r = getReg(Op0);
Chris Lattner9f1b5312004-05-13 15:12:43 +00001995 if (AllocaInst *AI =
1996 dyn_castFixedAlloca(cast<LoadInst>(Op1)->getOperand(0))) {
1997 unsigned FI = getFixedSizedAllocaFI(AI);
1998 addFrameReference(BuildMI(BB, Opcode, 5, DestReg).addReg(Op0r), FI);
1999
2000 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002001 X86AddressMode AM;
2002 getAddressingMode(cast<LoadInst>(Op1)->getOperand(0), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002003
Reid Spencerfc989e12004-08-30 00:13:26 +00002004 addFullAddress(BuildMI(BB, Opcode, 5, DestReg).addReg(Op0r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002005 }
Chris Lattner7dee5da2004-03-08 01:58:35 +00002006 return;
2007 }
2008
Chris Lattner95157f72004-04-11 22:05:45 +00002009 // If this is a floating point subtract, check to see if we can fold the first
2010 // operand in.
2011 if (Class == cFP && OperatorClass == 1 &&
2012 isa<LoadInst>(Op0) &&
2013 isSafeToFoldLoadIntoInstruction(*cast<LoadInst>(Op0), B)) {
2014 const Type *Ty = Op0->getType();
2015 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
2016 unsigned Opcode = Ty == Type::FloatTy ? X86::FSUBR32m : X86::FSUBR64m;
2017
Chris Lattner95157f72004-04-11 22:05:45 +00002018 unsigned Op1r = getReg(Op1);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002019 if (AllocaInst *AI =
2020 dyn_castFixedAlloca(cast<LoadInst>(Op0)->getOperand(0))) {
2021 unsigned FI = getFixedSizedAllocaFI(AI);
2022 addFrameReference(BuildMI(BB, Opcode, 5, DestReg).addReg(Op1r), FI);
2023 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002024 X86AddressMode AM;
2025 getAddressingMode(cast<LoadInst>(Op0)->getOperand(0), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002026
Reid Spencerfc989e12004-08-30 00:13:26 +00002027 addFullAddress(BuildMI(BB, Opcode, 5, DestReg).addReg(Op1r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002028 }
Chris Lattner95157f72004-04-11 22:05:45 +00002029 return;
2030 }
2031
Chris Lattner721d2d42004-03-08 01:18:36 +00002032 emitSimpleBinaryOperation(BB, MI, Op0, Op1, OperatorClass, DestReg);
Chris Lattnerb515f6d2003-05-08 20:49:25 +00002033}
Chris Lattner3e130a22003-01-13 00:32:26 +00002034
Chris Lattner6621ed92004-04-11 21:23:56 +00002035
2036/// emitBinaryFPOperation - This method handles emission of floating point
2037/// Add (0), Sub (1), Mul (2), and Div (3) operations.
2038void ISel::emitBinaryFPOperation(MachineBasicBlock *BB,
2039 MachineBasicBlock::iterator IP,
2040 Value *Op0, Value *Op1,
2041 unsigned OperatorClass, unsigned DestReg) {
2042
2043 // Special case: op Reg, <const fp>
2044 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1))
2045 if (!Op1C->isExactlyValue(+0.0) && !Op1C->isExactlyValue(+1.0)) {
2046 // Create a constant pool entry for this constant.
2047 MachineConstantPool *CP = F->getConstantPool();
2048 unsigned CPI = CP->getConstantPoolIndex(Op1C);
2049 const Type *Ty = Op1->getType();
2050
2051 static const unsigned OpcodeTab[][4] = {
2052 { X86::FADD32m, X86::FSUB32m, X86::FMUL32m, X86::FDIV32m }, // Float
2053 { X86::FADD64m, X86::FSUB64m, X86::FMUL64m, X86::FDIV64m }, // Double
2054 };
2055
2056 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
2057 unsigned Opcode = OpcodeTab[Ty != Type::FloatTy][OperatorClass];
2058 unsigned Op0r = getReg(Op0, BB, IP);
2059 addConstantPoolReference(BuildMI(*BB, IP, Opcode, 5,
2060 DestReg).addReg(Op0r), CPI);
2061 return;
2062 }
2063
Chris Lattner13c07fe2004-04-12 00:12:04 +00002064 // Special case: R1 = op <const fp>, R2
Chris Lattner6621ed92004-04-11 21:23:56 +00002065 if (ConstantFP *CFP = dyn_cast<ConstantFP>(Op0))
2066 if (CFP->isExactlyValue(-0.0) && OperatorClass == 1) {
2067 // -0.0 - X === -X
2068 unsigned op1Reg = getReg(Op1, BB, IP);
2069 BuildMI(*BB, IP, X86::FCHS, 1, DestReg).addReg(op1Reg);
2070 return;
2071 } else if (!CFP->isExactlyValue(+0.0) && !CFP->isExactlyValue(+1.0)) {
Chris Lattner13c07fe2004-04-12 00:12:04 +00002072 // R1 = op CST, R2 --> R1 = opr R2, CST
Chris Lattner6621ed92004-04-11 21:23:56 +00002073
2074 // Create a constant pool entry for this constant.
2075 MachineConstantPool *CP = F->getConstantPool();
2076 unsigned CPI = CP->getConstantPoolIndex(CFP);
2077 const Type *Ty = CFP->getType();
2078
2079 static const unsigned OpcodeTab[][4] = {
2080 { X86::FADD32m, X86::FSUBR32m, X86::FMUL32m, X86::FDIVR32m }, // Float
2081 { X86::FADD64m, X86::FSUBR64m, X86::FMUL64m, X86::FDIVR64m }, // Double
2082 };
2083
2084 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2085 unsigned Opcode = OpcodeTab[Ty != Type::FloatTy][OperatorClass];
2086 unsigned Op1r = getReg(Op1, BB, IP);
2087 addConstantPoolReference(BuildMI(*BB, IP, Opcode, 5,
2088 DestReg).addReg(Op1r), CPI);
2089 return;
2090 }
2091
2092 // General case.
2093 static const unsigned OpcodeTab[4] = {
2094 X86::FpADD, X86::FpSUB, X86::FpMUL, X86::FpDIV
2095 };
2096
2097 unsigned Opcode = OpcodeTab[OperatorClass];
2098 unsigned Op0r = getReg(Op0, BB, IP);
2099 unsigned Op1r = getReg(Op1, BB, IP);
2100 BuildMI(*BB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r);
2101}
2102
Chris Lattnerb2acc512003-10-19 21:09:10 +00002103/// emitSimpleBinaryOperation - Implement simple binary operators for integral
2104/// types... OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for
2105/// Or, 4 for Xor.
Chris Lattner68aad932002-11-02 20:13:22 +00002106///
Chris Lattnerb515f6d2003-05-08 20:49:25 +00002107/// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
2108/// and constant expression support.
Chris Lattnerb2acc512003-10-19 21:09:10 +00002109///
2110void ISel::emitSimpleBinaryOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002111 MachineBasicBlock::iterator IP,
Chris Lattnerb515f6d2003-05-08 20:49:25 +00002112 Value *Op0, Value *Op1,
Chris Lattnerb2acc512003-10-19 21:09:10 +00002113 unsigned OperatorClass, unsigned DestReg) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +00002114 unsigned Class = getClassB(Op0->getType());
Chris Lattnerb2acc512003-10-19 21:09:10 +00002115
Chris Lattner6621ed92004-04-11 21:23:56 +00002116 if (Class == cFP) {
2117 assert(OperatorClass < 2 && "No logical ops for FP!");
2118 emitBinaryFPOperation(MBB, IP, Op0, Op1, OperatorClass, DestReg);
2119 return;
2120 }
2121
Chris Lattner48b0c972004-04-11 20:26:20 +00002122 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0))
Chris Lattner667ea022004-06-18 00:50:37 +00002123 if (OperatorClass == 1) {
Chris Lattner48b0c972004-04-11 20:26:20 +00002124 static unsigned const NEGTab[] = {
2125 X86::NEG8r, X86::NEG16r, X86::NEG32r, 0, X86::NEG32r
2126 };
Chris Lattner667ea022004-06-18 00:50:37 +00002127
2128 // sub 0, X -> neg X
2129 if (CI->isNullValue()) {
2130 unsigned op1Reg = getReg(Op1, MBB, IP);
2131 BuildMI(*MBB, IP, NEGTab[Class], 1, DestReg).addReg(op1Reg);
Chris Lattner48b0c972004-04-11 20:26:20 +00002132
Chris Lattner667ea022004-06-18 00:50:37 +00002133 if (Class == cLong) {
2134 // We just emitted: Dl = neg Sl
2135 // Now emit : T = addc Sh, 0
2136 // : Dh = neg T
2137 unsigned T = makeAnotherReg(Type::IntTy);
2138 BuildMI(*MBB, IP, X86::ADC32ri, 2, T).addReg(op1Reg+1).addImm(0);
2139 BuildMI(*MBB, IP, X86::NEG32r, 1, DestReg+1).addReg(T);
2140 }
2141 return;
2142 } else if (Op1->hasOneUse() && Class != cLong) {
2143 // sub C, X -> tmp = neg X; DestReg = add tmp, C. This is better
2144 // than copying C into a temporary register, because of register
2145 // pressure (tmp and destreg can share a register.
2146 static unsigned const ADDRITab[] = {
2147 X86::ADD8ri, X86::ADD16ri, X86::ADD32ri, 0, X86::ADD32ri
2148 };
2149 unsigned op1Reg = getReg(Op1, MBB, IP);
2150 unsigned Tmp = makeAnotherReg(Op0->getType());
2151 BuildMI(*MBB, IP, NEGTab[Class], 1, Tmp).addReg(op1Reg);
Chris Lattner30483732004-06-20 07:49:54 +00002152 BuildMI(*MBB, IP, ADDRITab[Class], 2,
2153 DestReg).addReg(Tmp).addImm(CI->getRawValue());
Chris Lattner667ea022004-06-18 00:50:37 +00002154 return;
Chris Lattnerb2acc512003-10-19 21:09:10 +00002155 }
Chris Lattner48b0c972004-04-11 20:26:20 +00002156 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002157
Chris Lattner48b0c972004-04-11 20:26:20 +00002158 // Special case: op Reg, <const int>
2159 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner721d2d42004-03-08 01:18:36 +00002160 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002161
Chris Lattner721d2d42004-03-08 01:18:36 +00002162 // xor X, -1 -> not X
2163 if (OperatorClass == 4 && Op1C->isAllOnesValue()) {
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002164 static unsigned const NOTTab[] = {
2165 X86::NOT8r, X86::NOT16r, X86::NOT32r, 0, X86::NOT32r
2166 };
Chris Lattner721d2d42004-03-08 01:18:36 +00002167 BuildMI(*MBB, IP, NOTTab[Class], 1, DestReg).addReg(Op0r);
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002168 if (Class == cLong) // Invert the top part too
2169 BuildMI(*MBB, IP, X86::NOT32r, 1, DestReg+1).addReg(Op0r+1);
Chris Lattner721d2d42004-03-08 01:18:36 +00002170 return;
2171 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002172
Chris Lattner721d2d42004-03-08 01:18:36 +00002173 // add X, -1 -> dec X
Chris Lattner06521672004-04-06 03:36:57 +00002174 if (OperatorClass == 0 && Op1C->isAllOnesValue() && Class != cLong) {
2175 // Note that we can't use dec for 64-bit decrements, because it does not
2176 // set the carry flag!
2177 static unsigned const DECTab[] = { X86::DEC8r, X86::DEC16r, X86::DEC32r };
Chris Lattner721d2d42004-03-08 01:18:36 +00002178 BuildMI(*MBB, IP, DECTab[Class], 1, DestReg).addReg(Op0r);
2179 return;
2180 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002181
Chris Lattner721d2d42004-03-08 01:18:36 +00002182 // add X, 1 -> inc X
Chris Lattner06521672004-04-06 03:36:57 +00002183 if (OperatorClass == 0 && Op1C->equalsInt(1) && Class != cLong) {
2184 // Note that we can't use inc for 64-bit increments, because it does not
2185 // set the carry flag!
2186 static unsigned const INCTab[] = { X86::INC8r, X86::INC16r, X86::INC32r };
Alkis Evlogimenosbee8a092004-04-02 18:11:32 +00002187 BuildMI(*MBB, IP, INCTab[Class], 1, DestReg).addReg(Op0r);
Chris Lattner721d2d42004-03-08 01:18:36 +00002188 return;
2189 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002190
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002191 static const unsigned OpcodeTab[][5] = {
Chris Lattner721d2d42004-03-08 01:18:36 +00002192 // Arithmetic operators
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002193 { X86::ADD8ri, X86::ADD16ri, X86::ADD32ri, 0, X86::ADD32ri }, // ADD
2194 { X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, X86::SUB32ri }, // SUB
Chris Lattnerb2acc512003-10-19 21:09:10 +00002195
Chris Lattner721d2d42004-03-08 01:18:36 +00002196 // Bitwise operators
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002197 { X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, X86::AND32ri }, // AND
2198 { X86:: OR8ri, X86:: OR16ri, X86:: OR32ri, 0, X86::OR32ri }, // OR
2199 { X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, X86::XOR32ri }, // XOR
Chris Lattner721d2d42004-03-08 01:18:36 +00002200 };
2201
Chris Lattner721d2d42004-03-08 01:18:36 +00002202 unsigned Opcode = OpcodeTab[OperatorClass][Class];
Chris Lattner33f7fa32004-04-06 03:15:53 +00002203 unsigned Op1l = cast<ConstantInt>(Op1C)->getRawValue();
Chris Lattner721d2d42004-03-08 01:18:36 +00002204
Chris Lattner33f7fa32004-04-06 03:15:53 +00002205 if (Class != cLong) {
2206 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addImm(Op1l);
2207 return;
Chris Lattner6621ed92004-04-11 21:23:56 +00002208 }
2209
2210 // If this is a long value and the high or low bits have a special
2211 // property, emit some special cases.
2212 unsigned Op1h = cast<ConstantInt>(Op1C)->getRawValue() >> 32LL;
2213
2214 // If the constant is zero in the low 32-bits, just copy the low part
2215 // across and apply the normal 32-bit operation to the high parts. There
2216 // will be no carry or borrow into the top.
2217 if (Op1l == 0) {
2218 if (OperatorClass != 2) // All but and...
2219 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg).addReg(Op0r);
2220 else
2221 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
2222 BuildMI(*MBB, IP, OpcodeTab[OperatorClass][cLong], 2, DestReg+1)
2223 .addReg(Op0r+1).addImm(Op1h);
Chris Lattner33f7fa32004-04-06 03:15:53 +00002224 return;
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002225 }
Chris Lattner6621ed92004-04-11 21:23:56 +00002226
2227 // If this is a logical operation and the top 32-bits are zero, just
2228 // operate on the lower 32.
2229 if (Op1h == 0 && OperatorClass > 1) {
2230 BuildMI(*MBB, IP, OpcodeTab[OperatorClass][cLong], 2, DestReg)
2231 .addReg(Op0r).addImm(Op1l);
2232 if (OperatorClass != 2) // All but and
2233 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg+1).addReg(Op0r+1);
2234 else
2235 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
2236 return;
2237 }
2238
2239 // TODO: We could handle lots of other special cases here, such as AND'ing
2240 // with 0xFFFFFFFF00000000 -> noop, etc.
2241
2242 // Otherwise, code generate the full operation with a constant.
2243 static const unsigned TopTab[] = {
2244 X86::ADC32ri, X86::SBB32ri, X86::AND32ri, X86::OR32ri, X86::XOR32ri
2245 };
2246
2247 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addImm(Op1l);
2248 BuildMI(*MBB, IP, TopTab[OperatorClass], 2, DestReg+1)
2249 .addReg(Op0r+1).addImm(Op1h);
2250 return;
Chris Lattner721d2d42004-03-08 01:18:36 +00002251 }
2252
2253 // Finally, handle the general case now.
Chris Lattner7ba92302004-04-06 02:13:25 +00002254 static const unsigned OpcodeTab[][5] = {
Chris Lattner721d2d42004-03-08 01:18:36 +00002255 // Arithmetic operators
Chris Lattner6621ed92004-04-11 21:23:56 +00002256 { X86::ADD8rr, X86::ADD16rr, X86::ADD32rr, 0, X86::ADD32rr }, // ADD
2257 { X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, 0, X86::SUB32rr }, // SUB
Chris Lattner721d2d42004-03-08 01:18:36 +00002258
Chris Lattnerb2acc512003-10-19 21:09:10 +00002259 // Bitwise operators
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002260 { X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, X86::AND32rr }, // AND
2261 { X86:: OR8rr, X86:: OR16rr, X86:: OR32rr, 0, X86:: OR32rr }, // OR
2262 { X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, X86::XOR32rr }, // XOR
Chris Lattnerb2acc512003-10-19 21:09:10 +00002263 };
Chris Lattner721d2d42004-03-08 01:18:36 +00002264
Chris Lattnerb2acc512003-10-19 21:09:10 +00002265 unsigned Opcode = OpcodeTab[OperatorClass][Class];
Chris Lattner721d2d42004-03-08 01:18:36 +00002266 unsigned Op0r = getReg(Op0, MBB, IP);
2267 unsigned Op1r = getReg(Op1, MBB, IP);
2268 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r);
2269
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002270 if (Class == cLong) { // Handle the upper 32 bits of long values...
Chris Lattner721d2d42004-03-08 01:18:36 +00002271 static const unsigned TopTab[] = {
2272 X86::ADC32rr, X86::SBB32rr, X86::AND32rr, X86::OR32rr, X86::XOR32rr
2273 };
2274 BuildMI(*MBB, IP, TopTab[OperatorClass], 2,
2275 DestReg+1).addReg(Op0r+1).addReg(Op1r+1);
2276 }
Chris Lattnere2954c82002-11-02 20:04:26 +00002277}
2278
Chris Lattner3e130a22003-01-13 00:32:26 +00002279/// doMultiply - Emit appropriate instructions to multiply together the
2280/// registers op0Reg and op1Reg, and put the result in DestReg. The type of the
2281/// result should be given as DestTy.
2282///
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002283void ISel::doMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI,
Chris Lattner3e130a22003-01-13 00:32:26 +00002284 unsigned DestReg, const Type *DestTy,
Chris Lattner8a307e82002-12-16 19:32:50 +00002285 unsigned op0Reg, unsigned op1Reg) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002286 unsigned Class = getClass(DestTy);
Chris Lattner94af4142002-12-25 05:13:53 +00002287 switch (Class) {
Chris Lattner0f1c4612003-06-21 17:16:58 +00002288 case cInt:
2289 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002290 BuildMI(*MBB, MBBI, Class == cInt ? X86::IMUL32rr:X86::IMUL16rr, 2, DestReg)
Chris Lattner0f1c4612003-06-21 17:16:58 +00002291 .addReg(op0Reg).addReg(op1Reg);
2292 return;
2293 case cByte:
2294 // Must use the MUL instruction, which forces use of AL...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002295 BuildMI(*MBB, MBBI, X86::MOV8rr, 1, X86::AL).addReg(op0Reg);
2296 BuildMI(*MBB, MBBI, X86::MUL8r, 1).addReg(op1Reg);
2297 BuildMI(*MBB, MBBI, X86::MOV8rr, 1, DestReg).addReg(X86::AL);
Chris Lattner0f1c4612003-06-21 17:16:58 +00002298 return;
Chris Lattner94af4142002-12-25 05:13:53 +00002299 default:
Chris Lattner3e130a22003-01-13 00:32:26 +00002300 case cLong: assert(0 && "doMultiply cannot operate on LONG values!");
Chris Lattner94af4142002-12-25 05:13:53 +00002301 }
Brian Gaeke20244b72002-12-12 15:33:40 +00002302}
2303
Chris Lattnerb2acc512003-10-19 21:09:10 +00002304// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
2305// returns zero when the input is not exactly a power of two.
2306static unsigned ExactLog2(unsigned Val) {
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002307 if (Val == 0 || (Val & (Val-1))) return 0;
Chris Lattnerb2acc512003-10-19 21:09:10 +00002308 unsigned Count = 0;
2309 while (Val != 1) {
Chris Lattnerb2acc512003-10-19 21:09:10 +00002310 Val >>= 1;
2311 ++Count;
2312 }
2313 return Count+1;
2314}
2315
Chris Lattner462fa822004-04-11 20:56:28 +00002316
2317/// doMultiplyConst - This function is specialized to efficiently codegen an 8,
2318/// 16, or 32-bit integer multiply by a constant.
Chris Lattnerb2acc512003-10-19 21:09:10 +00002319void ISel::doMultiplyConst(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002320 MachineBasicBlock::iterator IP,
Chris Lattnerb2acc512003-10-19 21:09:10 +00002321 unsigned DestReg, const Type *DestTy,
2322 unsigned op0Reg, unsigned ConstRHS) {
Chris Lattner6ab06d52004-04-06 04:55:43 +00002323 static const unsigned MOVrrTab[] = {X86::MOV8rr, X86::MOV16rr, X86::MOV32rr};
2324 static const unsigned MOVriTab[] = {X86::MOV8ri, X86::MOV16ri, X86::MOV32ri};
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002325 static const unsigned ADDrrTab[] = {X86::ADD8rr, X86::ADD16rr, X86::ADD32rr};
Chris Lattner596b97f2004-07-19 23:47:21 +00002326 static const unsigned NEGrTab[] = {X86::NEG8r , X86::NEG16r , X86::NEG32r };
Chris Lattner6ab06d52004-04-06 04:55:43 +00002327
Chris Lattnerb2acc512003-10-19 21:09:10 +00002328 unsigned Class = getClass(DestTy);
Chris Lattner596b97f2004-07-19 23:47:21 +00002329 unsigned TmpReg;
Chris Lattnerb2acc512003-10-19 21:09:10 +00002330
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002331 // Handle special cases here.
2332 switch (ConstRHS) {
Chris Lattner596b97f2004-07-19 23:47:21 +00002333 case -2:
2334 TmpReg = makeAnotherReg(DestTy);
2335 BuildMI(*MBB, IP, NEGrTab[Class], 1, TmpReg).addReg(op0Reg);
2336 BuildMI(*MBB, IP, ADDrrTab[Class], 1,DestReg).addReg(TmpReg).addReg(TmpReg);
2337 return;
2338 case -1:
2339 BuildMI(*MBB, IP, NEGrTab[Class], 1, DestReg).addReg(op0Reg);
2340 return;
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002341 case 0:
Chris Lattner6ab06d52004-04-06 04:55:43 +00002342 BuildMI(*MBB, IP, MOVriTab[Class], 1, DestReg).addImm(0);
2343 return;
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002344 case 1:
Chris Lattner6ab06d52004-04-06 04:55:43 +00002345 BuildMI(*MBB, IP, MOVrrTab[Class], 1, DestReg).addReg(op0Reg);
2346 return;
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002347 case 2:
2348 BuildMI(*MBB, IP, ADDrrTab[Class], 1,DestReg).addReg(op0Reg).addReg(op0Reg);
2349 return;
2350 case 3:
2351 case 5:
2352 case 9:
2353 if (Class == cInt) {
Reid Spencerfc989e12004-08-30 00:13:26 +00002354 X86AddressMode AM;
2355 AM.BaseType = X86AddressMode::RegBase;
2356 AM.Base.Reg = op0Reg;
2357 AM.Scale = ConstRHS-1;
2358 AM.IndexReg = op0Reg;
2359 AM.Disp = 0;
2360 addFullAddress(BuildMI(*MBB, IP, X86::LEA32r, 5, DestReg), AM);
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002361 return;
2362 }
Chris Lattner596b97f2004-07-19 23:47:21 +00002363 case -3:
2364 case -5:
2365 case -9:
2366 if (Class == cInt) {
2367 TmpReg = makeAnotherReg(DestTy);
Reid Spencerfc989e12004-08-30 00:13:26 +00002368 X86AddressMode AM;
2369 AM.BaseType = X86AddressMode::RegBase;
2370 AM.Base.Reg = op0Reg;
2371 AM.Scale = -ConstRHS-1;
2372 AM.IndexReg = op0Reg;
2373 AM.Disp = 0;
2374 addFullAddress(BuildMI(*MBB, IP, X86::LEA32r, 5, TmpReg), AM);
Chris Lattner596b97f2004-07-19 23:47:21 +00002375 BuildMI(*MBB, IP, NEGrTab[Class], 1, DestReg).addReg(TmpReg);
2376 return;
2377 }
Chris Lattner6ab06d52004-04-06 04:55:43 +00002378 }
2379
Chris Lattnerb2acc512003-10-19 21:09:10 +00002380 // If the element size is exactly a power of 2, use a shift to get it.
2381 if (unsigned Shift = ExactLog2(ConstRHS)) {
2382 switch (Class) {
2383 default: assert(0 && "Unknown class for this function!");
2384 case cByte:
Chris Lattner596b97f2004-07-19 23:47:21 +00002385 BuildMI(*MBB, IP, X86::SHL8ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002386 return;
2387 case cShort:
Chris Lattner596b97f2004-07-19 23:47:21 +00002388 BuildMI(*MBB, IP, X86::SHL16ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002389 return;
2390 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002391 BuildMI(*MBB, IP, X86::SHL32ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002392 return;
2393 }
2394 }
Chris Lattner596b97f2004-07-19 23:47:21 +00002395
2396 // If the element size is a negative power of 2, use a shift/neg to get it.
2397 if (unsigned Shift = ExactLog2(-ConstRHS)) {
2398 TmpReg = makeAnotherReg(DestTy);
2399 BuildMI(*MBB, IP, NEGrTab[Class], 1, TmpReg).addReg(op0Reg);
2400 switch (Class) {
2401 default: assert(0 && "Unknown class for this function!");
2402 case cByte:
2403 BuildMI(*MBB, IP, X86::SHL8ri,2, DestReg).addReg(TmpReg).addImm(Shift-1);
2404 return;
2405 case cShort:
2406 BuildMI(*MBB, IP, X86::SHL16ri,2, DestReg).addReg(TmpReg).addImm(Shift-1);
2407 return;
2408 case cInt:
2409 BuildMI(*MBB, IP, X86::SHL32ri,2, DestReg).addReg(TmpReg).addImm(Shift-1);
2410 return;
2411 }
2412 }
Chris Lattnerc01d1232003-10-20 03:42:58 +00002413
2414 if (Class == cShort) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002415 BuildMI(*MBB, IP, X86::IMUL16rri,2,DestReg).addReg(op0Reg).addImm(ConstRHS);
Chris Lattnerc01d1232003-10-20 03:42:58 +00002416 return;
2417 } else if (Class == cInt) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002418 BuildMI(*MBB, IP, X86::IMUL32rri,2,DestReg).addReg(op0Reg).addImm(ConstRHS);
Chris Lattnerc01d1232003-10-20 03:42:58 +00002419 return;
2420 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002421
2422 // Most general case, emit a normal multiply...
Chris Lattner596b97f2004-07-19 23:47:21 +00002423 TmpReg = makeAnotherReg(DestTy);
Chris Lattneree352852004-02-29 07:22:16 +00002424 BuildMI(*MBB, IP, MOVriTab[Class], 1, TmpReg).addImm(ConstRHS);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002425
2426 // Emit a MUL to multiply the register holding the index by
2427 // elementSize, putting the result in OffsetReg.
2428 doMultiply(MBB, IP, DestReg, DestTy, op0Reg, TmpReg);
2429}
2430
Chris Lattnerca9671d2002-11-02 20:28:58 +00002431/// visitMul - Multiplies are not simple binary operators because they must deal
2432/// with the EAX register explicitly.
2433///
2434void ISel::visitMul(BinaryOperator &I) {
Chris Lattner462fa822004-04-11 20:56:28 +00002435 unsigned ResultReg = getReg(I);
2436
Chris Lattner95157f72004-04-11 22:05:45 +00002437 Value *Op0 = I.getOperand(0);
2438 Value *Op1 = I.getOperand(1);
2439
2440 // Fold loads into floating point multiplies.
2441 if (getClass(Op0->getType()) == cFP) {
2442 if (isa<LoadInst>(Op0) && !isa<LoadInst>(Op1))
2443 if (!I.swapOperands())
2444 std::swap(Op0, Op1); // Make sure any loads are in the RHS.
2445 if (LoadInst *LI = dyn_cast<LoadInst>(Op1))
2446 if (isSafeToFoldLoadIntoInstruction(*LI, I)) {
2447 const Type *Ty = Op0->getType();
2448 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2449 unsigned Opcode = Ty == Type::FloatTy ? X86::FMUL32m : X86::FMUL64m;
2450
Chris Lattner95157f72004-04-11 22:05:45 +00002451 unsigned Op0r = getReg(Op0);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002452 if (AllocaInst *AI = dyn_castFixedAlloca(LI->getOperand(0))) {
2453 unsigned FI = getFixedSizedAllocaFI(AI);
2454 addFrameReference(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op0r), FI);
2455 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002456 X86AddressMode AM;
2457 getAddressingMode(LI->getOperand(0), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002458
Reid Spencerfc989e12004-08-30 00:13:26 +00002459 addFullAddress(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op0r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002460 }
Chris Lattner95157f72004-04-11 22:05:45 +00002461 return;
2462 }
2463 }
2464
Chris Lattner462fa822004-04-11 20:56:28 +00002465 MachineBasicBlock::iterator IP = BB->end();
Chris Lattner95157f72004-04-11 22:05:45 +00002466 emitMultiply(BB, IP, Op0, Op1, ResultReg);
Chris Lattner462fa822004-04-11 20:56:28 +00002467}
2468
2469void ISel::emitMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
2470 Value *Op0, Value *Op1, unsigned DestReg) {
2471 MachineBasicBlock &BB = *MBB;
2472 TypeClass Class = getClass(Op0->getType());
Chris Lattner3e130a22003-01-13 00:32:26 +00002473
2474 // Simple scalar multiply?
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002475 unsigned Op0Reg = getReg(Op0, &BB, IP);
Chris Lattner462fa822004-04-11 20:56:28 +00002476 switch (Class) {
2477 case cByte:
2478 case cShort:
2479 case cInt:
2480 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner462fa822004-04-11 20:56:28 +00002481 unsigned Val = (unsigned)CI->getRawValue(); // Isn't a 64-bit constant
2482 doMultiplyConst(&BB, IP, DestReg, Op0->getType(), Op0Reg, Val);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002483 } else {
Chris Lattner462fa822004-04-11 20:56:28 +00002484 unsigned Op1Reg = getReg(Op1, &BB, IP);
2485 doMultiply(&BB, IP, DestReg, Op1->getType(), Op0Reg, Op1Reg);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002486 }
Chris Lattner462fa822004-04-11 20:56:28 +00002487 return;
2488 case cFP:
Chris Lattner6621ed92004-04-11 21:23:56 +00002489 emitBinaryFPOperation(MBB, IP, Op0, Op1, 2, DestReg);
2490 return;
Chris Lattner462fa822004-04-11 20:56:28 +00002491 case cLong:
2492 break;
2493 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002494
Chris Lattner462fa822004-04-11 20:56:28 +00002495 // Long value. We have to do things the hard way...
2496 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
2497 unsigned CLow = CI->getRawValue();
2498 unsigned CHi = CI->getRawValue() >> 32;
2499
2500 if (CLow == 0) {
2501 // If the low part of the constant is all zeros, things are simple.
2502 BuildMI(BB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
2503 doMultiplyConst(&BB, IP, DestReg+1, Type::UIntTy, Op0Reg, CHi);
2504 return;
2505 }
2506
2507 // Multiply the two low parts... capturing carry into EDX
2508 unsigned OverflowReg = 0;
2509 if (CLow == 1) {
2510 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg).addReg(Op0Reg);
Chris Lattner028adc42004-04-06 04:29:36 +00002511 } else {
Chris Lattner462fa822004-04-11 20:56:28 +00002512 unsigned Op1RegL = makeAnotherReg(Type::UIntTy);
2513 OverflowReg = makeAnotherReg(Type::UIntTy);
2514 BuildMI(BB, IP, X86::MOV32ri, 1, Op1RegL).addImm(CLow);
2515 BuildMI(BB, IP, X86::MOV32rr, 1, X86::EAX).addReg(Op0Reg);
2516 BuildMI(BB, IP, X86::MUL32r, 1).addReg(Op1RegL); // AL*BL
Chris Lattner028adc42004-04-06 04:29:36 +00002517
Chris Lattner462fa822004-04-11 20:56:28 +00002518 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg).addReg(X86::EAX); // AL*BL
2519 BuildMI(BB, IP, X86::MOV32rr, 1,
2520 OverflowReg).addReg(X86::EDX); // AL*BL >> 32
2521 }
2522
2523 unsigned AHBLReg = makeAnotherReg(Type::UIntTy); // AH*BL
2524 doMultiplyConst(&BB, IP, AHBLReg, Type::UIntTy, Op0Reg+1, CLow);
2525
2526 unsigned AHBLplusOverflowReg;
2527 if (OverflowReg) {
2528 AHBLplusOverflowReg = makeAnotherReg(Type::UIntTy);
2529 BuildMI(BB, IP, X86::ADD32rr, 2, // AH*BL+(AL*BL >> 32)
Chris Lattner028adc42004-04-06 04:29:36 +00002530 AHBLplusOverflowReg).addReg(AHBLReg).addReg(OverflowReg);
Chris Lattner462fa822004-04-11 20:56:28 +00002531 } else {
2532 AHBLplusOverflowReg = AHBLReg;
2533 }
2534
2535 if (CHi == 0) {
2536 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg+1).addReg(AHBLplusOverflowReg);
2537 } else {
Chris Lattner028adc42004-04-06 04:29:36 +00002538 unsigned ALBHReg = makeAnotherReg(Type::UIntTy); // AL*BH
Chris Lattner462fa822004-04-11 20:56:28 +00002539 doMultiplyConst(&BB, IP, ALBHReg, Type::UIntTy, Op0Reg, CHi);
Chris Lattner028adc42004-04-06 04:29:36 +00002540
Chris Lattner462fa822004-04-11 20:56:28 +00002541 BuildMI(BB, IP, X86::ADD32rr, 2, // AL*BH + AH*BL + (AL*BL >> 32)
Chris Lattner028adc42004-04-06 04:29:36 +00002542 DestReg+1).addReg(AHBLplusOverflowReg).addReg(ALBHReg);
2543 }
Chris Lattner462fa822004-04-11 20:56:28 +00002544 return;
Chris Lattner3e130a22003-01-13 00:32:26 +00002545 }
Chris Lattner462fa822004-04-11 20:56:28 +00002546
2547 // General 64x64 multiply
2548
2549 unsigned Op1Reg = getReg(Op1, &BB, IP);
2550 // Multiply the two low parts... capturing carry into EDX
2551 BuildMI(BB, IP, X86::MOV32rr, 1, X86::EAX).addReg(Op0Reg);
2552 BuildMI(BB, IP, X86::MUL32r, 1).addReg(Op1Reg); // AL*BL
2553
2554 unsigned OverflowReg = makeAnotherReg(Type::UIntTy);
2555 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg).addReg(X86::EAX); // AL*BL
2556 BuildMI(BB, IP, X86::MOV32rr, 1,
2557 OverflowReg).addReg(X86::EDX); // AL*BL >> 32
2558
2559 unsigned AHBLReg = makeAnotherReg(Type::UIntTy); // AH*BL
2560 BuildMI(BB, IP, X86::IMUL32rr, 2,
2561 AHBLReg).addReg(Op0Reg+1).addReg(Op1Reg);
2562
2563 unsigned AHBLplusOverflowReg = makeAnotherReg(Type::UIntTy);
2564 BuildMI(BB, IP, X86::ADD32rr, 2, // AH*BL+(AL*BL >> 32)
2565 AHBLplusOverflowReg).addReg(AHBLReg).addReg(OverflowReg);
2566
2567 unsigned ALBHReg = makeAnotherReg(Type::UIntTy); // AL*BH
2568 BuildMI(BB, IP, X86::IMUL32rr, 2,
2569 ALBHReg).addReg(Op0Reg).addReg(Op1Reg+1);
2570
2571 BuildMI(BB, IP, X86::ADD32rr, 2, // AL*BH + AH*BL + (AL*BL >> 32)
2572 DestReg+1).addReg(AHBLplusOverflowReg).addReg(ALBHReg);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002573}
Chris Lattnerca9671d2002-11-02 20:28:58 +00002574
Chris Lattner06925362002-11-17 21:56:38 +00002575
Chris Lattnerf01729e2002-11-02 20:54:46 +00002576/// visitDivRem - Handle division and remainder instructions... these
2577/// instruction both require the same instructions to be generated, they just
2578/// select the result from a different register. Note that both of these
2579/// instructions work differently for signed and unsigned operands.
2580///
2581void ISel::visitDivRem(BinaryOperator &I) {
Chris Lattnercadff442003-10-23 17:21:43 +00002582 unsigned ResultReg = getReg(I);
Chris Lattner95157f72004-04-11 22:05:45 +00002583 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2584
2585 // Fold loads into floating point divides.
2586 if (getClass(Op0->getType()) == cFP) {
2587 if (LoadInst *LI = dyn_cast<LoadInst>(Op1))
2588 if (isSafeToFoldLoadIntoInstruction(*LI, I)) {
2589 const Type *Ty = Op0->getType();
2590 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2591 unsigned Opcode = Ty == Type::FloatTy ? X86::FDIV32m : X86::FDIV64m;
2592
Chris Lattner95157f72004-04-11 22:05:45 +00002593 unsigned Op0r = getReg(Op0);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002594 if (AllocaInst *AI = dyn_castFixedAlloca(LI->getOperand(0))) {
2595 unsigned FI = getFixedSizedAllocaFI(AI);
2596 addFrameReference(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op0r), FI);
2597 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002598 X86AddressMode AM;
2599 getAddressingMode(LI->getOperand(0), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002600
Reid Spencerfc989e12004-08-30 00:13:26 +00002601 addFullAddress(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op0r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002602 }
Chris Lattner95157f72004-04-11 22:05:45 +00002603 return;
2604 }
2605
2606 if (LoadInst *LI = dyn_cast<LoadInst>(Op0))
2607 if (isSafeToFoldLoadIntoInstruction(*LI, I)) {
2608 const Type *Ty = Op0->getType();
2609 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2610 unsigned Opcode = Ty == Type::FloatTy ? X86::FDIVR32m : X86::FDIVR64m;
2611
Chris Lattner95157f72004-04-11 22:05:45 +00002612 unsigned Op1r = getReg(Op1);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002613 if (AllocaInst *AI = dyn_castFixedAlloca(LI->getOperand(0))) {
2614 unsigned FI = getFixedSizedAllocaFI(AI);
2615 addFrameReference(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op1r), FI);
2616 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002617 X86AddressMode AM;
2618 getAddressingMode(LI->getOperand(0), AM);
2619 addFullAddress(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op1r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002620 }
Chris Lattner95157f72004-04-11 22:05:45 +00002621 return;
2622 }
2623 }
2624
Chris Lattner94af4142002-12-25 05:13:53 +00002625
Chris Lattnercadff442003-10-23 17:21:43 +00002626 MachineBasicBlock::iterator IP = BB->end();
Chris Lattner95157f72004-04-11 22:05:45 +00002627 emitDivRemOperation(BB, IP, Op0, Op1,
Chris Lattner462fa822004-04-11 20:56:28 +00002628 I.getOpcode() == Instruction::Div, ResultReg);
Chris Lattnercadff442003-10-23 17:21:43 +00002629}
2630
2631void ISel::emitDivRemOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002632 MachineBasicBlock::iterator IP,
Chris Lattner462fa822004-04-11 20:56:28 +00002633 Value *Op0, Value *Op1, bool isDiv,
2634 unsigned ResultReg) {
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002635 const Type *Ty = Op0->getType();
2636 unsigned Class = getClass(Ty);
Chris Lattner94af4142002-12-25 05:13:53 +00002637 switch (Class) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002638 case cFP: // Floating point divide
Chris Lattnercadff442003-10-23 17:21:43 +00002639 if (isDiv) {
Chris Lattner6621ed92004-04-11 21:23:56 +00002640 emitBinaryFPOperation(BB, IP, Op0, Op1, 3, ResultReg);
2641 return;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00002642 } else { // Floating point remainder...
Chris Lattner462fa822004-04-11 20:56:28 +00002643 unsigned Op0Reg = getReg(Op0, BB, IP);
2644 unsigned Op1Reg = getReg(Op1, BB, IP);
Chris Lattner3e130a22003-01-13 00:32:26 +00002645 MachineInstr *TheCall =
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002646 BuildMI(X86::CALLpcrel32, 1).addExternalSymbol("fmod", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00002647 std::vector<ValueRecord> Args;
Chris Lattnercadff442003-10-23 17:21:43 +00002648 Args.push_back(ValueRecord(Op0Reg, Type::DoubleTy));
2649 Args.push_back(ValueRecord(Op1Reg, Type::DoubleTy));
Chris Lattner3e130a22003-01-13 00:32:26 +00002650 doCall(ValueRecord(ResultReg, Type::DoubleTy), TheCall, Args);
2651 }
Chris Lattner94af4142002-12-25 05:13:53 +00002652 return;
Chris Lattner3e130a22003-01-13 00:32:26 +00002653 case cLong: {
2654 static const char *FnName[] =
2655 { "__moddi3", "__divdi3", "__umoddi3", "__udivdi3" };
Chris Lattner462fa822004-04-11 20:56:28 +00002656 unsigned Op0Reg = getReg(Op0, BB, IP);
2657 unsigned Op1Reg = getReg(Op1, BB, IP);
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002658 unsigned NameIdx = Ty->isUnsigned()*2 + isDiv;
Chris Lattner3e130a22003-01-13 00:32:26 +00002659 MachineInstr *TheCall =
2660 BuildMI(X86::CALLpcrel32, 1).addExternalSymbol(FnName[NameIdx], true);
2661
2662 std::vector<ValueRecord> Args;
Chris Lattnercadff442003-10-23 17:21:43 +00002663 Args.push_back(ValueRecord(Op0Reg, Type::LongTy));
2664 Args.push_back(ValueRecord(Op1Reg, Type::LongTy));
Chris Lattner3e130a22003-01-13 00:32:26 +00002665 doCall(ValueRecord(ResultReg, Type::LongTy), TheCall, Args);
2666 return;
2667 }
2668 case cByte: case cShort: case cInt:
Misha Brukmancf00c4a2003-10-10 17:57:28 +00002669 break; // Small integrals, handled below...
Chris Lattner3e130a22003-01-13 00:32:26 +00002670 default: assert(0 && "Unknown class!");
Chris Lattner94af4142002-12-25 05:13:53 +00002671 }
Chris Lattnerf01729e2002-11-02 20:54:46 +00002672
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002673 static const unsigned MovOpcode[]={ X86::MOV8rr, X86::MOV16rr, X86::MOV32rr };
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002674 static const unsigned NEGOpcode[] = { X86::NEG8r, X86::NEG16r, X86::NEG32r };
2675 static const unsigned SAROpcode[]={ X86::SAR8ri, X86::SAR16ri, X86::SAR32ri };
2676 static const unsigned SHROpcode[]={ X86::SHR8ri, X86::SHR16ri, X86::SHR32ri };
2677 static const unsigned ADDOpcode[]={ X86::ADD8rr, X86::ADD16rr, X86::ADD32rr };
2678
2679 // Special case signed division by power of 2.
2680 if (isDiv)
2681 if (ConstantSInt *CI = dyn_cast<ConstantSInt>(Op1)) {
2682 assert(Class != cLong && "This doesn't handle 64-bit divides!");
2683 int V = CI->getValue();
2684
2685 if (V == 1) { // X /s 1 => X
2686 unsigned Op0Reg = getReg(Op0, BB, IP);
2687 BuildMI(*BB, IP, MovOpcode[Class], 1, ResultReg).addReg(Op0Reg);
2688 return;
2689 }
2690
2691 if (V == -1) { // X /s -1 => -X
2692 unsigned Op0Reg = getReg(Op0, BB, IP);
2693 BuildMI(*BB, IP, NEGOpcode[Class], 1, ResultReg).addReg(Op0Reg);
2694 return;
2695 }
2696
2697 bool isNeg = false;
2698 if (V < 0) { // Not a positive power of 2?
2699 V = -V;
2700 isNeg = true; // Maybe it's a negative power of 2.
2701 }
2702 if (unsigned Log = ExactLog2(V)) {
2703 --Log;
2704 unsigned Op0Reg = getReg(Op0, BB, IP);
2705 unsigned TmpReg = makeAnotherReg(Op0->getType());
2706 if (Log != 1)
2707 BuildMI(*BB, IP, SAROpcode[Class], 2, TmpReg)
2708 .addReg(Op0Reg).addImm(Log-1);
2709 else
2710 BuildMI(*BB, IP, MovOpcode[Class], 1, TmpReg).addReg(Op0Reg);
2711 unsigned TmpReg2 = makeAnotherReg(Op0->getType());
2712 BuildMI(*BB, IP, SHROpcode[Class], 2, TmpReg2)
2713 .addReg(TmpReg).addImm(32-Log);
2714 unsigned TmpReg3 = makeAnotherReg(Op0->getType());
2715 BuildMI(*BB, IP, ADDOpcode[Class], 2, TmpReg3)
2716 .addReg(Op0Reg).addReg(TmpReg2);
2717
2718 unsigned TmpReg4 = isNeg ? makeAnotherReg(Op0->getType()) : ResultReg;
2719 BuildMI(*BB, IP, SAROpcode[Class], 2, TmpReg4)
2720 .addReg(Op0Reg).addImm(Log);
2721 if (isNeg)
2722 BuildMI(*BB, IP, NEGOpcode[Class], 1, ResultReg).addReg(TmpReg4);
2723 return;
2724 }
2725 }
2726
2727 static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX };
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002728 static const unsigned ClrOpcode[]={ X86::MOV8ri, X86::MOV16ri, X86::MOV32ri };
Chris Lattnerf01729e2002-11-02 20:54:46 +00002729 static const unsigned ExtRegs[] ={ X86::AH , X86::DX , X86::EDX };
2730
2731 static const unsigned DivOpcode[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002732 { X86::DIV8r , X86::DIV16r , X86::DIV32r , 0 }, // Unsigned division
2733 { X86::IDIV8r, X86::IDIV16r, X86::IDIV32r, 0 }, // Signed division
Chris Lattnerf01729e2002-11-02 20:54:46 +00002734 };
2735
Chris Lattnerf01729e2002-11-02 20:54:46 +00002736 unsigned Reg = Regs[Class];
2737 unsigned ExtReg = ExtRegs[Class];
Chris Lattnerf01729e2002-11-02 20:54:46 +00002738
2739 // Put the first operand into one of the A registers...
Chris Lattner462fa822004-04-11 20:56:28 +00002740 unsigned Op0Reg = getReg(Op0, BB, IP);
2741 unsigned Op1Reg = getReg(Op1, BB, IP);
Chris Lattneree352852004-02-29 07:22:16 +00002742 BuildMI(*BB, IP, MovOpcode[Class], 1, Reg).addReg(Op0Reg);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002743
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002744 if (Ty->isSigned()) {
Chris Lattnerf01729e2002-11-02 20:54:46 +00002745 // Emit a sign extension instruction...
Chris Lattner462fa822004-04-11 20:56:28 +00002746 unsigned ShiftResult = makeAnotherReg(Op0->getType());
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002747 BuildMI(*BB, IP, SAROpcode[Class], 2,ShiftResult).addReg(Op0Reg).addImm(31);
Chris Lattneree352852004-02-29 07:22:16 +00002748 BuildMI(*BB, IP, MovOpcode[Class], 1, ExtReg).addReg(ShiftResult);
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002749
2750 // Emit the appropriate divide or remainder instruction...
2751 BuildMI(*BB, IP, DivOpcode[1][Class], 1).addReg(Op1Reg);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002752 } else {
Alkis Evlogimenosf998a7e2004-01-12 07:22:45 +00002753 // If unsigned, emit a zeroing instruction... (reg = 0)
Chris Lattneree352852004-02-29 07:22:16 +00002754 BuildMI(*BB, IP, ClrOpcode[Class], 2, ExtReg).addImm(0);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002755
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002756 // Emit the appropriate divide or remainder instruction...
2757 BuildMI(*BB, IP, DivOpcode[0][Class], 1).addReg(Op1Reg);
2758 }
Chris Lattner06925362002-11-17 21:56:38 +00002759
Chris Lattnerf01729e2002-11-02 20:54:46 +00002760 // Figure out which register we want to pick the result out of...
Chris Lattnercadff442003-10-23 17:21:43 +00002761 unsigned DestReg = isDiv ? Reg : ExtReg;
Chris Lattnerf01729e2002-11-02 20:54:46 +00002762
Chris Lattnerf01729e2002-11-02 20:54:46 +00002763 // Put the result into the destination register...
Chris Lattneree352852004-02-29 07:22:16 +00002764 BuildMI(*BB, IP, MovOpcode[Class], 1, ResultReg).addReg(DestReg);
Chris Lattnerca9671d2002-11-02 20:28:58 +00002765}
Chris Lattnere2954c82002-11-02 20:04:26 +00002766
Chris Lattner06925362002-11-17 21:56:38 +00002767
Brian Gaekea1719c92002-10-31 23:03:59 +00002768/// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here
2769/// for constant immediate shift values, and for constant immediate
2770/// shift values equal to 1. Even the general case is sort of special,
2771/// because the shift amount has to be in CL, not just any old register.
2772///
Chris Lattner3e130a22003-01-13 00:32:26 +00002773void ISel::visitShiftInst(ShiftInst &I) {
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002774 MachineBasicBlock::iterator IP = BB->end ();
2775 emitShiftOperation (BB, IP, I.getOperand (0), I.getOperand (1),
2776 I.getOpcode () == Instruction::Shl, I.getType (),
2777 getReg (I));
2778}
2779
2780/// emitShiftOperation - Common code shared between visitShiftInst and
2781/// constant expression support.
2782void ISel::emitShiftOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002783 MachineBasicBlock::iterator IP,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002784 Value *Op, Value *ShiftAmount, bool isLeftShift,
2785 const Type *ResultTy, unsigned DestReg) {
2786 unsigned SrcReg = getReg (Op, MBB, IP);
2787 bool isSigned = ResultTy->isSigned ();
2788 unsigned Class = getClass (ResultTy);
Chris Lattner3e130a22003-01-13 00:32:26 +00002789
2790 static const unsigned ConstantOperand[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002791 { X86::SHR8ri, X86::SHR16ri, X86::SHR32ri, X86::SHRD32rri8 }, // SHR
2792 { X86::SAR8ri, X86::SAR16ri, X86::SAR32ri, X86::SHRD32rri8 }, // SAR
2793 { X86::SHL8ri, X86::SHL16ri, X86::SHL32ri, X86::SHLD32rri8 }, // SHL
2794 { X86::SHL8ri, X86::SHL16ri, X86::SHL32ri, X86::SHLD32rri8 }, // SAL = SHL
Chris Lattner3e130a22003-01-13 00:32:26 +00002795 };
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002796
Chris Lattner3e130a22003-01-13 00:32:26 +00002797 static const unsigned NonConstantOperand[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002798 { X86::SHR8rCL, X86::SHR16rCL, X86::SHR32rCL }, // SHR
2799 { X86::SAR8rCL, X86::SAR16rCL, X86::SAR32rCL }, // SAR
2800 { X86::SHL8rCL, X86::SHL16rCL, X86::SHL32rCL }, // SHL
2801 { X86::SHL8rCL, X86::SHL16rCL, X86::SHL32rCL }, // SAL = SHL
Chris Lattner3e130a22003-01-13 00:32:26 +00002802 };
Chris Lattner796df732002-11-02 00:44:25 +00002803
Chris Lattner3e130a22003-01-13 00:32:26 +00002804 // Longs, as usual, are handled specially...
2805 if (Class == cLong) {
2806 // If we have a constant shift, we can generate much more efficient code
2807 // than otherwise...
2808 //
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002809 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002810 unsigned Amount = CUI->getValue();
2811 if (Amount < 32) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002812 const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned];
2813 if (isLeftShift) {
Chris Lattneree352852004-02-29 07:22:16 +00002814 BuildMI(*MBB, IP, Opc[3], 3,
2815 DestReg+1).addReg(SrcReg+1).addReg(SrcReg).addImm(Amount);
2816 BuildMI(*MBB, IP, Opc[2], 2, DestReg).addReg(SrcReg).addImm(Amount);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002817 } else {
Chris Lattneree352852004-02-29 07:22:16 +00002818 BuildMI(*MBB, IP, Opc[3], 3,
2819 DestReg).addReg(SrcReg ).addReg(SrcReg+1).addImm(Amount);
2820 BuildMI(*MBB, IP, Opc[2],2,DestReg+1).addReg(SrcReg+1).addImm(Amount);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002821 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002822 } else { // Shifting more than 32 bits
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002823 Amount -= 32;
2824 if (isLeftShift) {
Chris Lattner722070e2004-04-06 03:42:38 +00002825 if (Amount != 0) {
2826 BuildMI(*MBB, IP, X86::SHL32ri, 2,
2827 DestReg + 1).addReg(SrcReg).addImm(Amount);
2828 } else {
2829 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg+1).addReg(SrcReg);
2830 }
2831 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002832 } else {
Chris Lattner722070e2004-04-06 03:42:38 +00002833 if (Amount != 0) {
2834 BuildMI(*MBB, IP, isSigned ? X86::SAR32ri : X86::SHR32ri, 2,
2835 DestReg).addReg(SrcReg+1).addImm(Amount);
2836 } else {
2837 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg+1);
2838 }
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002839 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002840 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002841 }
2842 } else {
Chris Lattner9171ef52003-06-01 01:56:54 +00002843 unsigned TmpReg = makeAnotherReg(Type::IntTy);
2844
2845 if (!isLeftShift && isSigned) {
2846 // If this is a SHR of a Long, then we need to do funny sign extension
2847 // stuff. TmpReg gets the value to use as the high-part if we are
2848 // shifting more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002849 BuildMI(*MBB, IP, X86::SAR32ri, 2, TmpReg).addReg(SrcReg).addImm(31);
Chris Lattner9171ef52003-06-01 01:56:54 +00002850 } else {
2851 // Other shifts use a fixed zero value if the shift is more than 32
2852 // bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002853 BuildMI(*MBB, IP, X86::MOV32ri, 1, TmpReg).addImm(0);
Chris Lattner9171ef52003-06-01 01:56:54 +00002854 }
2855
2856 // Initialize CL with the shift amount...
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002857 unsigned ShiftAmountReg = getReg(ShiftAmount, MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002858 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002859
2860 unsigned TmpReg2 = makeAnotherReg(Type::IntTy);
2861 unsigned TmpReg3 = makeAnotherReg(Type::IntTy);
2862 if (isLeftShift) {
2863 // TmpReg2 = shld inHi, inLo
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002864 BuildMI(*MBB, IP, X86::SHLD32rrCL,2,TmpReg2).addReg(SrcReg+1)
Chris Lattneree352852004-02-29 07:22:16 +00002865 .addReg(SrcReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002866 // TmpReg3 = shl inLo, CL
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002867 BuildMI(*MBB, IP, X86::SHL32rCL, 1, TmpReg3).addReg(SrcReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002868
2869 // Set the flags to indicate whether the shift was by more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002870 BuildMI(*MBB, IP, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Chris Lattner9171ef52003-06-01 01:56:54 +00002871
2872 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002873 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00002874 DestReg+1).addReg(TmpReg2).addReg(TmpReg3);
2875 // DestLo = (>32) ? TmpReg : TmpReg3;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002876 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002877 DestReg).addReg(TmpReg3).addReg(TmpReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002878 } else {
2879 // TmpReg2 = shrd inLo, inHi
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002880 BuildMI(*MBB, IP, X86::SHRD32rrCL,2,TmpReg2).addReg(SrcReg)
Chris Lattneree352852004-02-29 07:22:16 +00002881 .addReg(SrcReg+1);
Chris Lattner9171ef52003-06-01 01:56:54 +00002882 // TmpReg3 = s[ah]r inHi, CL
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002883 BuildMI(*MBB, IP, isSigned ? X86::SAR32rCL : X86::SHR32rCL, 1, TmpReg3)
Chris Lattner9171ef52003-06-01 01:56:54 +00002884 .addReg(SrcReg+1);
2885
2886 // Set the flags to indicate whether the shift was by more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002887 BuildMI(*MBB, IP, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Chris Lattner9171ef52003-06-01 01:56:54 +00002888
2889 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002890 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00002891 DestReg).addReg(TmpReg2).addReg(TmpReg3);
2892
2893 // DestHi = (>32) ? TmpReg : TmpReg3;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002894 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00002895 DestReg+1).addReg(TmpReg3).addReg(TmpReg);
2896 }
Brian Gaekea1719c92002-10-31 23:03:59 +00002897 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002898 return;
2899 }
Chris Lattnere9913f22002-11-02 01:41:55 +00002900
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002901 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002902 // The shift amount is constant, guaranteed to be a ubyte. Get its value.
2903 assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?");
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002904
Chris Lattner3e130a22003-01-13 00:32:26 +00002905 const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned];
Chris Lattneree352852004-02-29 07:22:16 +00002906 BuildMI(*MBB, IP, Opc[Class], 2,
2907 DestReg).addReg(SrcReg).addImm(CUI->getValue());
Chris Lattner3e130a22003-01-13 00:32:26 +00002908 } else { // The shift amount is non-constant.
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002909 unsigned ShiftAmountReg = getReg (ShiftAmount, MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002910 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002911
Chris Lattner3e130a22003-01-13 00:32:26 +00002912 const unsigned *Opc = NonConstantOperand[isLeftShift*2+isSigned];
Chris Lattneree352852004-02-29 07:22:16 +00002913 BuildMI(*MBB, IP, Opc[Class], 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002914 }
2915}
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002916
Chris Lattner3e130a22003-01-13 00:32:26 +00002917
Chris Lattner6fc3c522002-11-17 21:11:55 +00002918/// visitLoadInst - Implement LLVM load instructions in terms of the x86 'mov'
Chris Lattnere8f0d922002-12-24 00:03:11 +00002919/// instruction. The load and store instructions are the only place where we
2920/// need to worry about the memory layout of the target machine.
Chris Lattner6fc3c522002-11-17 21:11:55 +00002921///
2922void ISel::visitLoadInst(LoadInst &I) {
Chris Lattner7dee5da2004-03-08 01:58:35 +00002923 // Check to see if this load instruction is going to be folded into a binary
2924 // instruction, like add. If so, we don't want to emit it. Wouldn't a real
2925 // pattern matching instruction selector be nice?
Chris Lattner95157f72004-04-11 22:05:45 +00002926 unsigned Class = getClassB(I.getType());
Chris Lattnerfeac3e12004-04-11 23:21:26 +00002927 if (I.hasOneUse()) {
Chris Lattner7dee5da2004-03-08 01:58:35 +00002928 Instruction *User = cast<Instruction>(I.use_back());
2929 switch (User->getOpcode()) {
Chris Lattnerfeac3e12004-04-11 23:21:26 +00002930 case Instruction::Cast:
2931 // If this is a cast from a signed-integer type to a floating point type,
2932 // fold the cast here.
John Criswell6b5bd582004-06-09 15:18:51 +00002933 if (getClassB(User->getType()) == cFP &&
Chris Lattnerfeac3e12004-04-11 23:21:26 +00002934 (I.getType() == Type::ShortTy || I.getType() == Type::IntTy ||
2935 I.getType() == Type::LongTy)) {
2936 unsigned DestReg = getReg(User);
2937 static const unsigned Opcode[] = {
2938 0/*BYTE*/, X86::FILD16m, X86::FILD32m, 0/*FP*/, X86::FILD64m
2939 };
Chris Lattner9f1b5312004-05-13 15:12:43 +00002940
2941 if (AllocaInst *AI = dyn_castFixedAlloca(I.getOperand(0))) {
2942 unsigned FI = getFixedSizedAllocaFI(AI);
2943 addFrameReference(BuildMI(BB, Opcode[Class], 4, DestReg), FI);
2944 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002945 X86AddressMode AM;
2946 getAddressingMode(I.getOperand(0), AM);
2947 addFullAddress(BuildMI(BB, Opcode[Class], 4, DestReg), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002948 }
Chris Lattnerfeac3e12004-04-11 23:21:26 +00002949 return;
2950 } else {
2951 User = 0;
2952 }
2953 break;
Chris Lattner13c07fe2004-04-12 00:12:04 +00002954
Chris Lattner7dee5da2004-03-08 01:58:35 +00002955 case Instruction::Add:
2956 case Instruction::Sub:
2957 case Instruction::And:
2958 case Instruction::Or:
2959 case Instruction::Xor:
Chris Lattnerfeac3e12004-04-11 23:21:26 +00002960 if (Class == cLong) User = 0;
Chris Lattner7dee5da2004-03-08 01:58:35 +00002961 break;
Chris Lattner95157f72004-04-11 22:05:45 +00002962 case Instruction::Mul:
2963 case Instruction::Div:
Chris Lattner13c07fe2004-04-12 00:12:04 +00002964 if (Class != cFP) User = 0;
Chris Lattnerfeac3e12004-04-11 23:21:26 +00002965 break; // Folding only implemented for floating point.
Chris Lattner95157f72004-04-11 22:05:45 +00002966 default: User = 0; break;
Chris Lattner7dee5da2004-03-08 01:58:35 +00002967 }
2968
2969 if (User) {
2970 // Okay, we found a user. If the load is the first operand and there is
2971 // no second operand load, reverse the operand ordering. Note that this
2972 // can fail for a subtract (ie, no change will be made).
Chris Lattner3dbb5042004-07-21 21:28:26 +00002973 bool Swapped = false;
Chris Lattner7dee5da2004-03-08 01:58:35 +00002974 if (!isa<LoadInst>(User->getOperand(1)))
Chris Lattner3dbb5042004-07-21 21:28:26 +00002975 Swapped = !cast<BinaryOperator>(User)->swapOperands();
Chris Lattner7dee5da2004-03-08 01:58:35 +00002976
2977 // Okay, now that everything is set up, if this load is used by the second
2978 // operand, and if there are no instructions that invalidate the load
2979 // before the binary operator, eliminate the load.
2980 if (User->getOperand(1) == &I &&
2981 isSafeToFoldLoadIntoInstruction(I, *User))
2982 return; // Eliminate the load!
Chris Lattner95157f72004-04-11 22:05:45 +00002983
2984 // If this is a floating point sub or div, we won't be able to swap the
2985 // operands, but we will still be able to eliminate the load.
2986 if (Class == cFP && User->getOperand(0) == &I &&
2987 !isa<LoadInst>(User->getOperand(1)) &&
2988 (User->getOpcode() == Instruction::Sub ||
2989 User->getOpcode() == Instruction::Div) &&
2990 isSafeToFoldLoadIntoInstruction(I, *User))
2991 return; // Eliminate the load!
Chris Lattner3dbb5042004-07-21 21:28:26 +00002992
2993 // If we swapped the operands to the instruction, but couldn't fold the
2994 // load anyway, swap them back. We don't want to break add X, int
2995 // folding.
2996 if (Swapped) cast<BinaryOperator>(User)->swapOperands();
Chris Lattner7dee5da2004-03-08 01:58:35 +00002997 }
2998 }
2999
Chris Lattner6ac1d712003-10-20 04:48:06 +00003000 static const unsigned Opcodes[] = {
Chris Lattner9f1b5312004-05-13 15:12:43 +00003001 X86::MOV8rm, X86::MOV16rm, X86::MOV32rm, X86::FLD32m, X86::MOV32rm
Chris Lattner3e130a22003-01-13 00:32:26 +00003002 };
Chris Lattner6ac1d712003-10-20 04:48:06 +00003003 unsigned Opcode = Opcodes[Class];
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003004 if (I.getType() == Type::DoubleTy) Opcode = X86::FLD64m;
Chris Lattner9f1b5312004-05-13 15:12:43 +00003005
3006 unsigned DestReg = getReg(I);
3007
3008 if (AllocaInst *AI = dyn_castFixedAlloca(I.getOperand(0))) {
3009 unsigned FI = getFixedSizedAllocaFI(AI);
3010 if (Class == cLong) {
3011 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, DestReg), FI);
3012 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, DestReg+1), FI, 4);
3013 } else {
3014 addFrameReference(BuildMI(BB, Opcode, 4, DestReg), FI);
3015 }
3016 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00003017 X86AddressMode AM;
3018 getAddressingMode(I.getOperand(0), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00003019
3020 if (Class == cLong) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003021 addFullAddress(BuildMI(BB, X86::MOV32rm, 4, DestReg), AM);
3022 AM.Disp += 4;
3023 addFullAddress(BuildMI(BB, X86::MOV32rm, 4, DestReg+1), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00003024 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00003025 addFullAddress(BuildMI(BB, Opcode, 4, DestReg), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00003026 }
3027 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003028}
3029
Chris Lattner6fc3c522002-11-17 21:11:55 +00003030/// visitStoreInst - Implement LLVM store instructions in terms of the x86 'mov'
3031/// instruction.
3032///
3033void ISel::visitStoreInst(StoreInst &I) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003034 X86AddressMode AM;
3035 getAddressingMode(I.getOperand(1), AM);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003036
Chris Lattner6c09db22003-10-20 04:11:23 +00003037 const Type *ValTy = I.getOperand(0)->getType();
3038 unsigned Class = getClassB(ValTy);
Chris Lattner6ac1d712003-10-20 04:48:06 +00003039
Chris Lattner5a830962004-02-25 02:56:58 +00003040 if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(0))) {
3041 uint64_t Val = CI->getRawValue();
3042 if (Class == cLong) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003043 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(Val & ~0U);
3044 AM.Disp += 4;
3045 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(Val>>32);
Chris Lattner5a830962004-02-25 02:56:58 +00003046 } else {
3047 static const unsigned Opcodes[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003048 X86::MOV8mi, X86::MOV16mi, X86::MOV32mi
Chris Lattner5a830962004-02-25 02:56:58 +00003049 };
3050 unsigned Opcode = Opcodes[Class];
Reid Spencerfc989e12004-08-30 00:13:26 +00003051 addFullAddress(BuildMI(BB, Opcode, 5), AM).addImm(Val);
Chris Lattner5a830962004-02-25 02:56:58 +00003052 }
Chris Lattnerb7cb9ff2004-05-13 15:26:48 +00003053 } else if (isa<ConstantPointerNull>(I.getOperand(0))) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003054 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(0);
Chris Lattner5a830962004-02-25 02:56:58 +00003055 } else if (ConstantBool *CB = dyn_cast<ConstantBool>(I.getOperand(0))) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003056 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CB->getValue());
Chris Lattnere7a31c92004-05-07 21:18:15 +00003057 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0))) {
3058 // Store constant FP values with integer instructions to avoid having to
3059 // load the constants from the constant pool then do a store.
3060 if (CFP->getType() == Type::FloatTy) {
3061 union {
3062 unsigned I;
3063 float F;
3064 } V;
3065 V.F = CFP->getValue();
Reid Spencerfc989e12004-08-30 00:13:26 +00003066 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(V.I);
Chris Lattner5a830962004-02-25 02:56:58 +00003067 } else {
Chris Lattnere7a31c92004-05-07 21:18:15 +00003068 union {
3069 uint64_t I;
3070 double F;
3071 } V;
3072 V.F = CFP->getValue();
Reid Spencerfc989e12004-08-30 00:13:26 +00003073 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm((unsigned)V.I);
3074 AM.Disp += 4;
3075 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(
Chris Lattnere7a31c92004-05-07 21:18:15 +00003076 unsigned(V.I >> 32));
Chris Lattner5a830962004-02-25 02:56:58 +00003077 }
Chris Lattnere7a31c92004-05-07 21:18:15 +00003078
3079 } else if (Class == cLong) {
3080 unsigned ValReg = getReg(I.getOperand(0));
Reid Spencerfc989e12004-08-30 00:13:26 +00003081 addFullAddress(BuildMI(BB, X86::MOV32mr, 5), AM).addReg(ValReg);
3082 AM.Disp += 4;
3083 addFullAddress(BuildMI(BB, X86::MOV32mr, 5), AM).addReg(ValReg+1);
Chris Lattnere7a31c92004-05-07 21:18:15 +00003084 } else {
3085 unsigned ValReg = getReg(I.getOperand(0));
3086 static const unsigned Opcodes[] = {
3087 X86::MOV8mr, X86::MOV16mr, X86::MOV32mr, X86::FST32m
3088 };
3089 unsigned Opcode = Opcodes[Class];
3090 if (ValTy == Type::DoubleTy) Opcode = X86::FST64m;
Chris Lattner9f1b5312004-05-13 15:12:43 +00003091
Reid Spencerfc989e12004-08-30 00:13:26 +00003092 addFullAddress(BuildMI(BB, Opcode, 1+4), AM).addReg(ValReg);
Chris Lattner94af4142002-12-25 05:13:53 +00003093 }
Chris Lattner6fc3c522002-11-17 21:11:55 +00003094}
3095
3096
Misha Brukman538607f2004-03-01 23:53:11 +00003097/// visitCastInst - Here we have various kinds of copying with or without sign
3098/// extension going on.
3099///
Chris Lattner3e130a22003-01-13 00:32:26 +00003100void ISel::visitCastInst(CastInst &CI) {
Chris Lattnerf5854472003-06-21 16:01:24 +00003101 Value *Op = CI.getOperand(0);
Chris Lattner427aeb42004-04-11 19:21:59 +00003102
Chris Lattner99382862004-04-12 00:23:04 +00003103 unsigned SrcClass = getClassB(Op->getType());
3104 unsigned DestClass = getClassB(CI.getType());
3105 // Noop casts are not emitted: getReg will return the source operand as the
3106 // register to use for any uses of the noop cast.
Chris Lattner8b486a12004-06-29 00:14:38 +00003107 if (DestClass == SrcClass) {
3108 // The only detail in this plan is that casts from double -> float are
3109 // truncating operations that we have to codegen through memory (despite
3110 // the fact that the source/dest registers are the same class).
3111 if (CI.getType() != Type::FloatTy || Op->getType() != Type::DoubleTy)
3112 return;
3113 }
Chris Lattner427aeb42004-04-11 19:21:59 +00003114
Chris Lattnerf5854472003-06-21 16:01:24 +00003115 // If this is a cast from a 32-bit integer to a Long type, and the only uses
3116 // of the case are GEP instructions, then the cast does not need to be
3117 // generated explicitly, it will be folded into the GEP.
Chris Lattner99382862004-04-12 00:23:04 +00003118 if (DestClass == cLong && SrcClass == cInt) {
Chris Lattnerf5854472003-06-21 16:01:24 +00003119 bool AllUsesAreGEPs = true;
3120 for (Value::use_iterator I = CI.use_begin(), E = CI.use_end(); I != E; ++I)
3121 if (!isa<GetElementPtrInst>(*I)) {
3122 AllUsesAreGEPs = false;
3123 break;
3124 }
3125
3126 // No need to codegen this cast if all users are getelementptr instrs...
3127 if (AllUsesAreGEPs) return;
3128 }
3129
Chris Lattner99382862004-04-12 00:23:04 +00003130 // If this cast converts a load from a short,int, or long integer to a FP
3131 // value, we will have folded this cast away.
3132 if (DestClass == cFP && isa<LoadInst>(Op) && Op->hasOneUse() &&
3133 (Op->getType() == Type::ShortTy || Op->getType() == Type::IntTy ||
3134 Op->getType() == Type::LongTy))
3135 return;
3136
3137
Chris Lattner548f61d2003-04-23 17:22:12 +00003138 unsigned DestReg = getReg(CI);
3139 MachineBasicBlock::iterator MI = BB->end();
Chris Lattnerf5854472003-06-21 16:01:24 +00003140 emitCastOperation(BB, MI, Op, CI.getType(), DestReg);
Chris Lattner548f61d2003-04-23 17:22:12 +00003141}
3142
Misha Brukman538607f2004-03-01 23:53:11 +00003143/// emitCastOperation - Common code shared between visitCastInst and constant
3144/// expression cast support.
3145///
Chris Lattner548f61d2003-04-23 17:22:12 +00003146void ISel::emitCastOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003147 MachineBasicBlock::iterator IP,
Chris Lattner548f61d2003-04-23 17:22:12 +00003148 Value *Src, const Type *DestTy,
3149 unsigned DestReg) {
Chris Lattner3e130a22003-01-13 00:32:26 +00003150 const Type *SrcTy = Src->getType();
3151 unsigned SrcClass = getClassB(SrcTy);
Chris Lattner3e130a22003-01-13 00:32:26 +00003152 unsigned DestClass = getClassB(DestTy);
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003153 unsigned SrcReg = getReg(Src, BB, IP);
3154
Chris Lattner3e130a22003-01-13 00:32:26 +00003155 // Implement casts to bool by using compare on the operand followed by set if
3156 // not zero on the result.
3157 if (DestTy == Type::BoolTy) {
Chris Lattner20772542003-06-01 03:38:24 +00003158 switch (SrcClass) {
3159 case cByte:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003160 BuildMI(*BB, IP, X86::TEST8rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00003161 break;
3162 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003163 BuildMI(*BB, IP, X86::TEST16rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00003164 break;
3165 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003166 BuildMI(*BB, IP, X86::TEST32rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00003167 break;
3168 case cLong: {
3169 unsigned TmpReg = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003170 BuildMI(*BB, IP, X86::OR32rr, 2, TmpReg).addReg(SrcReg).addReg(SrcReg+1);
Chris Lattner20772542003-06-01 03:38:24 +00003171 break;
3172 }
3173 case cFP:
Chris Lattneree352852004-02-29 07:22:16 +00003174 BuildMI(*BB, IP, X86::FTST, 1).addReg(SrcReg);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003175 BuildMI(*BB, IP, X86::FNSTSW8r, 0);
Chris Lattneree352852004-02-29 07:22:16 +00003176 BuildMI(*BB, IP, X86::SAHF, 1);
Chris Lattner311ca2e2004-02-23 03:21:41 +00003177 break;
Chris Lattner20772542003-06-01 03:38:24 +00003178 }
3179
3180 // If the zero flag is not set, then the value is true, set the byte to
3181 // true.
Chris Lattneree352852004-02-29 07:22:16 +00003182 BuildMI(*BB, IP, X86::SETNEr, 1, DestReg);
Chris Lattner94af4142002-12-25 05:13:53 +00003183 return;
3184 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003185
3186 static const unsigned RegRegMove[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003187 X86::MOV8rr, X86::MOV16rr, X86::MOV32rr, X86::FpMOV, X86::MOV32rr
Chris Lattner3e130a22003-01-13 00:32:26 +00003188 };
3189
3190 // Implement casts between values of the same type class (as determined by
3191 // getClass) by using a register-to-register move.
3192 if (SrcClass == DestClass) {
3193 if (SrcClass <= cInt || (SrcClass == cFP && SrcTy == DestTy)) {
Chris Lattneree352852004-02-29 07:22:16 +00003194 BuildMI(*BB, IP, RegRegMove[SrcClass], 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003195 } else if (SrcClass == cFP) {
3196 if (SrcTy == Type::FloatTy) { // double -> float
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003197 assert(DestTy == Type::DoubleTy && "Unknown cFP member!");
Chris Lattneree352852004-02-29 07:22:16 +00003198 BuildMI(*BB, IP, X86::FpMOV, 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003199 } else { // float -> double
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003200 assert(SrcTy == Type::DoubleTy && DestTy == Type::FloatTy &&
3201 "Unknown cFP member!");
3202 // Truncate from double to float by storing to memory as short, then
3203 // reading it back.
3204 unsigned FltAlign = TM.getTargetData().getFloatAlignment();
Chris Lattner3e130a22003-01-13 00:32:26 +00003205 int FrameIdx = F->getFrameInfo()->CreateStackObject(4, FltAlign);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003206 addFrameReference(BuildMI(*BB, IP, X86::FST32m, 5), FrameIdx).addReg(SrcReg);
3207 addFrameReference(BuildMI(*BB, IP, X86::FLD32m, 5, DestReg), FrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003208 }
3209 } else if (SrcClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003210 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg);
3211 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg+1).addReg(SrcReg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00003212 } else {
Chris Lattnerc53544a2003-05-12 20:16:58 +00003213 assert(0 && "Cannot handle this type of cast instruction!");
Chris Lattner548f61d2003-04-23 17:22:12 +00003214 abort();
Brian Gaeked474e9c2002-12-06 10:49:33 +00003215 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003216 return;
3217 }
3218
3219 // Handle cast of SMALLER int to LARGER int using a move with sign extension
3220 // or zero extension, depending on whether the source type was signed.
3221 if (SrcClass <= cInt && (DestClass <= cInt || DestClass == cLong) &&
3222 SrcClass < DestClass) {
3223 bool isLong = DestClass == cLong;
3224 if (isLong) DestClass = cInt;
3225
3226 static const unsigned Opc[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003227 { X86::MOVSX16rr8, X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOV32rr }, // s
3228 { X86::MOVZX16rr8, X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOV32rr } // u
Chris Lattner3e130a22003-01-13 00:32:26 +00003229 };
3230
Chris Lattner96e3b422004-05-09 22:28:45 +00003231 bool isUnsigned = SrcTy->isUnsigned() || SrcTy == Type::BoolTy;
Chris Lattneree352852004-02-29 07:22:16 +00003232 BuildMI(*BB, IP, Opc[isUnsigned][SrcClass + DestClass - 1], 1,
Chris Lattner548f61d2003-04-23 17:22:12 +00003233 DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003234
3235 if (isLong) { // Handle upper 32 bits as appropriate...
3236 if (isUnsigned) // Zero out top bits...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003237 BuildMI(*BB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
Chris Lattner3e130a22003-01-13 00:32:26 +00003238 else // Sign extend bottom half...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003239 BuildMI(*BB, IP, X86::SAR32ri, 2, DestReg+1).addReg(DestReg).addImm(31);
Brian Gaeked474e9c2002-12-06 10:49:33 +00003240 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003241 return;
3242 }
3243
3244 // Special case long -> int ...
3245 if (SrcClass == cLong && DestClass == cInt) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003246 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003247 return;
3248 }
3249
3250 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by a
3251 // move out of AX or AL.
3252 if ((SrcClass <= cInt || SrcClass == cLong) && DestClass <= cInt
3253 && SrcClass > DestClass) {
3254 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX, 0, X86::EAX };
Chris Lattneree352852004-02-29 07:22:16 +00003255 BuildMI(*BB, IP, RegRegMove[SrcClass], 1, AReg[SrcClass]).addReg(SrcReg);
3256 BuildMI(*BB, IP, RegRegMove[DestClass], 1, DestReg).addReg(AReg[DestClass]);
Chris Lattner3e130a22003-01-13 00:32:26 +00003257 return;
3258 }
3259
3260 // Handle casts from integer to floating point now...
3261 if (DestClass == cFP) {
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003262 // Promote the integer to a type supported by FLD. We do this because there
3263 // are no unsigned FLD instructions, so we must promote an unsigned value to
3264 // a larger signed value, then use FLD on the larger value.
3265 //
3266 const Type *PromoteType = 0;
Chris Lattner85aa7092004-04-10 18:32:01 +00003267 unsigned PromoteOpcode = 0;
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003268 unsigned RealDestReg = DestReg;
Chris Lattnerf70c22b2004-06-17 18:19:28 +00003269 switch (SrcTy->getTypeID()) {
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003270 case Type::BoolTyID:
3271 case Type::SByteTyID:
3272 // We don't have the facilities for directly loading byte sized data from
3273 // memory (even signed). Promote it to 16 bits.
3274 PromoteType = Type::ShortTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003275 PromoteOpcode = X86::MOVSX16rr8;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003276 break;
3277 case Type::UByteTyID:
3278 PromoteType = Type::ShortTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003279 PromoteOpcode = X86::MOVZX16rr8;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003280 break;
3281 case Type::UShortTyID:
3282 PromoteType = Type::IntTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003283 PromoteOpcode = X86::MOVZX32rr16;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003284 break;
3285 case Type::UIntTyID: {
3286 // Make a 64 bit temporary... and zero out the top of it...
3287 unsigned TmpReg = makeAnotherReg(Type::LongTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003288 BuildMI(*BB, IP, X86::MOV32rr, 1, TmpReg).addReg(SrcReg);
3289 BuildMI(*BB, IP, X86::MOV32ri, 1, TmpReg+1).addImm(0);
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003290 SrcTy = Type::LongTy;
3291 SrcClass = cLong;
3292 SrcReg = TmpReg;
3293 break;
3294 }
3295 case Type::ULongTyID:
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003296 // Don't fild into the read destination.
3297 DestReg = makeAnotherReg(Type::DoubleTy);
3298 break;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003299 default: // No promotion needed...
3300 break;
3301 }
3302
3303 if (PromoteType) {
3304 unsigned TmpReg = makeAnotherReg(PromoteType);
Chris Lattner7b92de1e2004-04-06 19:29:36 +00003305 BuildMI(*BB, IP, PromoteOpcode, 1, TmpReg).addReg(SrcReg);
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003306 SrcTy = PromoteType;
3307 SrcClass = getClass(PromoteType);
Chris Lattner3e130a22003-01-13 00:32:26 +00003308 SrcReg = TmpReg;
3309 }
3310
3311 // Spill the integer to memory and reload it from there...
Chris Lattnerb6bac512004-02-25 06:13:04 +00003312 int FrameIdx =
3313 F->getFrameInfo()->CreateStackObject(SrcTy, TM.getTargetData());
Chris Lattner3e130a22003-01-13 00:32:26 +00003314
3315 if (SrcClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003316 addFrameReference(BuildMI(*BB, IP, X86::MOV32mr, 5),
Chris Lattneree352852004-02-29 07:22:16 +00003317 FrameIdx).addReg(SrcReg);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003318 addFrameReference(BuildMI(*BB, IP, X86::MOV32mr, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003319 FrameIdx, 4).addReg(SrcReg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00003320 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003321 static const unsigned Op1[] = { X86::MOV8mr, X86::MOV16mr, X86::MOV32mr };
Chris Lattneree352852004-02-29 07:22:16 +00003322 addFrameReference(BuildMI(*BB, IP, Op1[SrcClass], 5),
3323 FrameIdx).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003324 }
3325
3326 static const unsigned Op2[] =
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003327 { 0/*byte*/, X86::FILD16m, X86::FILD32m, 0/*FP*/, X86::FILD64m };
Chris Lattneree352852004-02-29 07:22:16 +00003328 addFrameReference(BuildMI(*BB, IP, Op2[SrcClass], 5, DestReg), FrameIdx);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003329
3330 // We need special handling for unsigned 64-bit integer sources. If the
3331 // input number has the "sign bit" set, then we loaded it incorrectly as a
3332 // negative 64-bit number. In this case, add an offset value.
3333 if (SrcTy == Type::ULongTy) {
3334 // Emit a test instruction to see if the dynamic input value was signed.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003335 BuildMI(*BB, IP, X86::TEST32rr, 2).addReg(SrcReg+1).addReg(SrcReg+1);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003336
Chris Lattnerb6bac512004-02-25 06:13:04 +00003337 // If the sign bit is set, get a pointer to an offset, otherwise get a
3338 // pointer to a zero.
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003339 MachineConstantPool *CP = F->getConstantPool();
3340 unsigned Zero = makeAnotherReg(Type::IntTy);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003341 Constant *Null = Constant::getNullValue(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003342 addConstantPoolReference(BuildMI(*BB, IP, X86::LEA32r, 5, Zero),
Chris Lattnerb6bac512004-02-25 06:13:04 +00003343 CP->getConstantPoolIndex(Null));
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003344 unsigned Offset = makeAnotherReg(Type::IntTy);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003345 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
3346
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003347 addConstantPoolReference(BuildMI(*BB, IP, X86::LEA32r, 5, Offset),
Chris Lattnerb6bac512004-02-25 06:13:04 +00003348 CP->getConstantPoolIndex(OffsetCst));
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003349 unsigned Addr = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003350 BuildMI(*BB, IP, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003351
3352 // Load the constant for an add. FIXME: this could make an 'fadd' that
3353 // reads directly from memory, but we don't support these yet.
3354 unsigned ConstReg = makeAnotherReg(Type::DoubleTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003355 addDirectMem(BuildMI(*BB, IP, X86::FLD32m, 4, ConstReg), Addr);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003356
Chris Lattneree352852004-02-29 07:22:16 +00003357 BuildMI(*BB, IP, X86::FpADD, 2, RealDestReg)
3358 .addReg(ConstReg).addReg(DestReg);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003359 }
3360
Chris Lattner3e130a22003-01-13 00:32:26 +00003361 return;
3362 }
3363
3364 // Handle casts from floating point to integer now...
3365 if (SrcClass == cFP) {
3366 // Change the floating point control register to use "round towards zero"
3367 // mode when truncating to an integer value.
3368 //
3369 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003370 addFrameReference(BuildMI(*BB, IP, X86::FNSTCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003371
3372 // Load the old value of the high byte of the control word...
3373 unsigned HighPartOfCW = makeAnotherReg(Type::UByteTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003374 addFrameReference(BuildMI(*BB, IP, X86::MOV8rm, 4, HighPartOfCW),
Chris Lattneree352852004-02-29 07:22:16 +00003375 CWFrameIdx, 1);
Chris Lattner3e130a22003-01-13 00:32:26 +00003376
3377 // Set the high part to be round to zero...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003378 addFrameReference(BuildMI(*BB, IP, X86::MOV8mi, 5),
Chris Lattneree352852004-02-29 07:22:16 +00003379 CWFrameIdx, 1).addImm(12);
Chris Lattner3e130a22003-01-13 00:32:26 +00003380
3381 // Reload the modified control word now...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003382 addFrameReference(BuildMI(*BB, IP, X86::FLDCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003383
3384 // Restore the memory image of control word to original value
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003385 addFrameReference(BuildMI(*BB, IP, X86::MOV8mr, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003386 CWFrameIdx, 1).addReg(HighPartOfCW);
Chris Lattner3e130a22003-01-13 00:32:26 +00003387
3388 // We don't have the facilities for directly storing byte sized data to
3389 // memory. Promote it to 16 bits. We also must promote unsigned values to
3390 // larger classes because we only have signed FP stores.
3391 unsigned StoreClass = DestClass;
3392 const Type *StoreTy = DestTy;
3393 if (StoreClass == cByte || DestTy->isUnsigned())
3394 switch (StoreClass) {
3395 case cByte: StoreTy = Type::ShortTy; StoreClass = cShort; break;
3396 case cShort: StoreTy = Type::IntTy; StoreClass = cInt; break;
3397 case cInt: StoreTy = Type::LongTy; StoreClass = cLong; break;
Brian Gaeked4615052003-07-18 20:23:43 +00003398 // The following treatment of cLong may not be perfectly right,
3399 // but it survives chains of casts of the form
3400 // double->ulong->double.
3401 case cLong: StoreTy = Type::LongTy; StoreClass = cLong; break;
Chris Lattner3e130a22003-01-13 00:32:26 +00003402 default: assert(0 && "Unknown store class!");
3403 }
3404
3405 // Spill the integer to memory and reload it from there...
3406 int FrameIdx =
3407 F->getFrameInfo()->CreateStackObject(StoreTy, TM.getTargetData());
3408
3409 static const unsigned Op1[] =
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003410 { 0, X86::FIST16m, X86::FIST32m, 0, X86::FISTP64m };
Chris Lattneree352852004-02-29 07:22:16 +00003411 addFrameReference(BuildMI(*BB, IP, Op1[StoreClass], 5),
3412 FrameIdx).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003413
3414 if (DestClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003415 addFrameReference(BuildMI(*BB, IP, X86::MOV32rm, 4, DestReg), FrameIdx);
3416 addFrameReference(BuildMI(*BB, IP, X86::MOV32rm, 4, DestReg+1),
Chris Lattneree352852004-02-29 07:22:16 +00003417 FrameIdx, 4);
Chris Lattner3e130a22003-01-13 00:32:26 +00003418 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003419 static const unsigned Op2[] = { X86::MOV8rm, X86::MOV16rm, X86::MOV32rm };
Chris Lattneree352852004-02-29 07:22:16 +00003420 addFrameReference(BuildMI(*BB, IP, Op2[DestClass], 4, DestReg), FrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003421 }
3422
3423 // Reload the original control word now...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003424 addFrameReference(BuildMI(*BB, IP, X86::FLDCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003425 return;
3426 }
3427
Brian Gaeked474e9c2002-12-06 10:49:33 +00003428 // Anything we haven't handled already, we can't (yet) handle at all.
Chris Lattnerc53544a2003-05-12 20:16:58 +00003429 assert(0 && "Unhandled cast instruction!");
Chris Lattner548f61d2003-04-23 17:22:12 +00003430 abort();
Brian Gaekefa8d5712002-11-22 11:07:01 +00003431}
Brian Gaekea1719c92002-10-31 23:03:59 +00003432
Chris Lattner73815062003-10-18 05:56:40 +00003433/// visitVANextInst - Implement the va_next instruction...
Chris Lattnereca195e2003-05-08 19:44:13 +00003434///
Chris Lattner73815062003-10-18 05:56:40 +00003435void ISel::visitVANextInst(VANextInst &I) {
3436 unsigned VAList = getReg(I.getOperand(0));
Chris Lattnereca195e2003-05-08 19:44:13 +00003437 unsigned DestReg = getReg(I);
3438
Chris Lattnereca195e2003-05-08 19:44:13 +00003439 unsigned Size;
Chris Lattnerf70c22b2004-06-17 18:19:28 +00003440 switch (I.getArgType()->getTypeID()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00003441 default:
3442 std::cerr << I;
Chris Lattner73815062003-10-18 05:56:40 +00003443 assert(0 && "Error: bad type for va_next instruction!");
Chris Lattnereca195e2003-05-08 19:44:13 +00003444 return;
3445 case Type::PointerTyID:
3446 case Type::UIntTyID:
3447 case Type::IntTyID:
3448 Size = 4;
Chris Lattnereca195e2003-05-08 19:44:13 +00003449 break;
3450 case Type::ULongTyID:
3451 case Type::LongTyID:
Chris Lattnereca195e2003-05-08 19:44:13 +00003452 case Type::DoubleTyID:
3453 Size = 8;
Chris Lattnereca195e2003-05-08 19:44:13 +00003454 break;
3455 }
3456
3457 // Increment the VAList pointer...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003458 BuildMI(BB, X86::ADD32ri, 2, DestReg).addReg(VAList).addImm(Size);
Chris Lattner73815062003-10-18 05:56:40 +00003459}
Chris Lattnereca195e2003-05-08 19:44:13 +00003460
Chris Lattner73815062003-10-18 05:56:40 +00003461void ISel::visitVAArgInst(VAArgInst &I) {
3462 unsigned VAList = getReg(I.getOperand(0));
3463 unsigned DestReg = getReg(I);
3464
Chris Lattnerf70c22b2004-06-17 18:19:28 +00003465 switch (I.getType()->getTypeID()) {
Chris Lattner73815062003-10-18 05:56:40 +00003466 default:
3467 std::cerr << I;
3468 assert(0 && "Error: bad type for va_next instruction!");
3469 return;
3470 case Type::PointerTyID:
3471 case Type::UIntTyID:
3472 case Type::IntTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003473 addDirectMem(BuildMI(BB, X86::MOV32rm, 4, DestReg), VAList);
Chris Lattner73815062003-10-18 05:56:40 +00003474 break;
3475 case Type::ULongTyID:
3476 case Type::LongTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003477 addDirectMem(BuildMI(BB, X86::MOV32rm, 4, DestReg), VAList);
3478 addRegOffset(BuildMI(BB, X86::MOV32rm, 4, DestReg+1), VAList, 4);
Chris Lattner73815062003-10-18 05:56:40 +00003479 break;
3480 case Type::DoubleTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003481 addDirectMem(BuildMI(BB, X86::FLD64m, 4, DestReg), VAList);
Chris Lattner73815062003-10-18 05:56:40 +00003482 break;
3483 }
Chris Lattnereca195e2003-05-08 19:44:13 +00003484}
3485
Misha Brukman538607f2004-03-01 23:53:11 +00003486/// visitGetElementPtrInst - instruction-select GEP instructions
3487///
Chris Lattner3e130a22003-01-13 00:32:26 +00003488void ISel::visitGetElementPtrInst(GetElementPtrInst &I) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00003489 // If this GEP instruction will be folded into all of its users, we don't need
3490 // to explicitly calculate it!
Reid Spencerfc989e12004-08-30 00:13:26 +00003491 X86AddressMode AM;
3492 if (isGEPFoldable(0, I.getOperand(0), I.op_begin()+1, I.op_end(), AM)) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00003493 // Check all of the users of the instruction to see if they are loads and
3494 // stores.
3495 bool AllWillFold = true;
3496 for (Value::use_iterator UI = I.use_begin(), E = I.use_end(); UI != E; ++UI)
3497 if (cast<Instruction>(*UI)->getOpcode() != Instruction::Load)
3498 if (cast<Instruction>(*UI)->getOpcode() != Instruction::Store ||
3499 cast<Instruction>(*UI)->getOperand(0) == &I) {
3500 AllWillFold = false;
3501 break;
3502 }
3503
3504 // If the instruction is foldable, and will be folded into all users, don't
3505 // emit it!
3506 if (AllWillFold) return;
3507 }
3508
Chris Lattner3e130a22003-01-13 00:32:26 +00003509 unsigned outputReg = getReg(I);
Chris Lattner827832c2004-02-22 17:05:38 +00003510 emitGEPOperation(BB, BB->end(), I.getOperand(0),
Brian Gaeke68b1edc2002-12-16 04:23:29 +00003511 I.op_begin()+1, I.op_end(), outputReg);
Chris Lattnerc0812d82002-12-13 06:56:29 +00003512}
3513
Chris Lattner985fe3d2004-02-25 03:45:50 +00003514/// getGEPIndex - Inspect the getelementptr operands specified with GEPOps and
3515/// GEPTypes (the derived types being stepped through at each level). On return
3516/// from this function, if some indexes of the instruction are representable as
3517/// an X86 lea instruction, the machine operands are put into the Ops
3518/// instruction and the consumed indexes are poped from the GEPOps/GEPTypes
3519/// lists. Otherwise, GEPOps.size() is returned. If this returns a an
3520/// addressing mode that only partially consumes the input, the BaseReg input of
3521/// the addressing mode must be left free.
3522///
3523/// Note that there is one fewer entry in GEPTypes than there is in GEPOps.
3524///
Chris Lattnerb6bac512004-02-25 06:13:04 +00003525void ISel::getGEPIndex(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
3526 std::vector<Value*> &GEPOps,
Reid Spencerfc989e12004-08-30 00:13:26 +00003527 std::vector<const Type*> &GEPTypes,
3528 X86AddressMode &AM) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00003529 const TargetData &TD = TM.getTargetData();
3530
Chris Lattner985fe3d2004-02-25 03:45:50 +00003531 // Clear out the state we are working with...
Reid Spencerfc989e12004-08-30 00:13:26 +00003532 AM.BaseType = X86AddressMode::RegBase;
3533 AM.Base.Reg = 0; // No base register
3534 AM.Scale = 1; // Unit scale
3535 AM.IndexReg = 0; // No index register
3536 AM.Disp = 0; // No displacement
Chris Lattnerb6bac512004-02-25 06:13:04 +00003537
Chris Lattner985fe3d2004-02-25 03:45:50 +00003538 // While there are GEP indexes that can be folded into the current address,
3539 // keep processing them.
3540 while (!GEPTypes.empty()) {
3541 if (const StructType *StTy = dyn_cast<StructType>(GEPTypes.back())) {
3542 // It's a struct access. CUI is the index into the structure,
3543 // which names the field. This index must have unsigned type.
3544 const ConstantUInt *CUI = cast<ConstantUInt>(GEPOps.back());
3545
3546 // Use the TargetData structure to pick out what the layout of the
3547 // structure is in memory. Since the structure index must be constant, we
3548 // can get its value and use it to find the right byte offset from the
3549 // StructLayout class's list of structure member offsets.
Reid Spencerfc989e12004-08-30 00:13:26 +00003550 AM.Disp += TD.getStructLayout(StTy)->MemberOffsets[CUI->getValue()];
Chris Lattner985fe3d2004-02-25 03:45:50 +00003551 GEPOps.pop_back(); // Consume a GEP operand
3552 GEPTypes.pop_back();
3553 } else {
3554 // It's an array or pointer access: [ArraySize x ElementType].
3555 const SequentialType *SqTy = cast<SequentialType>(GEPTypes.back());
3556 Value *idx = GEPOps.back();
3557
3558 // idx is the index into the array. Unlike with structure
3559 // indices, we may not know its actual value at code-generation
3560 // time.
Chris Lattner985fe3d2004-02-25 03:45:50 +00003561
3562 // If idx is a constant, fold it into the offset.
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003563 unsigned TypeSize = TD.getTypeSize(SqTy->getElementType());
Chris Lattner985fe3d2004-02-25 03:45:50 +00003564 if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(idx)) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003565 AM.Disp += TypeSize*CSI->getValue();
Chris Lattner28977af2004-04-05 01:30:19 +00003566 } else if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(idx)) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003567 AM.Disp += TypeSize*CUI->getValue();
Chris Lattner985fe3d2004-02-25 03:45:50 +00003568 } else {
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003569 // If the index reg is already taken, we can't handle this index.
Reid Spencerfc989e12004-08-30 00:13:26 +00003570 if (AM.IndexReg) return;
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003571
3572 // If this is a size that we can handle, then add the index as
3573 switch (TypeSize) {
3574 case 1: case 2: case 4: case 8:
3575 // These are all acceptable scales on X86.
Reid Spencerfc989e12004-08-30 00:13:26 +00003576 AM.Scale = TypeSize;
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003577 break;
3578 default:
3579 // Otherwise, we can't handle this scale
3580 return;
3581 }
3582
3583 if (CastInst *CI = dyn_cast<CastInst>(idx))
3584 if (CI->getOperand(0)->getType() == Type::IntTy ||
3585 CI->getOperand(0)->getType() == Type::UIntTy)
3586 idx = CI->getOperand(0);
3587
Reid Spencerfc989e12004-08-30 00:13:26 +00003588 AM.IndexReg = MBB ? getReg(idx, MBB, IP) : 1;
Chris Lattner985fe3d2004-02-25 03:45:50 +00003589 }
3590
3591 GEPOps.pop_back(); // Consume a GEP operand
3592 GEPTypes.pop_back();
3593 }
3594 }
Chris Lattnerb6bac512004-02-25 06:13:04 +00003595
Chris Lattnerdf040972004-05-23 21:23:12 +00003596 // GEPTypes is empty, which means we have a single operand left. Set it as
3597 // the base register.
Chris Lattnerb6bac512004-02-25 06:13:04 +00003598 //
Reid Spencerfc989e12004-08-30 00:13:26 +00003599 assert(AM.Base.Reg == 0);
Chris Lattnerdf040972004-05-23 21:23:12 +00003600
Reid Spencerfc989e12004-08-30 00:13:26 +00003601 if (AllocaInst *AI = dyn_castFixedAlloca(GEPOps.back())) {
3602 AM.BaseType = X86AddressMode::FrameIndexBase;
3603 AM.Base.FrameIndex = getFixedSizedAllocaFI(AI);
Chris Lattnerdf040972004-05-23 21:23:12 +00003604 GEPOps.pop_back();
3605 return;
Reid Spencerfc989e12004-08-30 00:13:26 +00003606 }
3607
3608#if 0 // FIXME: TODO!
3609 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattnerdf040972004-05-23 21:23:12 +00003610 // FIXME: When addressing modes are more powerful/correct, we could load
3611 // global addresses directly as 32-bit immediates.
3612 }
3613#endif
3614
Reid Spencerfc989e12004-08-30 00:13:26 +00003615 AM.Base.Reg = MBB ? getReg(GEPOps[0], MBB, IP) : 1;
Chris Lattnerb6bac512004-02-25 06:13:04 +00003616 GEPOps.pop_back(); // Consume the last GEP operand
Chris Lattner985fe3d2004-02-25 03:45:50 +00003617}
3618
3619
Chris Lattnerb6bac512004-02-25 06:13:04 +00003620/// isGEPFoldable - Return true if the specified GEP can be completely
3621/// folded into the addressing mode of a load/store or lea instruction.
3622bool ISel::isGEPFoldable(MachineBasicBlock *MBB,
3623 Value *Src, User::op_iterator IdxBegin,
Reid Spencerfc989e12004-08-30 00:13:26 +00003624 User::op_iterator IdxEnd, X86AddressMode &AM) {
Chris Lattner7ca04092004-02-22 17:35:42 +00003625
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003626 std::vector<Value*> GEPOps;
3627 GEPOps.resize(IdxEnd-IdxBegin+1);
3628 GEPOps[0] = Src;
3629 std::copy(IdxBegin, IdxEnd, GEPOps.begin()+1);
3630
Chris Lattnerdf040972004-05-23 21:23:12 +00003631 std::vector<const Type*>
3632 GEPTypes(gep_type_begin(Src->getType(), IdxBegin, IdxEnd),
3633 gep_type_end(Src->getType(), IdxBegin, IdxEnd));
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003634
Chris Lattnerb6bac512004-02-25 06:13:04 +00003635 MachineBasicBlock::iterator IP;
3636 if (MBB) IP = MBB->end();
Reid Spencerfc989e12004-08-30 00:13:26 +00003637 getGEPIndex(MBB, IP, GEPOps, GEPTypes, AM);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003638
3639 // We can fold it away iff the getGEPIndex call eliminated all operands.
3640 return GEPOps.empty();
3641}
3642
3643void ISel::emitGEPOperation(MachineBasicBlock *MBB,
3644 MachineBasicBlock::iterator IP,
3645 Value *Src, User::op_iterator IdxBegin,
3646 User::op_iterator IdxEnd, unsigned TargetReg) {
3647 const TargetData &TD = TM.getTargetData();
Chris Lattnerb6bac512004-02-25 06:13:04 +00003648
Chris Lattnerd2995df2004-07-15 00:58:53 +00003649 // If this is a getelementptr null, with all constant integer indices, just
3650 // replace it with TargetReg = 42.
3651 if (isa<ConstantPointerNull>(Src)) {
3652 User::op_iterator I = IdxBegin;
3653 for (; I != IdxEnd; ++I)
3654 if (!isa<ConstantInt>(*I))
3655 break;
3656 if (I == IdxEnd) { // All constant indices
3657 unsigned Offset = TD.getIndexedOffset(Src->getType(),
3658 std::vector<Value*>(IdxBegin, IdxEnd));
3659 BuildMI(*MBB, IP, X86::MOV32ri, 1, TargetReg).addImm(Offset);
3660 return;
3661 }
3662 }
3663
Chris Lattnerb6bac512004-02-25 06:13:04 +00003664 std::vector<Value*> GEPOps;
3665 GEPOps.resize(IdxEnd-IdxBegin+1);
3666 GEPOps[0] = Src;
3667 std::copy(IdxBegin, IdxEnd, GEPOps.begin()+1);
3668
3669 std::vector<const Type*> GEPTypes;
3670 GEPTypes.assign(gep_type_begin(Src->getType(), IdxBegin, IdxEnd),
3671 gep_type_end(Src->getType(), IdxBegin, IdxEnd));
Chris Lattner985fe3d2004-02-25 03:45:50 +00003672
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003673 // Keep emitting instructions until we consume the entire GEP instruction.
3674 while (!GEPOps.empty()) {
3675 unsigned OldSize = GEPOps.size();
Reid Spencerfc989e12004-08-30 00:13:26 +00003676 X86AddressMode AM;
3677 getGEPIndex(MBB, IP, GEPOps, GEPTypes, AM);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003678
Chris Lattner985fe3d2004-02-25 03:45:50 +00003679 if (GEPOps.size() != OldSize) {
3680 // getGEPIndex consumed some of the input. Build an LEA instruction here.
Chris Lattnerb6bac512004-02-25 06:13:04 +00003681 unsigned NextTarget = 0;
3682 if (!GEPOps.empty()) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003683 assert(AM.Base.Reg == 0 &&
Chris Lattnerb6bac512004-02-25 06:13:04 +00003684 "getGEPIndex should have left the base register open for chaining!");
Reid Spencerfc989e12004-08-30 00:13:26 +00003685 NextTarget = AM.Base.Reg = makeAnotherReg(Type::UIntTy);
Chris Lattner985fe3d2004-02-25 03:45:50 +00003686 }
Chris Lattnerb6bac512004-02-25 06:13:04 +00003687
Reid Spencerfc989e12004-08-30 00:13:26 +00003688 if (AM.BaseType == X86AddressMode::RegBase &&
3689 AM.IndexReg == 0 && AM.Disp == 0)
3690 BuildMI(*MBB, IP, X86::MOV32rr, 1, TargetReg).addReg(AM.Base.Reg);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003691 else
Reid Spencerfc989e12004-08-30 00:13:26 +00003692 addFullAddress(BuildMI(*MBB, IP, X86::LEA32r, 5, TargetReg), AM);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003693 --IP;
3694 TargetReg = NextTarget;
Chris Lattner985fe3d2004-02-25 03:45:50 +00003695 } else if (GEPTypes.empty()) {
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003696 // The getGEPIndex operation didn't want to build an LEA. Check to see if
3697 // all operands are consumed but the base pointer. If so, just load it
3698 // into the register.
Chris Lattner7ca04092004-02-22 17:35:42 +00003699 if (GlobalValue *GV = dyn_cast<GlobalValue>(GEPOps[0])) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003700 BuildMI(*MBB, IP, X86::MOV32ri, 1, TargetReg).addGlobalAddress(GV);
Chris Lattner7ca04092004-02-22 17:35:42 +00003701 } else {
3702 unsigned BaseReg = getReg(GEPOps[0], MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003703 BuildMI(*MBB, IP, X86::MOV32rr, 1, TargetReg).addReg(BaseReg);
Chris Lattner7ca04092004-02-22 17:35:42 +00003704 }
3705 break; // we are now done
Chris Lattnerb6bac512004-02-25 06:13:04 +00003706
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003707 } else {
Brian Gaeke20244b72002-12-12 15:33:40 +00003708 // It's an array or pointer access: [ArraySize x ElementType].
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003709 const SequentialType *SqTy = cast<SequentialType>(GEPTypes.back());
3710 Value *idx = GEPOps.back();
3711 GEPOps.pop_back(); // Consume a GEP operand
3712 GEPTypes.pop_back();
Chris Lattner8a307e82002-12-16 19:32:50 +00003713
Chris Lattner28977af2004-04-05 01:30:19 +00003714 // Many GEP instructions use a [cast (int/uint) to LongTy] as their
Chris Lattnerf5854472003-06-21 16:01:24 +00003715 // operand on X86. Handle this case directly now...
3716 if (CastInst *CI = dyn_cast<CastInst>(idx))
3717 if (CI->getOperand(0)->getType() == Type::IntTy ||
3718 CI->getOperand(0)->getType() == Type::UIntTy)
3719 idx = CI->getOperand(0);
3720
Chris Lattner3e130a22003-01-13 00:32:26 +00003721 // We want to add BaseReg to(idxReg * sizeof ElementType). First, we
Chris Lattner8a307e82002-12-16 19:32:50 +00003722 // must find the size of the pointed-to type (Not coincidentally, the next
3723 // type is the type of the elements in the array).
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003724 const Type *ElTy = SqTy->getElementType();
3725 unsigned elementSize = TD.getTypeSize(ElTy);
Chris Lattner8a307e82002-12-16 19:32:50 +00003726
3727 // If idxReg is a constant, we don't need to perform the multiply!
Chris Lattner28977af2004-04-05 01:30:19 +00003728 if (ConstantInt *CSI = dyn_cast<ConstantInt>(idx)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00003729 if (!CSI->isNullValue()) {
Chris Lattner28977af2004-04-05 01:30:19 +00003730 unsigned Offset = elementSize*CSI->getRawValue();
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003731 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003732 BuildMI(*MBB, IP, X86::ADD32ri, 2, TargetReg)
Chris Lattneree352852004-02-29 07:22:16 +00003733 .addReg(Reg).addImm(Offset);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003734 --IP; // Insert the next instruction before this one.
3735 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00003736 }
3737 } else if (elementSize == 1) {
3738 // If the element size is 1, we don't have to multiply, just add
3739 unsigned idxReg = getReg(idx, MBB, IP);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003740 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003741 BuildMI(*MBB, IP, X86::ADD32rr, 2,TargetReg).addReg(Reg).addReg(idxReg);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003742 --IP; // Insert the next instruction before this one.
3743 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00003744 } else {
3745 unsigned idxReg = getReg(idx, MBB, IP);
3746 unsigned OffsetReg = makeAnotherReg(Type::UIntTy);
Chris Lattnerb2acc512003-10-19 21:09:10 +00003747
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003748 // Make sure we can back the iterator up to point to the first
3749 // instruction emitted.
3750 MachineBasicBlock::iterator BeforeIt = IP;
3751 if (IP == MBB->begin())
3752 BeforeIt = MBB->end();
3753 else
3754 --BeforeIt;
Chris Lattnerb2acc512003-10-19 21:09:10 +00003755 doMultiplyConst(MBB, IP, OffsetReg, Type::IntTy, idxReg, elementSize);
3756
Chris Lattner8a307e82002-12-16 19:32:50 +00003757 // Emit an ADD to add OffsetReg to the basePtr.
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003758 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003759 BuildMI(*MBB, IP, X86::ADD32rr, 2, TargetReg)
Chris Lattneree352852004-02-29 07:22:16 +00003760 .addReg(Reg).addReg(OffsetReg);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003761
3762 // Step to the first instruction of the multiply.
3763 if (BeforeIt == MBB->end())
3764 IP = MBB->begin();
3765 else
3766 IP = ++BeforeIt;
3767
3768 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00003769 }
Brian Gaeke20244b72002-12-12 15:33:40 +00003770 }
Brian Gaeke20244b72002-12-12 15:33:40 +00003771 }
Brian Gaeke20244b72002-12-12 15:33:40 +00003772}
3773
Chris Lattner065faeb2002-12-28 20:24:02 +00003774/// visitAllocaInst - If this is a fixed size alloca, allocate space from the
3775/// frame manager, otherwise do it the hard way.
3776///
3777void ISel::visitAllocaInst(AllocaInst &I) {
Chris Lattner9f1b5312004-05-13 15:12:43 +00003778 // If this is a fixed size alloca in the entry block for the function, we
3779 // statically stack allocate the space, so we don't need to do anything here.
3780 //
Chris Lattnercb2fd552004-05-13 07:40:27 +00003781 if (dyn_castFixedAlloca(&I)) return;
Chris Lattner9f1b5312004-05-13 15:12:43 +00003782
Brian Gaekee48ec012002-12-13 06:46:31 +00003783 // Find the data size of the alloca inst's getAllocatedType.
Chris Lattner065faeb2002-12-28 20:24:02 +00003784 const Type *Ty = I.getAllocatedType();
3785 unsigned TySize = TM.getTargetData().getTypeSize(Ty);
3786
Chris Lattner065faeb2002-12-28 20:24:02 +00003787 // Create a register to hold the temporary result of multiplying the type size
3788 // constant by the variable amount.
3789 unsigned TotalSizeReg = makeAnotherReg(Type::UIntTy);
3790 unsigned SrcReg1 = getReg(I.getArraySize());
Chris Lattner065faeb2002-12-28 20:24:02 +00003791
3792 // TotalSizeReg = mul <numelements>, <TypeSize>
3793 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattnerb2acc512003-10-19 21:09:10 +00003794 doMultiplyConst(BB, MBBI, TotalSizeReg, Type::UIntTy, SrcReg1, TySize);
Chris Lattner065faeb2002-12-28 20:24:02 +00003795
3796 // AddedSize = add <TotalSizeReg>, 15
3797 unsigned AddedSizeReg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003798 BuildMI(BB, X86::ADD32ri, 2, AddedSizeReg).addReg(TotalSizeReg).addImm(15);
Chris Lattner065faeb2002-12-28 20:24:02 +00003799
3800 // AlignedSize = and <AddedSize>, ~15
3801 unsigned AlignedSize = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003802 BuildMI(BB, X86::AND32ri, 2, AlignedSize).addReg(AddedSizeReg).addImm(~15);
Chris Lattner065faeb2002-12-28 20:24:02 +00003803
Brian Gaekee48ec012002-12-13 06:46:31 +00003804 // Subtract size from stack pointer, thereby allocating some space.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003805 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(AlignedSize);
Chris Lattner065faeb2002-12-28 20:24:02 +00003806
Brian Gaekee48ec012002-12-13 06:46:31 +00003807 // Put a pointer to the space into the result register, by copying
3808 // the stack pointer.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003809 BuildMI(BB, X86::MOV32rr, 1, getReg(I)).addReg(X86::ESP);
Chris Lattner065faeb2002-12-28 20:24:02 +00003810
Misha Brukman48196b32003-05-03 02:18:17 +00003811 // Inform the Frame Information that we have just allocated a variable-sized
Chris Lattner065faeb2002-12-28 20:24:02 +00003812 // object.
3813 F->getFrameInfo()->CreateVariableSizedObject();
Brian Gaeke20244b72002-12-12 15:33:40 +00003814}
Chris Lattner3e130a22003-01-13 00:32:26 +00003815
3816/// visitMallocInst - Malloc instructions are code generated into direct calls
3817/// to the library malloc.
3818///
3819void ISel::visitMallocInst(MallocInst &I) {
3820 unsigned AllocSize = TM.getTargetData().getTypeSize(I.getAllocatedType());
3821 unsigned Arg;
3822
3823 if (ConstantUInt *C = dyn_cast<ConstantUInt>(I.getOperand(0))) {
3824 Arg = getReg(ConstantUInt::get(Type::UIntTy, C->getValue() * AllocSize));
3825 } else {
3826 Arg = makeAnotherReg(Type::UIntTy);
Chris Lattnerb2acc512003-10-19 21:09:10 +00003827 unsigned Op0Reg = getReg(I.getOperand(0));
Chris Lattner3e130a22003-01-13 00:32:26 +00003828 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattnerb2acc512003-10-19 21:09:10 +00003829 doMultiplyConst(BB, MBBI, Arg, Type::UIntTy, Op0Reg, AllocSize);
Chris Lattner3e130a22003-01-13 00:32:26 +00003830 }
3831
3832 std::vector<ValueRecord> Args;
3833 Args.push_back(ValueRecord(Arg, Type::UIntTy));
3834 MachineInstr *TheCall = BuildMI(X86::CALLpcrel32,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003835 1).addExternalSymbol("malloc", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00003836 doCall(ValueRecord(getReg(I), I.getType()), TheCall, Args);
3837}
3838
3839
3840/// visitFreeInst - Free instructions are code gen'd to call the free libc
3841/// function.
3842///
3843void ISel::visitFreeInst(FreeInst &I) {
3844 std::vector<ValueRecord> Args;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00003845 Args.push_back(ValueRecord(I.getOperand(0)));
Chris Lattner3e130a22003-01-13 00:32:26 +00003846 MachineInstr *TheCall = BuildMI(X86::CALLpcrel32,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003847 1).addExternalSymbol("free", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00003848 doCall(ValueRecord(0, Type::VoidTy), TheCall, Args);
3849}
3850
Chris Lattnerd281de22003-07-26 23:49:58 +00003851/// createX86SimpleInstructionSelector - This pass converts an LLVM function
Chris Lattnerb4f68ed2002-10-29 22:37:54 +00003852/// into a machine code representation is a very simple peep-hole fashion. The
Chris Lattner72614082002-10-25 22:55:53 +00003853/// generated code sucks but the implementation is nice and simple.
3854///
Chris Lattnerf70e0c22003-12-28 21:23:38 +00003855FunctionPass *llvm::createX86SimpleInstructionSelector(TargetMachine &TM) {
3856 return new ISel(TM);
Chris Lattner72614082002-10-25 22:55:53 +00003857}