blob: 20a27d3f193714c55b55b9b03218812fbe1743f5 [file] [log] [blame]
Chris Lattner1c809c52004-02-29 00:27:00 +00001//===-- InstSelectSimple.cpp - A simple instruction selector for SparcV8 --===//
2//
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//===----------------------------------------------------------------------===//
9//
10// This file defines a simple peephole instruction selector for the V8 target
11//
12//===----------------------------------------------------------------------===//
13
14#include "SparcV8.h"
Brian Gaekebc1d27a2004-03-03 23:03:14 +000015#include "SparcV8InstrInfo.h"
Brian Gaeke6c868a42004-06-17 22:34:08 +000016#include "Support/Debug.h"
Chris Lattner1c809c52004-02-29 00:27:00 +000017#include "llvm/Instructions.h"
Chris Lattner1c809c52004-02-29 00:27:00 +000018#include "llvm/Pass.h"
Brian Gaekebc1d27a2004-03-03 23:03:14 +000019#include "llvm/Constants.h"
Chris Lattner30483732004-06-20 07:49:54 +000020#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattner1c809c52004-02-29 00:27:00 +000021#include "llvm/CodeGen/MachineInstrBuilder.h"
Brian Gaeke9df92822004-06-15 19:16:07 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Brian Gaekec93a7522004-06-18 05:19:16 +000023#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner1c809c52004-02-29 00:27:00 +000024#include "llvm/CodeGen/MachineFunction.h"
Brian Gaekebc1d27a2004-03-03 23:03:14 +000025#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner1c809c52004-02-29 00:27:00 +000026#include "llvm/Target/TargetMachine.h"
27#include "llvm/Support/GetElementPtrTypeIterator.h"
28#include "llvm/Support/InstVisitor.h"
29#include "llvm/Support/CFG.h"
30using namespace llvm;
31
32namespace {
33 struct V8ISel : public FunctionPass, public InstVisitor<V8ISel> {
34 TargetMachine &TM;
35 MachineFunction *F; // The function we are compiling into
36 MachineBasicBlock *BB; // The current MBB we are compiling
37
38 std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs
39
40 // MBBMap - Mapping between LLVM BB -> Machine BB
41 std::map<const BasicBlock*, MachineBasicBlock*> MBBMap;
42
43 V8ISel(TargetMachine &tm) : TM(tm), F(0), BB(0) {}
44
45 /// runOnFunction - Top level implementation of instruction selection for
46 /// the entire function.
47 ///
48 bool runOnFunction(Function &Fn);
49
50 virtual const char *getPassName() const {
51 return "SparcV8 Simple Instruction Selection";
52 }
53
Brian Gaeke532e60c2004-05-08 04:21:17 +000054 /// emitGEPOperation - Common code shared between visitGetElementPtrInst and
55 /// constant expression GEP support.
56 ///
57 void emitGEPOperation(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
58 Value *Src, User::op_iterator IdxBegin,
59 User::op_iterator IdxEnd, unsigned TargetReg);
60
Brian Gaeke00e514e2004-06-24 06:33:00 +000061 /// emitCastOperation - Common code shared between visitCastInst and
62 /// constant expression cast support.
63 ///
64 void emitCastOperation(MachineBasicBlock *BB,MachineBasicBlock::iterator IP,
65 Value *Src, const Type *DestTy, unsigned TargetReg);
66
Chris Lattner1c809c52004-02-29 00:27:00 +000067 /// visitBasicBlock - This method is called when we are visiting a new basic
68 /// block. This simply creates a new MachineBasicBlock to emit code into
69 /// and adds it to the current MachineFunction. Subsequent visit* for
70 /// instructions will be invoked for all instructions in the basic block.
71 ///
72 void visitBasicBlock(BasicBlock &LLVM_BB) {
73 BB = MBBMap[&LLVM_BB];
74 }
75
Chris Lattner4be7ca52004-04-07 04:27:16 +000076 void visitBinaryOperator(Instruction &I);
Brian Gaeked6a10532004-06-15 21:09:46 +000077 void visitShiftInst (ShiftInst &SI) { visitBinaryOperator (SI); }
Misha Brukmanea091262004-06-30 21:47:40 +000078 void visitSetCondInst(SetCondInst &I);
Chris Lattner4be7ca52004-04-07 04:27:16 +000079 void visitCallInst(CallInst &I);
Brian Gaekef3334eb2004-04-07 17:29:37 +000080 void visitReturnInst(ReturnInst &I);
Brian Gaeke532e60c2004-05-08 04:21:17 +000081 void visitBranchInst(BranchInst &I);
Brian Gaeke3d11e8a2004-04-13 18:27:46 +000082 void visitCastInst(CastInst &I);
Brian Gaekef3334eb2004-04-07 17:29:37 +000083 void visitLoadInst(LoadInst &I);
84 void visitStoreInst(StoreInst &I);
Brian Gaeke532e60c2004-05-08 04:21:17 +000085 void visitPHINode(PHINode &I) {} // PHI nodes handled by second pass
86 void visitGetElementPtrInst(GetElementPtrInst &I);
Brian Gaekec93a7522004-06-18 05:19:16 +000087 void visitAllocaInst(AllocaInst &I);
Brian Gaeke532e60c2004-05-08 04:21:17 +000088
Chris Lattner1c809c52004-02-29 00:27:00 +000089 void visitInstruction(Instruction &I) {
90 std::cerr << "Unhandled instruction: " << I;
91 abort();
92 }
93
94 /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
95 /// function, lowering any calls to unknown intrinsic functions into the
96 /// equivalent LLVM code.
97 void LowerUnknownIntrinsicFunctionCalls(Function &F);
Chris Lattner1c809c52004-02-29 00:27:00 +000098 void visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI);
99
Brian Gaeke562cb162004-04-07 17:04:09 +0000100 void LoadArgumentsToVirtualRegs(Function *F);
101
Brian Gaeke6c868a42004-06-17 22:34:08 +0000102 /// SelectPHINodes - Insert machine code to generate phis. This is tricky
103 /// because we have to generate our sources into the source basic blocks,
104 /// not the current one.
105 ///
106 void SelectPHINodes();
107
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000108 /// copyConstantToRegister - Output the instructions required to put the
109 /// specified constant into the specified register.
110 ///
111 void copyConstantToRegister(MachineBasicBlock *MBB,
112 MachineBasicBlock::iterator IP,
113 Constant *C, unsigned R);
114
115 /// makeAnotherReg - This method returns the next register number we haven't
116 /// yet used.
117 ///
118 /// Long values are handled somewhat specially. They are always allocated
119 /// as pairs of 32 bit integer values. The register number returned is the
120 /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits
121 /// of the long value.
122 ///
123 unsigned makeAnotherReg(const Type *Ty) {
124 assert(dynamic_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo()) &&
125 "Current target doesn't have SparcV8 reg info??");
126 const SparcV8RegisterInfo *MRI =
127 static_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo());
128 if (Ty == Type::LongTy || Ty == Type::ULongTy) {
129 const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy);
130 // Create the lower part
131 F->getSSARegMap()->createVirtualRegister(RC);
132 // Create the upper part.
133 return F->getSSARegMap()->createVirtualRegister(RC)-1;
134 }
135
136 // Add the mapping of regnumber => reg class to MachineFunction
137 const TargetRegisterClass *RC = MRI->getRegClassForType(Ty);
138 return F->getSSARegMap()->createVirtualRegister(RC);
139 }
140
141 unsigned getReg(Value &V) { return getReg (&V); } // allow refs.
142 unsigned getReg(Value *V) {
143 // Just append to the end of the current bb.
144 MachineBasicBlock::iterator It = BB->end();
145 return getReg(V, BB, It);
146 }
147 unsigned getReg(Value *V, MachineBasicBlock *MBB,
148 MachineBasicBlock::iterator IPt) {
149 unsigned &Reg = RegMap[V];
150 if (Reg == 0) {
151 Reg = makeAnotherReg(V->getType());
152 RegMap[V] = Reg;
153 }
154 // If this operand is a constant, emit the code to copy the constant into
155 // the register here...
156 //
157 if (Constant *C = dyn_cast<Constant>(V)) {
158 copyConstantToRegister(MBB, IPt, C, Reg);
159 RegMap.erase(V); // Assign a new name to this constant if ref'd again
160 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
161 // Move the address of the global into the register
Brian Gaekecf471982004-03-09 04:49:13 +0000162 unsigned TmpReg = makeAnotherReg(V->getType());
163 BuildMI (*MBB, IPt, V8::SETHIi, 1, TmpReg).addGlobalAddress (GV);
164 BuildMI (*MBB, IPt, V8::ORri, 2, Reg).addReg (TmpReg)
165 .addGlobalAddress (GV);
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000166 RegMap.erase(V); // Assign a new name to this address if ref'd again
167 }
168
169 return Reg;
170 }
171
Chris Lattner1c809c52004-02-29 00:27:00 +0000172 };
173}
174
175FunctionPass *llvm::createSparcV8SimpleInstructionSelector(TargetMachine &TM) {
176 return new V8ISel(TM);
177}
178
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000179enum TypeClass {
Brian Gaekef57e3642004-03-16 22:37:11 +0000180 cByte, cShort, cInt, cLong, cFloat, cDouble
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000181};
182
183static TypeClass getClass (const Type *T) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000184 switch (T->getTypeID()) {
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000185 case Type::UByteTyID: case Type::SByteTyID: return cByte;
186 case Type::UShortTyID: case Type::ShortTyID: return cShort;
Brian Gaeke562cb162004-04-07 17:04:09 +0000187 case Type::PointerTyID:
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000188 case Type::UIntTyID: case Type::IntTyID: return cInt;
Brian Gaekef57e3642004-03-16 22:37:11 +0000189 case Type::ULongTyID: case Type::LongTyID: return cLong;
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000190 case Type::FloatTyID: return cFloat;
191 case Type::DoubleTyID: return cDouble;
192 default:
193 assert (0 && "Type of unknown class passed to getClass?");
194 return cByte;
195 }
196}
Chris Lattner0d538bb2004-04-07 04:36:53 +0000197static TypeClass getClassB(const Type *T) {
198 if (T == Type::BoolTy) return cByte;
199 return getClass(T);
200}
201
202
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000203
204/// copyConstantToRegister - Output the instructions required to put the
205/// specified constant into the specified register.
206///
207void V8ISel::copyConstantToRegister(MachineBasicBlock *MBB,
208 MachineBasicBlock::iterator IP,
209 Constant *C, unsigned R) {
Brian Gaeke9df92822004-06-15 19:16:07 +0000210 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
211 switch (CE->getOpcode()) {
212 case Instruction::GetElementPtr:
213 emitGEPOperation(MBB, IP, CE->getOperand(0),
214 CE->op_begin()+1, CE->op_end(), R);
215 return;
Brian Gaeke00e514e2004-06-24 06:33:00 +0000216 case Instruction::Cast:
217 emitCastOperation(MBB, IP, CE->getOperand(0), CE->getType(), R);
218 return;
Brian Gaeke9df92822004-06-15 19:16:07 +0000219 default:
220 std::cerr << "Copying this constant expr not yet handled: " << *CE;
221 abort();
222 }
223 }
224
Brian Gaekee302a7e2004-05-07 21:39:30 +0000225 if (C->getType()->isIntegral ()) {
226 uint64_t Val;
Brian Gaeke9df92822004-06-15 19:16:07 +0000227 unsigned Class = getClassB (C->getType ());
228 if (Class == cLong) {
229 unsigned TmpReg = makeAnotherReg (Type::IntTy);
230 unsigned TmpReg2 = makeAnotherReg (Type::IntTy);
231 // Copy the value into the register pair.
232 // R = top(more-significant) half, R+1 = bottom(less-significant) half
233 uint64_t Val = cast<ConstantInt>(C)->getRawValue();
234 unsigned topHalf = Val & 0xffffffffU;
235 unsigned bottomHalf = Val >> 32;
236 unsigned HH = topHalf >> 10;
237 unsigned HM = topHalf & 0x03ff;
238 unsigned LM = bottomHalf >> 10;
239 unsigned LO = bottomHalf & 0x03ff;
Brian Gaekec7fd0f42004-06-24 08:55:09 +0000240 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addZImm(HH);
Brian Gaeke9df92822004-06-15 19:16:07 +0000241 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg)
Brian Gaekec7fd0f42004-06-24 08:55:09 +0000242 .addSImm (HM);
243 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg2).addZImm(LM);
Brian Gaeke9df92822004-06-15 19:16:07 +0000244 BuildMI (*MBB, IP, V8::ORri, 2, R+1).addReg (TmpReg2)
Brian Gaekec7fd0f42004-06-24 08:55:09 +0000245 .addSImm (LO);
Brian Gaeke9df92822004-06-15 19:16:07 +0000246 return;
247 }
248
249 assert(Class <= cInt && "Type not handled yet!");
250
Brian Gaekee302a7e2004-05-07 21:39:30 +0000251 if (C->getType() == Type::BoolTy) {
252 Val = (C == ConstantBool::True);
253 } else {
Brian Gaeke13dc4332004-06-24 09:17:47 +0000254 ConstantInt *CI = cast<ConstantInt> (C);
Brian Gaekee302a7e2004-05-07 21:39:30 +0000255 Val = CI->getRawValue ();
256 }
Brian Gaeke9df92822004-06-15 19:16:07 +0000257 switch (Class) {
Brian Gaeke13dc4332004-06-24 09:17:47 +0000258 case cByte: Val = (int8_t) Val; break;
259 case cShort: Val = (int16_t) Val; break;
260 case cInt: Val = (int32_t) Val; break;
Brian Gaekee8061732004-03-04 00:56:25 +0000261 default:
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +0000262 std::cerr << "Offending constant: " << *C << "\n";
Brian Gaeke775158d2004-03-04 04:37:45 +0000263 assert (0 && "Can't copy this kind of constant into register yet");
Brian Gaekee8061732004-03-04 00:56:25 +0000264 return;
265 }
Brian Gaeke13dc4332004-06-24 09:17:47 +0000266 if (Val == 0) {
267 BuildMI (*MBB, IP, V8::ORrr, 2, R).addReg (V8::G0).addReg(V8::G0);
268 } else if (((int64_t)Val >= -4096) && ((int64_t)Val <= 4095)) {
269 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (V8::G0).addSImm(Val);
270 } else {
271 unsigned TmpReg = makeAnotherReg (C->getType ());
272 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg)
273 .addSImm (((uint32_t) Val) >> 10);
274 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg)
275 .addSImm (((uint32_t) Val) & 0x03ff);
276 return;
277 }
Brian Gaekec93a7522004-06-18 05:19:16 +0000278 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
279 // We need to spill the constant to memory...
280 MachineConstantPool *CP = F->getConstantPool();
281 unsigned CPI = CP->getConstantPoolIndex(CFP);
282 const Type *Ty = CFP->getType();
283
284 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
Brian Gaeke44733032004-06-24 07:36:48 +0000285 unsigned LoadOpcode = Ty == Type::FloatTy ? V8::LDFri : V8::LDDFri;
Brian Gaekec93a7522004-06-18 05:19:16 +0000286 BuildMI (*MBB, IP, LoadOpcode, 2, R).addConstantPoolIndex (CPI).addSImm (0);
Brian Gaeke9df92822004-06-15 19:16:07 +0000287 } else if (isa<ConstantPointerNull>(C)) {
288 // Copy zero (null pointer) to the register.
Brian Gaekec7fd0f42004-06-24 08:55:09 +0000289 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (V8::G0).addSImm (0);
Brian Gaeke9df92822004-06-15 19:16:07 +0000290 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)) {
291 // Copy it with a SETHI/OR pair; the JIT + asmwriter should recognize
292 // that SETHI %reg,global == SETHI %reg,%hi(global) and
293 // OR %reg,global,%reg == OR %reg,%lo(global),%reg.
294 unsigned TmpReg = makeAnotherReg (C->getType ());
295 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addGlobalAddress (CPR->getValue());
296 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg)
297 .addGlobalAddress (CPR->getValue ());
298 } else {
299 std::cerr << "Offending constant: " << *C << "\n";
300 assert (0 && "Can't copy this kind of constant into register yet");
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000301 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000302}
Chris Lattner1c809c52004-02-29 00:27:00 +0000303
Brian Gaeke562cb162004-04-07 17:04:09 +0000304void V8ISel::LoadArgumentsToVirtualRegs (Function *F) {
305 unsigned ArgOffset = 0;
306 static const unsigned IncomingArgRegs[] = { V8::I0, V8::I1, V8::I2,
307 V8::I3, V8::I4, V8::I5 };
308 assert (F->asize () < 7
309 && "Can't handle loading excess call args off the stack yet");
310
311 for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I) {
312 unsigned Reg = getReg(*I);
313 switch (getClassB(I->getType())) {
314 case cByte:
315 case cShort:
316 case cInt:
317 BuildMI(BB, V8::ORrr, 2, Reg).addReg (V8::G0)
318 .addReg (IncomingArgRegs[ArgOffset]);
319 break;
320 default:
321 assert (0 && "Only <=32-bit, integral arguments currently handled");
322 return;
323 }
324 ++ArgOffset;
325 }
326}
327
Brian Gaeke6c868a42004-06-17 22:34:08 +0000328void V8ISel::SelectPHINodes() {
329 const TargetInstrInfo &TII = *TM.getInstrInfo();
330 const Function &LF = *F->getFunction(); // The LLVM function...
331 for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) {
332 const BasicBlock *BB = I;
333 MachineBasicBlock &MBB = *MBBMap[I];
334
335 // Loop over all of the PHI nodes in the LLVM basic block...
336 MachineBasicBlock::iterator PHIInsertPoint = MBB.begin();
337 for (BasicBlock::const_iterator I = BB->begin();
338 PHINode *PN = const_cast<PHINode*>(dyn_cast<PHINode>(I)); ++I) {
339
340 // Create a new machine instr PHI node, and insert it.
341 unsigned PHIReg = getReg(*PN);
342 MachineInstr *PhiMI = BuildMI(MBB, PHIInsertPoint,
343 V8::PHI, PN->getNumOperands(), PHIReg);
344
345 MachineInstr *LongPhiMI = 0;
346 if (PN->getType() == Type::LongTy || PN->getType() == Type::ULongTy)
347 LongPhiMI = BuildMI(MBB, PHIInsertPoint,
348 V8::PHI, PN->getNumOperands(), PHIReg+1);
349
350 // PHIValues - Map of blocks to incoming virtual registers. We use this
351 // so that we only initialize one incoming value for a particular block,
352 // even if the block has multiple entries in the PHI node.
353 //
354 std::map<MachineBasicBlock*, unsigned> PHIValues;
355
356 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
357 MachineBasicBlock *PredMBB = 0;
358 for (MachineBasicBlock::pred_iterator PI = MBB.pred_begin (),
359 PE = MBB.pred_end (); PI != PE; ++PI)
360 if (PN->getIncomingBlock(i) == (*PI)->getBasicBlock()) {
361 PredMBB = *PI;
362 break;
363 }
364 assert (PredMBB && "Couldn't find incoming machine-cfg edge for phi");
365
366 unsigned ValReg;
367 std::map<MachineBasicBlock*, unsigned>::iterator EntryIt =
368 PHIValues.lower_bound(PredMBB);
369
370 if (EntryIt != PHIValues.end() && EntryIt->first == PredMBB) {
371 // We already inserted an initialization of the register for this
372 // predecessor. Recycle it.
373 ValReg = EntryIt->second;
374
375 } else {
376 // Get the incoming value into a virtual register.
377 //
378 Value *Val = PN->getIncomingValue(i);
379
380 // If this is a constant or GlobalValue, we may have to insert code
381 // into the basic block to compute it into a virtual register.
382 if ((isa<Constant>(Val) && !isa<ConstantExpr>(Val)) ||
383 isa<GlobalValue>(Val)) {
384 // Simple constants get emitted at the end of the basic block,
385 // before any terminator instructions. We "know" that the code to
386 // move a constant into a register will never clobber any flags.
387 ValReg = getReg(Val, PredMBB, PredMBB->getFirstTerminator());
388 } else {
389 // Because we don't want to clobber any values which might be in
390 // physical registers with the computation of this constant (which
391 // might be arbitrarily complex if it is a constant expression),
392 // just insert the computation at the top of the basic block.
393 MachineBasicBlock::iterator PI = PredMBB->begin();
394
395 // Skip over any PHI nodes though!
396 while (PI != PredMBB->end() && PI->getOpcode() == V8::PHI)
397 ++PI;
398
399 ValReg = getReg(Val, PredMBB, PI);
400 }
401
402 // Remember that we inserted a value for this PHI for this predecessor
403 PHIValues.insert(EntryIt, std::make_pair(PredMBB, ValReg));
404 }
405
406 PhiMI->addRegOperand(ValReg);
407 PhiMI->addMachineBasicBlockOperand(PredMBB);
408 if (LongPhiMI) {
409 LongPhiMI->addRegOperand(ValReg+1);
410 LongPhiMI->addMachineBasicBlockOperand(PredMBB);
411 }
412 }
413
414 // Now that we emitted all of the incoming values for the PHI node, make
415 // sure to reposition the InsertPoint after the PHI that we just added.
416 // This is needed because we might have inserted a constant into this
417 // block, right after the PHI's which is before the old insert point!
418 PHIInsertPoint = LongPhiMI ? LongPhiMI : PhiMI;
419 ++PHIInsertPoint;
420 }
421 }
422}
423
Chris Lattner1c809c52004-02-29 00:27:00 +0000424bool V8ISel::runOnFunction(Function &Fn) {
425 // First pass over the function, lower any unknown intrinsic functions
426 // with the IntrinsicLowering class.
427 LowerUnknownIntrinsicFunctionCalls(Fn);
428
429 F = &MachineFunction::construct(&Fn, TM);
430
431 // Create all of the machine basic blocks for the function...
432 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
433 F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I));
434
435 BB = &F->front();
436
437 // Set up a frame object for the return address. This is used by the
438 // llvm.returnaddress & llvm.frameaddress intrinisics.
439 //ReturnAddressIndex = F->getFrameInfo()->CreateFixedObject(4, -4);
440
441 // Copy incoming arguments off of the stack and out of fixed registers.
Brian Gaeke562cb162004-04-07 17:04:09 +0000442 LoadArgumentsToVirtualRegs(&Fn);
Chris Lattner1c809c52004-02-29 00:27:00 +0000443
444 // Instruction select everything except PHI nodes
445 visit(Fn);
446
447 // Select the PHI nodes
Brian Gaeke6c868a42004-06-17 22:34:08 +0000448 SelectPHINodes();
Chris Lattner1c809c52004-02-29 00:27:00 +0000449
450 RegMap.clear();
451 MBBMap.clear();
452 F = 0;
453 // We always build a machine code representation for the function
454 return true;
455}
456
Brian Gaeke3d11e8a2004-04-13 18:27:46 +0000457void V8ISel::visitCastInst(CastInst &I) {
Brian Gaeke00e514e2004-06-24 06:33:00 +0000458 Value *Op = I.getOperand(0);
459 unsigned DestReg = getReg(I);
460 MachineBasicBlock::iterator MI = BB->end();
461 emitCastOperation(BB, MI, Op, I.getType(), DestReg);
462}
463
464/// emitCastOperation - Common code shared between visitCastInst and constant
465/// expression cast support.
466///
467void V8ISel::emitCastOperation(MachineBasicBlock *BB,
468 MachineBasicBlock::iterator IP,
469 Value *Src, const Type *DestTy,
470 unsigned DestReg) {
471 const Type *SrcTy = Src->getType();
472 unsigned SrcClass = getClassB(SrcTy);
473 unsigned DestClass = getClassB(DestTy);
474 unsigned SrcReg = getReg(Src, BB, IP);
475
476 const Type *oldTy = SrcTy;
477 const Type *newTy = DestTy;
478 unsigned oldTyClass = SrcClass;
479 unsigned newTyClass = DestClass;
Brian Gaeke3d11e8a2004-04-13 18:27:46 +0000480
Brian Gaeke429022b2004-05-08 06:36:14 +0000481 if (oldTyClass < cLong && newTyClass < cLong) {
482 if (oldTyClass >= newTyClass) {
483 // Emit a reg->reg copy to do a equal-size or narrowing cast,
484 // and do sign/zero extension (necessary if we change signedness).
485 unsigned TmpReg1 = makeAnotherReg (newTy);
486 unsigned TmpReg2 = makeAnotherReg (newTy);
Brian Gaeke00e514e2004-06-24 06:33:00 +0000487 BuildMI (*BB, IP, V8::ORrr, 2, TmpReg1).addReg (V8::G0).addReg (SrcReg);
Brian Gaeke429022b2004-05-08 06:36:14 +0000488 unsigned shiftWidth = 32 - (8 * TM.getTargetData ().getTypeSize (newTy));
Brian Gaeke00e514e2004-06-24 06:33:00 +0000489 BuildMI (*BB, IP, V8::SLLri, 2, TmpReg2).addZImm (shiftWidth).addReg(TmpReg1);
Brian Gaeke429022b2004-05-08 06:36:14 +0000490 if (newTy->isSigned ()) { // sign-extend with SRA
Brian Gaeke00e514e2004-06-24 06:33:00 +0000491 BuildMI(*BB, IP, V8::SRAri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg2);
Brian Gaeke429022b2004-05-08 06:36:14 +0000492 } else { // zero-extend with SRL
Brian Gaeke00e514e2004-06-24 06:33:00 +0000493 BuildMI(*BB, IP, V8::SRLri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg2);
Brian Gaeke429022b2004-05-08 06:36:14 +0000494 }
495 } else {
496 unsigned TmpReg1 = makeAnotherReg (oldTy);
497 unsigned TmpReg2 = makeAnotherReg (newTy);
498 unsigned TmpReg3 = makeAnotherReg (newTy);
499 // Widening integer cast. Make sure it's fully sign/zero-extended
500 // wrt the input type, then make sure it's fully sign/zero-extended wrt
501 // the output type. Kind of stupid, but simple...
502 unsigned shiftWidth = 32 - (8 * TM.getTargetData ().getTypeSize (oldTy));
Brian Gaeke00e514e2004-06-24 06:33:00 +0000503 BuildMI (*BB, IP, V8::SLLri, 2, TmpReg1).addZImm (shiftWidth).addReg(SrcReg);
Brian Gaeke429022b2004-05-08 06:36:14 +0000504 if (oldTy->isSigned ()) { // sign-extend with SRA
Brian Gaeke00e514e2004-06-24 06:33:00 +0000505 BuildMI(*BB, IP, V8::SRAri, 2, TmpReg2).addZImm (shiftWidth).addReg(TmpReg1);
Brian Gaeke429022b2004-05-08 06:36:14 +0000506 } else { // zero-extend with SRL
Brian Gaeke00e514e2004-06-24 06:33:00 +0000507 BuildMI(*BB, IP, V8::SRLri, 2, TmpReg2).addZImm (shiftWidth).addReg(TmpReg1);
Brian Gaeke429022b2004-05-08 06:36:14 +0000508 }
509 shiftWidth = 32 - (8 * TM.getTargetData ().getTypeSize (newTy));
Brian Gaeke00e514e2004-06-24 06:33:00 +0000510 BuildMI (*BB, IP, V8::SLLri, 2, TmpReg3).addZImm (shiftWidth).addReg(TmpReg2);
Brian Gaeke429022b2004-05-08 06:36:14 +0000511 if (newTy->isSigned ()) { // sign-extend with SRA
Brian Gaeke00e514e2004-06-24 06:33:00 +0000512 BuildMI(*BB, IP, V8::SRAri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg3);
Brian Gaeke429022b2004-05-08 06:36:14 +0000513 } else { // zero-extend with SRL
Brian Gaeke00e514e2004-06-24 06:33:00 +0000514 BuildMI(*BB, IP, V8::SRLri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg3);
Brian Gaeke429022b2004-05-08 06:36:14 +0000515 }
Brian Gaekee302a7e2004-05-07 21:39:30 +0000516 }
517 } else {
Brian Gaeke495a0972004-06-24 21:22:08 +0000518 if (newTyClass == cFloat) {
Brian Gaekeec3227f2004-06-27 22:47:33 +0000519 assert (oldTyClass != cLong && "cast long to float not implemented yet");
Brian Gaeke495a0972004-06-24 21:22:08 +0000520 switch (oldTyClass) {
521 case cFloat:
522 BuildMI (*BB, IP, V8::FMOVS, 1, DestReg).addReg (SrcReg);
523 break;
524 case cDouble:
525 BuildMI (*BB, IP, V8::FDTOS, 1, DestReg).addReg (SrcReg);
526 break;
Brian Gaekeec3227f2004-06-27 22:47:33 +0000527 default: {
528 unsigned FltAlign = TM.getTargetData().getFloatAlignment();
Brian Gaeke495a0972004-06-24 21:22:08 +0000529 // cast int to float. Store it to a stack slot and then load
530 // it using ldf into a floating point register. then do fitos.
Brian Gaekeec3227f2004-06-27 22:47:33 +0000531 unsigned TmpReg = makeAnotherReg (newTy);
532 int FI = F->getFrameInfo()->CreateStackObject(4, FltAlign);
533 BuildMI (*BB, IP, V8::ST, 3).addFrameIndex (FI).addSImm (0)
534 .addReg (SrcReg);
535 BuildMI (*BB, IP, V8::LDFri, 2, TmpReg).addFrameIndex (FI).addSImm (0);
536 BuildMI (*BB, IP, V8::FITOS, 1, DestReg).addReg(TmpReg);
Brian Gaeke495a0972004-06-24 21:22:08 +0000537 break;
538 }
Brian Gaekeec3227f2004-06-27 22:47:33 +0000539 }
Brian Gaeke495a0972004-06-24 21:22:08 +0000540 } else if (newTyClass == cDouble) {
Brian Gaekeec3227f2004-06-27 22:47:33 +0000541 assert (oldTyClass != cLong && "cast long to double not implemented yet");
Brian Gaeke495a0972004-06-24 21:22:08 +0000542 switch (oldTyClass) {
543 case cFloat:
544 BuildMI (*BB, IP, V8::FSTOD, 1, DestReg).addReg (SrcReg);
545 break;
Brian Gaekeec3227f2004-06-27 22:47:33 +0000546 case cDouble: {
547 // go through memory, for now
548 unsigned DoubleAlignment = TM.getTargetData().getDoubleAlignment();
549 int FI = F->getFrameInfo()->CreateStackObject(8, DoubleAlignment);
550 BuildMI (*BB, IP, V8::STDFri, 3).addFrameIndex (FI).addSImm (0)
551 .addReg (SrcReg);
552 BuildMI (*BB, IP, V8::LDDFri, 2, DestReg).addFrameIndex (FI)
553 .addSImm (0);
Brian Gaeke495a0972004-06-24 21:22:08 +0000554 break;
555 }
Brian Gaekeec3227f2004-06-27 22:47:33 +0000556 default: {
557 unsigned DoubleAlignment = TM.getTargetData().getDoubleAlignment();
558 unsigned TmpReg = makeAnotherReg (newTy);
559 int FI = F->getFrameInfo()->CreateStackObject(8, DoubleAlignment);
560 BuildMI (*BB, IP, V8::ST, 3).addFrameIndex (FI).addSImm (0)
561 .addReg (SrcReg);
562 BuildMI (*BB, IP, V8::LDDFri, 2, TmpReg).addFrameIndex (FI).addSImm (0);
563 BuildMI (*BB, IP, V8::FITOD, 1, DestReg).addReg(TmpReg);
564 break;
565 }
566 }
Brian Gaeke44733032004-06-24 07:36:48 +0000567 } else {
568 std::cerr << "Cast still unsupported: SrcTy = "
569 << *SrcTy << ", DestTy = " << *DestTy << "\n";
570 abort ();
571 }
Brian Gaekee302a7e2004-05-07 21:39:30 +0000572 }
Brian Gaeke3d11e8a2004-04-13 18:27:46 +0000573}
574
Brian Gaekef3334eb2004-04-07 17:29:37 +0000575void V8ISel::visitLoadInst(LoadInst &I) {
576 unsigned DestReg = getReg (I);
577 unsigned PtrReg = getReg (I.getOperand (0));
Brian Gaeke532e60c2004-05-08 04:21:17 +0000578 switch (getClassB (I.getType ())) {
Brian Gaekef3334eb2004-04-07 17:29:37 +0000579 case cByte:
580 if (I.getType ()->isSigned ())
Brian Gaeke44733032004-06-24 07:36:48 +0000581 BuildMI (BB, V8::LDSB, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000582 else
Brian Gaeke44733032004-06-24 07:36:48 +0000583 BuildMI (BB, V8::LDUB, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000584 return;
585 case cShort:
586 if (I.getType ()->isSigned ())
Brian Gaeke44733032004-06-24 07:36:48 +0000587 BuildMI (BB, V8::LDSH, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000588 else
Brian Gaeke44733032004-06-24 07:36:48 +0000589 BuildMI (BB, V8::LDUH, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000590 return;
591 case cInt:
Brian Gaeke44733032004-06-24 07:36:48 +0000592 BuildMI (BB, V8::LD, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000593 return;
594 case cLong:
Brian Gaeke44733032004-06-24 07:36:48 +0000595 BuildMI (BB, V8::LD, 2, DestReg).addReg (PtrReg).addSImm(0);
596 BuildMI (BB, V8::LD, 2, DestReg+1).addReg (PtrReg).addSImm(4);
597 return;
598 case cFloat:
599 BuildMI (BB, V8::LDFri, 2, DestReg).addReg (PtrReg).addSImm(0);
600 return;
601 case cDouble:
602 BuildMI (BB, V8::LDDFri, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000603 return;
604 default:
605 std::cerr << "Load instruction not handled: " << I;
606 abort ();
607 return;
608 }
609}
610
611void V8ISel::visitStoreInst(StoreInst &I) {
Brian Gaeke532e60c2004-05-08 04:21:17 +0000612 Value *SrcVal = I.getOperand (0);
613 unsigned SrcReg = getReg (SrcVal);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000614 unsigned PtrReg = getReg (I.getOperand (1));
Brian Gaeke532e60c2004-05-08 04:21:17 +0000615 switch (getClassB (SrcVal->getType ())) {
616 case cByte:
Brian Gaeke44733032004-06-24 07:36:48 +0000617 BuildMI (BB, V8::STB, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000618 return;
619 case cShort:
Brian Gaeke44733032004-06-24 07:36:48 +0000620 BuildMI (BB, V8::STH, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000621 return;
622 case cInt:
Brian Gaeke44733032004-06-24 07:36:48 +0000623 BuildMI (BB, V8::ST, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000624 return;
625 case cLong:
Brian Gaeke44733032004-06-24 07:36:48 +0000626 BuildMI (BB, V8::ST, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
627 BuildMI (BB, V8::ST, 3).addReg (PtrReg).addSImm (4).addReg (SrcReg+1);
628 return;
629 case cFloat:
630 BuildMI (BB, V8::STFri, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
631 return;
632 case cDouble:
633 BuildMI (BB, V8::STDFri, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000634 return;
635 default:
636 std::cerr << "Store instruction not handled: " << I;
637 abort ();
638 return;
639 }
Brian Gaekef3334eb2004-04-07 17:29:37 +0000640}
641
Brian Gaekef7e44ef2004-04-02 20:53:33 +0000642void V8ISel::visitCallInst(CallInst &I) {
Brian Gaeke9d67ea02004-06-18 06:27:48 +0000643 MachineInstr *TheCall;
644 // Is it an intrinsic function call?
645 if (Function *F = I.getCalledFunction()) {
646 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) {
647 visitIntrinsicCall(ID, I); // Special intrinsics are not handled here
648 return;
649 }
650 }
651
652 // Deal with args
Brian Gaeked54c38b2004-04-07 16:41:22 +0000653 assert (I.getNumOperands () < 8
654 && "Can't handle pushing excess call args on the stack yet");
Brian Gaeke562cb162004-04-07 17:04:09 +0000655 static const unsigned OutgoingArgRegs[] = { V8::O0, V8::O1, V8::O2, V8::O3,
Brian Gaeked54c38b2004-04-07 16:41:22 +0000656 V8::O4, V8::O5 };
657 for (unsigned i = 1; i < 7; ++i)
658 if (i < I.getNumOperands ()) {
Brian Gaekeec3227f2004-06-27 22:47:33 +0000659 assert (getClassB (I.getOperand (i)->getType ()) < cLong
660 && "Can't handle long or fp function call arguments yet");
Brian Gaeked54c38b2004-04-07 16:41:22 +0000661 unsigned ArgReg = getReg (I.getOperand (i));
662 // Schlep it over into the incoming arg register
Brian Gaeke562cb162004-04-07 17:04:09 +0000663 BuildMI (BB, V8::ORrr, 2, OutgoingArgRegs[i - 1]).addReg (V8::G0)
Brian Gaeked54c38b2004-04-07 16:41:22 +0000664 .addReg (ArgReg);
665 }
666
Brian Gaeke9d67ea02004-06-18 06:27:48 +0000667 // Emit call instruction
668 if (Function *F = I.getCalledFunction ()) {
669 BuildMI (BB, V8::CALL, 1).addGlobalAddress (F, true);
670 } else { // Emit an indirect call...
671 unsigned Reg = getReg (I.getCalledValue ());
672 BuildMI (BB, V8::JMPLrr, 3, V8::O7).addReg (Reg).addReg (V8::G0);
673 }
674
675 // Deal w/ return value: schlep it over into the destination register
Brian Gaekee14e3382004-06-15 20:06:32 +0000676 if (I.getType () == Type::VoidTy)
Brian Gaekeea8494b2004-04-06 22:09:23 +0000677 return;
Brian Gaekee14e3382004-06-15 20:06:32 +0000678 unsigned DestReg = getReg (I);
Brian Gaekeea8494b2004-04-06 22:09:23 +0000679 switch (getClass (I.getType ())) {
680 case cByte:
681 case cShort:
682 case cInt:
Brian Gaekeea8494b2004-04-06 22:09:23 +0000683 BuildMI (BB, V8::ORrr, 2, DestReg).addReg(V8::G0).addReg(V8::O0);
684 break;
Brian Gaeke9d67ea02004-06-18 06:27:48 +0000685 case cFloat:
686 BuildMI (BB, V8::FMOVS, 2, DestReg).addReg(V8::F0);
687 break;
Brian Gaekeea8494b2004-04-06 22:09:23 +0000688 default:
Brian Gaeke532e60c2004-05-08 04:21:17 +0000689 std::cerr << "Return type of call instruction not handled: " << I;
690 abort ();
Brian Gaekeea8494b2004-04-06 22:09:23 +0000691 }
Brian Gaekef7e44ef2004-04-02 20:53:33 +0000692}
Chris Lattner1c809c52004-02-29 00:27:00 +0000693
694void V8ISel::visitReturnInst(ReturnInst &I) {
Brian Gaeke08f64c32004-03-06 05:32:28 +0000695 if (I.getNumOperands () == 1) {
696 unsigned RetValReg = getReg (I.getOperand (0));
697 switch (getClass (I.getOperand (0)->getType ())) {
698 case cByte:
699 case cShort:
700 case cInt:
701 // Schlep it over into i0 (where it will become o0 after restore).
702 BuildMI (BB, V8::ORrr, 2, V8::I0).addReg(V8::G0).addReg(RetValReg);
703 break;
704 default:
Brian Gaeke532e60c2004-05-08 04:21:17 +0000705 std::cerr << "Return instruction of this type not handled: " << I;
706 abort ();
Brian Gaeke08f64c32004-03-06 05:32:28 +0000707 }
Chris Lattner1c809c52004-02-29 00:27:00 +0000708 }
Chris Lattner0d538bb2004-04-07 04:36:53 +0000709
Brian Gaeke08f64c32004-03-06 05:32:28 +0000710 // Just emit a 'retl' instruction to return.
711 BuildMI(BB, V8::RETL, 0);
712 return;
Chris Lattner1c809c52004-02-29 00:27:00 +0000713}
714
Brian Gaeke532e60c2004-05-08 04:21:17 +0000715static inline BasicBlock *getBlockAfter(BasicBlock *BB) {
716 Function::iterator I = BB; ++I; // Get iterator to next block
717 return I != BB->getParent()->end() ? &*I : 0;
718}
719
720/// visitBranchInst - Handles conditional and unconditional branches.
721///
722void V8ISel::visitBranchInst(BranchInst &I) {
Brian Gaeke532e60c2004-05-08 04:21:17 +0000723 BasicBlock *takenSucc = I.getSuccessor (0);
Brian Gaeke6c868a42004-06-17 22:34:08 +0000724 MachineBasicBlock *takenSuccMBB = MBBMap[takenSucc];
725 BB->addSuccessor (takenSuccMBB);
726 if (I.isConditional()) { // conditional branch
727 BasicBlock *notTakenSucc = I.getSuccessor (1);
728 MachineBasicBlock *notTakenSuccMBB = MBBMap[notTakenSucc];
729 BB->addSuccessor (notTakenSuccMBB);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000730
Brian Gaeke6c868a42004-06-17 22:34:08 +0000731 // CondReg=(<condition>);
732 // If (CondReg==0) goto notTakenSuccMBB;
733 unsigned CondReg = getReg (I.getCondition ());
734 BuildMI (BB, V8::CMPri, 2).addSImm (0).addReg (CondReg);
735 BuildMI (BB, V8::BE, 1).addMBB (notTakenSuccMBB);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000736 }
Brian Gaeke6c868a42004-06-17 22:34:08 +0000737 // goto takenSuccMBB;
738 BuildMI (BB, V8::BA, 1).addMBB (takenSuccMBB);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000739}
740
741/// emitGEPOperation - Common code shared between visitGetElementPtrInst and
742/// constant expression GEP support.
743///
Brian Gaeke9f564822004-05-08 05:27:20 +0000744void V8ISel::emitGEPOperation (MachineBasicBlock *MBB,
Brian Gaeke532e60c2004-05-08 04:21:17 +0000745 MachineBasicBlock::iterator IP,
746 Value *Src, User::op_iterator IdxBegin,
747 User::op_iterator IdxEnd, unsigned TargetReg) {
Brian Gaeke9f564822004-05-08 05:27:20 +0000748 const TargetData &TD = TM.getTargetData ();
749 const Type *Ty = Src->getType ();
Brian Gaekec7fd0f42004-06-24 08:55:09 +0000750 unsigned basePtrReg = getReg (Src, MBB, IP);
Brian Gaeke9f564822004-05-08 05:27:20 +0000751
752 // GEPs have zero or more indices; we must perform a struct access
753 // or array access for each one.
754 for (GetElementPtrInst::op_iterator oi = IdxBegin, oe = IdxEnd; oi != oe;
755 ++oi) {
756 Value *idx = *oi;
757 unsigned nextBasePtrReg = makeAnotherReg (Type::UIntTy);
758 if (const StructType *StTy = dyn_cast<StructType> (Ty)) {
759 // It's a struct access. idx is the index into the structure,
760 // which names the field. Use the TargetData structure to
761 // pick out what the layout of the structure is in memory.
762 // Use the (constant) structure index's value to find the
763 // right byte offset from the StructLayout class's list of
764 // structure member offsets.
765 unsigned fieldIndex = cast<ConstantUInt> (idx)->getValue ();
766 unsigned memberOffset =
767 TD.getStructLayout (StTy)->MemberOffsets[fieldIndex];
768 // Emit an ADD to add memberOffset to the basePtr.
769 BuildMI (*MBB, IP, V8::ADDri, 2,
770 nextBasePtrReg).addReg (basePtrReg).addZImm (memberOffset);
771 // The next type is the member of the structure selected by the
772 // index.
773 Ty = StTy->getElementType (fieldIndex);
774 } else if (const SequentialType *SqTy = dyn_cast<SequentialType> (Ty)) {
775 // It's an array or pointer access: [ArraySize x ElementType].
776 // We want to add basePtrReg to (idxReg * sizeof ElementType). First, we
777 // must find the size of the pointed-to type (Not coincidentally, the next
778 // type is the type of the elements in the array).
779 Ty = SqTy->getElementType ();
780 unsigned elementSize = TD.getTypeSize (Ty);
781 unsigned idxReg = getReg (idx, MBB, IP);
782 unsigned OffsetReg = makeAnotherReg (Type::IntTy);
783 unsigned elementSizeReg = makeAnotherReg (Type::UIntTy);
Brian Gaekec7fd0f42004-06-24 08:55:09 +0000784 copyConstantToRegister (MBB, IP,
785 ConstantUInt::get(Type::UIntTy, elementSize), elementSizeReg);
Brian Gaeke9f564822004-05-08 05:27:20 +0000786 // Emit a SMUL to multiply the register holding the index by
787 // elementSize, putting the result in OffsetReg.
788 BuildMI (*MBB, IP, V8::SMULrr, 2,
789 OffsetReg).addReg (elementSizeReg).addReg (idxReg);
790 // Emit an ADD to add OffsetReg to the basePtr.
791 BuildMI (*MBB, IP, V8::ADDrr, 2,
792 nextBasePtrReg).addReg (basePtrReg).addReg (OffsetReg);
793 }
794 basePtrReg = nextBasePtrReg;
795 }
796 // After we have processed all the indices, the result is left in
797 // basePtrReg. Move it to the register where we were expected to
798 // put the answer.
799 BuildMI (BB, V8::ORrr, 1, TargetReg).addReg (V8::G0).addReg (basePtrReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000800}
801
802void V8ISel::visitGetElementPtrInst (GetElementPtrInst &I) {
803 unsigned outputReg = getReg (I);
804 emitGEPOperation (BB, BB->end (), I.getOperand (0),
805 I.op_begin ()+1, I.op_end (), outputReg);
806}
807
Brian Gaeked6a10532004-06-15 21:09:46 +0000808
Chris Lattner4be7ca52004-04-07 04:27:16 +0000809void V8ISel::visitBinaryOperator (Instruction &I) {
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000810 unsigned DestReg = getReg (I);
811 unsigned Op0Reg = getReg (I.getOperand (0));
812 unsigned Op1Reg = getReg (I.getOperand (1));
813
Brian Gaekeec3227f2004-06-27 22:47:33 +0000814 unsigned Class = getClassB (I.getType());
Chris Lattner22ede702004-04-07 04:06:46 +0000815 unsigned OpCase = ~0;
816
Brian Gaekeec3227f2004-06-27 22:47:33 +0000817 if (Class > cLong) {
818 switch (I.getOpcode ()) {
819 case Instruction::Add: OpCase = 0; break;
820 case Instruction::Sub: OpCase = 1; break;
821 case Instruction::Mul: OpCase = 2; break;
822 case Instruction::Div: OpCase = 3; break;
823 default: visitInstruction (I); return;
824 }
825 static unsigned Opcodes[] = { V8::FADDS, V8::FADDD,
826 V8::FSUBS, V8::FSUBD,
827 V8::FMULS, V8::FMULD,
828 V8::FDIVS, V8::FDIVD };
829 BuildMI (BB, Opcodes[2*OpCase + (Class - cFloat)], 2, DestReg)
830 .addReg (Op0Reg).addReg (Op1Reg);
831 return;
832 }
833
834 unsigned ResultReg = DestReg;
835 if (Class != cInt)
836 ResultReg = makeAnotherReg (I.getType ());
837
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +0000838 // FIXME: support long, ulong, fp.
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000839 switch (I.getOpcode ()) {
Chris Lattner22ede702004-04-07 04:06:46 +0000840 case Instruction::Add: OpCase = 0; break;
841 case Instruction::Sub: OpCase = 1; break;
842 case Instruction::Mul: OpCase = 2; break;
843 case Instruction::And: OpCase = 3; break;
844 case Instruction::Or: OpCase = 4; break;
845 case Instruction::Xor: OpCase = 5; break;
Chris Lattner4be7ca52004-04-07 04:27:16 +0000846 case Instruction::Shl: OpCase = 6; break;
847 case Instruction::Shr: OpCase = 7+I.getType()->isSigned(); break;
Chris Lattner22ede702004-04-07 04:06:46 +0000848
849 case Instruction::Div:
850 case Instruction::Rem: {
851 unsigned Dest = ResultReg;
852 if (I.getOpcode() == Instruction::Rem)
853 Dest = makeAnotherReg(I.getType());
854
855 // FIXME: this is probably only right for 32 bit operands.
856 if (I.getType ()->isSigned()) {
857 unsigned Tmp = makeAnotherReg (I.getType ());
858 // Sign extend into the Y register
859 BuildMI (BB, V8::SRAri, 2, Tmp).addReg (Op0Reg).addZImm (31);
860 BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (Tmp).addReg (V8::G0);
861 BuildMI (BB, V8::SDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg);
862 } else {
863 // Zero extend into the Y register, ie, just set it to zero
864 BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (V8::G0).addReg (V8::G0);
865 BuildMI (BB, V8::UDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg);
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +0000866 }
Chris Lattner22ede702004-04-07 04:06:46 +0000867
868 if (I.getOpcode() == Instruction::Rem) {
869 unsigned Tmp = makeAnotherReg (I.getType ());
870 BuildMI (BB, V8::SMULrr, 2, Tmp).addReg(Dest).addReg(Op1Reg);
871 BuildMI (BB, V8::SUBrr, 2, ResultReg).addReg(Op0Reg).addReg(Tmp);
Brian Gaekef57e3642004-03-16 22:37:11 +0000872 }
Chris Lattner22ede702004-04-07 04:06:46 +0000873 break;
874 }
875 default:
876 visitInstruction (I);
877 return;
878 }
879
Brian Gaekec7fd0f42004-06-24 08:55:09 +0000880 static const unsigned Opcodes[] = {
881 V8::ADDrr, V8::SUBrr, V8::SMULrr, V8::ANDrr, V8::ORrr, V8::XORrr,
882 V8::SLLrr, V8::SRLrr, V8::SRArr
883 };
Chris Lattner22ede702004-04-07 04:06:46 +0000884 if (OpCase != ~0U) {
Chris Lattner22ede702004-04-07 04:06:46 +0000885 BuildMI (BB, Opcodes[OpCase], 2, ResultReg).addReg (Op0Reg).addReg (Op1Reg);
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000886 }
887
888 switch (getClass (I.getType ())) {
889 case cByte:
Brian Gaeke08f64c32004-03-06 05:32:28 +0000890 if (I.getType ()->isSigned ()) { // add byte
891 BuildMI (BB, V8::ANDri, 2, DestReg).addReg (ResultReg).addZImm (0xff);
892 } else { // add ubyte
893 unsigned TmpReg = makeAnotherReg (I.getType ());
894 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (24);
895 BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (24);
896 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000897 break;
898 case cShort:
Brian Gaeke08f64c32004-03-06 05:32:28 +0000899 if (I.getType ()->isSigned ()) { // add short
900 unsigned TmpReg = makeAnotherReg (I.getType ());
901 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16);
902 BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (16);
903 } else { // add ushort
904 unsigned TmpReg = makeAnotherReg (I.getType ());
Brian Gaeke6d339f92004-03-16 22:45:42 +0000905 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16);
906 BuildMI (BB, V8::SRLri, 2, DestReg).addReg (TmpReg).addZImm (16);
Brian Gaeke08f64c32004-03-06 05:32:28 +0000907 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000908 break;
909 case cInt:
Chris Lattner0d538bb2004-04-07 04:36:53 +0000910 // Nothing todo here.
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000911 break;
Brian Gaekec7fd0f42004-06-24 08:55:09 +0000912 case cLong:
913 // Only support and, or, xor.
914 if (OpCase < 3 || OpCase > 5) {
915 visitInstruction (I);
916 return;
917 }
918 // Do the other half of the value:
Brian Gaekeec3227f2004-06-27 22:47:33 +0000919 BuildMI (BB, Opcodes[OpCase], 2, ResultReg+1).addReg (Op0Reg+1)
920 .addReg (Op1Reg+1);
Brian Gaekec7fd0f42004-06-24 08:55:09 +0000921 break;
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000922 default:
Brian Gaeke08f64c32004-03-06 05:32:28 +0000923 visitInstruction (I);
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000924 }
925}
926
Misha Brukmanea091262004-06-30 21:47:40 +0000927void V8ISel::visitSetCondInst(SetCondInst &I) {
Chris Lattner4d0cda42004-04-07 05:04:51 +0000928 unsigned Op0Reg = getReg (I.getOperand (0));
929 unsigned Op1Reg = getReg (I.getOperand (1));
930 unsigned DestReg = getReg (I);
Brian Gaeke429022b2004-05-08 06:36:14 +0000931 const Type *Ty = I.getOperand (0)->getType ();
Chris Lattner4d0cda42004-04-07 05:04:51 +0000932
Brian Gaekeec3227f2004-06-27 22:47:33 +0000933 assert (getClass (Ty) < cLong && "can't setcc on longs or fp yet");
Chris Lattner4d0cda42004-04-07 05:04:51 +0000934 // Compare the two values.
935 BuildMI(BB, V8::SUBCCrr, 2, V8::G0).addReg(Op0Reg).addReg(Op1Reg);
936
Brian Gaeke429022b2004-05-08 06:36:14 +0000937 unsigned BranchIdx;
Chris Lattner4d0cda42004-04-07 05:04:51 +0000938 switch (I.getOpcode()) {
939 default: assert(0 && "Unknown setcc instruction!");
Brian Gaeke429022b2004-05-08 06:36:14 +0000940 case Instruction::SetEQ: BranchIdx = 0; break;
941 case Instruction::SetNE: BranchIdx = 1; break;
942 case Instruction::SetLT: BranchIdx = 2; break;
943 case Instruction::SetGT: BranchIdx = 3; break;
944 case Instruction::SetLE: BranchIdx = 4; break;
945 case Instruction::SetGE: BranchIdx = 5; break;
Chris Lattner4d0cda42004-04-07 05:04:51 +0000946 }
Brian Gaeke429022b2004-05-08 06:36:14 +0000947 static unsigned OpcodeTab[12] = {
Misha Brukmand2d5df22004-06-30 22:11:03 +0000948 // LLVM SparcV8
949 // unsigned signed
950 V8::BE, V8::BE, // seteq = be be
951 V8::BNE, V8::BNE, // setne = bne bne
952 V8::BCS, V8::BL, // setlt = bcs bl
953 V8::BGU, V8::BG, // setgt = bgu bg
954 V8::BLEU, V8::BLE, // setle = bleu ble
955 V8::BCC, V8::BGE // setge = bcc bge
Brian Gaeke429022b2004-05-08 06:36:14 +0000956 };
Brian Gaeke6c868a42004-06-17 22:34:08 +0000957 unsigned Opcode = OpcodeTab[2*BranchIdx + (Ty->isSigned() ? 1 : 0)];
958
959 MachineBasicBlock *thisMBB = BB;
960 const BasicBlock *LLVM_BB = BB->getBasicBlock ();
961 // thisMBB:
962 // ...
963 // subcc %reg0, %reg1, %g0
964 // bCC copy1MBB
965 // ba copy0MBB
966
967 // FIXME: we wouldn't need copy0MBB (we could fold it into thisMBB)
968 // if we could insert other, non-terminator instructions after the
969 // bCC. But MBB->getFirstTerminator() can't understand this.
970 MachineBasicBlock *copy1MBB = new MachineBasicBlock (LLVM_BB);
971 F->getBasicBlockList ().push_back (copy1MBB);
972 BuildMI (BB, Opcode, 1).addMBB (copy1MBB);
973 MachineBasicBlock *copy0MBB = new MachineBasicBlock (LLVM_BB);
974 F->getBasicBlockList ().push_back (copy0MBB);
975 BuildMI (BB, V8::BA, 1).addMBB (copy0MBB);
976 // Update machine-CFG edges
977 BB->addSuccessor (copy1MBB);
978 BB->addSuccessor (copy0MBB);
979
980 // copy0MBB:
981 // %FalseValue = or %G0, 0
982 // ba sinkMBB
983 BB = copy0MBB;
984 unsigned FalseValue = makeAnotherReg (I.getType ());
985 BuildMI (BB, V8::ORri, 2, FalseValue).addReg (V8::G0).addZImm (0);
986 MachineBasicBlock *sinkMBB = new MachineBasicBlock (LLVM_BB);
987 F->getBasicBlockList ().push_back (sinkMBB);
988 BuildMI (BB, V8::BA, 1).addMBB (sinkMBB);
989 // Update machine-CFG edges
990 BB->addSuccessor (sinkMBB);
991
992 DEBUG (std::cerr << "thisMBB is at " << (void*)thisMBB << "\n");
993 DEBUG (std::cerr << "copy1MBB is at " << (void*)copy1MBB << "\n");
994 DEBUG (std::cerr << "copy0MBB is at " << (void*)copy0MBB << "\n");
995 DEBUG (std::cerr << "sinkMBB is at " << (void*)sinkMBB << "\n");
996
997 // copy1MBB:
998 // %TrueValue = or %G0, 1
999 // ba sinkMBB
1000 BB = copy1MBB;
1001 unsigned TrueValue = makeAnotherReg (I.getType ());
1002 BuildMI (BB, V8::ORri, 2, TrueValue).addReg (V8::G0).addZImm (1);
1003 BuildMI (BB, V8::BA, 1).addMBB (sinkMBB);
1004 // Update machine-CFG edges
1005 BB->addSuccessor (sinkMBB);
1006
1007 // sinkMBB:
1008 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, copy1MBB ]
1009 // ...
1010 BB = sinkMBB;
1011 BuildMI (BB, V8::PHI, 4, DestReg).addReg (FalseValue)
1012 .addMBB (copy0MBB).addReg (TrueValue).addMBB (copy1MBB);
Chris Lattner4d0cda42004-04-07 05:04:51 +00001013}
1014
Brian Gaekec93a7522004-06-18 05:19:16 +00001015void V8ISel::visitAllocaInst(AllocaInst &I) {
1016 // Find the data size of the alloca inst's getAllocatedType.
1017 const Type *Ty = I.getAllocatedType();
1018 unsigned TySize = TM.getTargetData().getTypeSize(Ty);
Chris Lattner4d0cda42004-04-07 05:04:51 +00001019
Brian Gaekec93a7522004-06-18 05:19:16 +00001020 unsigned ArraySizeReg = getReg (I.getArraySize ());
1021 unsigned TySizeReg = getReg (ConstantUInt::get (Type::UIntTy, TySize));
1022 unsigned TmpReg1 = makeAnotherReg (Type::UIntTy);
1023 unsigned TmpReg2 = makeAnotherReg (Type::UIntTy);
1024 unsigned StackAdjReg = makeAnotherReg (Type::UIntTy);
Brian Gaekec93a7522004-06-18 05:19:16 +00001025
1026 // StackAdjReg = (ArraySize * TySize) rounded up to nearest doubleword boundary
1027 BuildMI (BB, V8::UMULrr, 2, TmpReg1).addReg (ArraySizeReg).addReg (TySizeReg);
Brian Gaekecfaf2242004-06-18 08:45:52 +00001028
Brian Gaekec93a7522004-06-18 05:19:16 +00001029 // Round up TmpReg1 to nearest doubleword boundary:
1030 BuildMI (BB, V8::ADDri, 2, TmpReg2).addReg (TmpReg1).addSImm (7);
1031 BuildMI (BB, V8::ANDri, 2, StackAdjReg).addReg (TmpReg2).addSImm (-8);
Brian Gaekecfaf2242004-06-18 08:45:52 +00001032
1033 // Subtract size from stack pointer, thereby allocating some space.
Brian Gaekec93a7522004-06-18 05:19:16 +00001034 BuildMI (BB, V8::SUBrr, 2, V8::SP).addReg (V8::SP).addReg (StackAdjReg);
Brian Gaekecfaf2242004-06-18 08:45:52 +00001035
1036 // Put a pointer to the space into the result register, by copying
1037 // the stack pointer.
1038 BuildMI (BB, V8::ADDri, 2, getReg(I)).addReg (V8::SP).addSImm (96);
1039
1040 // Inform the Frame Information that we have just allocated a variable-sized
1041 // object.
1042 F->getFrameInfo()->CreateVariableSizedObject();
Brian Gaekec93a7522004-06-18 05:19:16 +00001043}
Chris Lattner1c809c52004-02-29 00:27:00 +00001044
1045/// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
1046/// function, lowering any calls to unknown intrinsic functions into the
1047/// equivalent LLVM code.
1048void V8ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) {
1049 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1050 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
1051 if (CallInst *CI = dyn_cast<CallInst>(I++))
1052 if (Function *F = CI->getCalledFunction())
1053 switch (F->getIntrinsicID()) {
1054 case Intrinsic::not_intrinsic: break;
1055 default:
1056 // All other intrinsic calls we must lower.
1057 Instruction *Before = CI->getPrev();
1058 TM.getIntrinsicLowering().LowerIntrinsicCall(CI);
1059 if (Before) { // Move iterator to instruction after call
1060 I = Before; ++I;
1061 } else {
1062 I = BB->begin();
1063 }
1064 }
1065}
1066
1067
1068void V8ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) {
1069 unsigned TmpReg1, TmpReg2;
1070 switch (ID) {
1071 default: assert(0 && "Intrinsic not supported!");
1072 }
1073}