blob: a5277c0d2a6686067de41ced72cdbd7600a82b3e [file] [log] [blame]
Chris Lattner72614082002-10-25 22:55:53 +00001//===-- InstSelectSimple.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"
Chris Lattner44827152003-12-28 09:47:19 +000021#include "llvm/IntrinsicLowering.h"
Misha Brukmanc8893fc2003-10-23 16:22:08 +000022#include "llvm/Pass.h"
23#include "llvm/CodeGen/MachineConstantPool.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner341a9372002-10-29 17:43:55 +000025#include "llvm/CodeGen/MachineFunction.h"
Misha Brukmand2cc0172002-11-20 00:58:23 +000026#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner94af4142002-12-25 05:13:53 +000027#include "llvm/CodeGen/SSARegMap.h"
Misha Brukmand2cc0172002-11-20 00:58:23 +000028#include "llvm/Target/MRegisterInfo.h"
Misha Brukmanc8893fc2003-10-23 16:22:08 +000029#include "llvm/Target/TargetMachine.h"
Chris Lattner3f1e8e72004-02-22 07:04:00 +000030#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattner67580ed2003-05-13 20:21:19 +000031#include "llvm/Support/InstVisitor.h"
Chris Lattnercf93cdd2004-01-30 22:13:44 +000032#include "llvm/Support/CFG.h"
Chris Lattner986618e2004-02-22 19:47:26 +000033#include "Support/Statistic.h"
Chris Lattner44827152003-12-28 09:47:19 +000034using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000035
Chris Lattner986618e2004-02-22 19:47:26 +000036namespace {
37 Statistic<>
38 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
39}
Chris Lattnercf93cdd2004-01-30 22:13:44 +000040
Chris Lattner333b2fa2002-12-13 10:09:43 +000041/// BMI - A special BuildMI variant that takes an iterator to insert the
Chris Lattner8bdd1292003-04-25 21:58:54 +000042/// instruction at as well as a basic block. This is the version for when you
43/// have a destination register in mind.
Brian Gaeke71794c02002-12-13 11:22:48 +000044inline static MachineInstrBuilder BMI(MachineBasicBlock *MBB,
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +000045 MachineBasicBlock::iterator I,
Chris Lattner8cc72d22003-06-03 15:41:58 +000046 int Opcode, unsigned NumOperands,
Chris Lattner333b2fa2002-12-13 10:09:43 +000047 unsigned DestReg) {
48 MachineInstr *MI = new MachineInstr(Opcode, NumOperands+1, true, true);
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +000049 MBB->insert(I, MI);
Alkis Evlogimenos890f9232004-02-22 19:23:26 +000050 return MachineInstrBuilder(MI).addReg(DestReg, MachineOperand::Def);
Chris Lattner333b2fa2002-12-13 10:09:43 +000051}
52
Chris Lattnerf08ad9f2002-12-13 10:50:40 +000053/// BMI - A special BuildMI variant that takes an iterator to insert the
54/// instruction at as well as a basic block.
Brian Gaeke71794c02002-12-13 11:22:48 +000055inline static MachineInstrBuilder BMI(MachineBasicBlock *MBB,
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +000056 MachineBasicBlock::iterator I,
Chris Lattner8cc72d22003-06-03 15:41:58 +000057 int Opcode, unsigned NumOperands) {
Chris Lattnerf08ad9f2002-12-13 10:50:40 +000058 MachineInstr *MI = new MachineInstr(Opcode, NumOperands, true, true);
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +000059 MBB->insert(I, MI);
Chris Lattnerf08ad9f2002-12-13 10:50:40 +000060 return MachineInstrBuilder(MI);
61}
62
Chris Lattner333b2fa2002-12-13 10:09:43 +000063
Chris Lattner72614082002-10-25 22:55:53 +000064namespace {
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000065 struct ISel : public FunctionPass, InstVisitor<ISel> {
66 TargetMachine &TM;
Chris Lattnereca195e2003-05-08 19:44:13 +000067 MachineFunction *F; // The function we are compiling into
68 MachineBasicBlock *BB; // The current MBB we are compiling
69 int VarArgsFrameIndex; // FrameIndex for start of varargs area
Chris Lattner0e5b79c2004-02-15 01:04:03 +000070 int ReturnAddressIndex; // FrameIndex for the return address
Chris Lattner72614082002-10-25 22:55:53 +000071
Chris Lattner72614082002-10-25 22:55:53 +000072 std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs
73
Chris Lattner333b2fa2002-12-13 10:09:43 +000074 // MBBMap - Mapping between LLVM BB -> Machine BB
75 std::map<const BasicBlock*, MachineBasicBlock*> MBBMap;
76
Chris Lattnerf70e0c22003-12-28 21:23:38 +000077 ISel(TargetMachine &tm) : TM(tm), F(0), BB(0) {}
Chris Lattner72614082002-10-25 22:55:53 +000078
79 /// runOnFunction - Top level implementation of instruction selection for
80 /// the entire function.
81 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000082 bool runOnFunction(Function &Fn) {
Chris Lattner44827152003-12-28 09:47:19 +000083 // First pass over the function, lower any unknown intrinsic functions
84 // with the IntrinsicLowering class.
85 LowerUnknownIntrinsicFunctionCalls(Fn);
86
Chris Lattner36b36032002-10-29 23:40:58 +000087 F = &MachineFunction::construct(&Fn, TM);
Chris Lattner333b2fa2002-12-13 10:09:43 +000088
Chris Lattner065faeb2002-12-28 20:24:02 +000089 // Create all of the machine basic blocks for the function...
Chris Lattner333b2fa2002-12-13 10:09:43 +000090 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
91 F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I));
92
Chris Lattner14aa7fe2002-12-16 22:54:46 +000093 BB = &F->front();
Chris Lattnerdbd73722003-05-06 21:32:22 +000094
Chris Lattner0e5b79c2004-02-15 01:04:03 +000095 // Set up a frame object for the return address. This is used by the
96 // llvm.returnaddress & llvm.frameaddress intrinisics.
97 ReturnAddressIndex = F->getFrameInfo()->CreateFixedObject(4, -4);
98
Chris Lattnerdbd73722003-05-06 21:32:22 +000099 // Copy incoming arguments off of the stack...
Chris Lattner065faeb2002-12-28 20:24:02 +0000100 LoadArgumentsToVirtualRegs(Fn);
Chris Lattner14aa7fe2002-12-16 22:54:46 +0000101
Chris Lattner333b2fa2002-12-13 10:09:43 +0000102 // Instruction select everything except PHI nodes
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000103 visit(Fn);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000104
105 // Select the PHI nodes
106 SelectPHINodes();
107
Chris Lattner986618e2004-02-22 19:47:26 +0000108 // Insert the FP_REG_KILL instructions into blocks that need them.
109 InsertFPRegKills();
110
Chris Lattner72614082002-10-25 22:55:53 +0000111 RegMap.clear();
Chris Lattner333b2fa2002-12-13 10:09:43 +0000112 MBBMap.clear();
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000113 F = 0;
Chris Lattner2a865b02003-07-26 23:05:37 +0000114 // We always build a machine code representation for the function
115 return true;
Chris Lattner72614082002-10-25 22:55:53 +0000116 }
117
Chris Lattnerf0eb7be2002-12-15 21:13:40 +0000118 virtual const char *getPassName() const {
119 return "X86 Simple Instruction Selection";
120 }
121
Chris Lattner72614082002-10-25 22:55:53 +0000122 /// visitBasicBlock - This method is called when we are visiting a new basic
Chris Lattner33f53b52002-10-29 20:48:56 +0000123 /// block. This simply creates a new MachineBasicBlock to emit code into
124 /// and adds it to the current MachineFunction. Subsequent visit* for
125 /// instructions will be invoked for all instructions in the basic block.
Chris Lattner72614082002-10-25 22:55:53 +0000126 ///
127 void visitBasicBlock(BasicBlock &LLVM_BB) {
Chris Lattner333b2fa2002-12-13 10:09:43 +0000128 BB = MBBMap[&LLVM_BB];
Chris Lattner72614082002-10-25 22:55:53 +0000129 }
130
Chris Lattner44827152003-12-28 09:47:19 +0000131 /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
132 /// function, lowering any calls to unknown intrinsic functions into the
133 /// equivalent LLVM code.
134 void LowerUnknownIntrinsicFunctionCalls(Function &F);
135
Chris Lattner065faeb2002-12-28 20:24:02 +0000136 /// LoadArgumentsToVirtualRegs - Load all of the arguments to this function
137 /// from the stack into virtual registers.
138 ///
139 void LoadArgumentsToVirtualRegs(Function &F);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000140
141 /// SelectPHINodes - Insert machine code to generate phis. This is tricky
142 /// because we have to generate our sources into the source basic blocks,
143 /// not the current one.
144 ///
145 void SelectPHINodes();
146
Chris Lattner986618e2004-02-22 19:47:26 +0000147 /// InsertFPRegKills - Insert FP_REG_KILL instructions into basic blocks
148 /// that need them. This only occurs due to the floating point stackifier
149 /// not being aggressive enough to handle arbitrary global stackification.
150 ///
151 void InsertFPRegKills();
152
Chris Lattner72614082002-10-25 22:55:53 +0000153 // Visitation methods for various instructions. These methods simply emit
154 // fixed X86 code for each instruction.
155 //
Brian Gaekefa8d5712002-11-22 11:07:01 +0000156
157 // Control flow operators
Chris Lattner72614082002-10-25 22:55:53 +0000158 void visitReturnInst(ReturnInst &RI);
Chris Lattner2df035b2002-11-02 19:27:56 +0000159 void visitBranchInst(BranchInst &BI);
Chris Lattner3e130a22003-01-13 00:32:26 +0000160
161 struct ValueRecord {
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000162 Value *Val;
Chris Lattner3e130a22003-01-13 00:32:26 +0000163 unsigned Reg;
164 const Type *Ty;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000165 ValueRecord(unsigned R, const Type *T) : Val(0), Reg(R), Ty(T) {}
166 ValueRecord(Value *V) : Val(V), Reg(0), Ty(V->getType()) {}
Chris Lattner3e130a22003-01-13 00:32:26 +0000167 };
168 void doCall(const ValueRecord &Ret, MachineInstr *CallMI,
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000169 const std::vector<ValueRecord> &Args);
Brian Gaekefa8d5712002-11-22 11:07:01 +0000170 void visitCallInst(CallInst &I);
Brian Gaeked0fde302003-11-11 22:41:34 +0000171 void visitIntrinsicCall(Intrinsic::ID ID, CallInst &I);
Chris Lattnere2954c82002-11-02 20:04:26 +0000172
173 // Arithmetic operators
Chris Lattnerf01729e2002-11-02 20:54:46 +0000174 void visitSimpleBinary(BinaryOperator &B, unsigned OpcodeClass);
Chris Lattner68aad932002-11-02 20:13:22 +0000175 void visitAdd(BinaryOperator &B) { visitSimpleBinary(B, 0); }
176 void visitSub(BinaryOperator &B) { visitSimpleBinary(B, 1); }
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000177 void doMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI,
Chris Lattner3e130a22003-01-13 00:32:26 +0000178 unsigned DestReg, const Type *DestTy,
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000179 unsigned Op0Reg, unsigned Op1Reg);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000180 void doMultiplyConst(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000181 MachineBasicBlock::iterator MBBI,
Chris Lattnerb2acc512003-10-19 21:09:10 +0000182 unsigned DestReg, const Type *DestTy,
183 unsigned Op0Reg, unsigned Op1Val);
Chris Lattnerca9671d2002-11-02 20:28:58 +0000184 void visitMul(BinaryOperator &B);
Chris Lattnere2954c82002-11-02 20:04:26 +0000185
Chris Lattnerf01729e2002-11-02 20:54:46 +0000186 void visitDiv(BinaryOperator &B) { visitDivRem(B); }
187 void visitRem(BinaryOperator &B) { visitDivRem(B); }
188 void visitDivRem(BinaryOperator &B);
189
Chris Lattnere2954c82002-11-02 20:04:26 +0000190 // Bitwise operators
Chris Lattner68aad932002-11-02 20:13:22 +0000191 void visitAnd(BinaryOperator &B) { visitSimpleBinary(B, 2); }
192 void visitOr (BinaryOperator &B) { visitSimpleBinary(B, 3); }
193 void visitXor(BinaryOperator &B) { visitSimpleBinary(B, 4); }
Chris Lattnere2954c82002-11-02 20:04:26 +0000194
Chris Lattner6d40c192003-01-16 16:43:00 +0000195 // Comparison operators...
196 void visitSetCondInst(SetCondInst &I);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000197 unsigned EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
198 MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000199 MachineBasicBlock::iterator MBBI);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000200
Chris Lattner6fc3c522002-11-17 21:11:55 +0000201 // Memory Instructions
202 void visitLoadInst(LoadInst &I);
203 void visitStoreInst(StoreInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000204 void visitGetElementPtrInst(GetElementPtrInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000205 void visitAllocaInst(AllocaInst &I);
Chris Lattner3e130a22003-01-13 00:32:26 +0000206 void visitMallocInst(MallocInst &I);
207 void visitFreeInst(FreeInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000208
Chris Lattnere2954c82002-11-02 20:04:26 +0000209 // Other operators
Brian Gaekea1719c92002-10-31 23:03:59 +0000210 void visitShiftInst(ShiftInst &I);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000211 void visitPHINode(PHINode &I) {} // PHI nodes handled by second pass
Brian Gaekefa8d5712002-11-22 11:07:01 +0000212 void visitCastInst(CastInst &I);
Chris Lattner73815062003-10-18 05:56:40 +0000213 void visitVANextInst(VANextInst &I);
214 void visitVAArgInst(VAArgInst &I);
Chris Lattner72614082002-10-25 22:55:53 +0000215
216 void visitInstruction(Instruction &I) {
217 std::cerr << "Cannot instruction select: " << I;
218 abort();
219 }
220
Brian Gaeke95780cc2002-12-13 07:56:18 +0000221 /// promote32 - Make a value 32-bits wide, and put it somewhere.
Chris Lattner3e130a22003-01-13 00:32:26 +0000222 ///
223 void promote32(unsigned targetReg, const ValueRecord &VR);
224
Chris Lattner3e130a22003-01-13 00:32:26 +0000225 /// emitGEPOperation - Common code shared between visitGetElementPtrInst and
226 /// constant expression GEP support.
227 ///
Chris Lattner827832c2004-02-22 17:05:38 +0000228 void emitGEPOperation(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
Chris Lattner333b2fa2002-12-13 10:09:43 +0000229 Value *Src, User::op_iterator IdxBegin,
Chris Lattnerc0812d82002-12-13 06:56:29 +0000230 User::op_iterator IdxEnd, unsigned TargetReg);
231
Chris Lattner548f61d2003-04-23 17:22:12 +0000232 /// emitCastOperation - Common code shared between visitCastInst and
233 /// constant expression cast support.
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000234 void emitCastOperation(MachineBasicBlock *BB,MachineBasicBlock::iterator IP,
Chris Lattner548f61d2003-04-23 17:22:12 +0000235 Value *Src, const Type *DestTy, unsigned TargetReg);
236
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000237 /// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
238 /// and constant expression support.
239 void emitSimpleBinaryOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000240 MachineBasicBlock::iterator IP,
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000241 Value *Op0, Value *Op1,
242 unsigned OperatorClass, unsigned TargetReg);
243
Chris Lattnercadff442003-10-23 17:21:43 +0000244 void emitDivRemOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000245 MachineBasicBlock::iterator IP,
Chris Lattnercadff442003-10-23 17:21:43 +0000246 unsigned Op0Reg, unsigned Op1Reg, bool isDiv,
247 const Type *Ty, unsigned TargetReg);
248
Chris Lattner58c41fe2003-08-24 19:19:47 +0000249 /// emitSetCCOperation - Common code shared between visitSetCondInst and
250 /// constant expression support.
251 void emitSetCCOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000252 MachineBasicBlock::iterator IP,
Chris Lattner58c41fe2003-08-24 19:19:47 +0000253 Value *Op0, Value *Op1, unsigned Opcode,
254 unsigned TargetReg);
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000255
256 /// emitShiftOperation - Common code shared between visitShiftInst and
257 /// constant expression support.
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000258 void emitShiftOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000259 MachineBasicBlock::iterator IP,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000260 Value *Op, Value *ShiftAmount, bool isLeftShift,
261 const Type *ResultTy, unsigned DestReg);
262
Chris Lattner58c41fe2003-08-24 19:19:47 +0000263
Chris Lattnerc5291f52002-10-27 21:16:59 +0000264 /// copyConstantToRegister - Output the instructions required to put the
265 /// specified constant into the specified register.
266 ///
Chris Lattner8a307e82002-12-16 19:32:50 +0000267 void copyConstantToRegister(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000268 MachineBasicBlock::iterator MBBI,
Chris Lattner8a307e82002-12-16 19:32:50 +0000269 Constant *C, unsigned Reg);
Chris Lattnerc5291f52002-10-27 21:16:59 +0000270
Chris Lattner3e130a22003-01-13 00:32:26 +0000271 /// makeAnotherReg - This method returns the next register number we haven't
272 /// yet used.
273 ///
274 /// Long values are handled somewhat specially. They are always allocated
275 /// as pairs of 32 bit integer values. The register number returned is the
276 /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits
277 /// of the long value.
278 ///
Chris Lattnerc0812d82002-12-13 06:56:29 +0000279 unsigned makeAnotherReg(const Type *Ty) {
Chris Lattner7db1fa92003-07-30 05:33:48 +0000280 assert(dynamic_cast<const X86RegisterInfo*>(TM.getRegisterInfo()) &&
281 "Current target doesn't have X86 reg info??");
282 const X86RegisterInfo *MRI =
283 static_cast<const X86RegisterInfo*>(TM.getRegisterInfo());
Chris Lattner3e130a22003-01-13 00:32:26 +0000284 if (Ty == Type::LongTy || Ty == Type::ULongTy) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000285 const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy);
286 // Create the lower part
287 F->getSSARegMap()->createVirtualRegister(RC);
288 // Create the upper part.
289 return F->getSSARegMap()->createVirtualRegister(RC)-1;
Chris Lattner3e130a22003-01-13 00:32:26 +0000290 }
291
Chris Lattnerc0812d82002-12-13 06:56:29 +0000292 // Add the mapping of regnumber => reg class to MachineFunction
Chris Lattner7db1fa92003-07-30 05:33:48 +0000293 const TargetRegisterClass *RC = MRI->getRegClassForType(Ty);
Chris Lattner3e130a22003-01-13 00:32:26 +0000294 return F->getSSARegMap()->createVirtualRegister(RC);
Brian Gaeke20244b72002-12-12 15:33:40 +0000295 }
296
Chris Lattner72614082002-10-25 22:55:53 +0000297 /// getReg - This method turns an LLVM value into a register number. This
298 /// is guaranteed to produce the same register number for a particular value
299 /// every time it is queried.
300 ///
301 unsigned getReg(Value &V) { return getReg(&V); } // Allow references
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000302 unsigned getReg(Value *V) {
303 // Just append to the end of the current bb.
304 MachineBasicBlock::iterator It = BB->end();
305 return getReg(V, BB, It);
306 }
Brian Gaeke71794c02002-12-13 11:22:48 +0000307 unsigned getReg(Value *V, MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000308 MachineBasicBlock::iterator IPt) {
Chris Lattner72614082002-10-25 22:55:53 +0000309 unsigned &Reg = RegMap[V];
Misha Brukmand2cc0172002-11-20 00:58:23 +0000310 if (Reg == 0) {
Chris Lattnerc0812d82002-12-13 06:56:29 +0000311 Reg = makeAnotherReg(V->getType());
Misha Brukmand2cc0172002-11-20 00:58:23 +0000312 RegMap[V] = Reg;
Misha Brukmand2cc0172002-11-20 00:58:23 +0000313 }
Chris Lattner72614082002-10-25 22:55:53 +0000314
Chris Lattner6f8fd252002-10-27 21:23:43 +0000315 // If this operand is a constant, emit the code to copy the constant into
316 // the register here...
317 //
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000318 if (Constant *C = dyn_cast<Constant>(V)) {
Chris Lattner8a307e82002-12-16 19:32:50 +0000319 copyConstantToRegister(MBB, IPt, C, Reg);
Chris Lattner14aa7fe2002-12-16 22:54:46 +0000320 RegMap.erase(V); // Assign a new name to this constant if ref'd again
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000321 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
322 // Move the address of the global into the register
Chris Lattner6e173a02004-02-17 06:16:44 +0000323 BMI(MBB, IPt, X86::MOVri32, 1, Reg).addGlobalAddress(GV);
Chris Lattner14aa7fe2002-12-16 22:54:46 +0000324 RegMap.erase(V); // Assign a new name to this address if ref'd again
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000325 }
Chris Lattnerc5291f52002-10-27 21:16:59 +0000326
Chris Lattner72614082002-10-25 22:55:53 +0000327 return Reg;
328 }
Chris Lattner72614082002-10-25 22:55:53 +0000329 };
330}
331
Chris Lattner43189d12002-11-17 20:07:45 +0000332/// TypeClass - Used by the X86 backend to group LLVM types by their basic X86
333/// Representation.
334///
335enum TypeClass {
Chris Lattner94af4142002-12-25 05:13:53 +0000336 cByte, cShort, cInt, cFP, cLong
Chris Lattner43189d12002-11-17 20:07:45 +0000337};
338
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000339/// getClass - Turn a primitive type into a "class" number which is based on the
340/// size of the type, and whether or not it is floating point.
341///
Chris Lattner43189d12002-11-17 20:07:45 +0000342static inline TypeClass getClass(const Type *Ty) {
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000343 switch (Ty->getPrimitiveID()) {
344 case Type::SByteTyID:
Chris Lattner43189d12002-11-17 20:07:45 +0000345 case Type::UByteTyID: return cByte; // Byte operands are class #0
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000346 case Type::ShortTyID:
Chris Lattner43189d12002-11-17 20:07:45 +0000347 case Type::UShortTyID: return cShort; // Short operands are class #1
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000348 case Type::IntTyID:
349 case Type::UIntTyID:
Chris Lattner43189d12002-11-17 20:07:45 +0000350 case Type::PointerTyID: return cInt; // Int's and pointers are class #2
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000351
Chris Lattner94af4142002-12-25 05:13:53 +0000352 case Type::FloatTyID:
353 case Type::DoubleTyID: return cFP; // Floating Point is #3
Chris Lattner3e130a22003-01-13 00:32:26 +0000354
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000355 case Type::LongTyID:
Chris Lattner3e130a22003-01-13 00:32:26 +0000356 case Type::ULongTyID: return cLong; // Longs are class #4
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000357 default:
358 assert(0 && "Invalid type to getClass!");
Chris Lattner43189d12002-11-17 20:07:45 +0000359 return cByte; // not reached
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000360 }
361}
Chris Lattnerc5291f52002-10-27 21:16:59 +0000362
Chris Lattner6b993cc2002-12-15 08:02:15 +0000363// getClassB - Just like getClass, but treat boolean values as bytes.
364static inline TypeClass getClassB(const Type *Ty) {
365 if (Ty == Type::BoolTy) return cByte;
366 return getClass(Ty);
367}
368
Chris Lattner06925362002-11-17 21:56:38 +0000369
Chris Lattnerc5291f52002-10-27 21:16:59 +0000370/// copyConstantToRegister - Output the instructions required to put the
371/// specified constant into the specified register.
372///
Chris Lattner8a307e82002-12-16 19:32:50 +0000373void ISel::copyConstantToRegister(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000374 MachineBasicBlock::iterator IP,
Chris Lattner8a307e82002-12-16 19:32:50 +0000375 Constant *C, unsigned R) {
Chris Lattnerc0812d82002-12-13 06:56:29 +0000376 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000377 unsigned Class = 0;
378 switch (CE->getOpcode()) {
379 case Instruction::GetElementPtr:
Brian Gaeke68b1edc2002-12-16 04:23:29 +0000380 emitGEPOperation(MBB, IP, CE->getOperand(0),
Chris Lattner333b2fa2002-12-13 10:09:43 +0000381 CE->op_begin()+1, CE->op_end(), R);
Chris Lattnerc0812d82002-12-13 06:56:29 +0000382 return;
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000383 case Instruction::Cast:
Chris Lattner548f61d2003-04-23 17:22:12 +0000384 emitCastOperation(MBB, IP, CE->getOperand(0), CE->getType(), R);
Chris Lattner4b12cde2003-04-21 21:33:44 +0000385 return;
Chris Lattnerc0812d82002-12-13 06:56:29 +0000386
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000387 case Instruction::Xor: ++Class; // FALL THROUGH
388 case Instruction::Or: ++Class; // FALL THROUGH
389 case Instruction::And: ++Class; // FALL THROUGH
390 case Instruction::Sub: ++Class; // FALL THROUGH
391 case Instruction::Add:
392 emitSimpleBinaryOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
393 Class, R);
394 return;
395
Chris Lattnercadff442003-10-23 17:21:43 +0000396 case Instruction::Mul: {
397 unsigned Op0Reg = getReg(CE->getOperand(0), MBB, IP);
398 unsigned Op1Reg = getReg(CE->getOperand(1), MBB, IP);
399 doMultiply(MBB, IP, R, CE->getType(), Op0Reg, Op1Reg);
400 return;
401 }
402 case Instruction::Div:
403 case Instruction::Rem: {
404 unsigned Op0Reg = getReg(CE->getOperand(0), MBB, IP);
405 unsigned Op1Reg = getReg(CE->getOperand(1), MBB, IP);
406 emitDivRemOperation(MBB, IP, Op0Reg, Op1Reg,
407 CE->getOpcode() == Instruction::Div,
408 CE->getType(), R);
409 return;
410 }
411
Chris Lattner58c41fe2003-08-24 19:19:47 +0000412 case Instruction::SetNE:
413 case Instruction::SetEQ:
414 case Instruction::SetLT:
415 case Instruction::SetGT:
416 case Instruction::SetLE:
417 case Instruction::SetGE:
418 emitSetCCOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
419 CE->getOpcode(), R);
420 return;
421
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000422 case Instruction::Shl:
423 case Instruction::Shr:
424 emitShiftOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000425 CE->getOpcode() == Instruction::Shl, CE->getType(), R);
426 return;
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000427
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000428 default:
429 std::cerr << "Offending expr: " << C << "\n";
Chris Lattnerb2acc512003-10-19 21:09:10 +0000430 assert(0 && "Constant expression not yet handled!\n");
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000431 }
Brian Gaeke20244b72002-12-12 15:33:40 +0000432 }
Chris Lattnerc5291f52002-10-27 21:16:59 +0000433
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000434 if (C->getType()->isIntegral()) {
Chris Lattner6b993cc2002-12-15 08:02:15 +0000435 unsigned Class = getClassB(C->getType());
Chris Lattner3e130a22003-01-13 00:32:26 +0000436
437 if (Class == cLong) {
438 // Copy the value into the register pair.
Chris Lattnerc07736a2003-07-23 15:22:26 +0000439 uint64_t Val = cast<ConstantInt>(C)->getRawValue();
Chris Lattner6e173a02004-02-17 06:16:44 +0000440 BMI(MBB, IP, X86::MOVri32, 1, R).addZImm(Val & 0xFFFFFFFF);
441 BMI(MBB, IP, X86::MOVri32, 1, R+1).addZImm(Val >> 32);
Chris Lattner3e130a22003-01-13 00:32:26 +0000442 return;
443 }
444
Chris Lattner94af4142002-12-25 05:13:53 +0000445 assert(Class <= cInt && "Type not handled yet!");
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000446
447 static const unsigned IntegralOpcodeTab[] = {
Chris Lattner6e173a02004-02-17 06:16:44 +0000448 X86::MOVri8, X86::MOVri16, X86::MOVri32
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000449 };
450
Chris Lattner6b993cc2002-12-15 08:02:15 +0000451 if (C->getType() == Type::BoolTy) {
Chris Lattner6e173a02004-02-17 06:16:44 +0000452 BMI(MBB, IP, X86::MOVri8, 1, R).addZImm(C == ConstantBool::True);
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000453 } else {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000454 ConstantInt *CI = cast<ConstantInt>(C);
455 BMI(MBB, IP, IntegralOpcodeTab[Class], 1, R).addZImm(CI->getRawValue());
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000456 }
Chris Lattner94af4142002-12-25 05:13:53 +0000457 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
Chris Lattneraf703622004-02-02 18:56:30 +0000458 if (CFP->isExactlyValue(+0.0))
Chris Lattner94af4142002-12-25 05:13:53 +0000459 BMI(MBB, IP, X86::FLD0, 0, R);
Chris Lattneraf703622004-02-02 18:56:30 +0000460 else if (CFP->isExactlyValue(+1.0))
Chris Lattner94af4142002-12-25 05:13:53 +0000461 BMI(MBB, IP, X86::FLD1, 0, R);
462 else {
Chris Lattner3e130a22003-01-13 00:32:26 +0000463 // Otherwise we need to spill the constant to memory...
464 MachineConstantPool *CP = F->getConstantPool();
465 unsigned CPI = CP->getConstantPoolIndex(CFP);
Chris Lattner6c09db22003-10-20 04:11:23 +0000466 const Type *Ty = CFP->getType();
467
468 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
469 unsigned LoadOpcode = Ty == Type::FloatTy ? X86::FLDr32 : X86::FLDr64;
470 addConstantPoolReference(BMI(MBB, IP, LoadOpcode, 4, R), CPI);
Chris Lattner94af4142002-12-25 05:13:53 +0000471 }
472
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000473 } else if (isa<ConstantPointerNull>(C)) {
Brian Gaeke20244b72002-12-12 15:33:40 +0000474 // Copy zero (null pointer) to the register.
Chris Lattner6e173a02004-02-17 06:16:44 +0000475 BMI(MBB, IP, X86::MOVri32, 1, R).addZImm(0);
Chris Lattnerc0812d82002-12-13 06:56:29 +0000476 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)) {
Chris Lattner7ca04092004-02-22 17:35:42 +0000477 BMI(MBB, IP, X86::MOVri32, 1, R).addGlobalAddress(CPR->getValue());
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000478 } else {
Brian Gaeke20244b72002-12-12 15:33:40 +0000479 std::cerr << "Offending constant: " << C << "\n";
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000480 assert(0 && "Type not handled yet!");
Chris Lattnerc5291f52002-10-27 21:16:59 +0000481 }
482}
483
Chris Lattner065faeb2002-12-28 20:24:02 +0000484/// LoadArgumentsToVirtualRegs - Load all of the arguments to this function from
485/// the stack into virtual registers.
486///
487void ISel::LoadArgumentsToVirtualRegs(Function &Fn) {
488 // Emit instructions to load the arguments... On entry to a function on the
489 // X86, the stack frame looks like this:
490 //
491 // [ESP] -- return address
Chris Lattner3e130a22003-01-13 00:32:26 +0000492 // [ESP + 4] -- first argument (leftmost lexically)
493 // [ESP + 8] -- second argument, if first argument is four bytes in size
Chris Lattner065faeb2002-12-28 20:24:02 +0000494 // ...
495 //
Chris Lattnerf158da22003-01-16 02:20:12 +0000496 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattneraa09b752002-12-28 21:08:28 +0000497 MachineFrameInfo *MFI = F->getFrameInfo();
Chris Lattner065faeb2002-12-28 20:24:02 +0000498
499 for (Function::aiterator I = Fn.abegin(), E = Fn.aend(); I != E; ++I) {
500 unsigned Reg = getReg(*I);
501
Chris Lattner065faeb2002-12-28 20:24:02 +0000502 int FI; // Frame object index
Chris Lattner065faeb2002-12-28 20:24:02 +0000503 switch (getClassB(I->getType())) {
504 case cByte:
Chris Lattneraa09b752002-12-28 21:08:28 +0000505 FI = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattnere87331d2004-02-17 06:28:19 +0000506 addFrameReference(BuildMI(BB, X86::MOVrm8, 4, Reg), FI);
Chris Lattner065faeb2002-12-28 20:24:02 +0000507 break;
508 case cShort:
Chris Lattneraa09b752002-12-28 21:08:28 +0000509 FI = MFI->CreateFixedObject(2, ArgOffset);
Chris Lattnere87331d2004-02-17 06:28:19 +0000510 addFrameReference(BuildMI(BB, X86::MOVrm16, 4, Reg), FI);
Chris Lattner065faeb2002-12-28 20:24:02 +0000511 break;
512 case cInt:
Chris Lattneraa09b752002-12-28 21:08:28 +0000513 FI = MFI->CreateFixedObject(4, ArgOffset);
Chris Lattnere87331d2004-02-17 06:28:19 +0000514 addFrameReference(BuildMI(BB, X86::MOVrm32, 4, Reg), FI);
Chris Lattner065faeb2002-12-28 20:24:02 +0000515 break;
Chris Lattner3e130a22003-01-13 00:32:26 +0000516 case cLong:
517 FI = MFI->CreateFixedObject(8, ArgOffset);
Chris Lattnere87331d2004-02-17 06:28:19 +0000518 addFrameReference(BuildMI(BB, X86::MOVrm32, 4, Reg), FI);
519 addFrameReference(BuildMI(BB, X86::MOVrm32, 4, Reg+1), FI, 4);
Chris Lattner3e130a22003-01-13 00:32:26 +0000520 ArgOffset += 4; // longs require 4 additional bytes
521 break;
Chris Lattner065faeb2002-12-28 20:24:02 +0000522 case cFP:
523 unsigned Opcode;
524 if (I->getType() == Type::FloatTy) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000525 Opcode = X86::FLDr32;
526 FI = MFI->CreateFixedObject(4, ArgOffset);
Chris Lattner065faeb2002-12-28 20:24:02 +0000527 } else {
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000528 Opcode = X86::FLDr64;
529 FI = MFI->CreateFixedObject(8, ArgOffset);
530 ArgOffset += 4; // doubles require 4 additional bytes
Chris Lattner065faeb2002-12-28 20:24:02 +0000531 }
532 addFrameReference(BuildMI(BB, Opcode, 4, Reg), FI);
533 break;
534 default:
535 assert(0 && "Unhandled argument type!");
536 }
Chris Lattner3e130a22003-01-13 00:32:26 +0000537 ArgOffset += 4; // Each argument takes at least 4 bytes on the stack...
Chris Lattner065faeb2002-12-28 20:24:02 +0000538 }
Chris Lattnereca195e2003-05-08 19:44:13 +0000539
540 // If the function takes variable number of arguments, add a frame offset for
541 // the start of the first vararg value... this is used to expand
542 // llvm.va_start.
543 if (Fn.getFunctionType()->isVarArg())
544 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner065faeb2002-12-28 20:24:02 +0000545}
546
547
Chris Lattner333b2fa2002-12-13 10:09:43 +0000548/// SelectPHINodes - Insert machine code to generate phis. This is tricky
549/// because we have to generate our sources into the source basic blocks, not
550/// the current one.
551///
552void ISel::SelectPHINodes() {
Chris Lattner3501fea2003-01-14 22:00:31 +0000553 const TargetInstrInfo &TII = TM.getInstrInfo();
Chris Lattner333b2fa2002-12-13 10:09:43 +0000554 const Function &LF = *F->getFunction(); // The LLVM function...
555 for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) {
556 const BasicBlock *BB = I;
557 MachineBasicBlock *MBB = MBBMap[I];
558
559 // Loop over all of the PHI nodes in the LLVM basic block...
Chris Lattner986618e2004-02-22 19:47:26 +0000560 MachineBasicBlock::iterator instr = MBB->begin();
Chris Lattner333b2fa2002-12-13 10:09:43 +0000561 for (BasicBlock::const_iterator I = BB->begin();
Chris Lattnera81fc682003-10-19 00:26:11 +0000562 PHINode *PN = const_cast<PHINode*>(dyn_cast<PHINode>(I)); ++I) {
Chris Lattner3e130a22003-01-13 00:32:26 +0000563
Chris Lattner333b2fa2002-12-13 10:09:43 +0000564 // Create a new machine instr PHI node, and insert it.
Chris Lattner3e130a22003-01-13 00:32:26 +0000565 unsigned PHIReg = getReg(*PN);
566 MachineInstr *PhiMI = BuildMI(X86::PHI, PN->getNumOperands(), PHIReg);
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000567 MBB->insert(instr, PhiMI);
Chris Lattner3e130a22003-01-13 00:32:26 +0000568
569 MachineInstr *LongPhiMI = 0;
570 if (PN->getType() == Type::LongTy || PN->getType() == Type::ULongTy) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000571 LongPhiMI = BuildMI(X86::PHI, PN->getNumOperands(), PHIReg+1);
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000572 MBB->insert(instr, LongPhiMI);
Chris Lattner3e130a22003-01-13 00:32:26 +0000573 }
Chris Lattner333b2fa2002-12-13 10:09:43 +0000574
Chris Lattnera6e73f12003-05-12 14:22:21 +0000575 // PHIValues - Map of blocks to incoming virtual registers. We use this
576 // so that we only initialize one incoming value for a particular block,
577 // even if the block has multiple entries in the PHI node.
578 //
579 std::map<MachineBasicBlock*, unsigned> PHIValues;
580
Chris Lattner333b2fa2002-12-13 10:09:43 +0000581 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
582 MachineBasicBlock *PredMBB = MBBMap[PN->getIncomingBlock(i)];
Chris Lattnera6e73f12003-05-12 14:22:21 +0000583 unsigned ValReg;
584 std::map<MachineBasicBlock*, unsigned>::iterator EntryIt =
585 PHIValues.lower_bound(PredMBB);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000586
Chris Lattnera6e73f12003-05-12 14:22:21 +0000587 if (EntryIt != PHIValues.end() && EntryIt->first == PredMBB) {
588 // We already inserted an initialization of the register for this
589 // predecessor. Recycle it.
590 ValReg = EntryIt->second;
591
592 } else {
Chris Lattnera81fc682003-10-19 00:26:11 +0000593 // Get the incoming value into a virtual register.
Chris Lattnera6e73f12003-05-12 14:22:21 +0000594 //
Chris Lattnera81fc682003-10-19 00:26:11 +0000595 Value *Val = PN->getIncomingValue(i);
596
597 // If this is a constant or GlobalValue, we may have to insert code
598 // into the basic block to compute it into a virtual register.
599 if (isa<Constant>(Val) || isa<GlobalValue>(Val)) {
600 // Because we don't want to clobber any values which might be in
601 // physical registers with the computation of this constant (which
602 // might be arbitrarily complex if it is a constant expression),
603 // just insert the computation at the top of the basic block.
604 MachineBasicBlock::iterator PI = PredMBB->begin();
605
606 // Skip over any PHI nodes though!
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000607 while (PI != PredMBB->end() && PI->getOpcode() == X86::PHI)
Chris Lattnera81fc682003-10-19 00:26:11 +0000608 ++PI;
609
610 ValReg = getReg(Val, PredMBB, PI);
611 } else {
612 ValReg = getReg(Val);
613 }
Chris Lattnera6e73f12003-05-12 14:22:21 +0000614
615 // Remember that we inserted a value for this PHI for this predecessor
616 PHIValues.insert(EntryIt, std::make_pair(PredMBB, ValReg));
617 }
618
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000619 PhiMI->addRegOperand(ValReg);
Chris Lattner3e130a22003-01-13 00:32:26 +0000620 PhiMI->addMachineBasicBlockOperand(PredMBB);
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000621 if (LongPhiMI) {
622 LongPhiMI->addRegOperand(ValReg+1);
623 LongPhiMI->addMachineBasicBlockOperand(PredMBB);
624 }
Chris Lattner333b2fa2002-12-13 10:09:43 +0000625 }
626 }
627 }
628}
629
Chris Lattner986618e2004-02-22 19:47:26 +0000630/// RequiresFPRegKill - The floating point stackifier pass cannot insert
631/// compensation code on critical edges. As such, it requires that we kill all
632/// FP registers on the exit from any blocks that either ARE critical edges, or
633/// branch to a block that has incoming critical edges.
634///
635/// Note that this kill instruction will eventually be eliminated when
636/// restrictions in the stackifier are relaxed.
637///
638static bool RequiresFPRegKill(const BasicBlock *BB) {
639#if 0
640 for (succ_const_iterator SI = succ_begin(BB), E = succ_end(BB); SI!=E; ++SI) {
641 const BasicBlock *Succ = *SI;
642 pred_const_iterator PI = pred_begin(Succ), PE = pred_end(Succ);
643 ++PI; // Block have at least one predecessory
644 if (PI != PE) { // If it has exactly one, this isn't crit edge
645 // If this block has more than one predecessor, check all of the
646 // predecessors to see if they have multiple successors. If so, then the
647 // block we are analyzing needs an FPRegKill.
648 for (PI = pred_begin(Succ); PI != PE; ++PI) {
649 const BasicBlock *Pred = *PI;
650 succ_const_iterator SI2 = succ_begin(Pred);
651 ++SI2; // There must be at least one successor of this block.
652 if (SI2 != succ_end(Pred))
653 return true; // Yes, we must insert the kill on this edge.
654 }
655 }
656 }
657 // If we got this far, there is no need to insert the kill instruction.
658 return false;
659#else
660 return true;
661#endif
662}
663
664// InsertFPRegKills - Insert FP_REG_KILL instructions into basic blocks that
665// need them. This only occurs due to the floating point stackifier not being
666// aggressive enough to handle arbitrary global stackification.
667//
668// Currently we insert an FP_REG_KILL instruction into each block that uses or
669// defines a floating point virtual register.
670//
671// When the global register allocators (like linear scan) finally update live
672// variable analysis, we can keep floating point values in registers across
673// portions of the CFG that do not involve critical edges. This will be a big
674// win, but we are waiting on the global allocators before we can do this.
675//
676// With a bit of work, the floating point stackifier pass can be enhanced to
677// break critical edges as needed (to make a place to put compensation code),
678// but this will require some infrastructure improvements as well.
679//
680void ISel::InsertFPRegKills() {
681 SSARegMap &RegMap = *F->getSSARegMap();
682 const TargetInstrInfo &TII = TM.getInstrInfo();
683
684 for (MachineFunction::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
685 bool UsesFPReg = false;
686 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
687 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
688 if (I->getOperand(i).isRegister()) {
689 unsigned Reg = I->getOperand(i).getReg();
690 if (MRegisterInfo::isVirtualRegister(Reg))
691 if (RegMap.getRegClass(Reg)->getSize() == 10) {
692 UsesFPReg = true;
693 break;
694 }
695 }
696 if (UsesFPReg) {
697 // Okay, this block uses an FP register. If the block has successors (ie,
698 // it's not an unwind/return), insert the FP_REG_KILL instruction.
699 if (BB->getBasicBlock()->getTerminator()->getNumSuccessors() &&
700 RequiresFPRegKill(BB->getBasicBlock())) {
701 // Rewind past any terminator instructions that might exist.
702 MachineBasicBlock::iterator I = BB->end();
703 while (I != BB->begin() && TII.isTerminatorInstr((--I)->getOpcode()));
704 ++I;
705 BMI(BB, I, X86::FP_REG_KILL, 0);
706 ++NumFPKill;
707 }
708 }
709 }
710}
711
712
Chris Lattner6d40c192003-01-16 16:43:00 +0000713// canFoldSetCCIntoBranch - Return the setcc instruction if we can fold it into
714// the conditional branch instruction which is the only user of the cc
715// instruction. This is the case if the conditional branch is the only user of
716// the setcc, and if the setcc is in the same basic block as the conditional
717// branch. We also don't handle long arguments below, so we reject them here as
718// well.
719//
720static SetCondInst *canFoldSetCCIntoBranch(Value *V) {
721 if (SetCondInst *SCI = dyn_cast<SetCondInst>(V))
Chris Lattnerfd059242003-10-15 16:48:29 +0000722 if (SCI->hasOneUse() && isa<BranchInst>(SCI->use_back()) &&
Chris Lattner6d40c192003-01-16 16:43:00 +0000723 SCI->getParent() == cast<BranchInst>(SCI->use_back())->getParent()) {
724 const Type *Ty = SCI->getOperand(0)->getType();
725 if (Ty != Type::LongTy && Ty != Type::ULongTy)
726 return SCI;
727 }
728 return 0;
729}
Chris Lattner333b2fa2002-12-13 10:09:43 +0000730
Chris Lattner6d40c192003-01-16 16:43:00 +0000731// Return a fixed numbering for setcc instructions which does not depend on the
732// order of the opcodes.
733//
734static unsigned getSetCCNumber(unsigned Opcode) {
735 switch(Opcode) {
736 default: assert(0 && "Unknown setcc instruction!");
737 case Instruction::SetEQ: return 0;
738 case Instruction::SetNE: return 1;
739 case Instruction::SetLT: return 2;
Chris Lattner55f6fab2003-01-16 18:07:23 +0000740 case Instruction::SetGE: return 3;
741 case Instruction::SetGT: return 4;
742 case Instruction::SetLE: return 5;
Chris Lattner6d40c192003-01-16 16:43:00 +0000743 }
744}
Chris Lattner06925362002-11-17 21:56:38 +0000745
Chris Lattner6d40c192003-01-16 16:43:00 +0000746// LLVM -> X86 signed X86 unsigned
747// ----- ---------- ------------
748// seteq -> sete sete
749// setne -> setne setne
750// setlt -> setl setb
Chris Lattner55f6fab2003-01-16 18:07:23 +0000751// setge -> setge setae
Chris Lattner6d40c192003-01-16 16:43:00 +0000752// setgt -> setg seta
753// setle -> setle setbe
Chris Lattnerb2acc512003-10-19 21:09:10 +0000754// ----
755// sets // Used by comparison with 0 optimization
756// setns
757static const unsigned SetCCOpcodeTab[2][8] = {
758 { X86::SETEr, X86::SETNEr, X86::SETBr, X86::SETAEr, X86::SETAr, X86::SETBEr,
759 0, 0 },
760 { X86::SETEr, X86::SETNEr, X86::SETLr, X86::SETGEr, X86::SETGr, X86::SETLEr,
761 X86::SETSr, X86::SETNSr },
Chris Lattner6d40c192003-01-16 16:43:00 +0000762};
763
Chris Lattnerb2acc512003-10-19 21:09:10 +0000764// EmitComparison - This function emits a comparison of the two operands,
765// returning the extended setcc code to use.
766unsigned ISel::EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
767 MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000768 MachineBasicBlock::iterator IP) {
Brian Gaeke1749d632002-11-07 17:59:21 +0000769 // The arguments are already supposed to be of the same type.
Chris Lattner6d40c192003-01-16 16:43:00 +0000770 const Type *CompTy = Op0->getType();
Chris Lattner3e130a22003-01-13 00:32:26 +0000771 unsigned Class = getClassB(CompTy);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000772 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattner333864d2003-06-05 19:30:30 +0000773
774 // Special case handling of: cmp R, i
775 if (Class == cByte || Class == cShort || Class == cInt)
776 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000777 uint64_t Op1v = cast<ConstantInt>(CI)->getRawValue();
778
Chris Lattner333864d2003-06-05 19:30:30 +0000779 // Mask off any upper bits of the constant, if there are any...
780 Op1v &= (1ULL << (8 << Class)) - 1;
781
Chris Lattnerb2acc512003-10-19 21:09:10 +0000782 // If this is a comparison against zero, emit more efficient code. We
783 // can't handle unsigned comparisons against zero unless they are == or
784 // !=. These should have been strength reduced already anyway.
785 if (Op1v == 0 && (CompTy->isSigned() || OpNum < 2)) {
786 static const unsigned TESTTab[] = {
787 X86::TESTrr8, X86::TESTrr16, X86::TESTrr32
788 };
789 BMI(MBB, IP, TESTTab[Class], 2).addReg(Op0r).addReg(Op0r);
790
791 if (OpNum == 2) return 6; // Map jl -> js
792 if (OpNum == 3) return 7; // Map jg -> jns
793 return OpNum;
Chris Lattner333864d2003-06-05 19:30:30 +0000794 }
Chris Lattnerb2acc512003-10-19 21:09:10 +0000795
796 static const unsigned CMPTab[] = {
797 X86::CMPri8, X86::CMPri16, X86::CMPri32
798 };
799
800 BMI(MBB, IP, CMPTab[Class], 2).addReg(Op0r).addZImm(Op1v);
801 return OpNum;
Chris Lattner333864d2003-06-05 19:30:30 +0000802 }
803
Chris Lattner9f08a922004-02-03 18:54:04 +0000804 // Special case handling of comparison against +/- 0.0
805 if (ConstantFP *CFP = dyn_cast<ConstantFP>(Op1))
806 if (CFP->isExactlyValue(+0.0) || CFP->isExactlyValue(-0.0)) {
807 BMI(MBB, IP, X86::FTST, 1).addReg(Op0r);
808 BMI(MBB, IP, X86::FNSTSWr8, 0);
809 BMI(MBB, IP, X86::SAHF, 1);
810 return OpNum;
811 }
812
Chris Lattner58c41fe2003-08-24 19:19:47 +0000813 unsigned Op1r = getReg(Op1, MBB, IP);
Chris Lattner3e130a22003-01-13 00:32:26 +0000814 switch (Class) {
815 default: assert(0 && "Unknown type class!");
816 // Emit: cmp <var1>, <var2> (do the comparison). We can
817 // compare 8-bit with 8-bit, 16-bit with 16-bit, 32-bit with
818 // 32-bit.
819 case cByte:
Chris Lattner58c41fe2003-08-24 19:19:47 +0000820 BMI(MBB, IP, X86::CMPrr8, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +0000821 break;
822 case cShort:
Chris Lattner58c41fe2003-08-24 19:19:47 +0000823 BMI(MBB, IP, X86::CMPrr16, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +0000824 break;
825 case cInt:
Chris Lattner58c41fe2003-08-24 19:19:47 +0000826 BMI(MBB, IP, X86::CMPrr32, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +0000827 break;
828 case cFP:
Chris Lattner58c41fe2003-08-24 19:19:47 +0000829 BMI(MBB, IP, X86::FpUCOM, 2).addReg(Op0r).addReg(Op1r);
830 BMI(MBB, IP, X86::FNSTSWr8, 0);
831 BMI(MBB, IP, X86::SAHF, 1);
Chris Lattner3e130a22003-01-13 00:32:26 +0000832 break;
833
834 case cLong:
835 if (OpNum < 2) { // seteq, setne
836 unsigned LoTmp = makeAnotherReg(Type::IntTy);
837 unsigned HiTmp = makeAnotherReg(Type::IntTy);
838 unsigned FinalTmp = makeAnotherReg(Type::IntTy);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000839 BMI(MBB, IP, X86::XORrr32, 2, LoTmp).addReg(Op0r).addReg(Op1r);
840 BMI(MBB, IP, X86::XORrr32, 2, HiTmp).addReg(Op0r+1).addReg(Op1r+1);
841 BMI(MBB, IP, X86::ORrr32, 2, FinalTmp).addReg(LoTmp).addReg(HiTmp);
Chris Lattner3e130a22003-01-13 00:32:26 +0000842 break; // Allow the sete or setne to be generated from flags set by OR
843 } else {
844 // Emit a sequence of code which compares the high and low parts once
845 // each, then uses a conditional move to handle the overflow case. For
846 // example, a setlt for long would generate code like this:
847 //
848 // AL = lo(op1) < lo(op2) // Signedness depends on operands
849 // BL = hi(op1) < hi(op2) // Always unsigned comparison
850 // dest = hi(op1) == hi(op2) ? AL : BL;
851 //
852
Chris Lattner6d40c192003-01-16 16:43:00 +0000853 // FIXME: This would be much better if we had hierarchical register
Chris Lattner3e130a22003-01-13 00:32:26 +0000854 // classes! Until then, hardcode registers so that we can deal with their
855 // aliases (because we don't have conditional byte moves).
856 //
Chris Lattner58c41fe2003-08-24 19:19:47 +0000857 BMI(MBB, IP, X86::CMPrr32, 2).addReg(Op0r).addReg(Op1r);
858 BMI(MBB, IP, SetCCOpcodeTab[0][OpNum], 0, X86::AL);
859 BMI(MBB, IP, X86::CMPrr32, 2).addReg(Op0r+1).addReg(Op1r+1);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000860 BMI(MBB, IP, SetCCOpcodeTab[CompTy->isSigned()][OpNum], 0, X86::BL);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000861 BMI(MBB, IP, X86::IMPLICIT_DEF, 0, X86::BH);
862 BMI(MBB, IP, X86::IMPLICIT_DEF, 0, X86::AH);
863 BMI(MBB, IP, X86::CMOVErr16, 2, X86::BX).addReg(X86::BX).addReg(X86::AX);
Chris Lattner6d40c192003-01-16 16:43:00 +0000864 // NOTE: visitSetCondInst knows that the value is dumped into the BL
865 // register at this point for long values...
Chris Lattnerb2acc512003-10-19 21:09:10 +0000866 return OpNum;
Chris Lattner3e130a22003-01-13 00:32:26 +0000867 }
868 }
Chris Lattnerb2acc512003-10-19 21:09:10 +0000869 return OpNum;
Chris Lattner6d40c192003-01-16 16:43:00 +0000870}
Chris Lattner3e130a22003-01-13 00:32:26 +0000871
Chris Lattner6d40c192003-01-16 16:43:00 +0000872
873/// SetCC instructions - Here we just emit boilerplate code to set a byte-sized
874/// register, then move it to wherever the result should be.
875///
876void ISel::visitSetCondInst(SetCondInst &I) {
877 if (canFoldSetCCIntoBranch(&I)) return; // Fold this into a branch...
878
Chris Lattner6d40c192003-01-16 16:43:00 +0000879 unsigned DestReg = getReg(I);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000880 MachineBasicBlock::iterator MII = BB->end();
881 emitSetCCOperation(BB, MII, I.getOperand(0), I.getOperand(1), I.getOpcode(),
882 DestReg);
883}
Chris Lattner6d40c192003-01-16 16:43:00 +0000884
Chris Lattner58c41fe2003-08-24 19:19:47 +0000885/// emitSetCCOperation - Common code shared between visitSetCondInst and
886/// constant expression support.
887void ISel::emitSetCCOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000888 MachineBasicBlock::iterator IP,
Chris Lattner58c41fe2003-08-24 19:19:47 +0000889 Value *Op0, Value *Op1, unsigned Opcode,
890 unsigned TargetReg) {
891 unsigned OpNum = getSetCCNumber(Opcode);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000892 OpNum = EmitComparison(OpNum, Op0, Op1, MBB, IP);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000893
Chris Lattnerb2acc512003-10-19 21:09:10 +0000894 const Type *CompTy = Op0->getType();
895 unsigned CompClass = getClassB(CompTy);
896 bool isSigned = CompTy->isSigned() && CompClass != cFP;
897
898 if (CompClass != cLong || OpNum < 2) {
Chris Lattner6d40c192003-01-16 16:43:00 +0000899 // Handle normal comparisons with a setcc instruction...
Chris Lattner58c41fe2003-08-24 19:19:47 +0000900 BMI(MBB, IP, SetCCOpcodeTab[isSigned][OpNum], 0, TargetReg);
Chris Lattner6d40c192003-01-16 16:43:00 +0000901 } else {
902 // Handle long comparisons by copying the value which is already in BL into
903 // the register we want...
Chris Lattner58c41fe2003-08-24 19:19:47 +0000904 BMI(MBB, IP, X86::MOVrr8, 1, TargetReg).addReg(X86::BL);
Chris Lattner6d40c192003-01-16 16:43:00 +0000905 }
Brian Gaeke1749d632002-11-07 17:59:21 +0000906}
Chris Lattner51b49a92002-11-02 19:45:49 +0000907
Chris Lattner58c41fe2003-08-24 19:19:47 +0000908
909
910
Brian Gaekec2505982002-11-30 11:57:28 +0000911/// promote32 - Emit instructions to turn a narrow operand into a 32-bit-wide
912/// operand, in the specified target register.
Chris Lattner3e130a22003-01-13 00:32:26 +0000913void ISel::promote32(unsigned targetReg, const ValueRecord &VR) {
914 bool isUnsigned = VR.Ty->isUnsigned();
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000915
916 // Make sure we have the register number for this value...
917 unsigned Reg = VR.Val ? getReg(VR.Val) : VR.Reg;
918
Chris Lattner3e130a22003-01-13 00:32:26 +0000919 switch (getClassB(VR.Ty)) {
Chris Lattner94af4142002-12-25 05:13:53 +0000920 case cByte:
921 // Extend value into target register (8->32)
922 if (isUnsigned)
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000923 BuildMI(BB, X86::MOVZXr32r8, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +0000924 else
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000925 BuildMI(BB, X86::MOVSXr32r8, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +0000926 break;
927 case cShort:
928 // Extend value into target register (16->32)
929 if (isUnsigned)
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000930 BuildMI(BB, X86::MOVZXr32r16, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +0000931 else
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000932 BuildMI(BB, X86::MOVSXr32r16, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +0000933 break;
934 case cInt:
935 // Move value into target register (32->32)
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000936 BuildMI(BB, X86::MOVrr32, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +0000937 break;
938 default:
939 assert(0 && "Unpromotable operand class in promote32");
940 }
Brian Gaekec2505982002-11-30 11:57:28 +0000941}
Chris Lattnerc5291f52002-10-27 21:16:59 +0000942
Chris Lattner72614082002-10-25 22:55:53 +0000943/// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such,
944/// we have the following possibilities:
945///
946/// ret void: No return value, simply emit a 'ret' instruction
947/// ret sbyte, ubyte : Extend value into EAX and return
948/// ret short, ushort: Extend value into EAX and return
949/// ret int, uint : Move value into EAX and return
950/// ret pointer : Move value into EAX and return
Chris Lattner06925362002-11-17 21:56:38 +0000951/// ret long, ulong : Move value into EAX/EDX and return
952/// ret float/double : Top of FP stack
Chris Lattner72614082002-10-25 22:55:53 +0000953///
Chris Lattner3e130a22003-01-13 00:32:26 +0000954void ISel::visitReturnInst(ReturnInst &I) {
Chris Lattner94af4142002-12-25 05:13:53 +0000955 if (I.getNumOperands() == 0) {
956 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
957 return;
958 }
959
960 Value *RetVal = I.getOperand(0);
Chris Lattner3e130a22003-01-13 00:32:26 +0000961 unsigned RetReg = getReg(RetVal);
962 switch (getClassB(RetVal->getType())) {
Chris Lattner94af4142002-12-25 05:13:53 +0000963 case cByte: // integral return values: extend or move into EAX and return
964 case cShort:
965 case cInt:
Chris Lattner3e130a22003-01-13 00:32:26 +0000966 promote32(X86::EAX, ValueRecord(RetReg, RetVal->getType()));
Chris Lattnerdbd73722003-05-06 21:32:22 +0000967 // Declare that EAX is live on exit
Chris Lattnerc2489032003-05-07 19:21:28 +0000968 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
Chris Lattner94af4142002-12-25 05:13:53 +0000969 break;
970 case cFP: // Floats & Doubles: Return in ST(0)
Chris Lattner3e130a22003-01-13 00:32:26 +0000971 BuildMI(BB, X86::FpSETRESULT, 1).addReg(RetReg);
Chris Lattnerdbd73722003-05-06 21:32:22 +0000972 // Declare that top-of-stack is live on exit
Chris Lattnerc2489032003-05-07 19:21:28 +0000973 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
Chris Lattner94af4142002-12-25 05:13:53 +0000974 break;
975 case cLong:
Chris Lattner3e130a22003-01-13 00:32:26 +0000976 BuildMI(BB, X86::MOVrr32, 1, X86::EAX).addReg(RetReg);
977 BuildMI(BB, X86::MOVrr32, 1, X86::EDX).addReg(RetReg+1);
Chris Lattnerdbd73722003-05-06 21:32:22 +0000978 // Declare that EAX & EDX are live on exit
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000979 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
980 .addReg(X86::ESP);
Chris Lattner3e130a22003-01-13 00:32:26 +0000981 break;
Chris Lattner94af4142002-12-25 05:13:53 +0000982 default:
Chris Lattner3e130a22003-01-13 00:32:26 +0000983 visitInstruction(I);
Chris Lattner94af4142002-12-25 05:13:53 +0000984 }
Chris Lattner43189d12002-11-17 20:07:45 +0000985 // Emit a 'ret' instruction
Chris Lattner94af4142002-12-25 05:13:53 +0000986 BuildMI(BB, X86::RET, 0);
Chris Lattner72614082002-10-25 22:55:53 +0000987}
988
Chris Lattner55f6fab2003-01-16 18:07:23 +0000989// getBlockAfter - Return the basic block which occurs lexically after the
990// specified one.
991static inline BasicBlock *getBlockAfter(BasicBlock *BB) {
992 Function::iterator I = BB; ++I; // Get iterator to next block
993 return I != BB->getParent()->end() ? &*I : 0;
994}
995
Chris Lattner51b49a92002-11-02 19:45:49 +0000996/// visitBranchInst - Handle conditional and unconditional branches here. Note
997/// that since code layout is frozen at this point, that if we are trying to
998/// jump to a block that is the immediate successor of the current block, we can
Chris Lattner6d40c192003-01-16 16:43:00 +0000999/// just make a fall-through (but we don't currently).
Chris Lattner51b49a92002-11-02 19:45:49 +00001000///
Chris Lattner94af4142002-12-25 05:13:53 +00001001void ISel::visitBranchInst(BranchInst &BI) {
Chris Lattner55f6fab2003-01-16 18:07:23 +00001002 BasicBlock *NextBB = getBlockAfter(BI.getParent()); // BB after current one
1003
1004 if (!BI.isConditional()) { // Unconditional branch?
Chris Lattnercf93cdd2004-01-30 22:13:44 +00001005 if (BI.getSuccessor(0) != NextBB)
Chris Lattner55f6fab2003-01-16 18:07:23 +00001006 BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(0));
Chris Lattner6d40c192003-01-16 16:43:00 +00001007 return;
1008 }
1009
1010 // See if we can fold the setcc into the branch itself...
1011 SetCondInst *SCI = canFoldSetCCIntoBranch(BI.getCondition());
1012 if (SCI == 0) {
1013 // Nope, cannot fold setcc into this branch. Emit a branch on a condition
1014 // computed some other way...
Chris Lattner065faeb2002-12-28 20:24:02 +00001015 unsigned condReg = getReg(BI.getCondition());
Chris Lattner94af4142002-12-25 05:13:53 +00001016 BuildMI(BB, X86::CMPri8, 2).addReg(condReg).addZImm(0);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001017 if (BI.getSuccessor(1) == NextBB) {
1018 if (BI.getSuccessor(0) != NextBB)
1019 BuildMI(BB, X86::JNE, 1).addPCDisp(BI.getSuccessor(0));
1020 } else {
1021 BuildMI(BB, X86::JE, 1).addPCDisp(BI.getSuccessor(1));
1022
1023 if (BI.getSuccessor(0) != NextBB)
1024 BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(0));
1025 }
Chris Lattner6d40c192003-01-16 16:43:00 +00001026 return;
Chris Lattner94af4142002-12-25 05:13:53 +00001027 }
Chris Lattner6d40c192003-01-16 16:43:00 +00001028
1029 unsigned OpNum = getSetCCNumber(SCI->getOpcode());
Chris Lattner58c41fe2003-08-24 19:19:47 +00001030 MachineBasicBlock::iterator MII = BB->end();
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001031 OpNum = EmitComparison(OpNum, SCI->getOperand(0), SCI->getOperand(1), BB,MII);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001032
1033 const Type *CompTy = SCI->getOperand(0)->getType();
1034 bool isSigned = CompTy->isSigned() && getClassB(CompTy) != cFP;
Chris Lattner6d40c192003-01-16 16:43:00 +00001035
Chris Lattnerb2acc512003-10-19 21:09:10 +00001036
Chris Lattner6d40c192003-01-16 16:43:00 +00001037 // LLVM -> X86 signed X86 unsigned
1038 // ----- ---------- ------------
1039 // seteq -> je je
1040 // setne -> jne jne
1041 // setlt -> jl jb
Chris Lattner55f6fab2003-01-16 18:07:23 +00001042 // setge -> jge jae
Chris Lattner6d40c192003-01-16 16:43:00 +00001043 // setgt -> jg ja
1044 // setle -> jle jbe
Chris Lattnerb2acc512003-10-19 21:09:10 +00001045 // ----
1046 // js // Used by comparison with 0 optimization
1047 // jns
1048
1049 static const unsigned OpcodeTab[2][8] = {
1050 { X86::JE, X86::JNE, X86::JB, X86::JAE, X86::JA, X86::JBE, 0, 0 },
1051 { X86::JE, X86::JNE, X86::JL, X86::JGE, X86::JG, X86::JLE,
1052 X86::JS, X86::JNS },
Chris Lattner6d40c192003-01-16 16:43:00 +00001053 };
1054
Chris Lattner55f6fab2003-01-16 18:07:23 +00001055 if (BI.getSuccessor(0) != NextBB) {
1056 BuildMI(BB, OpcodeTab[isSigned][OpNum], 1).addPCDisp(BI.getSuccessor(0));
1057 if (BI.getSuccessor(1) != NextBB)
1058 BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(1));
1059 } else {
1060 // Change to the inverse condition...
1061 if (BI.getSuccessor(1) != NextBB) {
1062 OpNum ^= 1;
1063 BuildMI(BB, OpcodeTab[isSigned][OpNum], 1).addPCDisp(BI.getSuccessor(1));
1064 }
1065 }
Chris Lattner2df035b2002-11-02 19:27:56 +00001066}
1067
Chris Lattner3e130a22003-01-13 00:32:26 +00001068
1069/// doCall - This emits an abstract call instruction, setting up the arguments
1070/// and the return value as appropriate. For the actual function call itself,
1071/// it inserts the specified CallMI instruction into the stream.
1072///
1073void ISel::doCall(const ValueRecord &Ret, MachineInstr *CallMI,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001074 const std::vector<ValueRecord> &Args) {
Chris Lattner3e130a22003-01-13 00:32:26 +00001075
Chris Lattner065faeb2002-12-28 20:24:02 +00001076 // Count how many bytes are to be pushed on the stack...
1077 unsigned NumBytes = 0;
Misha Brukman0d2cf3a2002-12-04 19:22:53 +00001078
Chris Lattner3e130a22003-01-13 00:32:26 +00001079 if (!Args.empty()) {
1080 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1081 switch (getClassB(Args[i].Ty)) {
Chris Lattner065faeb2002-12-28 20:24:02 +00001082 case cByte: case cShort: case cInt:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001083 NumBytes += 4; break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001084 case cLong:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001085 NumBytes += 8; break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001086 case cFP:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001087 NumBytes += Args[i].Ty == Type::FloatTy ? 4 : 8;
1088 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001089 default: assert(0 && "Unknown class!");
1090 }
1091
1092 // Adjust the stack pointer for the new arguments...
1093 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addZImm(NumBytes);
1094
1095 // Arguments go on the stack in reverse order, as specified by the ABI.
1096 unsigned ArgOffset = 0;
Chris Lattner3e130a22003-01-13 00:32:26 +00001097 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001098 unsigned ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
Chris Lattner3e130a22003-01-13 00:32:26 +00001099 switch (getClassB(Args[i].Ty)) {
Chris Lattner065faeb2002-12-28 20:24:02 +00001100 case cByte:
1101 case cShort: {
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001102 // Promote arg to 32 bits wide into a temporary register...
1103 unsigned R = makeAnotherReg(Type::UIntTy);
1104 promote32(R, Args[i]);
Chris Lattnere87331d2004-02-17 06:28:19 +00001105 addRegOffset(BuildMI(BB, X86::MOVmr32, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001106 X86::ESP, ArgOffset).addReg(R);
1107 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001108 }
1109 case cInt:
Chris Lattnere87331d2004-02-17 06:28:19 +00001110 addRegOffset(BuildMI(BB, X86::MOVmr32, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001111 X86::ESP, ArgOffset).addReg(ArgReg);
1112 break;
Chris Lattner3e130a22003-01-13 00:32:26 +00001113 case cLong:
Chris Lattnere87331d2004-02-17 06:28:19 +00001114 addRegOffset(BuildMI(BB, X86::MOVmr32, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001115 X86::ESP, ArgOffset).addReg(ArgReg);
Chris Lattnere87331d2004-02-17 06:28:19 +00001116 addRegOffset(BuildMI(BB, X86::MOVmr32, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001117 X86::ESP, ArgOffset+4).addReg(ArgReg+1);
1118 ArgOffset += 4; // 8 byte entry, not 4.
1119 break;
1120
Chris Lattner065faeb2002-12-28 20:24:02 +00001121 case cFP:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001122 if (Args[i].Ty == Type::FloatTy) {
1123 addRegOffset(BuildMI(BB, X86::FSTr32, 5),
1124 X86::ESP, ArgOffset).addReg(ArgReg);
1125 } else {
1126 assert(Args[i].Ty == Type::DoubleTy && "Unknown FP type!");
1127 addRegOffset(BuildMI(BB, X86::FSTr64, 5),
1128 X86::ESP, ArgOffset).addReg(ArgReg);
1129 ArgOffset += 4; // 8 byte entry, not 4.
1130 }
1131 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001132
Chris Lattner3e130a22003-01-13 00:32:26 +00001133 default: assert(0 && "Unknown class!");
Chris Lattner065faeb2002-12-28 20:24:02 +00001134 }
1135 ArgOffset += 4;
Chris Lattner94af4142002-12-25 05:13:53 +00001136 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001137 } else {
1138 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addZImm(0);
Chris Lattner94af4142002-12-25 05:13:53 +00001139 }
Chris Lattner6e49a4b2002-12-13 14:13:27 +00001140
Chris Lattner3e130a22003-01-13 00:32:26 +00001141 BB->push_back(CallMI);
Misha Brukman0d2cf3a2002-12-04 19:22:53 +00001142
Chris Lattner065faeb2002-12-28 20:24:02 +00001143 BuildMI(BB, X86::ADJCALLSTACKUP, 1).addZImm(NumBytes);
Chris Lattnera3243642002-12-04 23:45:28 +00001144
1145 // If there is a return value, scavenge the result from the location the call
1146 // leaves it in...
1147 //
Chris Lattner3e130a22003-01-13 00:32:26 +00001148 if (Ret.Ty != Type::VoidTy) {
1149 unsigned DestClass = getClassB(Ret.Ty);
1150 switch (DestClass) {
Brian Gaeke20244b72002-12-12 15:33:40 +00001151 case cByte:
1152 case cShort:
1153 case cInt: {
1154 // Integral results are in %eax, or the appropriate portion
1155 // thereof.
1156 static const unsigned regRegMove[] = {
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001157 X86::MOVrr8, X86::MOVrr16, X86::MOVrr32
Brian Gaeke20244b72002-12-12 15:33:40 +00001158 };
1159 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX };
Chris Lattner3e130a22003-01-13 00:32:26 +00001160 BuildMI(BB, regRegMove[DestClass], 1, Ret.Reg).addReg(AReg[DestClass]);
Chris Lattner4fa1acc2002-12-04 23:50:28 +00001161 break;
Brian Gaeke20244b72002-12-12 15:33:40 +00001162 }
Chris Lattner94af4142002-12-25 05:13:53 +00001163 case cFP: // Floating-point return values live in %ST(0)
Chris Lattner3e130a22003-01-13 00:32:26 +00001164 BuildMI(BB, X86::FpGETRESULT, 1, Ret.Reg);
Brian Gaeke20244b72002-12-12 15:33:40 +00001165 break;
Chris Lattner3e130a22003-01-13 00:32:26 +00001166 case cLong: // Long values are left in EDX:EAX
1167 BuildMI(BB, X86::MOVrr32, 1, Ret.Reg).addReg(X86::EAX);
1168 BuildMI(BB, X86::MOVrr32, 1, Ret.Reg+1).addReg(X86::EDX);
1169 break;
1170 default: assert(0 && "Unknown class!");
Chris Lattner4fa1acc2002-12-04 23:50:28 +00001171 }
Chris Lattnera3243642002-12-04 23:45:28 +00001172 }
Brian Gaekefa8d5712002-11-22 11:07:01 +00001173}
Chris Lattner2df035b2002-11-02 19:27:56 +00001174
Chris Lattner3e130a22003-01-13 00:32:26 +00001175
1176/// visitCallInst - Push args on stack and do a procedure call instruction.
1177void ISel::visitCallInst(CallInst &CI) {
1178 MachineInstr *TheCall;
1179 if (Function *F = CI.getCalledFunction()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001180 // Is it an intrinsic function call?
Brian Gaeked0fde302003-11-11 22:41:34 +00001181 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001182 visitIntrinsicCall(ID, CI); // Special intrinsics are not handled here
1183 return;
1184 }
1185
Chris Lattner3e130a22003-01-13 00:32:26 +00001186 // Emit a CALL instruction with PC-relative displacement.
1187 TheCall = BuildMI(X86::CALLpcrel32, 1).addGlobalAddress(F, true);
1188 } else { // Emit an indirect call...
1189 unsigned Reg = getReg(CI.getCalledValue());
1190 TheCall = BuildMI(X86::CALLr32, 1).addReg(Reg);
1191 }
1192
1193 std::vector<ValueRecord> Args;
1194 for (unsigned i = 1, e = CI.getNumOperands(); i != e; ++i)
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001195 Args.push_back(ValueRecord(CI.getOperand(i)));
Chris Lattner3e130a22003-01-13 00:32:26 +00001196
1197 unsigned DestReg = CI.getType() != Type::VoidTy ? getReg(CI) : 0;
1198 doCall(ValueRecord(DestReg, CI.getType()), TheCall, Args);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001199}
Chris Lattner3e130a22003-01-13 00:32:26 +00001200
Chris Lattneraeb54b82003-08-28 21:23:43 +00001201
Chris Lattner44827152003-12-28 09:47:19 +00001202/// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
1203/// function, lowering any calls to unknown intrinsic functions into the
1204/// equivalent LLVM code.
1205void ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) {
1206 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1207 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
1208 if (CallInst *CI = dyn_cast<CallInst>(I++))
1209 if (Function *F = CI->getCalledFunction())
1210 switch (F->getIntrinsicID()) {
Chris Lattneraed386e2003-12-28 09:53:23 +00001211 case Intrinsic::not_intrinsic:
Chris Lattner44827152003-12-28 09:47:19 +00001212 case Intrinsic::va_start:
1213 case Intrinsic::va_copy:
1214 case Intrinsic::va_end:
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001215 case Intrinsic::returnaddress:
1216 case Intrinsic::frameaddress:
Chris Lattner915e5e52004-02-12 17:53:22 +00001217 case Intrinsic::memcpy:
Chris Lattner2a0f2242004-02-14 04:46:05 +00001218 case Intrinsic::memset:
Chris Lattner44827152003-12-28 09:47:19 +00001219 // We directly implement these intrinsics
1220 break;
1221 default:
1222 // All other intrinsic calls we must lower.
1223 Instruction *Before = CI->getPrev();
Chris Lattnerf70e0c22003-12-28 21:23:38 +00001224 TM.getIntrinsicLowering().LowerIntrinsicCall(CI);
Chris Lattner44827152003-12-28 09:47:19 +00001225 if (Before) { // Move iterator to instruction after call
1226 I = Before; ++I;
1227 } else {
1228 I = BB->begin();
1229 }
1230 }
1231
1232}
1233
Brian Gaeked0fde302003-11-11 22:41:34 +00001234void ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001235 unsigned TmpReg1, TmpReg2;
1236 switch (ID) {
Brian Gaeked0fde302003-11-11 22:41:34 +00001237 case Intrinsic::va_start:
Chris Lattnereca195e2003-05-08 19:44:13 +00001238 // Get the address of the first vararg value...
Chris Lattner73815062003-10-18 05:56:40 +00001239 TmpReg1 = getReg(CI);
Chris Lattnereca195e2003-05-08 19:44:13 +00001240 addFrameReference(BuildMI(BB, X86::LEAr32, 5, TmpReg1), VarArgsFrameIndex);
Chris Lattnereca195e2003-05-08 19:44:13 +00001241 return;
1242
Brian Gaeked0fde302003-11-11 22:41:34 +00001243 case Intrinsic::va_copy:
Chris Lattner73815062003-10-18 05:56:40 +00001244 TmpReg1 = getReg(CI);
1245 TmpReg2 = getReg(CI.getOperand(1));
1246 BuildMI(BB, X86::MOVrr32, 1, TmpReg1).addReg(TmpReg2);
Chris Lattnereca195e2003-05-08 19:44:13 +00001247 return;
Brian Gaeked0fde302003-11-11 22:41:34 +00001248 case Intrinsic::va_end: return; // Noop on X86
Chris Lattnereca195e2003-05-08 19:44:13 +00001249
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001250 case Intrinsic::returnaddress:
1251 case Intrinsic::frameaddress:
1252 TmpReg1 = getReg(CI);
1253 if (cast<Constant>(CI.getOperand(1))->isNullValue()) {
1254 if (ID == Intrinsic::returnaddress) {
1255 // Just load the return address
Chris Lattnere87331d2004-02-17 06:28:19 +00001256 addFrameReference(BuildMI(BB, X86::MOVrm32, 4, TmpReg1),
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001257 ReturnAddressIndex);
1258 } else {
1259 addFrameReference(BuildMI(BB, X86::LEAr32, 4, TmpReg1),
1260 ReturnAddressIndex, -4);
1261 }
1262 } else {
1263 // Values other than zero are not implemented yet.
Chris Lattner6e173a02004-02-17 06:16:44 +00001264 BuildMI(BB, X86::MOVri32, 1, TmpReg1).addZImm(0);
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001265 }
1266 return;
1267
Chris Lattner915e5e52004-02-12 17:53:22 +00001268 case Intrinsic::memcpy: {
1269 assert(CI.getNumOperands() == 5 && "Illegal llvm.memcpy call!");
1270 unsigned Align = 1;
1271 if (ConstantInt *AlignC = dyn_cast<ConstantInt>(CI.getOperand(4))) {
1272 Align = AlignC->getRawValue();
1273 if (Align == 0) Align = 1;
1274 }
1275
1276 // Turn the byte code into # iterations
Chris Lattner07122832004-02-13 23:36:47 +00001277 unsigned ByteReg;
Chris Lattner915e5e52004-02-12 17:53:22 +00001278 unsigned CountReg;
Chris Lattner2a0f2242004-02-14 04:46:05 +00001279 unsigned Opcode;
Chris Lattner915e5e52004-02-12 17:53:22 +00001280 switch (Align & 3) {
1281 case 2: // WORD aligned
Chris Lattner07122832004-02-13 23:36:47 +00001282 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
1283 CountReg = getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/2));
1284 } else {
1285 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner7ddc3fb2004-02-17 06:24:02 +00001286 BuildMI(BB, X86::SHRri32, 2, CountReg).addReg(ByteReg).addZImm(1);
Chris Lattner07122832004-02-13 23:36:47 +00001287 }
Chris Lattner2a0f2242004-02-14 04:46:05 +00001288 Opcode = X86::REP_MOVSW;
Chris Lattner915e5e52004-02-12 17:53:22 +00001289 break;
1290 case 0: // DWORD aligned
Chris Lattner07122832004-02-13 23:36:47 +00001291 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
1292 CountReg = getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/4));
1293 } else {
1294 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner7ddc3fb2004-02-17 06:24:02 +00001295 BuildMI(BB, X86::SHRri32, 2, CountReg).addReg(ByteReg).addZImm(2);
Chris Lattner07122832004-02-13 23:36:47 +00001296 }
Chris Lattner2a0f2242004-02-14 04:46:05 +00001297 Opcode = X86::REP_MOVSD;
Chris Lattner915e5e52004-02-12 17:53:22 +00001298 break;
1299 case 1: // BYTE aligned
1300 case 3: // BYTE aligned
Chris Lattner07122832004-02-13 23:36:47 +00001301 CountReg = getReg(CI.getOperand(3));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001302 Opcode = X86::REP_MOVSB;
Chris Lattner915e5e52004-02-12 17:53:22 +00001303 break;
1304 }
1305
1306 // No matter what the alignment is, we put the source in ESI, the
1307 // destination in EDI, and the count in ECX.
1308 TmpReg1 = getReg(CI.getOperand(1));
1309 TmpReg2 = getReg(CI.getOperand(2));
1310 BuildMI(BB, X86::MOVrr32, 1, X86::ECX).addReg(CountReg);
1311 BuildMI(BB, X86::MOVrr32, 1, X86::EDI).addReg(TmpReg1);
1312 BuildMI(BB, X86::MOVrr32, 1, X86::ESI).addReg(TmpReg2);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001313 BuildMI(BB, Opcode, 0);
1314 return;
1315 }
1316 case Intrinsic::memset: {
1317 assert(CI.getNumOperands() == 5 && "Illegal llvm.memset call!");
1318 unsigned Align = 1;
1319 if (ConstantInt *AlignC = dyn_cast<ConstantInt>(CI.getOperand(4))) {
1320 Align = AlignC->getRawValue();
1321 if (Align == 0) Align = 1;
Chris Lattner915e5e52004-02-12 17:53:22 +00001322 }
1323
Chris Lattner2a0f2242004-02-14 04:46:05 +00001324 // Turn the byte code into # iterations
1325 unsigned ByteReg;
1326 unsigned CountReg;
1327 unsigned Opcode;
1328 if (ConstantInt *ValC = dyn_cast<ConstantInt>(CI.getOperand(2))) {
1329 unsigned Val = ValC->getRawValue() & 255;
1330
1331 // If the value is a constant, then we can potentially use larger copies.
1332 switch (Align & 3) {
1333 case 2: // WORD aligned
1334 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
Chris Lattner300d0ed2004-02-14 06:00:36 +00001335 CountReg =getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/2));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001336 } else {
1337 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner7ddc3fb2004-02-17 06:24:02 +00001338 BuildMI(BB, X86::SHRri32, 2, CountReg).addReg(ByteReg).addZImm(1);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001339 }
Chris Lattner6e173a02004-02-17 06:16:44 +00001340 BuildMI(BB, X86::MOVri16, 1, X86::AX).addZImm((Val << 8) | Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001341 Opcode = X86::REP_STOSW;
1342 break;
1343 case 0: // DWORD aligned
1344 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
Chris Lattner300d0ed2004-02-14 06:00:36 +00001345 CountReg =getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/4));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001346 } else {
1347 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner7ddc3fb2004-02-17 06:24:02 +00001348 BuildMI(BB, X86::SHRri32, 2, CountReg).addReg(ByteReg).addZImm(2);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001349 }
1350 Val = (Val << 8) | Val;
Chris Lattner6e173a02004-02-17 06:16:44 +00001351 BuildMI(BB, X86::MOVri32, 1, X86::EAX).addZImm((Val << 16) | Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001352 Opcode = X86::REP_STOSD;
1353 break;
1354 case 1: // BYTE aligned
1355 case 3: // BYTE aligned
1356 CountReg = getReg(CI.getOperand(3));
Chris Lattner6e173a02004-02-17 06:16:44 +00001357 BuildMI(BB, X86::MOVri8, 1, X86::AL).addZImm(Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001358 Opcode = X86::REP_STOSB;
1359 break;
1360 }
1361 } else {
1362 // If it's not a constant value we are storing, just fall back. We could
1363 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
1364 unsigned ValReg = getReg(CI.getOperand(2));
1365 BuildMI(BB, X86::MOVrr8, 1, X86::AL).addReg(ValReg);
1366 CountReg = getReg(CI.getOperand(3));
1367 Opcode = X86::REP_STOSB;
1368 }
1369
1370 // No matter what the alignment is, we put the source in ESI, the
1371 // destination in EDI, and the count in ECX.
1372 TmpReg1 = getReg(CI.getOperand(1));
1373 //TmpReg2 = getReg(CI.getOperand(2));
1374 BuildMI(BB, X86::MOVrr32, 1, X86::ECX).addReg(CountReg);
1375 BuildMI(BB, X86::MOVrr32, 1, X86::EDI).addReg(TmpReg1);
1376 BuildMI(BB, Opcode, 0);
Chris Lattner915e5e52004-02-12 17:53:22 +00001377 return;
1378 }
1379
Chris Lattner44827152003-12-28 09:47:19 +00001380 default: assert(0 && "Error: unknown intrinsics should have been lowered!");
Chris Lattnereca195e2003-05-08 19:44:13 +00001381 }
1382}
1383
1384
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001385/// visitSimpleBinary - Implement simple binary operators for integral types...
1386/// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or, 4 for
1387/// Xor.
1388void ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) {
1389 unsigned DestReg = getReg(B);
1390 MachineBasicBlock::iterator MI = BB->end();
1391 emitSimpleBinaryOperation(BB, MI, B.getOperand(0), B.getOperand(1),
1392 OperatorClass, DestReg);
1393}
Chris Lattner3e130a22003-01-13 00:32:26 +00001394
Chris Lattnerb2acc512003-10-19 21:09:10 +00001395/// emitSimpleBinaryOperation - Implement simple binary operators for integral
1396/// types... OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for
1397/// Or, 4 for Xor.
Chris Lattner68aad932002-11-02 20:13:22 +00001398///
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001399/// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
1400/// and constant expression support.
Chris Lattnerb2acc512003-10-19 21:09:10 +00001401///
1402void ISel::emitSimpleBinaryOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00001403 MachineBasicBlock::iterator IP,
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001404 Value *Op0, Value *Op1,
Chris Lattnerb2acc512003-10-19 21:09:10 +00001405 unsigned OperatorClass, unsigned DestReg) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001406 unsigned Class = getClassB(Op0->getType());
Chris Lattnerb2acc512003-10-19 21:09:10 +00001407
1408 // sub 0, X -> neg X
1409 if (OperatorClass == 1 && Class != cLong)
Chris Lattneraf703622004-02-02 18:56:30 +00001410 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0)) {
Chris Lattnerb2acc512003-10-19 21:09:10 +00001411 if (CI->isNullValue()) {
1412 unsigned op1Reg = getReg(Op1, MBB, IP);
1413 switch (Class) {
1414 default: assert(0 && "Unknown class for this function!");
1415 case cByte:
1416 BMI(MBB, IP, X86::NEGr8, 1, DestReg).addReg(op1Reg);
1417 return;
1418 case cShort:
1419 BMI(MBB, IP, X86::NEGr16, 1, DestReg).addReg(op1Reg);
1420 return;
1421 case cInt:
1422 BMI(MBB, IP, X86::NEGr32, 1, DestReg).addReg(op1Reg);
1423 return;
1424 }
1425 }
Chris Lattner9f8fd6d2004-02-02 19:31:38 +00001426 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(Op0))
1427 if (CFP->isExactlyValue(-0.0)) {
1428 // -0.0 - X === -X
1429 unsigned op1Reg = getReg(Op1, MBB, IP);
1430 BMI(MBB, IP, X86::FCHS, 1, DestReg).addReg(op1Reg);
1431 return;
1432 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001433
Chris Lattner35333e12003-06-05 18:28:55 +00001434 if (!isa<ConstantInt>(Op1) || Class == cLong) {
1435 static const unsigned OpcodeTab[][4] = {
1436 // Arithmetic operators
1437 { X86::ADDrr8, X86::ADDrr16, X86::ADDrr32, X86::FpADD }, // ADD
1438 { X86::SUBrr8, X86::SUBrr16, X86::SUBrr32, X86::FpSUB }, // SUB
1439
1440 // Bitwise operators
1441 { X86::ANDrr8, X86::ANDrr16, X86::ANDrr32, 0 }, // AND
1442 { X86:: ORrr8, X86:: ORrr16, X86:: ORrr32, 0 }, // OR
1443 { X86::XORrr8, X86::XORrr16, X86::XORrr32, 0 }, // XOR
Chris Lattner3e130a22003-01-13 00:32:26 +00001444 };
Chris Lattner35333e12003-06-05 18:28:55 +00001445
1446 bool isLong = false;
1447 if (Class == cLong) {
1448 isLong = true;
1449 Class = cInt; // Bottom 32 bits are handled just like ints
1450 }
1451
1452 unsigned Opcode = OpcodeTab[OperatorClass][Class];
1453 assert(Opcode && "Floating point arguments to logical inst?");
Chris Lattnerb2acc512003-10-19 21:09:10 +00001454 unsigned Op0r = getReg(Op0, MBB, IP);
1455 unsigned Op1r = getReg(Op1, MBB, IP);
1456 BMI(MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r);
Chris Lattner35333e12003-06-05 18:28:55 +00001457
1458 if (isLong) { // Handle the upper 32 bits of long values...
1459 static const unsigned TopTab[] = {
1460 X86::ADCrr32, X86::SBBrr32, X86::ANDrr32, X86::ORrr32, X86::XORrr32
1461 };
Chris Lattnerb2acc512003-10-19 21:09:10 +00001462 BMI(MBB, IP, TopTab[OperatorClass], 2,
1463 DestReg+1).addReg(Op0r+1).addReg(Op1r+1);
Chris Lattner35333e12003-06-05 18:28:55 +00001464 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001465 return;
Chris Lattner3e130a22003-01-13 00:32:26 +00001466 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001467
1468 // Special case: op Reg, <const>
1469 ConstantInt *Op1C = cast<ConstantInt>(Op1);
1470 unsigned Op0r = getReg(Op0, MBB, IP);
1471
1472 // xor X, -1 -> not X
1473 if (OperatorClass == 4 && Op1C->isAllOnesValue()) {
1474 static unsigned const NOTTab[] = { X86::NOTr8, X86::NOTr16, X86::NOTr32 };
1475 BMI(MBB, IP, NOTTab[Class], 1, DestReg).addReg(Op0r);
1476 return;
1477 }
1478
1479 // add X, -1 -> dec X
1480 if (OperatorClass == 0 && Op1C->isAllOnesValue()) {
1481 static unsigned const DECTab[] = { X86::DECr8, X86::DECr16, X86::DECr32 };
1482 BMI(MBB, IP, DECTab[Class], 1, DestReg).addReg(Op0r);
1483 return;
1484 }
1485
1486 // add X, 1 -> inc X
1487 if (OperatorClass == 0 && Op1C->equalsInt(1)) {
1488 static unsigned const DECTab[] = { X86::INCr8, X86::INCr16, X86::INCr32 };
1489 BMI(MBB, IP, DECTab[Class], 1, DestReg).addReg(Op0r);
1490 return;
1491 }
1492
1493 static const unsigned OpcodeTab[][3] = {
1494 // Arithmetic operators
1495 { X86::ADDri8, X86::ADDri16, X86::ADDri32 }, // ADD
1496 { X86::SUBri8, X86::SUBri16, X86::SUBri32 }, // SUB
1497
1498 // Bitwise operators
1499 { X86::ANDri8, X86::ANDri16, X86::ANDri32 }, // AND
1500 { X86:: ORri8, X86:: ORri16, X86:: ORri32 }, // OR
1501 { X86::XORri8, X86::XORri16, X86::XORri32 }, // XOR
1502 };
1503
1504 assert(Class < 3 && "General code handles 64-bit integer types!");
1505 unsigned Opcode = OpcodeTab[OperatorClass][Class];
1506 uint64_t Op1v = cast<ConstantInt>(Op1C)->getRawValue();
1507
1508 // Mask off any upper bits of the constant, if there are any...
1509 Op1v &= (1ULL << (8 << Class)) - 1;
1510 BMI(MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addZImm(Op1v);
Chris Lattnere2954c82002-11-02 20:04:26 +00001511}
1512
Chris Lattner3e130a22003-01-13 00:32:26 +00001513/// doMultiply - Emit appropriate instructions to multiply together the
1514/// registers op0Reg and op1Reg, and put the result in DestReg. The type of the
1515/// result should be given as DestTy.
1516///
Chris Lattnerbaa58a52004-02-23 03:10:10 +00001517void ISel::doMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI,
Chris Lattner3e130a22003-01-13 00:32:26 +00001518 unsigned DestReg, const Type *DestTy,
Chris Lattner8a307e82002-12-16 19:32:50 +00001519 unsigned op0Reg, unsigned op1Reg) {
Chris Lattner3e130a22003-01-13 00:32:26 +00001520 unsigned Class = getClass(DestTy);
Chris Lattner94af4142002-12-25 05:13:53 +00001521 switch (Class) {
1522 case cFP: // Floating point multiply
Chris Lattner3e130a22003-01-13 00:32:26 +00001523 BMI(BB, MBBI, X86::FpMUL, 2, DestReg).addReg(op0Reg).addReg(op1Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001524 return;
Chris Lattner0f1c4612003-06-21 17:16:58 +00001525 case cInt:
1526 case cShort:
Chris Lattnerc01d1232003-10-20 03:42:58 +00001527 BMI(BB, MBBI, Class == cInt ? X86::IMULrr32 : X86::IMULrr16, 2, DestReg)
Chris Lattner0f1c4612003-06-21 17:16:58 +00001528 .addReg(op0Reg).addReg(op1Reg);
1529 return;
1530 case cByte:
1531 // Must use the MUL instruction, which forces use of AL...
1532 BMI(MBB, MBBI, X86::MOVrr8, 1, X86::AL).addReg(op0Reg);
1533 BMI(MBB, MBBI, X86::MULr8, 1).addReg(op1Reg);
1534 BMI(MBB, MBBI, X86::MOVrr8, 1, DestReg).addReg(X86::AL);
1535 return;
Chris Lattner94af4142002-12-25 05:13:53 +00001536 default:
Chris Lattner3e130a22003-01-13 00:32:26 +00001537 case cLong: assert(0 && "doMultiply cannot operate on LONG values!");
Chris Lattner94af4142002-12-25 05:13:53 +00001538 }
Brian Gaeke20244b72002-12-12 15:33:40 +00001539}
1540
Chris Lattnerb2acc512003-10-19 21:09:10 +00001541// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
1542// returns zero when the input is not exactly a power of two.
1543static unsigned ExactLog2(unsigned Val) {
1544 if (Val == 0) return 0;
1545 unsigned Count = 0;
1546 while (Val != 1) {
1547 if (Val & 1) return 0;
1548 Val >>= 1;
1549 ++Count;
1550 }
1551 return Count+1;
1552}
1553
1554void ISel::doMultiplyConst(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00001555 MachineBasicBlock::iterator IP,
Chris Lattnerb2acc512003-10-19 21:09:10 +00001556 unsigned DestReg, const Type *DestTy,
1557 unsigned op0Reg, unsigned ConstRHS) {
1558 unsigned Class = getClass(DestTy);
1559
1560 // If the element size is exactly a power of 2, use a shift to get it.
1561 if (unsigned Shift = ExactLog2(ConstRHS)) {
1562 switch (Class) {
1563 default: assert(0 && "Unknown class for this function!");
1564 case cByte:
Chris Lattner7ddc3fb2004-02-17 06:24:02 +00001565 BMI(MBB, IP, X86::SHLri32, 2, DestReg).addReg(op0Reg).addZImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001566 return;
1567 case cShort:
Chris Lattner7ddc3fb2004-02-17 06:24:02 +00001568 BMI(MBB, IP, X86::SHLri32, 2, DestReg).addReg(op0Reg).addZImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001569 return;
1570 case cInt:
Chris Lattner7ddc3fb2004-02-17 06:24:02 +00001571 BMI(MBB, IP, X86::SHLri32, 2, DestReg).addReg(op0Reg).addZImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001572 return;
1573 }
1574 }
Chris Lattnerc01d1232003-10-20 03:42:58 +00001575
1576 if (Class == cShort) {
Chris Lattner55b54812004-02-17 04:26:43 +00001577 BMI(MBB, IP, X86::IMULrri16, 2, DestReg).addReg(op0Reg).addZImm(ConstRHS);
Chris Lattnerc01d1232003-10-20 03:42:58 +00001578 return;
1579 } else if (Class == cInt) {
Chris Lattner55b54812004-02-17 04:26:43 +00001580 BMI(MBB, IP, X86::IMULrri32, 2, DestReg).addReg(op0Reg).addZImm(ConstRHS);
Chris Lattnerc01d1232003-10-20 03:42:58 +00001581 return;
1582 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001583
1584 // Most general case, emit a normal multiply...
Chris Lattner6e173a02004-02-17 06:16:44 +00001585 static const unsigned MOVriTab[] = {
1586 X86::MOVri8, X86::MOVri16, X86::MOVri32
Chris Lattnerb2acc512003-10-19 21:09:10 +00001587 };
1588
1589 unsigned TmpReg = makeAnotherReg(DestTy);
Chris Lattner6e173a02004-02-17 06:16:44 +00001590 BMI(MBB, IP, MOVriTab[Class], 1, TmpReg).addZImm(ConstRHS);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001591
1592 // Emit a MUL to multiply the register holding the index by
1593 // elementSize, putting the result in OffsetReg.
1594 doMultiply(MBB, IP, DestReg, DestTy, op0Reg, TmpReg);
1595}
1596
Chris Lattnerca9671d2002-11-02 20:28:58 +00001597/// visitMul - Multiplies are not simple binary operators because they must deal
1598/// with the EAX register explicitly.
1599///
1600void ISel::visitMul(BinaryOperator &I) {
Chris Lattner202a2d02002-12-13 13:07:42 +00001601 unsigned Op0Reg = getReg(I.getOperand(0));
Chris Lattner3e130a22003-01-13 00:32:26 +00001602 unsigned DestReg = getReg(I);
1603
1604 // Simple scalar multiply?
1605 if (I.getType() != Type::LongTy && I.getType() != Type::ULongTy) {
Chris Lattnerb2acc512003-10-19 21:09:10 +00001606 if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1))) {
1607 unsigned Val = (unsigned)CI->getRawValue(); // Cannot be 64-bit constant
1608 MachineBasicBlock::iterator MBBI = BB->end();
1609 doMultiplyConst(BB, MBBI, DestReg, I.getType(), Op0Reg, Val);
1610 } else {
1611 unsigned Op1Reg = getReg(I.getOperand(1));
1612 MachineBasicBlock::iterator MBBI = BB->end();
1613 doMultiply(BB, MBBI, DestReg, I.getType(), Op0Reg, Op1Reg);
1614 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001615 } else {
Chris Lattnerb2acc512003-10-19 21:09:10 +00001616 unsigned Op1Reg = getReg(I.getOperand(1));
1617
Chris Lattner3e130a22003-01-13 00:32:26 +00001618 // Long value. We have to do things the hard way...
1619 // Multiply the two low parts... capturing carry into EDX
1620 BuildMI(BB, X86::MOVrr32, 1, X86::EAX).addReg(Op0Reg);
1621 BuildMI(BB, X86::MULr32, 1).addReg(Op1Reg); // AL*BL
1622
1623 unsigned OverflowReg = makeAnotherReg(Type::UIntTy);
1624 BuildMI(BB, X86::MOVrr32, 1, DestReg).addReg(X86::EAX); // AL*BL
1625 BuildMI(BB, X86::MOVrr32, 1, OverflowReg).addReg(X86::EDX); // AL*BL >> 32
1626
1627 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattner034acf02003-06-21 18:15:27 +00001628 unsigned AHBLReg = makeAnotherReg(Type::UIntTy); // AH*BL
Chris Lattnerc01d1232003-10-20 03:42:58 +00001629 BMI(BB, MBBI, X86::IMULrr32, 2, AHBLReg).addReg(Op0Reg+1).addReg(Op1Reg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001630
1631 unsigned AHBLplusOverflowReg = makeAnotherReg(Type::UIntTy);
1632 BuildMI(BB, X86::ADDrr32, 2, // AH*BL+(AL*BL >> 32)
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001633 AHBLplusOverflowReg).addReg(AHBLReg).addReg(OverflowReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001634
1635 MBBI = BB->end();
Chris Lattner034acf02003-06-21 18:15:27 +00001636 unsigned ALBHReg = makeAnotherReg(Type::UIntTy); // AL*BH
Chris Lattnerc01d1232003-10-20 03:42:58 +00001637 BMI(BB, MBBI, X86::IMULrr32, 2, ALBHReg).addReg(Op0Reg).addReg(Op1Reg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00001638
1639 BuildMI(BB, X86::ADDrr32, 2, // AL*BH + AH*BL + (AL*BL >> 32)
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001640 DestReg+1).addReg(AHBLplusOverflowReg).addReg(ALBHReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001641 }
Chris Lattnerf01729e2002-11-02 20:54:46 +00001642}
Chris Lattnerca9671d2002-11-02 20:28:58 +00001643
Chris Lattner06925362002-11-17 21:56:38 +00001644
Chris Lattnerf01729e2002-11-02 20:54:46 +00001645/// visitDivRem - Handle division and remainder instructions... these
1646/// instruction both require the same instructions to be generated, they just
1647/// select the result from a different register. Note that both of these
1648/// instructions work differently for signed and unsigned operands.
1649///
1650void ISel::visitDivRem(BinaryOperator &I) {
Chris Lattnercadff442003-10-23 17:21:43 +00001651 unsigned Op0Reg = getReg(I.getOperand(0));
1652 unsigned Op1Reg = getReg(I.getOperand(1));
1653 unsigned ResultReg = getReg(I);
Chris Lattner94af4142002-12-25 05:13:53 +00001654
Chris Lattnercadff442003-10-23 17:21:43 +00001655 MachineBasicBlock::iterator IP = BB->end();
1656 emitDivRemOperation(BB, IP, Op0Reg, Op1Reg, I.getOpcode() == Instruction::Div,
1657 I.getType(), ResultReg);
1658}
1659
1660void ISel::emitDivRemOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00001661 MachineBasicBlock::iterator IP,
Chris Lattnercadff442003-10-23 17:21:43 +00001662 unsigned Op0Reg, unsigned Op1Reg, bool isDiv,
1663 const Type *Ty, unsigned ResultReg) {
1664 unsigned Class = getClass(Ty);
Chris Lattner94af4142002-12-25 05:13:53 +00001665 switch (Class) {
Chris Lattner3e130a22003-01-13 00:32:26 +00001666 case cFP: // Floating point divide
Chris Lattnercadff442003-10-23 17:21:43 +00001667 if (isDiv) {
Chris Lattner62b767b2003-11-18 17:47:05 +00001668 BMI(BB, IP, X86::FpDIV, 2, ResultReg).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001669 } else { // Floating point remainder...
Chris Lattner3e130a22003-01-13 00:32:26 +00001670 MachineInstr *TheCall =
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001671 BuildMI(X86::CALLpcrel32, 1).addExternalSymbol("fmod", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00001672 std::vector<ValueRecord> Args;
Chris Lattnercadff442003-10-23 17:21:43 +00001673 Args.push_back(ValueRecord(Op0Reg, Type::DoubleTy));
1674 Args.push_back(ValueRecord(Op1Reg, Type::DoubleTy));
Chris Lattner3e130a22003-01-13 00:32:26 +00001675 doCall(ValueRecord(ResultReg, Type::DoubleTy), TheCall, Args);
1676 }
Chris Lattner94af4142002-12-25 05:13:53 +00001677 return;
Chris Lattner3e130a22003-01-13 00:32:26 +00001678 case cLong: {
1679 static const char *FnName[] =
1680 { "__moddi3", "__divdi3", "__umoddi3", "__udivdi3" };
1681
Chris Lattnercadff442003-10-23 17:21:43 +00001682 unsigned NameIdx = Ty->isUnsigned()*2 + isDiv;
Chris Lattner3e130a22003-01-13 00:32:26 +00001683 MachineInstr *TheCall =
1684 BuildMI(X86::CALLpcrel32, 1).addExternalSymbol(FnName[NameIdx], true);
1685
1686 std::vector<ValueRecord> Args;
Chris Lattnercadff442003-10-23 17:21:43 +00001687 Args.push_back(ValueRecord(Op0Reg, Type::LongTy));
1688 Args.push_back(ValueRecord(Op1Reg, Type::LongTy));
Chris Lattner3e130a22003-01-13 00:32:26 +00001689 doCall(ValueRecord(ResultReg, Type::LongTy), TheCall, Args);
1690 return;
1691 }
1692 case cByte: case cShort: case cInt:
Misha Brukmancf00c4a2003-10-10 17:57:28 +00001693 break; // Small integrals, handled below...
Chris Lattner3e130a22003-01-13 00:32:26 +00001694 default: assert(0 && "Unknown class!");
Chris Lattner94af4142002-12-25 05:13:53 +00001695 }
Chris Lattnerf01729e2002-11-02 20:54:46 +00001696
1697 static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX };
1698 static const unsigned MovOpcode[]={ X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 };
Chris Lattner7ddc3fb2004-02-17 06:24:02 +00001699 static const unsigned SarOpcode[]={ X86::SARri8, X86::SARri16, X86::SARri32 };
Chris Lattner6e173a02004-02-17 06:16:44 +00001700 static const unsigned ClrOpcode[]={ X86::MOVri8, X86::MOVri16, X86::MOVri32 };
Chris Lattnerf01729e2002-11-02 20:54:46 +00001701 static const unsigned ExtRegs[] ={ X86::AH , X86::DX , X86::EDX };
1702
1703 static const unsigned DivOpcode[][4] = {
Chris Lattner3e130a22003-01-13 00:32:26 +00001704 { X86::DIVr8 , X86::DIVr16 , X86::DIVr32 , 0 }, // Unsigned division
1705 { X86::IDIVr8, X86::IDIVr16, X86::IDIVr32, 0 }, // Signed division
Chris Lattnerf01729e2002-11-02 20:54:46 +00001706 };
1707
Chris Lattnercadff442003-10-23 17:21:43 +00001708 bool isSigned = Ty->isSigned();
Chris Lattnerf01729e2002-11-02 20:54:46 +00001709 unsigned Reg = Regs[Class];
1710 unsigned ExtReg = ExtRegs[Class];
Chris Lattnerf01729e2002-11-02 20:54:46 +00001711
1712 // Put the first operand into one of the A registers...
Chris Lattner62b767b2003-11-18 17:47:05 +00001713 BMI(BB, IP, MovOpcode[Class], 1, Reg).addReg(Op0Reg);
Chris Lattnerf01729e2002-11-02 20:54:46 +00001714
1715 if (isSigned) {
1716 // Emit a sign extension instruction...
Chris Lattnercadff442003-10-23 17:21:43 +00001717 unsigned ShiftResult = makeAnotherReg(Ty);
Chris Lattner62b767b2003-11-18 17:47:05 +00001718 BMI(BB, IP, SarOpcode[Class], 2, ShiftResult).addReg(Op0Reg).addZImm(31);
1719 BMI(BB, IP, MovOpcode[Class], 1, ExtReg).addReg(ShiftResult);
Chris Lattnerf01729e2002-11-02 20:54:46 +00001720 } else {
Alkis Evlogimenosf998a7e2004-01-12 07:22:45 +00001721 // If unsigned, emit a zeroing instruction... (reg = 0)
1722 BMI(BB, IP, ClrOpcode[Class], 2, ExtReg).addZImm(0);
Chris Lattnerf01729e2002-11-02 20:54:46 +00001723 }
1724
Chris Lattner06925362002-11-17 21:56:38 +00001725 // Emit the appropriate divide or remainder instruction...
Chris Lattner62b767b2003-11-18 17:47:05 +00001726 BMI(BB, IP, DivOpcode[isSigned][Class], 1).addReg(Op1Reg);
Chris Lattner06925362002-11-17 21:56:38 +00001727
Chris Lattnerf01729e2002-11-02 20:54:46 +00001728 // Figure out which register we want to pick the result out of...
Chris Lattnercadff442003-10-23 17:21:43 +00001729 unsigned DestReg = isDiv ? Reg : ExtReg;
Chris Lattnerf01729e2002-11-02 20:54:46 +00001730
Chris Lattnerf01729e2002-11-02 20:54:46 +00001731 // Put the result into the destination register...
Chris Lattner62b767b2003-11-18 17:47:05 +00001732 BMI(BB, IP, MovOpcode[Class], 1, ResultReg).addReg(DestReg);
Chris Lattnerca9671d2002-11-02 20:28:58 +00001733}
Chris Lattnere2954c82002-11-02 20:04:26 +00001734
Chris Lattner06925362002-11-17 21:56:38 +00001735
Brian Gaekea1719c92002-10-31 23:03:59 +00001736/// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here
1737/// for constant immediate shift values, and for constant immediate
1738/// shift values equal to 1. Even the general case is sort of special,
1739/// because the shift amount has to be in CL, not just any old register.
1740///
Chris Lattner3e130a22003-01-13 00:32:26 +00001741void ISel::visitShiftInst(ShiftInst &I) {
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001742 MachineBasicBlock::iterator IP = BB->end ();
1743 emitShiftOperation (BB, IP, I.getOperand (0), I.getOperand (1),
1744 I.getOpcode () == Instruction::Shl, I.getType (),
1745 getReg (I));
1746}
1747
1748/// emitShiftOperation - Common code shared between visitShiftInst and
1749/// constant expression support.
1750void ISel::emitShiftOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00001751 MachineBasicBlock::iterator IP,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001752 Value *Op, Value *ShiftAmount, bool isLeftShift,
1753 const Type *ResultTy, unsigned DestReg) {
1754 unsigned SrcReg = getReg (Op, MBB, IP);
1755 bool isSigned = ResultTy->isSigned ();
1756 unsigned Class = getClass (ResultTy);
Chris Lattner3e130a22003-01-13 00:32:26 +00001757
1758 static const unsigned ConstantOperand[][4] = {
Chris Lattner7ddc3fb2004-02-17 06:24:02 +00001759 { X86::SHRri8, X86::SHRri16, X86::SHRri32, X86::SHRDri32 }, // SHR
1760 { X86::SARri8, X86::SARri16, X86::SARri32, X86::SHRDri32 }, // SAR
1761 { X86::SHLri8, X86::SHLri16, X86::SHLri32, X86::SHLDri32 }, // SHL
1762 { X86::SHLri8, X86::SHLri16, X86::SHLri32, X86::SHLDri32 }, // SAL = SHL
Chris Lattner3e130a22003-01-13 00:32:26 +00001763 };
Chris Lattnerb1761fc2002-11-02 01:15:18 +00001764
Chris Lattner3e130a22003-01-13 00:32:26 +00001765 static const unsigned NonConstantOperand[][4] = {
1766 { X86::SHRrr8, X86::SHRrr16, X86::SHRrr32 }, // SHR
1767 { X86::SARrr8, X86::SARrr16, X86::SARrr32 }, // SAR
1768 { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32 }, // SHL
1769 { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32 }, // SAL = SHL
1770 };
Chris Lattner796df732002-11-02 00:44:25 +00001771
Chris Lattner3e130a22003-01-13 00:32:26 +00001772 // Longs, as usual, are handled specially...
1773 if (Class == cLong) {
1774 // If we have a constant shift, we can generate much more efficient code
1775 // than otherwise...
1776 //
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001777 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00001778 unsigned Amount = CUI->getValue();
1779 if (Amount < 32) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001780 const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned];
1781 if (isLeftShift) {
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001782 BMI(MBB, IP, Opc[3], 3,
1783 DestReg+1).addReg(SrcReg+1).addReg(SrcReg).addZImm(Amount);
1784 BMI(MBB, IP, Opc[2], 2, DestReg).addReg(SrcReg).addZImm(Amount);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001785 } else {
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001786 BMI(MBB, IP, Opc[3], 3,
1787 DestReg).addReg(SrcReg ).addReg(SrcReg+1).addZImm(Amount);
1788 BMI(MBB, IP, Opc[2], 2, DestReg+1).addReg(SrcReg+1).addZImm(Amount);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001789 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001790 } else { // Shifting more than 32 bits
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001791 Amount -= 32;
1792 if (isLeftShift) {
Chris Lattner7ddc3fb2004-02-17 06:24:02 +00001793 BMI(MBB, IP, X86::SHLri32, 2,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001794 DestReg + 1).addReg(SrcReg).addZImm(Amount);
Chris Lattner6e173a02004-02-17 06:16:44 +00001795 BMI(MBB, IP, X86::MOVri32, 1,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001796 DestReg).addZImm(0);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001797 } else {
Chris Lattner7ddc3fb2004-02-17 06:24:02 +00001798 unsigned Opcode = isSigned ? X86::SARri32 : X86::SHRri32;
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001799 BMI(MBB, IP, Opcode, 2, DestReg).addReg(SrcReg+1).addZImm(Amount);
Chris Lattner6e173a02004-02-17 06:16:44 +00001800 BMI(MBB, IP, X86::MOVri32, 1, DestReg+1).addZImm(0);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001801 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001802 }
1803 } else {
Chris Lattner9171ef52003-06-01 01:56:54 +00001804 unsigned TmpReg = makeAnotherReg(Type::IntTy);
1805
1806 if (!isLeftShift && isSigned) {
1807 // If this is a SHR of a Long, then we need to do funny sign extension
1808 // stuff. TmpReg gets the value to use as the high-part if we are
1809 // shifting more than 32 bits.
Chris Lattner7ddc3fb2004-02-17 06:24:02 +00001810 BMI(MBB, IP, X86::SARri32, 2, TmpReg).addReg(SrcReg).addZImm(31);
Chris Lattner9171ef52003-06-01 01:56:54 +00001811 } else {
1812 // Other shifts use a fixed zero value if the shift is more than 32
1813 // bits.
Chris Lattner6e173a02004-02-17 06:16:44 +00001814 BMI(MBB, IP, X86::MOVri32, 1, TmpReg).addZImm(0);
Chris Lattner9171ef52003-06-01 01:56:54 +00001815 }
1816
1817 // Initialize CL with the shift amount...
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001818 unsigned ShiftAmountReg = getReg(ShiftAmount, MBB, IP);
1819 BMI(MBB, IP, X86::MOVrr8, 1, X86::CL).addReg(ShiftAmountReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00001820
1821 unsigned TmpReg2 = makeAnotherReg(Type::IntTy);
1822 unsigned TmpReg3 = makeAnotherReg(Type::IntTy);
1823 if (isLeftShift) {
1824 // TmpReg2 = shld inHi, inLo
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001825 BMI(MBB, IP, X86::SHLDrr32, 2, TmpReg2).addReg(SrcReg+1).addReg(SrcReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00001826 // TmpReg3 = shl inLo, CL
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001827 BMI(MBB, IP, X86::SHLrr32, 1, TmpReg3).addReg(SrcReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00001828
1829 // Set the flags to indicate whether the shift was by more than 32 bits.
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001830 BMI(MBB, IP, X86::TESTri8, 2).addReg(X86::CL).addZImm(32);
Chris Lattner9171ef52003-06-01 01:56:54 +00001831
1832 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001833 BMI(MBB, IP, X86::CMOVNErr32, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00001834 DestReg+1).addReg(TmpReg2).addReg(TmpReg3);
1835 // DestLo = (>32) ? TmpReg : TmpReg3;
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001836 BMI(MBB, IP, X86::CMOVNErr32, 2,
1837 DestReg).addReg(TmpReg3).addReg(TmpReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00001838 } else {
1839 // TmpReg2 = shrd inLo, inHi
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001840 BMI(MBB, IP, X86::SHRDrr32, 2, TmpReg2).addReg(SrcReg).addReg(SrcReg+1);
Chris Lattner9171ef52003-06-01 01:56:54 +00001841 // TmpReg3 = s[ah]r inHi, CL
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001842 BMI(MBB, IP, isSigned ? X86::SARrr32 : X86::SHRrr32, 1, TmpReg3)
Chris Lattner9171ef52003-06-01 01:56:54 +00001843 .addReg(SrcReg+1);
1844
1845 // Set the flags to indicate whether the shift was by more than 32 bits.
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001846 BMI(MBB, IP, X86::TESTri8, 2).addReg(X86::CL).addZImm(32);
Chris Lattner9171ef52003-06-01 01:56:54 +00001847
1848 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001849 BMI(MBB, IP, X86::CMOVNErr32, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00001850 DestReg).addReg(TmpReg2).addReg(TmpReg3);
1851
1852 // DestHi = (>32) ? TmpReg : TmpReg3;
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001853 BMI(MBB, IP, X86::CMOVNErr32, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00001854 DestReg+1).addReg(TmpReg3).addReg(TmpReg);
1855 }
Brian Gaekea1719c92002-10-31 23:03:59 +00001856 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001857 return;
1858 }
Chris Lattnere9913f22002-11-02 01:41:55 +00001859
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001860 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00001861 // The shift amount is constant, guaranteed to be a ubyte. Get its value.
1862 assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?");
Chris Lattnerb1761fc2002-11-02 01:15:18 +00001863
Chris Lattner3e130a22003-01-13 00:32:26 +00001864 const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned];
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001865 BMI(MBB, IP, Opc[Class], 2,
1866 DestReg).addReg(SrcReg).addZImm(CUI->getValue());
Chris Lattner3e130a22003-01-13 00:32:26 +00001867 } else { // The shift amount is non-constant.
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001868 unsigned ShiftAmountReg = getReg (ShiftAmount, MBB, IP);
1869 BMI(MBB, IP, X86::MOVrr8, 1, X86::CL).addReg(ShiftAmountReg);
Chris Lattnerb1761fc2002-11-02 01:15:18 +00001870
Chris Lattner3e130a22003-01-13 00:32:26 +00001871 const unsigned *Opc = NonConstantOperand[isLeftShift*2+isSigned];
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001872 BMI(MBB, IP, Opc[Class], 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001873 }
1874}
Chris Lattnerb1761fc2002-11-02 01:15:18 +00001875
Chris Lattner3e130a22003-01-13 00:32:26 +00001876
Chris Lattner6fc3c522002-11-17 21:11:55 +00001877/// visitLoadInst - Implement LLVM load instructions in terms of the x86 'mov'
Chris Lattnere8f0d922002-12-24 00:03:11 +00001878/// instruction. The load and store instructions are the only place where we
1879/// need to worry about the memory layout of the target machine.
Chris Lattner6fc3c522002-11-17 21:11:55 +00001880///
1881void ISel::visitLoadInst(LoadInst &I) {
Chris Lattner94af4142002-12-25 05:13:53 +00001882 unsigned SrcAddrReg = getReg(I.getOperand(0));
1883 unsigned DestReg = getReg(I);
Chris Lattnere8f0d922002-12-24 00:03:11 +00001884
Brian Gaekebfedb912003-07-17 21:30:06 +00001885 unsigned Class = getClassB(I.getType());
Chris Lattner6ac1d712003-10-20 04:48:06 +00001886
1887 if (Class == cLong) {
Chris Lattnere87331d2004-02-17 06:28:19 +00001888 addDirectMem(BuildMI(BB, X86::MOVrm32, 4, DestReg), SrcAddrReg);
1889 addRegOffset(BuildMI(BB, X86::MOVrm32, 4, DestReg+1), SrcAddrReg, 4);
Chris Lattner94af4142002-12-25 05:13:53 +00001890 return;
1891 }
Chris Lattner6fc3c522002-11-17 21:11:55 +00001892
Chris Lattner6ac1d712003-10-20 04:48:06 +00001893 static const unsigned Opcodes[] = {
Chris Lattnere87331d2004-02-17 06:28:19 +00001894 X86::MOVrm8, X86::MOVrm16, X86::MOVrm32, X86::FLDr32
Chris Lattner3e130a22003-01-13 00:32:26 +00001895 };
Chris Lattner6ac1d712003-10-20 04:48:06 +00001896 unsigned Opcode = Opcodes[Class];
1897 if (I.getType() == Type::DoubleTy) Opcode = X86::FLDr64;
1898 addDirectMem(BuildMI(BB, Opcode, 4, DestReg), SrcAddrReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001899}
1900
Chris Lattner6fc3c522002-11-17 21:11:55 +00001901/// visitStoreInst - Implement LLVM store instructions in terms of the x86 'mov'
1902/// instruction.
1903///
1904void ISel::visitStoreInst(StoreInst &I) {
Chris Lattner3e130a22003-01-13 00:32:26 +00001905 unsigned ValReg = getReg(I.getOperand(0));
1906 unsigned AddressReg = getReg(I.getOperand(1));
Chris Lattner6c09db22003-10-20 04:11:23 +00001907
1908 const Type *ValTy = I.getOperand(0)->getType();
1909 unsigned Class = getClassB(ValTy);
Chris Lattner6ac1d712003-10-20 04:48:06 +00001910
1911 if (Class == cLong) {
Chris Lattnere87331d2004-02-17 06:28:19 +00001912 addDirectMem(BuildMI(BB, X86::MOVmr32, 1+4), AddressReg).addReg(ValReg);
1913 addRegOffset(BuildMI(BB, X86::MOVmr32, 1+4), AddressReg,4).addReg(ValReg+1);
Chris Lattner94af4142002-12-25 05:13:53 +00001914 return;
Chris Lattner94af4142002-12-25 05:13:53 +00001915 }
1916
Chris Lattner6ac1d712003-10-20 04:48:06 +00001917 static const unsigned Opcodes[] = {
Chris Lattnere87331d2004-02-17 06:28:19 +00001918 X86::MOVmr8, X86::MOVmr16, X86::MOVmr32, X86::FSTr32
Chris Lattner6ac1d712003-10-20 04:48:06 +00001919 };
1920 unsigned Opcode = Opcodes[Class];
1921 if (ValTy == Type::DoubleTy) Opcode = X86::FSTr64;
1922 addDirectMem(BuildMI(BB, Opcode, 1+4), AddressReg).addReg(ValReg);
Chris Lattner6fc3c522002-11-17 21:11:55 +00001923}
1924
1925
Brian Gaekec11232a2002-11-26 10:43:30 +00001926/// visitCastInst - Here we have various kinds of copying with or without
1927/// sign extension going on.
Chris Lattner3e130a22003-01-13 00:32:26 +00001928void ISel::visitCastInst(CastInst &CI) {
Chris Lattnerf5854472003-06-21 16:01:24 +00001929 Value *Op = CI.getOperand(0);
1930 // If this is a cast from a 32-bit integer to a Long type, and the only uses
1931 // of the case are GEP instructions, then the cast does not need to be
1932 // generated explicitly, it will be folded into the GEP.
1933 if (CI.getType() == Type::LongTy &&
1934 (Op->getType() == Type::IntTy || Op->getType() == Type::UIntTy)) {
1935 bool AllUsesAreGEPs = true;
1936 for (Value::use_iterator I = CI.use_begin(), E = CI.use_end(); I != E; ++I)
1937 if (!isa<GetElementPtrInst>(*I)) {
1938 AllUsesAreGEPs = false;
1939 break;
1940 }
1941
1942 // No need to codegen this cast if all users are getelementptr instrs...
1943 if (AllUsesAreGEPs) return;
1944 }
1945
Chris Lattner548f61d2003-04-23 17:22:12 +00001946 unsigned DestReg = getReg(CI);
1947 MachineBasicBlock::iterator MI = BB->end();
Chris Lattnerf5854472003-06-21 16:01:24 +00001948 emitCastOperation(BB, MI, Op, CI.getType(), DestReg);
Chris Lattner548f61d2003-04-23 17:22:12 +00001949}
1950
1951/// emitCastOperation - Common code shared between visitCastInst and
1952/// constant expression cast support.
1953void ISel::emitCastOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00001954 MachineBasicBlock::iterator IP,
Chris Lattner548f61d2003-04-23 17:22:12 +00001955 Value *Src, const Type *DestTy,
1956 unsigned DestReg) {
Chris Lattner3907d112003-04-23 17:57:48 +00001957 unsigned SrcReg = getReg(Src, BB, IP);
Chris Lattner3e130a22003-01-13 00:32:26 +00001958 const Type *SrcTy = Src->getType();
1959 unsigned SrcClass = getClassB(SrcTy);
Chris Lattner3e130a22003-01-13 00:32:26 +00001960 unsigned DestClass = getClassB(DestTy);
Chris Lattner7d255892002-12-13 11:31:59 +00001961
Chris Lattner3e130a22003-01-13 00:32:26 +00001962 // Implement casts to bool by using compare on the operand followed by set if
1963 // not zero on the result.
1964 if (DestTy == Type::BoolTy) {
Chris Lattner20772542003-06-01 03:38:24 +00001965 switch (SrcClass) {
1966 case cByte:
1967 BMI(BB, IP, X86::TESTrr8, 2).addReg(SrcReg).addReg(SrcReg);
1968 break;
1969 case cShort:
1970 BMI(BB, IP, X86::TESTrr16, 2).addReg(SrcReg).addReg(SrcReg);
1971 break;
1972 case cInt:
1973 BMI(BB, IP, X86::TESTrr32, 2).addReg(SrcReg).addReg(SrcReg);
1974 break;
1975 case cLong: {
1976 unsigned TmpReg = makeAnotherReg(Type::IntTy);
1977 BMI(BB, IP, X86::ORrr32, 2, TmpReg).addReg(SrcReg).addReg(SrcReg+1);
1978 break;
1979 }
1980 case cFP:
Chris Lattner311ca2e2004-02-23 03:21:41 +00001981 BMI(BB, IP, X86::FTST, 1).addReg(SrcReg);
1982 BMI(BB, IP, X86::FNSTSWr8, 0);
1983 BMI(BB, IP, X86::SAHF, 1);
1984 break;
Chris Lattner20772542003-06-01 03:38:24 +00001985 }
1986
1987 // If the zero flag is not set, then the value is true, set the byte to
1988 // true.
Chris Lattner548f61d2003-04-23 17:22:12 +00001989 BMI(BB, IP, X86::SETNEr, 1, DestReg);
Chris Lattner94af4142002-12-25 05:13:53 +00001990 return;
1991 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001992
1993 static const unsigned RegRegMove[] = {
1994 X86::MOVrr8, X86::MOVrr16, X86::MOVrr32, X86::FpMOV, X86::MOVrr32
1995 };
1996
1997 // Implement casts between values of the same type class (as determined by
1998 // getClass) by using a register-to-register move.
1999 if (SrcClass == DestClass) {
2000 if (SrcClass <= cInt || (SrcClass == cFP && SrcTy == DestTy)) {
Chris Lattner548f61d2003-04-23 17:22:12 +00002001 BMI(BB, IP, RegRegMove[SrcClass], 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002002 } else if (SrcClass == cFP) {
2003 if (SrcTy == Type::FloatTy) { // double -> float
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002004 assert(DestTy == Type::DoubleTy && "Unknown cFP member!");
2005 BMI(BB, IP, X86::FpMOV, 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002006 } else { // float -> double
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002007 assert(SrcTy == Type::DoubleTy && DestTy == Type::FloatTy &&
2008 "Unknown cFP member!");
2009 // Truncate from double to float by storing to memory as short, then
2010 // reading it back.
2011 unsigned FltAlign = TM.getTargetData().getFloatAlignment();
Chris Lattner3e130a22003-01-13 00:32:26 +00002012 int FrameIdx = F->getFrameInfo()->CreateStackObject(4, FltAlign);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002013 addFrameReference(BMI(BB, IP, X86::FSTr32, 5), FrameIdx).addReg(SrcReg);
2014 addFrameReference(BMI(BB, IP, X86::FLDr32, 5, DestReg), FrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00002015 }
2016 } else if (SrcClass == cLong) {
Chris Lattner548f61d2003-04-23 17:22:12 +00002017 BMI(BB, IP, X86::MOVrr32, 1, DestReg).addReg(SrcReg);
2018 BMI(BB, IP, X86::MOVrr32, 1, DestReg+1).addReg(SrcReg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00002019 } else {
Chris Lattnerc53544a2003-05-12 20:16:58 +00002020 assert(0 && "Cannot handle this type of cast instruction!");
Chris Lattner548f61d2003-04-23 17:22:12 +00002021 abort();
Brian Gaeked474e9c2002-12-06 10:49:33 +00002022 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002023 return;
2024 }
2025
2026 // Handle cast of SMALLER int to LARGER int using a move with sign extension
2027 // or zero extension, depending on whether the source type was signed.
2028 if (SrcClass <= cInt && (DestClass <= cInt || DestClass == cLong) &&
2029 SrcClass < DestClass) {
2030 bool isLong = DestClass == cLong;
2031 if (isLong) DestClass = cInt;
2032
2033 static const unsigned Opc[][4] = {
2034 { X86::MOVSXr16r8, X86::MOVSXr32r8, X86::MOVSXr32r16, X86::MOVrr32 }, // s
2035 { X86::MOVZXr16r8, X86::MOVZXr32r8, X86::MOVZXr32r16, X86::MOVrr32 } // u
2036 };
2037
2038 bool isUnsigned = SrcTy->isUnsigned();
Chris Lattner548f61d2003-04-23 17:22:12 +00002039 BMI(BB, IP, Opc[isUnsigned][SrcClass + DestClass - 1], 1,
2040 DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002041
2042 if (isLong) { // Handle upper 32 bits as appropriate...
2043 if (isUnsigned) // Zero out top bits...
Chris Lattner6e173a02004-02-17 06:16:44 +00002044 BMI(BB, IP, X86::MOVri32, 1, DestReg+1).addZImm(0);
Chris Lattner3e130a22003-01-13 00:32:26 +00002045 else // Sign extend bottom half...
Chris Lattner7ddc3fb2004-02-17 06:24:02 +00002046 BMI(BB, IP, X86::SARri32, 2, DestReg+1).addReg(DestReg).addZImm(31);
Brian Gaeked474e9c2002-12-06 10:49:33 +00002047 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002048 return;
2049 }
2050
2051 // Special case long -> int ...
2052 if (SrcClass == cLong && DestClass == cInt) {
Chris Lattner548f61d2003-04-23 17:22:12 +00002053 BMI(BB, IP, X86::MOVrr32, 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002054 return;
2055 }
2056
2057 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by a
2058 // move out of AX or AL.
2059 if ((SrcClass <= cInt || SrcClass == cLong) && DestClass <= cInt
2060 && SrcClass > DestClass) {
2061 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX, 0, X86::EAX };
Chris Lattner548f61d2003-04-23 17:22:12 +00002062 BMI(BB, IP, RegRegMove[SrcClass], 1, AReg[SrcClass]).addReg(SrcReg);
2063 BMI(BB, IP, RegRegMove[DestClass], 1, DestReg).addReg(AReg[DestClass]);
Chris Lattner3e130a22003-01-13 00:32:26 +00002064 return;
2065 }
2066
2067 // Handle casts from integer to floating point now...
2068 if (DestClass == cFP) {
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002069 // Promote the integer to a type supported by FLD. We do this because there
2070 // are no unsigned FLD instructions, so we must promote an unsigned value to
2071 // a larger signed value, then use FLD on the larger value.
2072 //
2073 const Type *PromoteType = 0;
2074 unsigned PromoteOpcode;
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002075 unsigned RealDestReg = DestReg;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002076 switch (SrcTy->getPrimitiveID()) {
2077 case Type::BoolTyID:
2078 case Type::SByteTyID:
2079 // We don't have the facilities for directly loading byte sized data from
2080 // memory (even signed). Promote it to 16 bits.
2081 PromoteType = Type::ShortTy;
2082 PromoteOpcode = X86::MOVSXr16r8;
2083 break;
2084 case Type::UByteTyID:
2085 PromoteType = Type::ShortTy;
2086 PromoteOpcode = X86::MOVZXr16r8;
2087 break;
2088 case Type::UShortTyID:
2089 PromoteType = Type::IntTy;
2090 PromoteOpcode = X86::MOVZXr32r16;
2091 break;
2092 case Type::UIntTyID: {
2093 // Make a 64 bit temporary... and zero out the top of it...
2094 unsigned TmpReg = makeAnotherReg(Type::LongTy);
2095 BMI(BB, IP, X86::MOVrr32, 1, TmpReg).addReg(SrcReg);
Chris Lattner6e173a02004-02-17 06:16:44 +00002096 BMI(BB, IP, X86::MOVri32, 1, TmpReg+1).addZImm(0);
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002097 SrcTy = Type::LongTy;
2098 SrcClass = cLong;
2099 SrcReg = TmpReg;
2100 break;
2101 }
2102 case Type::ULongTyID:
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002103 // Don't fild into the read destination.
2104 DestReg = makeAnotherReg(Type::DoubleTy);
2105 break;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002106 default: // No promotion needed...
2107 break;
2108 }
2109
2110 if (PromoteType) {
2111 unsigned TmpReg = makeAnotherReg(PromoteType);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002112 unsigned Opc = SrcTy->isSigned() ? X86::MOVSXr16r8 : X86::MOVZXr16r8;
2113 BMI(BB, IP, Opc, 1, TmpReg).addReg(SrcReg);
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002114 SrcTy = PromoteType;
2115 SrcClass = getClass(PromoteType);
Chris Lattner3e130a22003-01-13 00:32:26 +00002116 SrcReg = TmpReg;
2117 }
2118
2119 // Spill the integer to memory and reload it from there...
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002120 int FrameIdx = F->getFrameInfo()->CreateStackObject(SrcTy, TM.getTargetData());
Chris Lattner3e130a22003-01-13 00:32:26 +00002121
2122 if (SrcClass == cLong) {
Chris Lattnere87331d2004-02-17 06:28:19 +00002123 addFrameReference(BMI(BB, IP, X86::MOVmr32, 5), FrameIdx).addReg(SrcReg);
2124 addFrameReference(BMI(BB, IP, X86::MOVmr32, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002125 FrameIdx, 4).addReg(SrcReg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00002126 } else {
Chris Lattnere87331d2004-02-17 06:28:19 +00002127 static const unsigned Op1[] = { X86::MOVmr8, X86::MOVmr16, X86::MOVmr32 };
Chris Lattner548f61d2003-04-23 17:22:12 +00002128 addFrameReference(BMI(BB, IP, Op1[SrcClass], 5), FrameIdx).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002129 }
2130
2131 static const unsigned Op2[] =
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002132 { 0/*byte*/, X86::FILDr16, X86::FILDr32, 0/*FP*/, X86::FILDr64 };
Chris Lattner548f61d2003-04-23 17:22:12 +00002133 addFrameReference(BMI(BB, IP, Op2[SrcClass], 5, DestReg), FrameIdx);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002134
2135 // We need special handling for unsigned 64-bit integer sources. If the
2136 // input number has the "sign bit" set, then we loaded it incorrectly as a
2137 // negative 64-bit number. In this case, add an offset value.
2138 if (SrcTy == Type::ULongTy) {
2139 // Emit a test instruction to see if the dynamic input value was signed.
2140 BMI(BB, IP, X86::TESTrr32, 2).addReg(SrcReg+1).addReg(SrcReg+1);
2141
2142 // If the sign bit is set, get a pointer to an offset, otherwise get a pointer to a zero.
2143 MachineConstantPool *CP = F->getConstantPool();
2144 unsigned Zero = makeAnotherReg(Type::IntTy);
2145 addConstantPoolReference(BMI(BB, IP, X86::LEAr32, 5, Zero),
2146 CP->getConstantPoolIndex(Constant::getNullValue(Type::UIntTy)));
2147 unsigned Offset = makeAnotherReg(Type::IntTy);
2148 addConstantPoolReference(BMI(BB, IP, X86::LEAr32, 5, Offset),
2149 CP->getConstantPoolIndex(ConstantUInt::get(Type::UIntTy,
2150 0x5f800000)));
2151 unsigned Addr = makeAnotherReg(Type::IntTy);
2152 BMI(BB, IP, X86::CMOVSrr32, 2, Addr).addReg(Zero).addReg(Offset);
2153
2154 // Load the constant for an add. FIXME: this could make an 'fadd' that
2155 // reads directly from memory, but we don't support these yet.
2156 unsigned ConstReg = makeAnotherReg(Type::DoubleTy);
2157 addDirectMem(BMI(BB, IP, X86::FLDr32, 4, ConstReg), Addr);
2158
2159 BMI(BB, IP, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(DestReg);
2160 }
2161
Chris Lattner3e130a22003-01-13 00:32:26 +00002162 return;
2163 }
2164
2165 // Handle casts from floating point to integer now...
2166 if (SrcClass == cFP) {
2167 // Change the floating point control register to use "round towards zero"
2168 // mode when truncating to an integer value.
2169 //
2170 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Chris Lattner548f61d2003-04-23 17:22:12 +00002171 addFrameReference(BMI(BB, IP, X86::FNSTCWm16, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00002172
2173 // Load the old value of the high byte of the control word...
2174 unsigned HighPartOfCW = makeAnotherReg(Type::UByteTy);
Chris Lattnere87331d2004-02-17 06:28:19 +00002175 addFrameReference(BMI(BB, IP, X86::MOVrm8, 4, HighPartOfCW), CWFrameIdx, 1);
Chris Lattner3e130a22003-01-13 00:32:26 +00002176
2177 // Set the high part to be round to zero...
Chris Lattner6e173a02004-02-17 06:16:44 +00002178 addFrameReference(BMI(BB, IP, X86::MOVmi8, 5), CWFrameIdx, 1).addZImm(12);
Chris Lattner3e130a22003-01-13 00:32:26 +00002179
2180 // Reload the modified control word now...
Chris Lattner548f61d2003-04-23 17:22:12 +00002181 addFrameReference(BMI(BB, IP, X86::FLDCWm16, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00002182
2183 // Restore the memory image of control word to original value
Chris Lattnere87331d2004-02-17 06:28:19 +00002184 addFrameReference(BMI(BB, IP, X86::MOVmr8, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002185 CWFrameIdx, 1).addReg(HighPartOfCW);
Chris Lattner3e130a22003-01-13 00:32:26 +00002186
2187 // We don't have the facilities for directly storing byte sized data to
2188 // memory. Promote it to 16 bits. We also must promote unsigned values to
2189 // larger classes because we only have signed FP stores.
2190 unsigned StoreClass = DestClass;
2191 const Type *StoreTy = DestTy;
2192 if (StoreClass == cByte || DestTy->isUnsigned())
2193 switch (StoreClass) {
2194 case cByte: StoreTy = Type::ShortTy; StoreClass = cShort; break;
2195 case cShort: StoreTy = Type::IntTy; StoreClass = cInt; break;
2196 case cInt: StoreTy = Type::LongTy; StoreClass = cLong; break;
Brian Gaeked4615052003-07-18 20:23:43 +00002197 // The following treatment of cLong may not be perfectly right,
2198 // but it survives chains of casts of the form
2199 // double->ulong->double.
2200 case cLong: StoreTy = Type::LongTy; StoreClass = cLong; break;
Chris Lattner3e130a22003-01-13 00:32:26 +00002201 default: assert(0 && "Unknown store class!");
2202 }
2203
2204 // Spill the integer to memory and reload it from there...
2205 int FrameIdx =
2206 F->getFrameInfo()->CreateStackObject(StoreTy, TM.getTargetData());
2207
2208 static const unsigned Op1[] =
2209 { 0, X86::FISTr16, X86::FISTr32, 0, X86::FISTPr64 };
Chris Lattner548f61d2003-04-23 17:22:12 +00002210 addFrameReference(BMI(BB, IP, Op1[StoreClass], 5), FrameIdx).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002211
2212 if (DestClass == cLong) {
Chris Lattnere87331d2004-02-17 06:28:19 +00002213 addFrameReference(BMI(BB, IP, X86::MOVrm32, 4, DestReg), FrameIdx);
2214 addFrameReference(BMI(BB, IP, X86::MOVrm32, 4, DestReg+1), FrameIdx, 4);
Chris Lattner3e130a22003-01-13 00:32:26 +00002215 } else {
Chris Lattnere87331d2004-02-17 06:28:19 +00002216 static const unsigned Op2[] = { X86::MOVrm8, X86::MOVrm16, X86::MOVrm32 };
Chris Lattner548f61d2003-04-23 17:22:12 +00002217 addFrameReference(BMI(BB, IP, Op2[DestClass], 4, DestReg), FrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00002218 }
2219
2220 // Reload the original control word now...
Chris Lattner548f61d2003-04-23 17:22:12 +00002221 addFrameReference(BMI(BB, IP, X86::FLDCWm16, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00002222 return;
2223 }
2224
Brian Gaeked474e9c2002-12-06 10:49:33 +00002225 // Anything we haven't handled already, we can't (yet) handle at all.
Chris Lattnerc53544a2003-05-12 20:16:58 +00002226 assert(0 && "Unhandled cast instruction!");
Chris Lattner548f61d2003-04-23 17:22:12 +00002227 abort();
Brian Gaekefa8d5712002-11-22 11:07:01 +00002228}
Brian Gaekea1719c92002-10-31 23:03:59 +00002229
Chris Lattner73815062003-10-18 05:56:40 +00002230/// visitVANextInst - Implement the va_next instruction...
Chris Lattnereca195e2003-05-08 19:44:13 +00002231///
Chris Lattner73815062003-10-18 05:56:40 +00002232void ISel::visitVANextInst(VANextInst &I) {
2233 unsigned VAList = getReg(I.getOperand(0));
Chris Lattnereca195e2003-05-08 19:44:13 +00002234 unsigned DestReg = getReg(I);
2235
Chris Lattnereca195e2003-05-08 19:44:13 +00002236 unsigned Size;
Chris Lattner73815062003-10-18 05:56:40 +00002237 switch (I.getArgType()->getPrimitiveID()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00002238 default:
2239 std::cerr << I;
Chris Lattner73815062003-10-18 05:56:40 +00002240 assert(0 && "Error: bad type for va_next instruction!");
Chris Lattnereca195e2003-05-08 19:44:13 +00002241 return;
2242 case Type::PointerTyID:
2243 case Type::UIntTyID:
2244 case Type::IntTyID:
2245 Size = 4;
Chris Lattnereca195e2003-05-08 19:44:13 +00002246 break;
2247 case Type::ULongTyID:
2248 case Type::LongTyID:
Chris Lattnereca195e2003-05-08 19:44:13 +00002249 case Type::DoubleTyID:
2250 Size = 8;
Chris Lattnereca195e2003-05-08 19:44:13 +00002251 break;
2252 }
2253
2254 // Increment the VAList pointer...
Chris Lattner73815062003-10-18 05:56:40 +00002255 BuildMI(BB, X86::ADDri32, 2, DestReg).addReg(VAList).addZImm(Size);
2256}
Chris Lattnereca195e2003-05-08 19:44:13 +00002257
Chris Lattner73815062003-10-18 05:56:40 +00002258void ISel::visitVAArgInst(VAArgInst &I) {
2259 unsigned VAList = getReg(I.getOperand(0));
2260 unsigned DestReg = getReg(I);
2261
2262 switch (I.getType()->getPrimitiveID()) {
2263 default:
2264 std::cerr << I;
2265 assert(0 && "Error: bad type for va_next instruction!");
2266 return;
2267 case Type::PointerTyID:
2268 case Type::UIntTyID:
2269 case Type::IntTyID:
Chris Lattnere87331d2004-02-17 06:28:19 +00002270 addDirectMem(BuildMI(BB, X86::MOVrm32, 4, DestReg), VAList);
Chris Lattner73815062003-10-18 05:56:40 +00002271 break;
2272 case Type::ULongTyID:
2273 case Type::LongTyID:
Chris Lattnere87331d2004-02-17 06:28:19 +00002274 addDirectMem(BuildMI(BB, X86::MOVrm32, 4, DestReg), VAList);
2275 addRegOffset(BuildMI(BB, X86::MOVrm32, 4, DestReg+1), VAList, 4);
Chris Lattner73815062003-10-18 05:56:40 +00002276 break;
2277 case Type::DoubleTyID:
2278 addDirectMem(BuildMI(BB, X86::FLDr64, 4, DestReg), VAList);
2279 break;
2280 }
Chris Lattnereca195e2003-05-08 19:44:13 +00002281}
2282
2283
Chris Lattner3e130a22003-01-13 00:32:26 +00002284void ISel::visitGetElementPtrInst(GetElementPtrInst &I) {
2285 unsigned outputReg = getReg(I);
Chris Lattner827832c2004-02-22 17:05:38 +00002286 emitGEPOperation(BB, BB->end(), I.getOperand(0),
Brian Gaeke68b1edc2002-12-16 04:23:29 +00002287 I.op_begin()+1, I.op_end(), outputReg);
Chris Lattnerc0812d82002-12-13 06:56:29 +00002288}
2289
Brian Gaeke71794c02002-12-13 11:22:48 +00002290void ISel::emitGEPOperation(MachineBasicBlock *MBB,
Chris Lattner827832c2004-02-22 17:05:38 +00002291 MachineBasicBlock::iterator IP,
Chris Lattner333b2fa2002-12-13 10:09:43 +00002292 Value *Src, User::op_iterator IdxBegin,
Chris Lattnerc0812d82002-12-13 06:56:29 +00002293 User::op_iterator IdxEnd, unsigned TargetReg) {
2294 const TargetData &TD = TM.getTargetData();
Chris Lattnerc0812d82002-12-13 06:56:29 +00002295
Chris Lattner7ca04092004-02-22 17:35:42 +00002296 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Src))
2297 Src = CPR->getValue();
2298
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002299 std::vector<Value*> GEPOps;
2300 GEPOps.resize(IdxEnd-IdxBegin+1);
2301 GEPOps[0] = Src;
2302 std::copy(IdxBegin, IdxEnd, GEPOps.begin()+1);
2303
2304 std::vector<const Type*> GEPTypes;
2305 GEPTypes.assign(gep_type_begin(Src->getType(), IdxBegin, IdxEnd),
2306 gep_type_end(Src->getType(), IdxBegin, IdxEnd));
2307
2308 // Keep emitting instructions until we consume the entire GEP instruction.
2309 while (!GEPOps.empty()) {
2310 unsigned OldSize = GEPOps.size();
2311
2312 if (GEPTypes.empty()) {
2313 // The getGEPIndex operation didn't want to build an LEA. Check to see if
2314 // all operands are consumed but the base pointer. If so, just load it
2315 // into the register.
Chris Lattner7ca04092004-02-22 17:35:42 +00002316 if (GlobalValue *GV = dyn_cast<GlobalValue>(GEPOps[0])) {
2317 BMI(MBB, IP, X86::MOVri32, 1, TargetReg).addGlobalAddress(GV);
2318 } else {
2319 unsigned BaseReg = getReg(GEPOps[0], MBB, IP);
2320 BMI(MBB, IP, X86::MOVrr32, 1, TargetReg).addReg(BaseReg);
2321 }
2322 break; // we are now done
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002323 } else if (const StructType *StTy = dyn_cast<StructType>(GEPTypes.back())) {
2324 // It's a struct access. CUI is the index into the structure,
2325 // which names the field. This index must have unsigned type.
2326 const ConstantUInt *CUI = cast<ConstantUInt>(GEPOps.back());
2327 GEPOps.pop_back(); // Consume a GEP operand
2328 GEPTypes.pop_back();
2329
2330 // Use the TargetData structure to pick out what the layout of the
2331 // structure is in memory. Since the structure index must be constant, we
2332 // can get its value and use it to find the right byte offset from the
2333 // StructLayout class's list of structure member offsets.
Chris Lattnere8f0d922002-12-24 00:03:11 +00002334 unsigned idxValue = CUI->getValue();
Chris Lattner3e130a22003-01-13 00:32:26 +00002335 unsigned FieldOff = TD.getStructLayout(StTy)->MemberOffsets[idxValue];
2336 if (FieldOff) {
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002337 unsigned Reg = makeAnotherReg(Type::UIntTy);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002338 // Emit an ADD to add FieldOff to the basePtr.
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002339 BMI(MBB, IP, X86::ADDri32, 2, TargetReg).addReg(Reg).addZImm(FieldOff);
2340 --IP; // Insert the next instruction before this one.
2341 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner3e130a22003-01-13 00:32:26 +00002342 }
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002343
2344 } else {
Brian Gaeke20244b72002-12-12 15:33:40 +00002345 // It's an array or pointer access: [ArraySize x ElementType].
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002346 const SequentialType *SqTy = cast<SequentialType>(GEPTypes.back());
2347 Value *idx = GEPOps.back();
2348 GEPOps.pop_back(); // Consume a GEP operand
2349 GEPTypes.pop_back();
Chris Lattner8a307e82002-12-16 19:32:50 +00002350
Brian Gaeke20244b72002-12-12 15:33:40 +00002351 // idx is the index into the array. Unlike with structure
2352 // indices, we may not know its actual value at code-generation
2353 // time.
Chris Lattner8a307e82002-12-16 19:32:50 +00002354 assert(idx->getType() == Type::LongTy && "Bad GEP array index!");
2355
Chris Lattnerf5854472003-06-21 16:01:24 +00002356 // Most GEP instructions use a [cast (int/uint) to LongTy] as their
2357 // operand on X86. Handle this case directly now...
2358 if (CastInst *CI = dyn_cast<CastInst>(idx))
2359 if (CI->getOperand(0)->getType() == Type::IntTy ||
2360 CI->getOperand(0)->getType() == Type::UIntTy)
2361 idx = CI->getOperand(0);
2362
Chris Lattner3e130a22003-01-13 00:32:26 +00002363 // We want to add BaseReg to(idxReg * sizeof ElementType). First, we
Chris Lattner8a307e82002-12-16 19:32:50 +00002364 // must find the size of the pointed-to type (Not coincidentally, the next
2365 // type is the type of the elements in the array).
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002366 const Type *ElTy = SqTy->getElementType();
2367 unsigned elementSize = TD.getTypeSize(ElTy);
Chris Lattner8a307e82002-12-16 19:32:50 +00002368
2369 // If idxReg is a constant, we don't need to perform the multiply!
2370 if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(idx)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002371 if (!CSI->isNullValue()) {
Chris Lattner8a307e82002-12-16 19:32:50 +00002372 unsigned Offset = elementSize*CSI->getValue();
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002373 unsigned Reg = makeAnotherReg(Type::UIntTy);
2374 BMI(MBB, IP, X86::ADDri32, 2, TargetReg).addReg(Reg).addZImm(Offset);
2375 --IP; // Insert the next instruction before this one.
2376 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00002377 }
2378 } else if (elementSize == 1) {
2379 // If the element size is 1, we don't have to multiply, just add
2380 unsigned idxReg = getReg(idx, MBB, IP);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002381 unsigned Reg = makeAnotherReg(Type::UIntTy);
2382 BMI(MBB, IP, X86::ADDrr32, 2, TargetReg).addReg(Reg).addReg(idxReg);
2383 --IP; // Insert the next instruction before this one.
2384 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00002385 } else {
2386 unsigned idxReg = getReg(idx, MBB, IP);
2387 unsigned OffsetReg = makeAnotherReg(Type::UIntTy);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002388
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002389 // Make sure we can back the iterator up to point to the first
2390 // instruction emitted.
2391 MachineBasicBlock::iterator BeforeIt = IP;
2392 if (IP == MBB->begin())
2393 BeforeIt = MBB->end();
2394 else
2395 --BeforeIt;
Chris Lattnerb2acc512003-10-19 21:09:10 +00002396 doMultiplyConst(MBB, IP, OffsetReg, Type::IntTy, idxReg, elementSize);
2397
Chris Lattner8a307e82002-12-16 19:32:50 +00002398 // Emit an ADD to add OffsetReg to the basePtr.
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002399 unsigned Reg = makeAnotherReg(Type::UIntTy);
2400 BMI(MBB, IP, X86::ADDrr32, 2, TargetReg).addReg(Reg).addReg(OffsetReg);
2401
2402 // Step to the first instruction of the multiply.
2403 if (BeforeIt == MBB->end())
2404 IP = MBB->begin();
2405 else
2406 IP = ++BeforeIt;
2407
2408 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00002409 }
Brian Gaeke20244b72002-12-12 15:33:40 +00002410 }
Brian Gaeke20244b72002-12-12 15:33:40 +00002411 }
Brian Gaeke20244b72002-12-12 15:33:40 +00002412}
2413
2414
Chris Lattner065faeb2002-12-28 20:24:02 +00002415/// visitAllocaInst - If this is a fixed size alloca, allocate space from the
2416/// frame manager, otherwise do it the hard way.
2417///
2418void ISel::visitAllocaInst(AllocaInst &I) {
Brian Gaekee48ec012002-12-13 06:46:31 +00002419 // Find the data size of the alloca inst's getAllocatedType.
Chris Lattner065faeb2002-12-28 20:24:02 +00002420 const Type *Ty = I.getAllocatedType();
2421 unsigned TySize = TM.getTargetData().getTypeSize(Ty);
2422
2423 // If this is a fixed size alloca in the entry block for the function,
2424 // statically stack allocate the space.
2425 //
2426 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(I.getArraySize())) {
2427 if (I.getParent() == I.getParent()->getParent()->begin()) {
2428 TySize *= CUI->getValue(); // Get total allocated size...
2429 unsigned Alignment = TM.getTargetData().getTypeAlignment(Ty);
2430
2431 // Create a new stack object using the frame manager...
2432 int FrameIdx = F->getFrameInfo()->CreateStackObject(TySize, Alignment);
2433 addFrameReference(BuildMI(BB, X86::LEAr32, 5, getReg(I)), FrameIdx);
2434 return;
2435 }
2436 }
2437
2438 // Create a register to hold the temporary result of multiplying the type size
2439 // constant by the variable amount.
2440 unsigned TotalSizeReg = makeAnotherReg(Type::UIntTy);
2441 unsigned SrcReg1 = getReg(I.getArraySize());
Chris Lattner065faeb2002-12-28 20:24:02 +00002442
2443 // TotalSizeReg = mul <numelements>, <TypeSize>
2444 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattnerb2acc512003-10-19 21:09:10 +00002445 doMultiplyConst(BB, MBBI, TotalSizeReg, Type::UIntTy, SrcReg1, TySize);
Chris Lattner065faeb2002-12-28 20:24:02 +00002446
2447 // AddedSize = add <TotalSizeReg>, 15
2448 unsigned AddedSizeReg = makeAnotherReg(Type::UIntTy);
2449 BuildMI(BB, X86::ADDri32, 2, AddedSizeReg).addReg(TotalSizeReg).addZImm(15);
2450
2451 // AlignedSize = and <AddedSize>, ~15
2452 unsigned AlignedSize = makeAnotherReg(Type::UIntTy);
2453 BuildMI(BB, X86::ANDri32, 2, AlignedSize).addReg(AddedSizeReg).addZImm(~15);
2454
Brian Gaekee48ec012002-12-13 06:46:31 +00002455 // Subtract size from stack pointer, thereby allocating some space.
Chris Lattner3e130a22003-01-13 00:32:26 +00002456 BuildMI(BB, X86::SUBrr32, 2, X86::ESP).addReg(X86::ESP).addReg(AlignedSize);
Chris Lattner065faeb2002-12-28 20:24:02 +00002457
Brian Gaekee48ec012002-12-13 06:46:31 +00002458 // Put a pointer to the space into the result register, by copying
2459 // the stack pointer.
Chris Lattner065faeb2002-12-28 20:24:02 +00002460 BuildMI(BB, X86::MOVrr32, 1, getReg(I)).addReg(X86::ESP);
2461
Misha Brukman48196b32003-05-03 02:18:17 +00002462 // Inform the Frame Information that we have just allocated a variable-sized
Chris Lattner065faeb2002-12-28 20:24:02 +00002463 // object.
2464 F->getFrameInfo()->CreateVariableSizedObject();
Brian Gaeke20244b72002-12-12 15:33:40 +00002465}
Chris Lattner3e130a22003-01-13 00:32:26 +00002466
2467/// visitMallocInst - Malloc instructions are code generated into direct calls
2468/// to the library malloc.
2469///
2470void ISel::visitMallocInst(MallocInst &I) {
2471 unsigned AllocSize = TM.getTargetData().getTypeSize(I.getAllocatedType());
2472 unsigned Arg;
2473
2474 if (ConstantUInt *C = dyn_cast<ConstantUInt>(I.getOperand(0))) {
2475 Arg = getReg(ConstantUInt::get(Type::UIntTy, C->getValue() * AllocSize));
2476 } else {
2477 Arg = makeAnotherReg(Type::UIntTy);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002478 unsigned Op0Reg = getReg(I.getOperand(0));
Chris Lattner3e130a22003-01-13 00:32:26 +00002479 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattnerb2acc512003-10-19 21:09:10 +00002480 doMultiplyConst(BB, MBBI, Arg, Type::UIntTy, Op0Reg, AllocSize);
Chris Lattner3e130a22003-01-13 00:32:26 +00002481 }
2482
2483 std::vector<ValueRecord> Args;
2484 Args.push_back(ValueRecord(Arg, Type::UIntTy));
2485 MachineInstr *TheCall = BuildMI(X86::CALLpcrel32,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002486 1).addExternalSymbol("malloc", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00002487 doCall(ValueRecord(getReg(I), I.getType()), TheCall, Args);
2488}
2489
2490
2491/// visitFreeInst - Free instructions are code gen'd to call the free libc
2492/// function.
2493///
2494void ISel::visitFreeInst(FreeInst &I) {
2495 std::vector<ValueRecord> Args;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00002496 Args.push_back(ValueRecord(I.getOperand(0)));
Chris Lattner3e130a22003-01-13 00:32:26 +00002497 MachineInstr *TheCall = BuildMI(X86::CALLpcrel32,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002498 1).addExternalSymbol("free", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00002499 doCall(ValueRecord(0, Type::VoidTy), TheCall, Args);
2500}
2501
Chris Lattnerd281de22003-07-26 23:49:58 +00002502/// createX86SimpleInstructionSelector - This pass converts an LLVM function
Chris Lattnerb4f68ed2002-10-29 22:37:54 +00002503/// into a machine code representation is a very simple peep-hole fashion. The
Chris Lattner72614082002-10-25 22:55:53 +00002504/// generated code sucks but the implementation is nice and simple.
2505///
Chris Lattnerf70e0c22003-12-28 21:23:38 +00002506FunctionPass *llvm::createX86SimpleInstructionSelector(TargetMachine &TM) {
2507 return new ISel(TM);
Chris Lattner72614082002-10-25 22:55:53 +00002508}