blob: 6b87f7ecf6b3e0bb2bf1d93721ed8d0d0d73eecc [file] [log] [blame]
Chris Lattner72614082002-10-25 22:55:53 +00001//===-- InstSelectSimple.cpp - A simple instruction selector for x86 ------===//
2//
3// This file defines a simple peephole instruction selector for the x86 platform
4//
5//===----------------------------------------------------------------------===//
6
7#include "X86.h"
Chris Lattner055c9652002-10-29 21:05:24 +00008#include "X86InstrInfo.h"
Chris Lattner6fc3c522002-11-17 21:11:55 +00009#include "X86InstrBuilder.h"
Chris Lattner72614082002-10-25 22:55:53 +000010#include "llvm/Function.h"
11#include "llvm/iTerminators.h"
Brian Gaeke1749d632002-11-07 17:59:21 +000012#include "llvm/iOperators.h"
Brian Gaekea1719c92002-10-31 23:03:59 +000013#include "llvm/iOther.h"
Chris Lattner51b49a92002-11-02 19:45:49 +000014#include "llvm/iPHINode.h"
Chris Lattner6fc3c522002-11-17 21:11:55 +000015#include "llvm/iMemory.h"
Chris Lattner72614082002-10-25 22:55:53 +000016#include "llvm/Type.h"
Brian Gaeke20244b72002-12-12 15:33:40 +000017#include "llvm/DerivedTypes.h"
Chris Lattnerc5291f52002-10-27 21:16:59 +000018#include "llvm/Constants.h"
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000019#include "llvm/Pass.h"
Chris Lattner341a9372002-10-29 17:43:55 +000020#include "llvm/CodeGen/MachineFunction.h"
Misha Brukmand2cc0172002-11-20 00:58:23 +000021#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner94af4142002-12-25 05:13:53 +000022#include "llvm/CodeGen/SSARegMap.h"
Misha Brukmand2cc0172002-11-20 00:58:23 +000023#include "llvm/Target/TargetMachine.h"
Chris Lattner72614082002-10-25 22:55:53 +000024#include "llvm/Support/InstVisitor.h"
Misha Brukmand2cc0172002-11-20 00:58:23 +000025#include "llvm/Target/MRegisterInfo.h"
26#include <map>
Chris Lattner72614082002-10-25 22:55:53 +000027
Chris Lattner333b2fa2002-12-13 10:09:43 +000028/// BMI - A special BuildMI variant that takes an iterator to insert the
29/// instruction at as well as a basic block.
Brian Gaeke71794c02002-12-13 11:22:48 +000030/// this is the version for when you have a destination register in mind.
31inline static MachineInstrBuilder BMI(MachineBasicBlock *MBB,
Chris Lattner333b2fa2002-12-13 10:09:43 +000032 MachineBasicBlock::iterator &I,
33 MachineOpCode Opcode,
34 unsigned NumOperands,
35 unsigned DestReg) {
Chris Lattnerd7d38722002-12-13 13:04:04 +000036 assert(I >= MBB->begin() && I <= MBB->end() && "Bad iterator!");
Chris Lattner333b2fa2002-12-13 10:09:43 +000037 MachineInstr *MI = new MachineInstr(Opcode, NumOperands+1, true, true);
Chris Lattnere8f0d922002-12-24 00:03:11 +000038 I = MBB->insert(I, MI)+1;
Chris Lattner333b2fa2002-12-13 10:09:43 +000039 return MachineInstrBuilder(MI).addReg(DestReg, MOTy::Def);
40}
41
Chris Lattnerf08ad9f2002-12-13 10:50:40 +000042/// BMI - A special BuildMI variant that takes an iterator to insert the
43/// instruction at as well as a basic block.
Brian Gaeke71794c02002-12-13 11:22:48 +000044inline static MachineInstrBuilder BMI(MachineBasicBlock *MBB,
Chris Lattnerf08ad9f2002-12-13 10:50:40 +000045 MachineBasicBlock::iterator &I,
46 MachineOpCode Opcode,
47 unsigned NumOperands) {
Chris Lattnerd7d38722002-12-13 13:04:04 +000048 assert(I > MBB->begin() && I <= MBB->end() && "Bad iterator!");
Chris Lattnerf08ad9f2002-12-13 10:50:40 +000049 MachineInstr *MI = new MachineInstr(Opcode, NumOperands, true, true);
Chris Lattnere8f0d922002-12-24 00:03:11 +000050 I = MBB->insert(I, MI)+1;
Chris Lattnerf08ad9f2002-12-13 10:50:40 +000051 return MachineInstrBuilder(MI);
52}
53
Chris Lattner333b2fa2002-12-13 10:09:43 +000054
Chris Lattner72614082002-10-25 22:55:53 +000055namespace {
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000056 struct ISel : public FunctionPass, InstVisitor<ISel> {
57 TargetMachine &TM;
Chris Lattner341a9372002-10-29 17:43:55 +000058 MachineFunction *F; // The function we are compiling into
59 MachineBasicBlock *BB; // The current MBB we are compiling
Chris Lattner72614082002-10-25 22:55:53 +000060
61 unsigned CurReg;
62 std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs
63
Chris Lattner333b2fa2002-12-13 10:09:43 +000064 // MBBMap - Mapping between LLVM BB -> Machine BB
65 std::map<const BasicBlock*, MachineBasicBlock*> MBBMap;
66
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000067 ISel(TargetMachine &tm)
68 : TM(tm), F(0), BB(0), CurReg(MRegisterInfo::FirstVirtualRegister) {}
Chris Lattner72614082002-10-25 22:55:53 +000069
70 /// runOnFunction - Top level implementation of instruction selection for
71 /// the entire function.
72 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000073 bool runOnFunction(Function &Fn) {
Chris Lattner36b36032002-10-29 23:40:58 +000074 F = &MachineFunction::construct(&Fn, TM);
Chris Lattner333b2fa2002-12-13 10:09:43 +000075
76 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
77 F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I));
78
Chris Lattner14aa7fe2002-12-16 22:54:46 +000079 // Emit instructions to load the arguments... The function's arguments
80 // look like this:
81 //
82 // [EBP] -- copy of old EBP
83 // [EBP + 4] -- return address
84 // [EBP + 8] -- first argument (leftmost lexically)
85 //
86 // So we want to start with counter = 2.
87 //
88 BB = &F->front();
89 unsigned ArgOffset = 8;
90 for (Function::aiterator I = Fn.abegin(), E = Fn.aend(); I != E;
91 ++I, ArgOffset += 4) {
92 unsigned Reg = getReg(*I);
93
94 // Load it out of the stack frame at EBP + 4*argPos.
95
96 // FIXME: This should load the argument of the appropriate size!!
97 addRegOffset(BuildMI(BB, X86::MOVmr32, 4, Reg), X86::EBP, ArgOffset);
98 }
99
Chris Lattner333b2fa2002-12-13 10:09:43 +0000100 // Instruction select everything except PHI nodes
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000101 visit(Fn);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000102
103 // Select the PHI nodes
104 SelectPHINodes();
105
Chris Lattner72614082002-10-25 22:55:53 +0000106 RegMap.clear();
Chris Lattner333b2fa2002-12-13 10:09:43 +0000107 MBBMap.clear();
Chris Lattner94e8ee22002-11-21 17:26:58 +0000108 CurReg = MRegisterInfo::FirstVirtualRegister;
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000109 F = 0;
Chris Lattner72614082002-10-25 22:55:53 +0000110 return false; // We never modify the LLVM itself.
111 }
112
Chris Lattnerf0eb7be2002-12-15 21:13:40 +0000113 virtual const char *getPassName() const {
114 return "X86 Simple Instruction Selection";
115 }
116
Chris Lattner72614082002-10-25 22:55:53 +0000117 /// visitBasicBlock - This method is called when we are visiting a new basic
Chris Lattner33f53b52002-10-29 20:48:56 +0000118 /// block. This simply creates a new MachineBasicBlock to emit code into
119 /// and adds it to the current MachineFunction. Subsequent visit* for
120 /// instructions will be invoked for all instructions in the basic block.
Chris Lattner72614082002-10-25 22:55:53 +0000121 ///
122 void visitBasicBlock(BasicBlock &LLVM_BB) {
Chris Lattner333b2fa2002-12-13 10:09:43 +0000123 BB = MBBMap[&LLVM_BB];
Chris Lattner72614082002-10-25 22:55:53 +0000124 }
125
Chris Lattner333b2fa2002-12-13 10:09:43 +0000126
127 /// SelectPHINodes - Insert machine code to generate phis. This is tricky
128 /// because we have to generate our sources into the source basic blocks,
129 /// not the current one.
130 ///
131 void SelectPHINodes();
132
Chris Lattner72614082002-10-25 22:55:53 +0000133 // Visitation methods for various instructions. These methods simply emit
134 // fixed X86 code for each instruction.
135 //
Brian Gaekefa8d5712002-11-22 11:07:01 +0000136
137 // Control flow operators
Chris Lattner72614082002-10-25 22:55:53 +0000138 void visitReturnInst(ReturnInst &RI);
Chris Lattner2df035b2002-11-02 19:27:56 +0000139 void visitBranchInst(BranchInst &BI);
Brian Gaekefa8d5712002-11-22 11:07:01 +0000140 void visitCallInst(CallInst &I);
Chris Lattnere2954c82002-11-02 20:04:26 +0000141
142 // Arithmetic operators
Chris Lattnerf01729e2002-11-02 20:54:46 +0000143 void visitSimpleBinary(BinaryOperator &B, unsigned OpcodeClass);
Chris Lattner68aad932002-11-02 20:13:22 +0000144 void visitAdd(BinaryOperator &B) { visitSimpleBinary(B, 0); }
145 void visitSub(BinaryOperator &B) { visitSimpleBinary(B, 1); }
Chris Lattner8a307e82002-12-16 19:32:50 +0000146 void doMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator &MBBI,
147 unsigned destReg, const Type *resultType,
148 unsigned op0Reg, unsigned op1Reg);
Chris Lattnerca9671d2002-11-02 20:28:58 +0000149 void visitMul(BinaryOperator &B);
Chris Lattnere2954c82002-11-02 20:04:26 +0000150
Chris Lattnerf01729e2002-11-02 20:54:46 +0000151 void visitDiv(BinaryOperator &B) { visitDivRem(B); }
152 void visitRem(BinaryOperator &B) { visitDivRem(B); }
153 void visitDivRem(BinaryOperator &B);
154
Chris Lattnere2954c82002-11-02 20:04:26 +0000155 // Bitwise operators
Chris Lattner68aad932002-11-02 20:13:22 +0000156 void visitAnd(BinaryOperator &B) { visitSimpleBinary(B, 2); }
157 void visitOr (BinaryOperator &B) { visitSimpleBinary(B, 3); }
158 void visitXor(BinaryOperator &B) { visitSimpleBinary(B, 4); }
Chris Lattnere2954c82002-11-02 20:04:26 +0000159
160 // Binary comparison operators
Chris Lattner05093a52002-11-21 15:52:38 +0000161 void visitSetCCInst(SetCondInst &I, unsigned OpNum);
162 void visitSetEQ(SetCondInst &I) { visitSetCCInst(I, 0); }
163 void visitSetNE(SetCondInst &I) { visitSetCCInst(I, 1); }
164 void visitSetLT(SetCondInst &I) { visitSetCCInst(I, 2); }
165 void visitSetGT(SetCondInst &I) { visitSetCCInst(I, 3); }
166 void visitSetLE(SetCondInst &I) { visitSetCCInst(I, 4); }
167 void visitSetGE(SetCondInst &I) { visitSetCCInst(I, 5); }
Chris Lattner6fc3c522002-11-17 21:11:55 +0000168
169 // Memory Instructions
170 void visitLoadInst(LoadInst &I);
171 void visitStoreInst(StoreInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000172 void visitGetElementPtrInst(GetElementPtrInst &I);
173 void visitMallocInst(MallocInst &I);
Brian Gaekee48ec012002-12-13 06:46:31 +0000174 void visitFreeInst(FreeInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000175 void visitAllocaInst(AllocaInst &I);
176
Chris Lattnere2954c82002-11-02 20:04:26 +0000177 // Other operators
Brian Gaekea1719c92002-10-31 23:03:59 +0000178 void visitShiftInst(ShiftInst &I);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000179 void visitPHINode(PHINode &I) {} // PHI nodes handled by second pass
Brian Gaekefa8d5712002-11-22 11:07:01 +0000180 void visitCastInst(CastInst &I);
Chris Lattner72614082002-10-25 22:55:53 +0000181
182 void visitInstruction(Instruction &I) {
183 std::cerr << "Cannot instruction select: " << I;
184 abort();
185 }
186
Brian Gaeke95780cc2002-12-13 07:56:18 +0000187 /// promote32 - Make a value 32-bits wide, and put it somewhere.
188 void promote32 (const unsigned targetReg, Value *v);
189
190 // emitGEPOperation - Common code shared between visitGetElementPtrInst and
Chris Lattnerc0812d82002-12-13 06:56:29 +0000191 // constant expression GEP support.
192 //
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000193 void emitGEPOperation(MachineBasicBlock *BB, MachineBasicBlock::iterator&IP,
Chris Lattner333b2fa2002-12-13 10:09:43 +0000194 Value *Src, User::op_iterator IdxBegin,
Chris Lattnerc0812d82002-12-13 06:56:29 +0000195 User::op_iterator IdxEnd, unsigned TargetReg);
196
Chris Lattnerc5291f52002-10-27 21:16:59 +0000197 /// copyConstantToRegister - Output the instructions required to put the
198 /// specified constant into the specified register.
199 ///
Chris Lattner8a307e82002-12-16 19:32:50 +0000200 void copyConstantToRegister(MachineBasicBlock *MBB,
201 MachineBasicBlock::iterator &MBBI,
202 Constant *C, unsigned Reg);
Chris Lattnerc5291f52002-10-27 21:16:59 +0000203
Brian Gaeke20244b72002-12-12 15:33:40 +0000204 /// makeAnotherReg - This method returns the next register number
205 /// we haven't yet used.
Chris Lattnerc0812d82002-12-13 06:56:29 +0000206 unsigned makeAnotherReg(const Type *Ty) {
207 // Add the mapping of regnumber => reg class to MachineFunction
Chris Lattner94af4142002-12-25 05:13:53 +0000208 const TargetRegisterClass *RC =
209 TM.getRegisterInfo()->getRegClassForType(Ty);
210 F->getSSARegMap()->addRegMap(CurReg, RC);
Chris Lattnerc0812d82002-12-13 06:56:29 +0000211 return CurReg++;
Brian Gaeke20244b72002-12-12 15:33:40 +0000212 }
213
Chris Lattner72614082002-10-25 22:55:53 +0000214 /// getReg - This method turns an LLVM value into a register number. This
215 /// is guaranteed to produce the same register number for a particular value
216 /// every time it is queried.
217 ///
218 unsigned getReg(Value &V) { return getReg(&V); } // Allow references
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000219 unsigned getReg(Value *V) {
220 // Just append to the end of the current bb.
221 MachineBasicBlock::iterator It = BB->end();
222 return getReg(V, BB, It);
223 }
Brian Gaeke71794c02002-12-13 11:22:48 +0000224 unsigned getReg(Value *V, MachineBasicBlock *MBB,
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000225 MachineBasicBlock::iterator &IPt) {
Chris Lattner72614082002-10-25 22:55:53 +0000226 unsigned &Reg = RegMap[V];
Misha Brukmand2cc0172002-11-20 00:58:23 +0000227 if (Reg == 0) {
Chris Lattnerc0812d82002-12-13 06:56:29 +0000228 Reg = makeAnotherReg(V->getType());
Misha Brukmand2cc0172002-11-20 00:58:23 +0000229 RegMap[V] = Reg;
Misha Brukmand2cc0172002-11-20 00:58:23 +0000230 }
Chris Lattner72614082002-10-25 22:55:53 +0000231
Chris Lattner6f8fd252002-10-27 21:23:43 +0000232 // If this operand is a constant, emit the code to copy the constant into
233 // the register here...
234 //
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000235 if (Constant *C = dyn_cast<Constant>(V)) {
Chris Lattner8a307e82002-12-16 19:32:50 +0000236 copyConstantToRegister(MBB, IPt, C, Reg);
Chris Lattner14aa7fe2002-12-16 22:54:46 +0000237 RegMap.erase(V); // Assign a new name to this constant if ref'd again
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000238 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
239 // Move the address of the global into the register
Brian Gaeke71794c02002-12-13 11:22:48 +0000240 BMI(MBB, IPt, X86::MOVir32, 1, Reg).addReg(GV);
Chris Lattner14aa7fe2002-12-16 22:54:46 +0000241 RegMap.erase(V); // Assign a new name to this address if ref'd again
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000242 }
Chris Lattnerc5291f52002-10-27 21:16:59 +0000243
Chris Lattner72614082002-10-25 22:55:53 +0000244 return Reg;
245 }
Chris Lattner72614082002-10-25 22:55:53 +0000246 };
247}
248
Chris Lattner43189d12002-11-17 20:07:45 +0000249/// TypeClass - Used by the X86 backend to group LLVM types by their basic X86
250/// Representation.
251///
252enum TypeClass {
Chris Lattner94af4142002-12-25 05:13:53 +0000253 cByte, cShort, cInt, cFP, cLong
Chris Lattner43189d12002-11-17 20:07:45 +0000254};
255
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000256/// getClass - Turn a primitive type into a "class" number which is based on the
257/// size of the type, and whether or not it is floating point.
258///
Chris Lattner43189d12002-11-17 20:07:45 +0000259static inline TypeClass getClass(const Type *Ty) {
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000260 switch (Ty->getPrimitiveID()) {
261 case Type::SByteTyID:
Chris Lattner43189d12002-11-17 20:07:45 +0000262 case Type::UByteTyID: return cByte; // Byte operands are class #0
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000263 case Type::ShortTyID:
Chris Lattner43189d12002-11-17 20:07:45 +0000264 case Type::UShortTyID: return cShort; // Short operands are class #1
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000265 case Type::IntTyID:
266 case Type::UIntTyID:
Chris Lattner43189d12002-11-17 20:07:45 +0000267 case Type::PointerTyID: return cInt; // Int's and pointers are class #2
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000268
Chris Lattner94af4142002-12-25 05:13:53 +0000269 case Type::FloatTyID:
270 case Type::DoubleTyID: return cFP; // Floating Point is #3
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000271 case Type::LongTyID:
Chris Lattnerc0812d82002-12-13 06:56:29 +0000272 case Type::ULongTyID: //return cLong; // Longs are class #3
273 return cInt; // FIXME: LONGS ARE TREATED AS INTS!
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000274 default:
275 assert(0 && "Invalid type to getClass!");
Chris Lattner43189d12002-11-17 20:07:45 +0000276 return cByte; // not reached
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000277 }
278}
Chris Lattnerc5291f52002-10-27 21:16:59 +0000279
Chris Lattner6b993cc2002-12-15 08:02:15 +0000280// getClassB - Just like getClass, but treat boolean values as bytes.
281static inline TypeClass getClassB(const Type *Ty) {
282 if (Ty == Type::BoolTy) return cByte;
283 return getClass(Ty);
284}
285
Chris Lattner06925362002-11-17 21:56:38 +0000286
Chris Lattnerc5291f52002-10-27 21:16:59 +0000287/// copyConstantToRegister - Output the instructions required to put the
288/// specified constant into the specified register.
289///
Chris Lattner8a307e82002-12-16 19:32:50 +0000290void ISel::copyConstantToRegister(MachineBasicBlock *MBB,
291 MachineBasicBlock::iterator &IP,
292 Constant *C, unsigned R) {
Chris Lattnerc0812d82002-12-13 06:56:29 +0000293 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
294 if (CE->getOpcode() == Instruction::GetElementPtr) {
Brian Gaeke68b1edc2002-12-16 04:23:29 +0000295 emitGEPOperation(MBB, IP, CE->getOperand(0),
Chris Lattner333b2fa2002-12-13 10:09:43 +0000296 CE->op_begin()+1, CE->op_end(), R);
Chris Lattnerc0812d82002-12-13 06:56:29 +0000297 return;
298 }
299
Brian Gaeke20244b72002-12-12 15:33:40 +0000300 std::cerr << "Offending expr: " << C << "\n";
Chris Lattner94af4142002-12-25 05:13:53 +0000301 assert(0 && "Constant expressions not yet handled!\n");
Brian Gaeke20244b72002-12-12 15:33:40 +0000302 }
Chris Lattnerc5291f52002-10-27 21:16:59 +0000303
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000304 if (C->getType()->isIntegral()) {
Chris Lattner6b993cc2002-12-15 08:02:15 +0000305 unsigned Class = getClassB(C->getType());
Chris Lattner94af4142002-12-25 05:13:53 +0000306 assert(Class <= cInt && "Type not handled yet!");
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000307
308 static const unsigned IntegralOpcodeTab[] = {
309 X86::MOVir8, X86::MOVir16, X86::MOVir32
310 };
311
Chris Lattner6b993cc2002-12-15 08:02:15 +0000312 if (C->getType() == Type::BoolTy) {
313 BMI(MBB, IP, X86::MOVir8, 1, R).addZImm(C == ConstantBool::True);
314 } else if (C->getType()->isSigned()) {
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000315 ConstantSInt *CSI = cast<ConstantSInt>(C);
Brian Gaeke71794c02002-12-13 11:22:48 +0000316 BMI(MBB, IP, IntegralOpcodeTab[Class], 1, R).addSImm(CSI->getValue());
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000317 } else {
318 ConstantUInt *CUI = cast<ConstantUInt>(C);
Brian Gaeke71794c02002-12-13 11:22:48 +0000319 BMI(MBB, IP, IntegralOpcodeTab[Class], 1, R).addZImm(CUI->getValue());
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000320 }
Chris Lattner94af4142002-12-25 05:13:53 +0000321 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
322 double Value = CFP->getValue();
323 if (Value == +0.0)
324 BMI(MBB, IP, X86::FLD0, 0, R);
325 else if (Value == +1.0)
326 BMI(MBB, IP, X86::FLD1, 0, R);
327 else {
328 std::cerr << "Cannot load constant '" << Value << "'!\n";
329 assert(0);
330 }
331
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000332 } else if (isa<ConstantPointerNull>(C)) {
Brian Gaeke20244b72002-12-12 15:33:40 +0000333 // Copy zero (null pointer) to the register.
Brian Gaeke71794c02002-12-13 11:22:48 +0000334 BMI(MBB, IP, X86::MOVir32, 1, R).addZImm(0);
Chris Lattnerc0812d82002-12-13 06:56:29 +0000335 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)) {
Brian Gaeke68b1edc2002-12-16 04:23:29 +0000336 unsigned SrcReg = getReg(CPR->getValue(), MBB, IP);
Brian Gaeke71794c02002-12-13 11:22:48 +0000337 BMI(MBB, IP, X86::MOVrr32, 1, R).addReg(SrcReg);
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000338 } else {
Brian Gaeke20244b72002-12-12 15:33:40 +0000339 std::cerr << "Offending constant: " << C << "\n";
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000340 assert(0 && "Type not handled yet!");
Chris Lattnerc5291f52002-10-27 21:16:59 +0000341 }
342}
343
Chris Lattner333b2fa2002-12-13 10:09:43 +0000344/// SelectPHINodes - Insert machine code to generate phis. This is tricky
345/// because we have to generate our sources into the source basic blocks, not
346/// the current one.
347///
348void ISel::SelectPHINodes() {
349 const Function &LF = *F->getFunction(); // The LLVM function...
350 for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) {
351 const BasicBlock *BB = I;
352 MachineBasicBlock *MBB = MBBMap[I];
353
354 // Loop over all of the PHI nodes in the LLVM basic block...
355 unsigned NumPHIs = 0;
356 for (BasicBlock::const_iterator I = BB->begin();
357 PHINode *PN = (PHINode*)dyn_cast<PHINode>(&*I); ++I) {
358 // Create a new machine instr PHI node, and insert it.
359 MachineInstr *MI = BuildMI(X86::PHI, PN->getNumOperands(), getReg(*PN));
360 MBB->insert(MBB->begin()+NumPHIs++, MI); // Insert it at the top of the BB
361
362 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
363 MachineBasicBlock *PredMBB = MBBMap[PN->getIncomingBlock(i)];
364
365 // Get the incoming value into a virtual register. If it is not already
366 // available in a virtual register, insert the computation code into
367 // PredMBB
Chris Lattner92053632002-12-13 11:52:34 +0000368 //
369
370 MachineBasicBlock::iterator PI = PredMBB->begin();
371 while ((*PI)->getOpcode() == X86::PHI) ++PI;
372
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000373 MI->addRegOperand(getReg(PN->getIncomingValue(i), PredMBB, PI));
Chris Lattner6b993cc2002-12-15 08:02:15 +0000374 MI->addMachineBasicBlockOperand(PredMBB);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000375 }
376 }
377 }
378}
379
380
Chris Lattner06925362002-11-17 21:56:38 +0000381
Brian Gaeke1749d632002-11-07 17:59:21 +0000382/// SetCC instructions - Here we just emit boilerplate code to set a byte-sized
383/// register, then move it to wherever the result should be.
384/// We handle FP setcc instructions by pushing them, doing a
385/// compare-and-pop-twice, and then copying the concodes to the main
386/// processor's concodes (I didn't make this up, it's in the Intel manual)
387///
Chris Lattner05093a52002-11-21 15:52:38 +0000388void ISel::visitSetCCInst(SetCondInst &I, unsigned OpNum) {
Brian Gaeke1749d632002-11-07 17:59:21 +0000389 // The arguments are already supposed to be of the same type.
Chris Lattner05093a52002-11-21 15:52:38 +0000390 const Type *CompTy = I.getOperand(0)->getType();
391 unsigned reg1 = getReg(I.getOperand(0));
392 unsigned reg2 = getReg(I.getOperand(1));
393
394 unsigned Class = getClass(CompTy);
395 switch (Class) {
396 // Emit: cmp <var1>, <var2> (do the comparison). We can
397 // compare 8-bit with 8-bit, 16-bit with 16-bit, 32-bit with
398 // 32-bit.
399 case cByte:
400 BuildMI (BB, X86::CMPrr8, 2).addReg (reg1).addReg (reg2);
401 break;
402 case cShort:
403 BuildMI (BB, X86::CMPrr16, 2).addReg (reg1).addReg (reg2);
404 break;
405 case cInt:
406 BuildMI (BB, X86::CMPrr32, 2).addReg (reg1).addReg (reg2);
407 break;
408
Chris Lattner94af4142002-12-25 05:13:53 +0000409#if 0
Chris Lattner05093a52002-11-21 15:52:38 +0000410 // Push the variables on the stack with fldl opcodes.
411 // FIXME: assuming var1, var2 are in memory, if not, spill to
412 // stack first
Chris Lattner94af4142002-12-25 05:13:53 +0000413 case cFP: // Floats
Brian Gaeke20244b72002-12-12 15:33:40 +0000414 BuildMI (BB, X86::FLDr32, 1).addReg (reg1);
415 BuildMI (BB, X86::FLDr32, 1).addReg (reg2);
Chris Lattner05093a52002-11-21 15:52:38 +0000416 break;
Chris Lattner94af4142002-12-25 05:13:53 +0000417 case cFP (doubles): // Doubles
Brian Gaeke20244b72002-12-12 15:33:40 +0000418 BuildMI (BB, X86::FLDr64, 1).addReg (reg1);
419 BuildMI (BB, X86::FLDr64, 1).addReg (reg2);
Chris Lattner05093a52002-11-21 15:52:38 +0000420 break;
Chris Lattner94af4142002-12-25 05:13:53 +0000421#endif
Chris Lattner05093a52002-11-21 15:52:38 +0000422 case cLong:
423 default:
424 visitInstruction(I);
425 }
426
Chris Lattner94af4142002-12-25 05:13:53 +0000427#if 0
Chris Lattner05093a52002-11-21 15:52:38 +0000428 if (CompTy->isFloatingPoint()) {
429 // (Non-trapping) compare and pop twice.
430 BuildMI (BB, X86::FUCOMPP, 0);
431 // Move fp status word (concodes) to ax.
432 BuildMI (BB, X86::FNSTSWr8, 1, X86::AX);
433 // Load real concodes from ax.
434 BuildMI (BB, X86::SAHF, 1).addReg(X86::AH);
435 }
Chris Lattner94af4142002-12-25 05:13:53 +0000436#endif
Chris Lattner05093a52002-11-21 15:52:38 +0000437
Brian Gaeke1749d632002-11-07 17:59:21 +0000438 // Emit setOp instruction (extract concode; clobbers ax),
439 // using the following mapping:
440 // LLVM -> X86 signed X86 unsigned
441 // ----- ----- -----
442 // seteq -> sete sete
443 // setne -> setne setne
444 // setlt -> setl setb
445 // setgt -> setg seta
446 // setle -> setle setbe
447 // setge -> setge setae
Chris Lattner05093a52002-11-21 15:52:38 +0000448
449 static const unsigned OpcodeTab[2][6] = {
Chris Lattner4b4e9dd2002-11-21 16:19:42 +0000450 {X86::SETEr, X86::SETNEr, X86::SETBr, X86::SETAr, X86::SETBEr, X86::SETAEr},
451 {X86::SETEr, X86::SETNEr, X86::SETLr, X86::SETGr, X86::SETLEr, X86::SETGEr},
Chris Lattner05093a52002-11-21 15:52:38 +0000452 };
453
Chris Lattner89fab072002-12-18 01:44:31 +0000454 BuildMI(BB, OpcodeTab[CompTy->isSigned()][OpNum], 0, getReg(I));
Brian Gaeke1749d632002-11-07 17:59:21 +0000455}
Chris Lattner51b49a92002-11-02 19:45:49 +0000456
Brian Gaekec2505982002-11-30 11:57:28 +0000457/// promote32 - Emit instructions to turn a narrow operand into a 32-bit-wide
458/// operand, in the specified target register.
Chris Lattner94af4142002-12-25 05:13:53 +0000459void ISel::promote32 (unsigned targetReg, Value *v) {
460 unsigned vReg = getReg(v);
461 bool isUnsigned = v->getType()->isUnsigned();
462 switch (getClass(v->getType())) {
463 case cByte:
464 // Extend value into target register (8->32)
465 if (isUnsigned)
466 BuildMI(BB, X86::MOVZXr32r8, 1, targetReg).addReg(vReg);
467 else
468 BuildMI(BB, X86::MOVSXr32r8, 1, targetReg).addReg(vReg);
469 break;
470 case cShort:
471 // Extend value into target register (16->32)
472 if (isUnsigned)
473 BuildMI(BB, X86::MOVZXr32r16, 1, targetReg).addReg(vReg);
474 else
475 BuildMI(BB, X86::MOVSXr32r16, 1, targetReg).addReg(vReg);
476 break;
477 case cInt:
478 // Move value into target register (32->32)
479 BuildMI(BB, X86::MOVrr32, 1, targetReg).addReg(vReg);
480 break;
481 default:
482 assert(0 && "Unpromotable operand class in promote32");
483 }
Brian Gaekec2505982002-11-30 11:57:28 +0000484}
Chris Lattnerc5291f52002-10-27 21:16:59 +0000485
Chris Lattner72614082002-10-25 22:55:53 +0000486/// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such,
487/// we have the following possibilities:
488///
489/// ret void: No return value, simply emit a 'ret' instruction
490/// ret sbyte, ubyte : Extend value into EAX and return
491/// ret short, ushort: Extend value into EAX and return
492/// ret int, uint : Move value into EAX and return
493/// ret pointer : Move value into EAX and return
Chris Lattner06925362002-11-17 21:56:38 +0000494/// ret long, ulong : Move value into EAX/EDX and return
495/// ret float/double : Top of FP stack
Chris Lattner72614082002-10-25 22:55:53 +0000496///
Chris Lattner94af4142002-12-25 05:13:53 +0000497void ISel::visitReturnInst (ReturnInst &I) {
498 if (I.getNumOperands() == 0) {
499 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
500 return;
501 }
502
503 Value *RetVal = I.getOperand(0);
504 switch (getClass(RetVal->getType())) {
505 case cByte: // integral return values: extend or move into EAX and return
506 case cShort:
507 case cInt:
508 promote32(X86::EAX, RetVal);
509 break;
510 case cFP: // Floats & Doubles: Return in ST(0)
511 BuildMI(BB, X86::FpMOV, 1, X86::ST0).addReg(getReg(RetVal));
512 break;
513 case cLong:
514 // ret long: use EAX(least significant 32 bits)/EDX (most
515 // significant 32)...
516 default:
517 visitInstruction (I);
518 }
Chris Lattner43189d12002-11-17 20:07:45 +0000519 // Emit a 'ret' instruction
Chris Lattner94af4142002-12-25 05:13:53 +0000520 BuildMI(BB, X86::RET, 0);
Chris Lattner72614082002-10-25 22:55:53 +0000521}
522
Chris Lattner51b49a92002-11-02 19:45:49 +0000523/// visitBranchInst - Handle conditional and unconditional branches here. Note
524/// that since code layout is frozen at this point, that if we are trying to
525/// jump to a block that is the immediate successor of the current block, we can
526/// just make a fall-through. (but we don't currently).
527///
Chris Lattner94af4142002-12-25 05:13:53 +0000528void ISel::visitBranchInst(BranchInst &BI) {
529 if (BI.isConditional()) {
530 BasicBlock *ifTrue = BI.getSuccessor(0);
531 BasicBlock *ifFalse = BI.getSuccessor(1);
Chris Lattner2df035b2002-11-02 19:27:56 +0000532
Chris Lattner94af4142002-12-25 05:13:53 +0000533 // Compare condition with zero, followed by jump-if-equal to ifFalse, and
534 // jump-if-nonequal to ifTrue
535 unsigned int condReg = getReg(BI.getCondition());
536 BuildMI(BB, X86::CMPri8, 2).addReg(condReg).addZImm(0);
537 BuildMI(BB, X86::JNE, 1).addPCDisp(BI.getSuccessor(0));
538 BuildMI(BB, X86::JE, 1).addPCDisp(BI.getSuccessor(1));
539 } else { // unconditional branch
540 BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(0));
541 }
Chris Lattner2df035b2002-11-02 19:27:56 +0000542}
543
Brian Gaeke18a20212002-11-29 12:01:58 +0000544/// visitCallInst - Push args on stack and do a procedure call instruction.
Chris Lattner94af4142002-12-25 05:13:53 +0000545void ISel::visitCallInst(CallInst &CI) {
Misha Brukman0d2cf3a2002-12-04 19:22:53 +0000546 // keep a counter of how many bytes we pushed on the stack
547 unsigned bytesPushed = 0;
548
Brian Gaeke18a20212002-11-29 12:01:58 +0000549 // Push the arguments on the stack in reverse order, as specified by
550 // the ABI.
Chris Lattner94af4142002-12-25 05:13:53 +0000551 for (unsigned i = CI.getNumOperands()-1; i >= 1; --i) {
552 Value *v = CI.getOperand(i);
553 switch (getClass(v->getType())) {
554 case cByte:
555 case cShort:
556 // Promote V to 32 bits wide, and move the result into EAX,
557 // then push EAX.
558 promote32 (X86::EAX, v);
559 BuildMI(BB, X86::PUSHr32, 1).addReg(X86::EAX);
560 bytesPushed += 4;
561 break;
562 case cInt: {
563 unsigned Reg = getReg(v);
564 BuildMI(BB, X86::PUSHr32, 1).addReg(Reg);
565 bytesPushed += 4;
566 break;
Brian Gaeke18a20212002-11-29 12:01:58 +0000567 }
Chris Lattner94af4142002-12-25 05:13:53 +0000568 default:
569 // FIXME: long/ulong/float/double args not handled.
570 visitInstruction(CI);
571 break;
572 }
573 }
Chris Lattner6e49a4b2002-12-13 14:13:27 +0000574
575 if (Function *F = CI.getCalledFunction()) {
576 // Emit a CALL instruction with PC-relative displacement.
577 BuildMI(BB, X86::CALLpcrel32, 1).addPCDisp(F);
578 } else {
579 unsigned Reg = getReg(CI.getCalledValue());
580 BuildMI(BB, X86::CALLr32, 1).addReg(Reg);
581 }
Misha Brukman0d2cf3a2002-12-04 19:22:53 +0000582
583 // Adjust the stack by `bytesPushed' amount if non-zero
584 if (bytesPushed > 0)
Chris Lattner94af4142002-12-25 05:13:53 +0000585 BuildMI(BB, X86::ADDri32,2, X86::ESP).addReg(X86::ESP).addZImm(bytesPushed);
Chris Lattnera3243642002-12-04 23:45:28 +0000586
587 // If there is a return value, scavenge the result from the location the call
588 // leaves it in...
589 //
Chris Lattner4fa1acc2002-12-04 23:50:28 +0000590 if (CI.getType() != Type::VoidTy) {
Chris Lattner94af4142002-12-25 05:13:53 +0000591 unsigned resultTypeClass = getClass(CI.getType());
Brian Gaeke20244b72002-12-12 15:33:40 +0000592 switch (resultTypeClass) {
593 case cByte:
594 case cShort:
595 case cInt: {
596 // Integral results are in %eax, or the appropriate portion
597 // thereof.
598 static const unsigned regRegMove[] = {
599 X86::MOVrr8, X86::MOVrr16, X86::MOVrr32
600 };
601 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX };
Chris Lattner94af4142002-12-25 05:13:53 +0000602 BuildMI(BB, regRegMove[resultTypeClass], 1, getReg(CI))
603 .addReg(AReg[resultTypeClass]);
Chris Lattner4fa1acc2002-12-04 23:50:28 +0000604 break;
Brian Gaeke20244b72002-12-12 15:33:40 +0000605 }
Chris Lattner94af4142002-12-25 05:13:53 +0000606 case cFP: // Floating-point return values live in %ST(0)
607 BuildMI(BB, X86::FpMOV, 1, getReg(CI)).addReg(X86::ST0);
Brian Gaeke20244b72002-12-12 15:33:40 +0000608 break;
Chris Lattner4fa1acc2002-12-04 23:50:28 +0000609 default:
610 std::cerr << "Cannot get return value for call of type '"
611 << *CI.getType() << "'\n";
612 visitInstruction(CI);
613 }
Chris Lattnera3243642002-12-04 23:45:28 +0000614 }
Brian Gaekefa8d5712002-11-22 11:07:01 +0000615}
Chris Lattner2df035b2002-11-02 19:27:56 +0000616
Chris Lattner68aad932002-11-02 20:13:22 +0000617/// visitSimpleBinary - Implement simple binary operators for integral types...
618/// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or,
619/// 4 for Xor.
620///
621void ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) {
622 if (B.getType() == Type::BoolTy) // FIXME: Handle bools for logicals
Chris Lattnere2954c82002-11-02 20:04:26 +0000623 visitInstruction(B);
624
625 unsigned Class = getClass(B.getType());
Chris Lattner94af4142002-12-25 05:13:53 +0000626 if (Class > cFP) // FIXME: Handle longs
Chris Lattnere2954c82002-11-02 20:04:26 +0000627 visitInstruction(B);
628
629 static const unsigned OpcodeTab[][4] = {
Chris Lattner68aad932002-11-02 20:13:22 +0000630 // Arithmetic operators
Chris Lattner94af4142002-12-25 05:13:53 +0000631 { X86::ADDrr8, X86::ADDrr16, X86::ADDrr32, X86::FpADD }, // ADD
632 { X86::SUBrr8, X86::SUBrr16, X86::SUBrr32, X86::FpSUB }, // SUB
Chris Lattner68aad932002-11-02 20:13:22 +0000633
634 // Bitwise operators
Chris Lattnere2954c82002-11-02 20:04:26 +0000635 { X86::ANDrr8, X86::ANDrr16, X86::ANDrr32, 0 }, // AND
636 { X86:: ORrr8, X86:: ORrr16, X86:: ORrr32, 0 }, // OR
637 { X86::XORrr8, X86::XORrr16, X86::XORrr32, 0 }, // XOR
638 };
639
640 unsigned Opcode = OpcodeTab[OperatorClass][Class];
Chris Lattner94af4142002-12-25 05:13:53 +0000641 assert(Opcode && "Floating point arguments to logical inst?");
Chris Lattnere2954c82002-11-02 20:04:26 +0000642 unsigned Op0r = getReg(B.getOperand(0));
643 unsigned Op1r = getReg(B.getOperand(1));
644 BuildMI(BB, Opcode, 2, getReg(B)).addReg(Op0r).addReg(Op1r);
645}
646
Brian Gaeke20244b72002-12-12 15:33:40 +0000647/// doMultiply - Emit appropriate instructions to multiply together
648/// the registers op0Reg and op1Reg, and put the result in destReg.
649/// The type of the result should be given as resultType.
Chris Lattner8a307e82002-12-16 19:32:50 +0000650void ISel::doMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator &MBBI,
651 unsigned destReg, const Type *resultType,
652 unsigned op0Reg, unsigned op1Reg) {
Chris Lattner94af4142002-12-25 05:13:53 +0000653 unsigned Class = getClass(resultType);
654 switch (Class) {
655 case cFP: // Floating point multiply
656 BuildMI(BB, X86::FpMUL, 2, destReg).addReg(op0Reg).addReg(op1Reg);
657 return;
658 default:
659 case cLong:
660 assert(0 && "doMultiply not implemented for this class yet!");
661 case cByte:
662 case cShort:
663 case cInt: // Small integerals, handled below...
664 break;
665 }
Brian Gaeke20244b72002-12-12 15:33:40 +0000666
667 static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX };
668 static const unsigned MulOpcode[]={ X86::MULrr8, X86::MULrr16, X86::MULrr32 };
669 static const unsigned MovOpcode[]={ X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 };
670 unsigned Reg = Regs[Class];
671
672 // Emit a MOV to put the first operand into the appropriately-sized
673 // subreg of EAX.
Brian Gaeke71794c02002-12-13 11:22:48 +0000674 BMI(MBB, MBBI, MovOpcode[Class], 1, Reg).addReg (op0Reg);
Brian Gaeke20244b72002-12-12 15:33:40 +0000675
676 // Emit the appropriate multiply instruction.
Brian Gaeke71794c02002-12-13 11:22:48 +0000677 BMI(MBB, MBBI, MulOpcode[Class], 1).addReg (op1Reg);
Brian Gaeke20244b72002-12-12 15:33:40 +0000678
679 // Emit another MOV to put the result into the destination register.
Brian Gaeke71794c02002-12-13 11:22:48 +0000680 BMI(MBB, MBBI, MovOpcode[Class], 1, destReg).addReg (Reg);
Brian Gaeke20244b72002-12-12 15:33:40 +0000681}
682
Chris Lattnerca9671d2002-11-02 20:28:58 +0000683/// visitMul - Multiplies are not simple binary operators because they must deal
684/// with the EAX register explicitly.
685///
686void ISel::visitMul(BinaryOperator &I) {
Chris Lattner202a2d02002-12-13 13:07:42 +0000687 unsigned DestReg = getReg(I);
688 unsigned Op0Reg = getReg(I.getOperand(0));
689 unsigned Op1Reg = getReg(I.getOperand(1));
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000690 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattner8a307e82002-12-16 19:32:50 +0000691 doMultiply(BB, MBBI, DestReg, I.getType(), Op0Reg, Op1Reg);
Chris Lattnerf01729e2002-11-02 20:54:46 +0000692}
Chris Lattnerca9671d2002-11-02 20:28:58 +0000693
Chris Lattner06925362002-11-17 21:56:38 +0000694
Chris Lattnerf01729e2002-11-02 20:54:46 +0000695/// visitDivRem - Handle division and remainder instructions... these
696/// instruction both require the same instructions to be generated, they just
697/// select the result from a different register. Note that both of these
698/// instructions work differently for signed and unsigned operands.
699///
700void ISel::visitDivRem(BinaryOperator &I) {
Chris Lattner94af4142002-12-25 05:13:53 +0000701 unsigned Class = getClass(I.getType());
702 unsigned Op0Reg = getReg(I.getOperand(0));
703 unsigned Op1Reg = getReg(I.getOperand(1));
704 unsigned ResultReg = getReg(I);
705
706 switch (Class) {
707 case cFP: // Floating point multiply
708 if (I.getOpcode() == Instruction::Div)
709 BuildMI(BB, X86::FpDIV, 2, ResultReg).addReg(Op0Reg).addReg(Op1Reg);
710 else
711 BuildMI(BB, X86::FpREM, 2, ResultReg).addReg(Op0Reg).addReg(Op1Reg);
712 return;
713 default:
714 case cLong:
715 assert(0 && "div/rem not implemented for this class yet!");
716 case cByte:
717 case cShort:
718 case cInt: // Small integerals, handled below...
719 break;
720 }
Chris Lattnerf01729e2002-11-02 20:54:46 +0000721
722 static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX };
723 static const unsigned MovOpcode[]={ X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 };
Brian Gaeke6559bb92002-11-14 22:32:30 +0000724 static const unsigned ExtOpcode[]={ X86::CBW , X86::CWD , X86::CDQ };
Chris Lattnerf01729e2002-11-02 20:54:46 +0000725 static const unsigned ClrOpcode[]={ X86::XORrr8, X86::XORrr16, X86::XORrr32 };
726 static const unsigned ExtRegs[] ={ X86::AH , X86::DX , X86::EDX };
727
728 static const unsigned DivOpcode[][4] = {
729 { X86::DIVrr8 , X86::DIVrr16 , X86::DIVrr32 , 0 }, // Unsigned division
730 { X86::IDIVrr8, X86::IDIVrr16, X86::IDIVrr32, 0 }, // Signed division
731 };
732
733 bool isSigned = I.getType()->isSigned();
734 unsigned Reg = Regs[Class];
735 unsigned ExtReg = ExtRegs[Class];
Chris Lattnerf01729e2002-11-02 20:54:46 +0000736
737 // Put the first operand into one of the A registers...
738 BuildMI(BB, MovOpcode[Class], 1, Reg).addReg(Op0Reg);
739
740 if (isSigned) {
741 // Emit a sign extension instruction...
Chris Lattnera4978cc2002-12-01 23:24:58 +0000742 BuildMI(BB, ExtOpcode[Class], 0);
Chris Lattnerf01729e2002-11-02 20:54:46 +0000743 } else {
744 // If unsigned, emit a zeroing instruction... (reg = xor reg, reg)
745 BuildMI(BB, ClrOpcode[Class], 2, ExtReg).addReg(ExtReg).addReg(ExtReg);
746 }
747
Chris Lattner06925362002-11-17 21:56:38 +0000748 // Emit the appropriate divide or remainder instruction...
Chris Lattner92845e32002-11-21 18:54:29 +0000749 BuildMI(BB, DivOpcode[isSigned][Class], 1).addReg(Op1Reg);
Chris Lattner06925362002-11-17 21:56:38 +0000750
Chris Lattnerf01729e2002-11-02 20:54:46 +0000751 // Figure out which register we want to pick the result out of...
752 unsigned DestReg = (I.getOpcode() == Instruction::Div) ? Reg : ExtReg;
753
Chris Lattnerf01729e2002-11-02 20:54:46 +0000754 // Put the result into the destination register...
Chris Lattner94af4142002-12-25 05:13:53 +0000755 BuildMI(BB, MovOpcode[Class], 1, ResultReg).addReg(DestReg);
Chris Lattnerca9671d2002-11-02 20:28:58 +0000756}
Chris Lattnere2954c82002-11-02 20:04:26 +0000757
Chris Lattner06925362002-11-17 21:56:38 +0000758
Brian Gaekea1719c92002-10-31 23:03:59 +0000759/// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here
760/// for constant immediate shift values, and for constant immediate
761/// shift values equal to 1. Even the general case is sort of special,
762/// because the shift amount has to be in CL, not just any old register.
763///
Chris Lattnerf01729e2002-11-02 20:54:46 +0000764void ISel::visitShiftInst (ShiftInst &I) {
765 unsigned Op0r = getReg (I.getOperand(0));
766 unsigned DestReg = getReg(I);
Chris Lattnere9913f22002-11-02 01:41:55 +0000767 bool isLeftShift = I.getOpcode() == Instruction::Shl;
768 bool isOperandSigned = I.getType()->isUnsigned();
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000769 unsigned OperandClass = getClass(I.getType());
770
Chris Lattner94af4142002-12-25 05:13:53 +0000771 if (OperandClass > cInt)
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000772 visitInstruction(I); // Can't handle longs yet!
Chris Lattner796df732002-11-02 00:44:25 +0000773
Brian Gaekea1719c92002-10-31 23:03:59 +0000774 if (ConstantUInt *CUI = dyn_cast <ConstantUInt> (I.getOperand (1)))
775 {
Chris Lattner796df732002-11-02 00:44:25 +0000776 // The shift amount is constant, guaranteed to be a ubyte. Get its value.
777 assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?");
778 unsigned char shAmt = CUI->getValue();
779
Chris Lattnere9913f22002-11-02 01:41:55 +0000780 static const unsigned ConstantOperand[][4] = {
781 { X86::SHRir8, X86::SHRir16, X86::SHRir32, 0 }, // SHR
782 { X86::SARir8, X86::SARir16, X86::SARir32, 0 }, // SAR
783 { X86::SHLir8, X86::SHLir16, X86::SHLir32, 0 }, // SHL
784 { X86::SHLir8, X86::SHLir16, X86::SHLir32, 0 }, // SAL = SHL
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000785 };
786
Chris Lattnere9913f22002-11-02 01:41:55 +0000787 const unsigned *OpTab = // Figure out the operand table to use
788 ConstantOperand[isLeftShift*2+isOperandSigned];
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000789
Brian Gaekea1719c92002-10-31 23:03:59 +0000790 // Emit: <insn> reg, shamt (shift-by-immediate opcode "ir" form.)
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000791 BuildMI(BB, OpTab[OperandClass], 2, DestReg).addReg(Op0r).addZImm(shAmt);
Brian Gaekea1719c92002-10-31 23:03:59 +0000792 }
793 else
794 {
795 // The shift amount is non-constant.
796 //
797 // In fact, you can only shift with a variable shift amount if
798 // that amount is already in the CL register, so we have to put it
799 // there first.
800 //
Chris Lattnere9913f22002-11-02 01:41:55 +0000801
Brian Gaekea1719c92002-10-31 23:03:59 +0000802 // Emit: move cl, shiftAmount (put the shift amount in CL.)
Chris Lattnerca9671d2002-11-02 20:28:58 +0000803 BuildMI(BB, X86::MOVrr8, 1, X86::CL).addReg(getReg(I.getOperand(1)));
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000804
805 // This is a shift right (SHR).
Chris Lattnere9913f22002-11-02 01:41:55 +0000806 static const unsigned NonConstantOperand[][4] = {
807 { X86::SHRrr8, X86::SHRrr16, X86::SHRrr32, 0 }, // SHR
808 { X86::SARrr8, X86::SARrr16, X86::SARrr32, 0 }, // SAR
809 { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32, 0 }, // SHL
810 { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32, 0 }, // SAL = SHL
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000811 };
812
Chris Lattnere9913f22002-11-02 01:41:55 +0000813 const unsigned *OpTab = // Figure out the operand table to use
814 NonConstantOperand[isLeftShift*2+isOperandSigned];
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000815
Chris Lattner3a9a6932002-11-21 22:49:20 +0000816 BuildMI(BB, OpTab[OperandClass], 1, DestReg).addReg(Op0r);
Brian Gaekea1719c92002-10-31 23:03:59 +0000817 }
818}
819
Chris Lattner06925362002-11-17 21:56:38 +0000820
Chris Lattner6fc3c522002-11-17 21:11:55 +0000821/// visitLoadInst - Implement LLVM load instructions in terms of the x86 'mov'
Chris Lattnere8f0d922002-12-24 00:03:11 +0000822/// instruction. The load and store instructions are the only place where we
823/// need to worry about the memory layout of the target machine.
Chris Lattner6fc3c522002-11-17 21:11:55 +0000824///
825void ISel::visitLoadInst(LoadInst &I) {
Chris Lattnere8f0d922002-12-24 00:03:11 +0000826 bool isLittleEndian = TM.getTargetData().isLittleEndian();
827 bool hasLongPointers = TM.getTargetData().getPointerSize() == 8;
Chris Lattner94af4142002-12-25 05:13:53 +0000828 unsigned SrcAddrReg = getReg(I.getOperand(0));
829 unsigned DestReg = getReg(I);
Chris Lattnere8f0d922002-12-24 00:03:11 +0000830
Chris Lattner6fc3c522002-11-17 21:11:55 +0000831 unsigned Class = getClass(I.getType());
Chris Lattner94af4142002-12-25 05:13:53 +0000832 switch (Class) {
833 default: visitInstruction(I); // FIXME: Handle longs...
834 case cFP: {
835 // FIXME: Handle endian swapping for FP values.
836 unsigned Opcode = I.getType() == Type::FloatTy ? X86::FLDr32 : X86::FLDr64;
837 addDirectMem(BuildMI(BB, Opcode, 4, DestReg), SrcAddrReg);
838 return;
839 }
840 case cInt: // Integers of various sizes handled below
841 case cShort:
842 case cByte: break;
843 }
Chris Lattner6fc3c522002-11-17 21:11:55 +0000844
Chris Lattnere8f0d922002-12-24 00:03:11 +0000845 // We need to adjust the input pointer if we are emulating a big-endian
846 // long-pointer target. On these systems, the pointer that we are interested
847 // in is in the upper part of the eight byte memory image of the pointer. It
848 // also happens to be byte-swapped, but this will be handled later.
849 //
850 if (!isLittleEndian && hasLongPointers && isa<PointerType>(I.getType())) {
851 unsigned R = makeAnotherReg(Type::UIntTy);
852 BuildMI(BB, X86::ADDri32, 2, R).addReg(SrcAddrReg).addZImm(4);
853 SrcAddrReg = R;
854 }
Chris Lattner94af4142002-12-25 05:13:53 +0000855
Chris Lattnere8f0d922002-12-24 00:03:11 +0000856 unsigned IReg = DestReg;
857 if (!isLittleEndian) { // If big endian we need an intermediate stage
858 IReg = makeAnotherReg(I.getType());
859 std::swap(IReg, DestReg);
860 }
Chris Lattner94af4142002-12-25 05:13:53 +0000861
862 static const unsigned Opcode[] = { X86::MOVmr8, X86::MOVmr16, X86::MOVmr32 };
Chris Lattnere8f0d922002-12-24 00:03:11 +0000863 addDirectMem(BuildMI(BB, Opcode[Class], 4, DestReg), SrcAddrReg);
864
865 if (!isLittleEndian) {
866 // Emit the byte swap instruction...
Chris Lattner94af4142002-12-25 05:13:53 +0000867 switch (Class) {
868 case cByte:
869 // No byteswap neccesary for 8 bit value...
870 BuildMI(BB, X86::MOVrr8, 1, IReg).addReg(DestReg);
871 break;
872 case cInt:
873 // Use the 32 bit bswap instruction to do a 32 bit swap...
874 BuildMI(BB, X86::BSWAPr32, 1, IReg).addReg(DestReg);
875 break;
876
877 case cShort:
878 // For 16 bit we have to use an xchg instruction, because there is no
879 // 16-bit bswap. XCHG is neccesarily not in SSA form, so we force things
880 // into AX to do the xchg.
881 //
882 BuildMI(BB, X86::MOVrr16, 1, X86::AX).addReg(DestReg);
883 BuildMI(BB, X86::XCHGrr8, 2).addReg(X86::AL, MOTy::UseAndDef)
884 .addReg(X86::AH, MOTy::UseAndDef);
885 BuildMI(BB, X86::MOVrr16, 1, DestReg).addReg(X86::AX);
886 break;
887 default: assert(0 && "Class not handled yet!");
888 }
Chris Lattnere8f0d922002-12-24 00:03:11 +0000889 }
Chris Lattner6fc3c522002-11-17 21:11:55 +0000890}
891
Chris Lattner06925362002-11-17 21:56:38 +0000892
Chris Lattner6fc3c522002-11-17 21:11:55 +0000893/// visitStoreInst - Implement LLVM store instructions in terms of the x86 'mov'
894/// instruction.
895///
896void ISel::visitStoreInst(StoreInst &I) {
Chris Lattnere8f0d922002-12-24 00:03:11 +0000897 bool isLittleEndian = TM.getTargetData().isLittleEndian();
898 bool hasLongPointers = TM.getTargetData().getPointerSize() == 8;
Chris Lattner6fc3c522002-11-17 21:11:55 +0000899 unsigned ValReg = getReg(I.getOperand(0));
900 unsigned AddressReg = getReg(I.getOperand(1));
Chris Lattnere8f0d922002-12-24 00:03:11 +0000901
Chris Lattner94af4142002-12-25 05:13:53 +0000902 unsigned Class = getClass(I.getOperand(0)->getType());
903 switch (Class) {
904 default: visitInstruction(I); // FIXME: Handle longs...
905 case cFP: {
906 // FIXME: Handle endian swapping for FP values.
907 unsigned Opcode = I.getOperand(0)->getType() == Type::FloatTy ?
908 X86::FSTr32 : X86::FSTr64;
909 addDirectMem(BuildMI(BB, Opcode, 1+4), AddressReg).addReg(ValReg);
910 return;
911 }
912 case cInt: // Integers of various sizes handled below
913 case cShort:
914 case cByte: break;
915 }
916
917 if (!isLittleEndian && hasLongPointers &&
918 isa<PointerType>(I.getOperand(0)->getType())) {
Chris Lattnere8f0d922002-12-24 00:03:11 +0000919 unsigned R = makeAnotherReg(Type::UIntTy);
920 BuildMI(BB, X86::ADDri32, 2, R).addReg(AddressReg).addZImm(4);
921 AddressReg = R;
922 }
923
Chris Lattner94af4142002-12-25 05:13:53 +0000924 if (!isLittleEndian && Class != cByte) {
925 // Emit a byte swap instruction...
926 switch (Class) {
927 case cInt: {
928 unsigned R = makeAnotherReg(I.getOperand(0)->getType());
929 BuildMI(BB, X86::BSWAPr32, 1, R).addReg(ValReg);
930 ValReg = R;
931 break;
932 }
933 case cShort:
934 // For 16 bit we have to use an xchg instruction, because there is no
935 // 16-bit bswap. XCHG is neccesarily not in SSA form, so we force things
936 // into AX to do the xchg.
937 //
938 BuildMI(BB, X86::MOVrr16, 1, X86::AX).addReg(ValReg);
939 BuildMI(BB, X86::XCHGrr8, 2).addReg(X86::AL, MOTy::UseAndDef)
940 .addReg(X86::AH, MOTy::UseAndDef);
941 ValReg = X86::AX;
942 break;
943 default: assert(0 && "Unknown class!");
944 }
Chris Lattnere8f0d922002-12-24 00:03:11 +0000945 }
946
Chris Lattner94af4142002-12-25 05:13:53 +0000947 static const unsigned Opcode[] = { X86::MOVrm8, X86::MOVrm16, X86::MOVrm32 };
Chris Lattner6fc3c522002-11-17 21:11:55 +0000948 addDirectMem(BuildMI(BB, Opcode[Class], 1+4), AddressReg).addReg(ValReg);
949}
950
951
Brian Gaekec11232a2002-11-26 10:43:30 +0000952/// visitCastInst - Here we have various kinds of copying with or without
953/// sign extension going on.
Brian Gaekefa8d5712002-11-22 11:07:01 +0000954void
955ISel::visitCastInst (CastInst &CI)
956{
Chris Lattnerf18a36e2002-12-03 18:15:59 +0000957 const Type *targetType = CI.getType ();
Brian Gaeke07f02612002-12-03 07:36:03 +0000958 Value *operand = CI.getOperand (0);
959 unsigned int operandReg = getReg (operand);
Chris Lattnerf18a36e2002-12-03 18:15:59 +0000960 const Type *sourceType = operand->getType ();
Brian Gaeke07f02612002-12-03 07:36:03 +0000961 unsigned int destReg = getReg (CI);
Brian Gaeked474e9c2002-12-06 10:49:33 +0000962 //
963 // Currently we handle:
964 //
965 // 1) cast * to bool
966 //
967 // 2) cast {sbyte, ubyte} to {sbyte, ubyte}
968 // cast {short, ushort} to {ushort, short}
969 // cast {int, uint, ptr} to {int, uint, ptr}
970 //
971 // 3) cast {sbyte, ubyte} to {ushort, short}
972 // cast {sbyte, ubyte} to {int, uint, ptr}
973 // cast {short, ushort} to {int, uint, ptr}
974 //
975 // 4) cast {int, uint, ptr} to {short, ushort}
976 // cast {int, uint, ptr} to {sbyte, ubyte}
977 // cast {short, ushort} to {sbyte, ubyte}
Chris Lattner7d255892002-12-13 11:31:59 +0000978
Brian Gaeked474e9c2002-12-06 10:49:33 +0000979 // 1) Implement casts to bool by using compare on the operand followed
980 // by set if not zero on the result.
981 if (targetType == Type::BoolTy)
982 {
983 BuildMI (BB, X86::CMPri8, 2).addReg (operandReg).addZImm (0);
984 BuildMI (BB, X86::SETNEr, 1, destReg);
985 return;
986 }
Chris Lattner7d255892002-12-13 11:31:59 +0000987
Brian Gaeked474e9c2002-12-06 10:49:33 +0000988 // 2) Implement casts between values of the same type class (as determined
989 // by getClass) by using a register-to-register move.
Chris Lattner94af4142002-12-25 05:13:53 +0000990 unsigned srcClass = getClassB(sourceType);
991 unsigned targClass = getClass(targetType);
Brian Gaeked474e9c2002-12-06 10:49:33 +0000992 static const unsigned regRegMove[] = {
993 X86::MOVrr8, X86::MOVrr16, X86::MOVrr32
994 };
Chris Lattner94af4142002-12-25 05:13:53 +0000995
996 if (srcClass <= cInt && targClass <= cInt && srcClass == targClass) {
997 BuildMI(BB, regRegMove[srcClass], 1, destReg).addReg(operandReg);
998 return;
999 }
Brian Gaeked474e9c2002-12-06 10:49:33 +00001000 // 3) Handle cast of SMALLER int to LARGER int using a move with sign
1001 // extension or zero extension, depending on whether the source type
1002 // was signed.
Chris Lattner94af4142002-12-25 05:13:53 +00001003 if ((srcClass <= cInt) && (targClass <= cInt) && (srcClass < targClass))
Brian Gaeked474e9c2002-12-06 10:49:33 +00001004 {
1005 static const unsigned ops[] = {
1006 X86::MOVSXr16r8, X86::MOVSXr32r8, X86::MOVSXr32r16,
1007 X86::MOVZXr16r8, X86::MOVZXr32r8, X86::MOVZXr32r16
1008 };
1009 unsigned srcSigned = sourceType->isSigned ();
1010 BuildMI (BB, ops[3 * srcSigned + srcClass + targClass - 1], 1,
1011 destReg).addReg (operandReg);
1012 return;
1013 }
1014 // 4) Handle cast of LARGER int to SMALLER int using a move to EAX
1015 // followed by a move out of AX or AL.
Chris Lattner94af4142002-12-25 05:13:53 +00001016 if ((srcClass <= cInt) && (targClass <= cInt) && (srcClass > targClass))
Brian Gaeked474e9c2002-12-06 10:49:33 +00001017 {
1018 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX };
1019 BuildMI (BB, regRegMove[srcClass], 1,
1020 AReg[srcClass]).addReg (operandReg);
1021 BuildMI (BB, regRegMove[targClass], 1, destReg).addReg (AReg[srcClass]);
1022 return;
1023 }
1024 // Anything we haven't handled already, we can't (yet) handle at all.
Brian Gaeke20244b72002-12-12 15:33:40 +00001025 //
1026 // FP to integral casts can be handled with FISTP to store onto the
1027 // stack while converting to integer, followed by a MOV to load from
1028 // the stack into the result register. Integral to FP casts can be
1029 // handled with MOV to store onto the stack, followed by a FILD to
1030 // load from the stack while converting to FP. For the moment, I
1031 // can't quite get straight in my head how to borrow myself some
1032 // stack space and write on it. Otherwise, this would be trivial.
Brian Gaekefa8d5712002-11-22 11:07:01 +00001033 visitInstruction (CI);
1034}
Brian Gaekea1719c92002-10-31 23:03:59 +00001035
Chris Lattner8a307e82002-12-16 19:32:50 +00001036// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
1037// returns zero when the input is not exactly a power of two.
1038static unsigned ExactLog2(unsigned Val) {
1039 if (Val == 0) return 0;
1040 unsigned Count = 0;
1041 while (Val != 1) {
1042 if (Val & 1) return 0;
1043 Val >>= 1;
1044 ++Count;
1045 }
1046 return Count+1;
1047}
1048
Brian Gaeke20244b72002-12-12 15:33:40 +00001049/// visitGetElementPtrInst - I don't know, most programs don't have
1050/// getelementptr instructions, right? That means we can put off
1051/// implementing this, right? Right. This method emits machine
1052/// instructions to perform type-safe pointer arithmetic. I am
1053/// guessing this could be cleaned up somewhat to use fewer temporary
1054/// registers.
1055void
1056ISel::visitGetElementPtrInst (GetElementPtrInst &I)
1057{
Brian Gaeke68b1edc2002-12-16 04:23:29 +00001058 unsigned outputReg = getReg (I);
Chris Lattnerf08ad9f2002-12-13 10:50:40 +00001059 MachineBasicBlock::iterator MI = BB->end();
1060 emitGEPOperation(BB, MI, I.getOperand(0),
Brian Gaeke68b1edc2002-12-16 04:23:29 +00001061 I.op_begin()+1, I.op_end(), outputReg);
Chris Lattnerc0812d82002-12-13 06:56:29 +00001062}
1063
Brian Gaeke71794c02002-12-13 11:22:48 +00001064void ISel::emitGEPOperation(MachineBasicBlock *MBB,
Chris Lattnerf08ad9f2002-12-13 10:50:40 +00001065 MachineBasicBlock::iterator &IP,
Chris Lattner333b2fa2002-12-13 10:09:43 +00001066 Value *Src, User::op_iterator IdxBegin,
Chris Lattnerc0812d82002-12-13 06:56:29 +00001067 User::op_iterator IdxEnd, unsigned TargetReg) {
1068 const TargetData &TD = TM.getTargetData();
1069 const Type *Ty = Src->getType();
Brian Gaeke68b1edc2002-12-16 04:23:29 +00001070 unsigned basePtrReg = getReg(Src, MBB, IP);
Chris Lattnerc0812d82002-12-13 06:56:29 +00001071
Brian Gaeke20244b72002-12-12 15:33:40 +00001072 // GEPs have zero or more indices; we must perform a struct access
1073 // or array access for each one.
Chris Lattnerc0812d82002-12-13 06:56:29 +00001074 for (GetElementPtrInst::op_iterator oi = IdxBegin,
1075 oe = IdxEnd; oi != oe; ++oi) {
Brian Gaeke20244b72002-12-12 15:33:40 +00001076 Value *idx = *oi;
Chris Lattnerc0812d82002-12-13 06:56:29 +00001077 unsigned nextBasePtrReg = makeAnotherReg(Type::UIntTy);
Brian Gaeke20244b72002-12-12 15:33:40 +00001078 if (const StructType *StTy = dyn_cast <StructType> (Ty)) {
1079 // It's a struct access. idx is the index into the structure,
1080 // which names the field. This index must have ubyte type.
1081 const ConstantUInt *CUI = cast <ConstantUInt> (idx);
1082 assert (CUI->getType () == Type::UByteTy
1083 && "Funny-looking structure index in GEP");
1084 // Use the TargetData structure to pick out what the layout of
1085 // the structure is in memory. Since the structure index must
1086 // be constant, we can get its value and use it to find the
1087 // right byte offset from the StructLayout class's list of
1088 // structure member offsets.
Chris Lattnere8f0d922002-12-24 00:03:11 +00001089 unsigned idxValue = CUI->getValue();
Brian Gaeke20244b72002-12-12 15:33:40 +00001090 unsigned memberOffset =
1091 TD.getStructLayout (StTy)->MemberOffsets[idxValue];
1092 // Emit an ADD to add memberOffset to the basePtr.
Brian Gaeke71794c02002-12-13 11:22:48 +00001093 BMI(MBB, IP, X86::ADDri32, 2,
Chris Lattnerf08ad9f2002-12-13 10:50:40 +00001094 nextBasePtrReg).addReg (basePtrReg).addZImm (memberOffset);
Brian Gaeke20244b72002-12-12 15:33:40 +00001095 // The next type is the member of the structure selected by the
1096 // index.
1097 Ty = StTy->getElementTypes ()[idxValue];
Chris Lattner8a307e82002-12-16 19:32:50 +00001098 } else if (const SequentialType *SqTy = cast <SequentialType>(Ty)) {
Brian Gaeke20244b72002-12-12 15:33:40 +00001099 // It's an array or pointer access: [ArraySize x ElementType].
Chris Lattner8a307e82002-12-16 19:32:50 +00001100
Brian Gaeke20244b72002-12-12 15:33:40 +00001101 // idx is the index into the array. Unlike with structure
1102 // indices, we may not know its actual value at code-generation
1103 // time.
Chris Lattner8a307e82002-12-16 19:32:50 +00001104 assert(idx->getType() == Type::LongTy && "Bad GEP array index!");
1105
1106 // We want to add basePtrReg to (idxReg * sizeof ElementType). First, we
1107 // must find the size of the pointed-to type (Not coincidentally, the next
1108 // type is the type of the elements in the array).
1109 Ty = SqTy->getElementType();
1110 unsigned elementSize = TD.getTypeSize(Ty);
1111
1112 // If idxReg is a constant, we don't need to perform the multiply!
1113 if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(idx)) {
1114 if (CSI->isNullValue()) {
1115 BMI(MBB, IP, X86::MOVrr32, 1, nextBasePtrReg).addReg(basePtrReg);
1116 } else {
1117 unsigned Offset = elementSize*CSI->getValue();
1118
1119 BMI(MBB, IP, X86::ADDri32, 2,
1120 nextBasePtrReg).addReg(basePtrReg).addZImm(Offset);
1121 }
1122 } else if (elementSize == 1) {
1123 // If the element size is 1, we don't have to multiply, just add
1124 unsigned idxReg = getReg(idx, MBB, IP);
1125 BMI(MBB, IP, X86::ADDrr32, 2,
1126 nextBasePtrReg).addReg(basePtrReg).addReg(idxReg);
1127 } else {
1128 unsigned idxReg = getReg(idx, MBB, IP);
1129 unsigned OffsetReg = makeAnotherReg(Type::UIntTy);
1130 if (unsigned Shift = ExactLog2(elementSize)) {
1131 // If the element size is exactly a power of 2, use a shift to get it.
1132
1133 BMI(MBB, IP, X86::SHLir32, 2,
1134 OffsetReg).addReg(idxReg).addZImm(Shift-1);
1135 } else {
1136 // Most general case, emit a multiply...
1137 unsigned elementSizeReg = makeAnotherReg(Type::LongTy);
1138 BMI(MBB, IP, X86::MOVir32, 1, elementSizeReg).addZImm(elementSize);
1139
1140 // Emit a MUL to multiply the register holding the index by
1141 // elementSize, putting the result in OffsetReg.
1142 doMultiply(MBB, IP, OffsetReg, Type::LongTy, idxReg, elementSizeReg);
1143 }
1144 // Emit an ADD to add OffsetReg to the basePtr.
1145 BMI(MBB, IP, X86::ADDrr32, 2,
1146 nextBasePtrReg).addReg (basePtrReg).addReg (OffsetReg);
1147 }
Brian Gaeke20244b72002-12-12 15:33:40 +00001148 }
1149 // Now that we are here, further indices refer to subtypes of this
1150 // one, so we don't need to worry about basePtrReg itself, anymore.
1151 basePtrReg = nextBasePtrReg;
1152 }
1153 // After we have processed all the indices, the result is left in
1154 // basePtrReg. Move it to the register where we were expected to
1155 // put the answer. A 32-bit move should do it, because we are in
1156 // ILP32 land.
Brian Gaeke71794c02002-12-13 11:22:48 +00001157 BMI(MBB, IP, X86::MOVrr32, 1, TargetReg).addReg (basePtrReg);
Brian Gaeke20244b72002-12-12 15:33:40 +00001158}
1159
1160
1161/// visitMallocInst - I know that personally, whenever I want to remember
1162/// something, I have to clear off some space in my brain.
1163void
1164ISel::visitMallocInst (MallocInst &I)
1165{
Brian Gaekee48ec012002-12-13 06:46:31 +00001166 // We assume that by this point, malloc instructions have been
1167 // lowered to calls, and dlsym will magically find malloc for us.
1168 // So we do not want to see malloc instructions here.
1169 visitInstruction (I);
1170}
1171
1172
1173/// visitFreeInst - same story as MallocInst
1174void
1175ISel::visitFreeInst (FreeInst &I)
1176{
1177 // We assume that by this point, free instructions have been
1178 // lowered to calls, and dlsym will magically find free for us.
1179 // So we do not want to see free instructions here.
Brian Gaeke20244b72002-12-12 15:33:40 +00001180 visitInstruction (I);
1181}
1182
1183
1184/// visitAllocaInst - I want some stack space. Come on, man, I said I
1185/// want some freakin' stack space.
1186void
1187ISel::visitAllocaInst (AllocaInst &I)
1188{
Brian Gaekee48ec012002-12-13 06:46:31 +00001189 // Find the data size of the alloca inst's getAllocatedType.
1190 const Type *allocatedType = I.getAllocatedType ();
1191 const TargetData &TD = TM.DataLayout;
1192 unsigned allocatedTypeSize = TD.getTypeSize (allocatedType);
1193 // Keep stack 32-bit aligned.
1194 unsigned int allocatedTypeWords = allocatedTypeSize / 4;
1195 if (allocatedTypeSize % 4 != 0) { allocatedTypeWords++; }
1196 // Subtract size from stack pointer, thereby allocating some space.
Chris Lattner4863fe12002-12-16 22:29:06 +00001197 BuildMI(BB, X86::SUBri32, 2,
1198 X86::ESP).addReg(X86::ESP).addZImm(allocatedTypeWords * 4);
Brian Gaekee48ec012002-12-13 06:46:31 +00001199 // Put a pointer to the space into the result register, by copying
1200 // the stack pointer.
1201 BuildMI (BB, X86::MOVrr32, 1, getReg (I)).addReg (X86::ESP);
Brian Gaeke20244b72002-12-12 15:33:40 +00001202}
1203
1204
Chris Lattnerb4f68ed2002-10-29 22:37:54 +00001205/// createSimpleX86InstructionSelector - This pass converts an LLVM function
1206/// into a machine code representation is a very simple peep-hole fashion. The
Chris Lattner72614082002-10-25 22:55:53 +00001207/// generated code sucks but the implementation is nice and simple.
1208///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +00001209Pass *createSimpleX86InstructionSelector(TargetMachine &TM) {
1210 return new ISel(TM);
Chris Lattner72614082002-10-25 22:55:53 +00001211}