blob: 43e2eade0e3c6da3947753460133724c45c5149f [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"
18#include "llvm/IntrinsicLowering.h"
19#include "llvm/Pass.h"
Brian Gaekebc1d27a2004-03-03 23:03:14 +000020#include "llvm/Constants.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
Chris Lattner1c809c52004-02-29 00:27:00 +000061 /// visitBasicBlock - This method is called when we are visiting a new basic
62 /// block. This simply creates a new MachineBasicBlock to emit code into
63 /// and adds it to the current MachineFunction. Subsequent visit* for
64 /// instructions will be invoked for all instructions in the basic block.
65 ///
66 void visitBasicBlock(BasicBlock &LLVM_BB) {
67 BB = MBBMap[&LLVM_BB];
68 }
69
Chris Lattner4be7ca52004-04-07 04:27:16 +000070 void visitBinaryOperator(Instruction &I);
Brian Gaeked6a10532004-06-15 21:09:46 +000071 void visitShiftInst (ShiftInst &SI) { visitBinaryOperator (SI); }
Chris Lattner4d0cda42004-04-07 05:04:51 +000072 void visitSetCondInst(Instruction &I);
Chris Lattner4be7ca52004-04-07 04:27:16 +000073 void visitCallInst(CallInst &I);
Brian Gaekef3334eb2004-04-07 17:29:37 +000074 void visitReturnInst(ReturnInst &I);
Brian Gaeke532e60c2004-05-08 04:21:17 +000075 void visitBranchInst(BranchInst &I);
Brian Gaeke3d11e8a2004-04-13 18:27:46 +000076 void visitCastInst(CastInst &I);
Brian Gaekef3334eb2004-04-07 17:29:37 +000077 void visitLoadInst(LoadInst &I);
78 void visitStoreInst(StoreInst &I);
Brian Gaeke532e60c2004-05-08 04:21:17 +000079 void visitPHINode(PHINode &I) {} // PHI nodes handled by second pass
80 void visitGetElementPtrInst(GetElementPtrInst &I);
Brian Gaekec93a7522004-06-18 05:19:16 +000081 void visitAllocaInst(AllocaInst &I);
Brian Gaeke532e60c2004-05-08 04:21:17 +000082
83
Chris Lattner1c809c52004-02-29 00:27:00 +000084
85 void visitInstruction(Instruction &I) {
86 std::cerr << "Unhandled instruction: " << I;
87 abort();
88 }
89
90 /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
91 /// function, lowering any calls to unknown intrinsic functions into the
92 /// equivalent LLVM code.
93 void LowerUnknownIntrinsicFunctionCalls(Function &F);
Chris Lattner1c809c52004-02-29 00:27:00 +000094 void visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI);
95
Brian Gaeke562cb162004-04-07 17:04:09 +000096 void LoadArgumentsToVirtualRegs(Function *F);
97
Brian Gaeke6c868a42004-06-17 22:34:08 +000098 /// SelectPHINodes - Insert machine code to generate phis. This is tricky
99 /// because we have to generate our sources into the source basic blocks,
100 /// not the current one.
101 ///
102 void SelectPHINodes();
103
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000104 /// copyConstantToRegister - Output the instructions required to put the
105 /// specified constant into the specified register.
106 ///
107 void copyConstantToRegister(MachineBasicBlock *MBB,
108 MachineBasicBlock::iterator IP,
109 Constant *C, unsigned R);
110
111 /// makeAnotherReg - This method returns the next register number we haven't
112 /// yet used.
113 ///
114 /// Long values are handled somewhat specially. They are always allocated
115 /// as pairs of 32 bit integer values. The register number returned is the
116 /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits
117 /// of the long value.
118 ///
119 unsigned makeAnotherReg(const Type *Ty) {
120 assert(dynamic_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo()) &&
121 "Current target doesn't have SparcV8 reg info??");
122 const SparcV8RegisterInfo *MRI =
123 static_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo());
124 if (Ty == Type::LongTy || Ty == Type::ULongTy) {
125 const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy);
126 // Create the lower part
127 F->getSSARegMap()->createVirtualRegister(RC);
128 // Create the upper part.
129 return F->getSSARegMap()->createVirtualRegister(RC)-1;
130 }
131
132 // Add the mapping of regnumber => reg class to MachineFunction
133 const TargetRegisterClass *RC = MRI->getRegClassForType(Ty);
134 return F->getSSARegMap()->createVirtualRegister(RC);
135 }
136
137 unsigned getReg(Value &V) { return getReg (&V); } // allow refs.
138 unsigned getReg(Value *V) {
139 // Just append to the end of the current bb.
140 MachineBasicBlock::iterator It = BB->end();
141 return getReg(V, BB, It);
142 }
143 unsigned getReg(Value *V, MachineBasicBlock *MBB,
144 MachineBasicBlock::iterator IPt) {
145 unsigned &Reg = RegMap[V];
146 if (Reg == 0) {
147 Reg = makeAnotherReg(V->getType());
148 RegMap[V] = Reg;
149 }
150 // If this operand is a constant, emit the code to copy the constant into
151 // the register here...
152 //
153 if (Constant *C = dyn_cast<Constant>(V)) {
154 copyConstantToRegister(MBB, IPt, C, Reg);
155 RegMap.erase(V); // Assign a new name to this constant if ref'd again
156 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
157 // Move the address of the global into the register
Brian Gaekecf471982004-03-09 04:49:13 +0000158 unsigned TmpReg = makeAnotherReg(V->getType());
159 BuildMI (*MBB, IPt, V8::SETHIi, 1, TmpReg).addGlobalAddress (GV);
160 BuildMI (*MBB, IPt, V8::ORri, 2, Reg).addReg (TmpReg)
161 .addGlobalAddress (GV);
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000162 RegMap.erase(V); // Assign a new name to this address if ref'd again
163 }
164
165 return Reg;
166 }
167
Chris Lattner1c809c52004-02-29 00:27:00 +0000168 };
169}
170
171FunctionPass *llvm::createSparcV8SimpleInstructionSelector(TargetMachine &TM) {
172 return new V8ISel(TM);
173}
174
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000175enum TypeClass {
Brian Gaekef57e3642004-03-16 22:37:11 +0000176 cByte, cShort, cInt, cLong, cFloat, cDouble
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000177};
178
179static TypeClass getClass (const Type *T) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000180 switch (T->getTypeID()) {
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000181 case Type::UByteTyID: case Type::SByteTyID: return cByte;
182 case Type::UShortTyID: case Type::ShortTyID: return cShort;
Brian Gaeke562cb162004-04-07 17:04:09 +0000183 case Type::PointerTyID:
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000184 case Type::UIntTyID: case Type::IntTyID: return cInt;
Brian Gaekef57e3642004-03-16 22:37:11 +0000185 case Type::ULongTyID: case Type::LongTyID: return cLong;
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000186 case Type::FloatTyID: return cFloat;
187 case Type::DoubleTyID: return cDouble;
188 default:
189 assert (0 && "Type of unknown class passed to getClass?");
190 return cByte;
191 }
192}
Chris Lattner0d538bb2004-04-07 04:36:53 +0000193static TypeClass getClassB(const Type *T) {
194 if (T == Type::BoolTy) return cByte;
195 return getClass(T);
196}
197
198
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000199
200/// copyConstantToRegister - Output the instructions required to put the
201/// specified constant into the specified register.
202///
203void V8ISel::copyConstantToRegister(MachineBasicBlock *MBB,
204 MachineBasicBlock::iterator IP,
205 Constant *C, unsigned R) {
Brian Gaeke9df92822004-06-15 19:16:07 +0000206 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
207 switch (CE->getOpcode()) {
208 case Instruction::GetElementPtr:
209 emitGEPOperation(MBB, IP, CE->getOperand(0),
210 CE->op_begin()+1, CE->op_end(), R);
211 return;
212 default:
213 std::cerr << "Copying this constant expr not yet handled: " << *CE;
214 abort();
215 }
216 }
217
Brian Gaekee302a7e2004-05-07 21:39:30 +0000218 if (C->getType()->isIntegral ()) {
219 uint64_t Val;
Brian Gaeke9df92822004-06-15 19:16:07 +0000220 unsigned Class = getClassB (C->getType ());
221 if (Class == cLong) {
222 unsigned TmpReg = makeAnotherReg (Type::IntTy);
223 unsigned TmpReg2 = makeAnotherReg (Type::IntTy);
224 // Copy the value into the register pair.
225 // R = top(more-significant) half, R+1 = bottom(less-significant) half
226 uint64_t Val = cast<ConstantInt>(C)->getRawValue();
227 unsigned topHalf = Val & 0xffffffffU;
228 unsigned bottomHalf = Val >> 32;
229 unsigned HH = topHalf >> 10;
230 unsigned HM = topHalf & 0x03ff;
231 unsigned LM = bottomHalf >> 10;
232 unsigned LO = bottomHalf & 0x03ff;
233 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addImm(HH);
234 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg)
235 .addImm (HM);
236 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg2).addImm(LM);
237 BuildMI (*MBB, IP, V8::ORri, 2, R+1).addReg (TmpReg2)
238 .addImm (LO);
239 return;
240 }
241
242 assert(Class <= cInt && "Type not handled yet!");
243
Brian Gaekee302a7e2004-05-07 21:39:30 +0000244 if (C->getType() == Type::BoolTy) {
245 Val = (C == ConstantBool::True);
246 } else {
247 ConstantInt *CI = dyn_cast<ConstantInt> (C);
248 Val = CI->getRawValue ();
249 }
Brian Gaeke9df92822004-06-15 19:16:07 +0000250 switch (Class) {
Brian Gaekee8061732004-03-04 00:56:25 +0000251 case cByte:
Chris Lattner4be7ca52004-04-07 04:27:16 +0000252 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (V8::G0).addImm((uint8_t)Val);
Brian Gaekee8061732004-03-04 00:56:25 +0000253 return;
254 case cShort: {
255 unsigned TmpReg = makeAnotherReg (C->getType ());
Chris Lattner4be7ca52004-04-07 04:27:16 +0000256 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg)
257 .addImm (((uint16_t) Val) >> 10);
258 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg)
259 .addImm (((uint16_t) Val) & 0x03ff);
Brian Gaekee8061732004-03-04 00:56:25 +0000260 return;
261 }
262 case cInt: {
263 unsigned TmpReg = makeAnotherReg (C->getType ());
Chris Lattner4be7ca52004-04-07 04:27:16 +0000264 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addImm(((uint32_t)Val) >> 10);
265 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg)
266 .addImm (((uint32_t) Val) & 0x03ff);
Brian Gaekee8061732004-03-04 00:56:25 +0000267 return;
268 }
269 default:
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +0000270 std::cerr << "Offending constant: " << *C << "\n";
Brian Gaeke775158d2004-03-04 04:37:45 +0000271 assert (0 && "Can't copy this kind of constant into register yet");
Brian Gaekee8061732004-03-04 00:56:25 +0000272 return;
273 }
Brian Gaekec93a7522004-06-18 05:19:16 +0000274 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
275 // We need to spill the constant to memory...
276 MachineConstantPool *CP = F->getConstantPool();
277 unsigned CPI = CP->getConstantPoolIndex(CFP);
278 const Type *Ty = CFP->getType();
279
280 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
281 unsigned LoadOpcode = Ty == Type::FloatTy ? V8::LDFmr : V8::LDDFmr;
282 BuildMI (*MBB, IP, LoadOpcode, 2, R).addConstantPoolIndex (CPI).addSImm (0);
Brian Gaeke9df92822004-06-15 19:16:07 +0000283 } else if (isa<ConstantPointerNull>(C)) {
284 // Copy zero (null pointer) to the register.
285 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (V8::G0).addImm (0);
286 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)) {
287 // Copy it with a SETHI/OR pair; the JIT + asmwriter should recognize
288 // that SETHI %reg,global == SETHI %reg,%hi(global) and
289 // OR %reg,global,%reg == OR %reg,%lo(global),%reg.
290 unsigned TmpReg = makeAnotherReg (C->getType ());
291 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addGlobalAddress (CPR->getValue());
292 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg)
293 .addGlobalAddress (CPR->getValue ());
294 } else {
295 std::cerr << "Offending constant: " << *C << "\n";
296 assert (0 && "Can't copy this kind of constant into register yet");
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000297 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000298}
Chris Lattner1c809c52004-02-29 00:27:00 +0000299
Brian Gaeke562cb162004-04-07 17:04:09 +0000300void V8ISel::LoadArgumentsToVirtualRegs (Function *F) {
301 unsigned ArgOffset = 0;
302 static const unsigned IncomingArgRegs[] = { V8::I0, V8::I1, V8::I2,
303 V8::I3, V8::I4, V8::I5 };
304 assert (F->asize () < 7
305 && "Can't handle loading excess call args off the stack yet");
306
307 for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I) {
308 unsigned Reg = getReg(*I);
309 switch (getClassB(I->getType())) {
310 case cByte:
311 case cShort:
312 case cInt:
313 BuildMI(BB, V8::ORrr, 2, Reg).addReg (V8::G0)
314 .addReg (IncomingArgRegs[ArgOffset]);
315 break;
316 default:
317 assert (0 && "Only <=32-bit, integral arguments currently handled");
318 return;
319 }
320 ++ArgOffset;
321 }
322}
323
Brian Gaeke6c868a42004-06-17 22:34:08 +0000324void V8ISel::SelectPHINodes() {
325 const TargetInstrInfo &TII = *TM.getInstrInfo();
326 const Function &LF = *F->getFunction(); // The LLVM function...
327 for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) {
328 const BasicBlock *BB = I;
329 MachineBasicBlock &MBB = *MBBMap[I];
330
331 // Loop over all of the PHI nodes in the LLVM basic block...
332 MachineBasicBlock::iterator PHIInsertPoint = MBB.begin();
333 for (BasicBlock::const_iterator I = BB->begin();
334 PHINode *PN = const_cast<PHINode*>(dyn_cast<PHINode>(I)); ++I) {
335
336 // Create a new machine instr PHI node, and insert it.
337 unsigned PHIReg = getReg(*PN);
338 MachineInstr *PhiMI = BuildMI(MBB, PHIInsertPoint,
339 V8::PHI, PN->getNumOperands(), PHIReg);
340
341 MachineInstr *LongPhiMI = 0;
342 if (PN->getType() == Type::LongTy || PN->getType() == Type::ULongTy)
343 LongPhiMI = BuildMI(MBB, PHIInsertPoint,
344 V8::PHI, PN->getNumOperands(), PHIReg+1);
345
346 // PHIValues - Map of blocks to incoming virtual registers. We use this
347 // so that we only initialize one incoming value for a particular block,
348 // even if the block has multiple entries in the PHI node.
349 //
350 std::map<MachineBasicBlock*, unsigned> PHIValues;
351
352 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
353 MachineBasicBlock *PredMBB = 0;
354 for (MachineBasicBlock::pred_iterator PI = MBB.pred_begin (),
355 PE = MBB.pred_end (); PI != PE; ++PI)
356 if (PN->getIncomingBlock(i) == (*PI)->getBasicBlock()) {
357 PredMBB = *PI;
358 break;
359 }
360 assert (PredMBB && "Couldn't find incoming machine-cfg edge for phi");
361
362 unsigned ValReg;
363 std::map<MachineBasicBlock*, unsigned>::iterator EntryIt =
364 PHIValues.lower_bound(PredMBB);
365
366 if (EntryIt != PHIValues.end() && EntryIt->first == PredMBB) {
367 // We already inserted an initialization of the register for this
368 // predecessor. Recycle it.
369 ValReg = EntryIt->second;
370
371 } else {
372 // Get the incoming value into a virtual register.
373 //
374 Value *Val = PN->getIncomingValue(i);
375
376 // If this is a constant or GlobalValue, we may have to insert code
377 // into the basic block to compute it into a virtual register.
378 if ((isa<Constant>(Val) && !isa<ConstantExpr>(Val)) ||
379 isa<GlobalValue>(Val)) {
380 // Simple constants get emitted at the end of the basic block,
381 // before any terminator instructions. We "know" that the code to
382 // move a constant into a register will never clobber any flags.
383 ValReg = getReg(Val, PredMBB, PredMBB->getFirstTerminator());
384 } else {
385 // Because we don't want to clobber any values which might be in
386 // physical registers with the computation of this constant (which
387 // might be arbitrarily complex if it is a constant expression),
388 // just insert the computation at the top of the basic block.
389 MachineBasicBlock::iterator PI = PredMBB->begin();
390
391 // Skip over any PHI nodes though!
392 while (PI != PredMBB->end() && PI->getOpcode() == V8::PHI)
393 ++PI;
394
395 ValReg = getReg(Val, PredMBB, PI);
396 }
397
398 // Remember that we inserted a value for this PHI for this predecessor
399 PHIValues.insert(EntryIt, std::make_pair(PredMBB, ValReg));
400 }
401
402 PhiMI->addRegOperand(ValReg);
403 PhiMI->addMachineBasicBlockOperand(PredMBB);
404 if (LongPhiMI) {
405 LongPhiMI->addRegOperand(ValReg+1);
406 LongPhiMI->addMachineBasicBlockOperand(PredMBB);
407 }
408 }
409
410 // Now that we emitted all of the incoming values for the PHI node, make
411 // sure to reposition the InsertPoint after the PHI that we just added.
412 // This is needed because we might have inserted a constant into this
413 // block, right after the PHI's which is before the old insert point!
414 PHIInsertPoint = LongPhiMI ? LongPhiMI : PhiMI;
415 ++PHIInsertPoint;
416 }
417 }
418}
419
Chris Lattner1c809c52004-02-29 00:27:00 +0000420bool V8ISel::runOnFunction(Function &Fn) {
421 // First pass over the function, lower any unknown intrinsic functions
422 // with the IntrinsicLowering class.
423 LowerUnknownIntrinsicFunctionCalls(Fn);
424
425 F = &MachineFunction::construct(&Fn, TM);
426
427 // Create all of the machine basic blocks for the function...
428 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
429 F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I));
430
431 BB = &F->front();
432
433 // Set up a frame object for the return address. This is used by the
434 // llvm.returnaddress & llvm.frameaddress intrinisics.
435 //ReturnAddressIndex = F->getFrameInfo()->CreateFixedObject(4, -4);
436
437 // Copy incoming arguments off of the stack and out of fixed registers.
Brian Gaeke562cb162004-04-07 17:04:09 +0000438 LoadArgumentsToVirtualRegs(&Fn);
Chris Lattner1c809c52004-02-29 00:27:00 +0000439
440 // Instruction select everything except PHI nodes
441 visit(Fn);
442
443 // Select the PHI nodes
Brian Gaeke6c868a42004-06-17 22:34:08 +0000444 SelectPHINodes();
Chris Lattner1c809c52004-02-29 00:27:00 +0000445
446 RegMap.clear();
447 MBBMap.clear();
448 F = 0;
449 // We always build a machine code representation for the function
450 return true;
451}
452
Brian Gaeke3d11e8a2004-04-13 18:27:46 +0000453void V8ISel::visitCastInst(CastInst &I) {
454 unsigned SrcReg = getReg (I.getOperand (0));
Brian Gaekee302a7e2004-05-07 21:39:30 +0000455 unsigned DestReg = getReg (I);
Brian Gaeke3d11e8a2004-04-13 18:27:46 +0000456 const Type *oldTy = I.getOperand (0)->getType ();
457 const Type *newTy = I.getType ();
Brian Gaekee302a7e2004-05-07 21:39:30 +0000458 unsigned oldTyClass = getClassB (oldTy);
459 unsigned newTyClass = getClassB (newTy);
Brian Gaeke3d11e8a2004-04-13 18:27:46 +0000460
Brian Gaeke429022b2004-05-08 06:36:14 +0000461 if (oldTyClass < cLong && newTyClass < cLong) {
462 if (oldTyClass >= newTyClass) {
463 // Emit a reg->reg copy to do a equal-size or narrowing cast,
464 // and do sign/zero extension (necessary if we change signedness).
465 unsigned TmpReg1 = makeAnotherReg (newTy);
466 unsigned TmpReg2 = makeAnotherReg (newTy);
467 BuildMI (BB, V8::ORrr, 2, TmpReg1).addReg (V8::G0).addReg (SrcReg);
468 unsigned shiftWidth = 32 - (8 * TM.getTargetData ().getTypeSize (newTy));
469 BuildMI (BB, V8::SLLri, 2, TmpReg2).addZImm (shiftWidth).addReg(TmpReg1);
470 if (newTy->isSigned ()) { // sign-extend with SRA
471 BuildMI(BB, V8::SRAri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg2);
472 } else { // zero-extend with SRL
473 BuildMI(BB, V8::SRLri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg2);
474 }
475 } else {
476 unsigned TmpReg1 = makeAnotherReg (oldTy);
477 unsigned TmpReg2 = makeAnotherReg (newTy);
478 unsigned TmpReg3 = makeAnotherReg (newTy);
479 // Widening integer cast. Make sure it's fully sign/zero-extended
480 // wrt the input type, then make sure it's fully sign/zero-extended wrt
481 // the output type. Kind of stupid, but simple...
482 unsigned shiftWidth = 32 - (8 * TM.getTargetData ().getTypeSize (oldTy));
483 BuildMI (BB, V8::SLLri, 2, TmpReg1).addZImm (shiftWidth).addReg(SrcReg);
484 if (oldTy->isSigned ()) { // sign-extend with SRA
485 BuildMI(BB, V8::SRAri, 2, TmpReg2).addZImm (shiftWidth).addReg(TmpReg1);
486 } else { // zero-extend with SRL
487 BuildMI(BB, V8::SRLri, 2, TmpReg2).addZImm (shiftWidth).addReg(TmpReg1);
488 }
489 shiftWidth = 32 - (8 * TM.getTargetData ().getTypeSize (newTy));
490 BuildMI (BB, V8::SLLri, 2, TmpReg3).addZImm (shiftWidth).addReg(TmpReg2);
491 if (newTy->isSigned ()) { // sign-extend with SRA
492 BuildMI(BB, V8::SRAri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg3);
493 } else { // zero-extend with SRL
494 BuildMI(BB, V8::SRLri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg3);
495 }
Brian Gaekee302a7e2004-05-07 21:39:30 +0000496 }
497 } else {
Brian Gaeke429022b2004-05-08 06:36:14 +0000498 std::cerr << "Casts w/ long, fp, double still unsupported: " << I;
Brian Gaekee302a7e2004-05-07 21:39:30 +0000499 abort ();
500 }
Brian Gaeke3d11e8a2004-04-13 18:27:46 +0000501}
502
Brian Gaekef3334eb2004-04-07 17:29:37 +0000503void V8ISel::visitLoadInst(LoadInst &I) {
504 unsigned DestReg = getReg (I);
505 unsigned PtrReg = getReg (I.getOperand (0));
Brian Gaeke532e60c2004-05-08 04:21:17 +0000506 switch (getClassB (I.getType ())) {
Brian Gaekef3334eb2004-04-07 17:29:37 +0000507 case cByte:
508 if (I.getType ()->isSigned ())
509 BuildMI (BB, V8::LDSBmr, 1, DestReg).addReg (PtrReg).addSImm(0);
510 else
511 BuildMI (BB, V8::LDUBmr, 1, DestReg).addReg (PtrReg).addSImm(0);
512 return;
513 case cShort:
514 if (I.getType ()->isSigned ())
515 BuildMI (BB, V8::LDSHmr, 1, DestReg).addReg (PtrReg).addSImm(0);
516 else
517 BuildMI (BB, V8::LDUHmr, 1, DestReg).addReg (PtrReg).addSImm(0);
518 return;
519 case cInt:
520 BuildMI (BB, V8::LDmr, 1, DestReg).addReg (PtrReg).addSImm(0);
521 return;
522 case cLong:
523 BuildMI (BB, V8::LDDmr, 1, DestReg).addReg (PtrReg).addSImm(0);
524 return;
525 default:
526 std::cerr << "Load instruction not handled: " << I;
527 abort ();
528 return;
529 }
530}
531
532void V8ISel::visitStoreInst(StoreInst &I) {
Brian Gaeke532e60c2004-05-08 04:21:17 +0000533 Value *SrcVal = I.getOperand (0);
534 unsigned SrcReg = getReg (SrcVal);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000535 unsigned PtrReg = getReg (I.getOperand (1));
Brian Gaeke532e60c2004-05-08 04:21:17 +0000536 switch (getClassB (SrcVal->getType ())) {
537 case cByte:
Brian Gaeke6c868a42004-06-17 22:34:08 +0000538 BuildMI (BB, V8::STBrm, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000539 return;
540 case cShort:
Brian Gaeke6c868a42004-06-17 22:34:08 +0000541 BuildMI (BB, V8::STHrm, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000542 return;
543 case cInt:
Brian Gaeke6c868a42004-06-17 22:34:08 +0000544 BuildMI (BB, V8::STrm, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000545 return;
546 case cLong:
Brian Gaeke6c868a42004-06-17 22:34:08 +0000547 BuildMI (BB, V8::STDrm, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000548 return;
549 default:
550 std::cerr << "Store instruction not handled: " << I;
551 abort ();
552 return;
553 }
Brian Gaekef3334eb2004-04-07 17:29:37 +0000554}
555
Brian Gaekef7e44ef2004-04-02 20:53:33 +0000556void V8ISel::visitCallInst(CallInst &I) {
Brian Gaeked54c38b2004-04-07 16:41:22 +0000557 assert (I.getNumOperands () < 8
558 && "Can't handle pushing excess call args on the stack yet");
Brian Gaeke562cb162004-04-07 17:04:09 +0000559 static const unsigned OutgoingArgRegs[] = { V8::O0, V8::O1, V8::O2, V8::O3,
Brian Gaeked54c38b2004-04-07 16:41:22 +0000560 V8::O4, V8::O5 };
561 for (unsigned i = 1; i < 7; ++i)
562 if (i < I.getNumOperands ()) {
563 unsigned ArgReg = getReg (I.getOperand (i));
564 // Schlep it over into the incoming arg register
Brian Gaeke562cb162004-04-07 17:04:09 +0000565 BuildMI (BB, V8::ORrr, 2, OutgoingArgRegs[i - 1]).addReg (V8::G0)
Brian Gaeked54c38b2004-04-07 16:41:22 +0000566 .addReg (ArgReg);
567 }
568
Brian Gaekec93a7522004-06-18 05:19:16 +0000569 assert (I.getCalledFunction() && "don't know what to do with NULL function!");
Brian Gaeke09c13092004-06-17 19:39:23 +0000570 BuildMI (BB, V8::CALL, 1).addGlobalAddress(I.getCalledFunction (), true);
Brian Gaekee14e3382004-06-15 20:06:32 +0000571 if (I.getType () == Type::VoidTy)
Brian Gaekeea8494b2004-04-06 22:09:23 +0000572 return;
Brian Gaekee14e3382004-06-15 20:06:32 +0000573 unsigned DestReg = getReg (I);
Brian Gaekeea8494b2004-04-06 22:09:23 +0000574 // Deal w/ return value
575 switch (getClass (I.getType ())) {
576 case cByte:
577 case cShort:
578 case cInt:
579 // Schlep it over into the destination register
580 BuildMI (BB, V8::ORrr, 2, DestReg).addReg(V8::G0).addReg(V8::O0);
581 break;
582 default:
Brian Gaeke532e60c2004-05-08 04:21:17 +0000583 std::cerr << "Return type of call instruction not handled: " << I;
584 abort ();
Brian Gaekeea8494b2004-04-06 22:09:23 +0000585 }
Brian Gaekef7e44ef2004-04-02 20:53:33 +0000586}
Chris Lattner1c809c52004-02-29 00:27:00 +0000587
588void V8ISel::visitReturnInst(ReturnInst &I) {
Brian Gaeke08f64c32004-03-06 05:32:28 +0000589 if (I.getNumOperands () == 1) {
590 unsigned RetValReg = getReg (I.getOperand (0));
591 switch (getClass (I.getOperand (0)->getType ())) {
592 case cByte:
593 case cShort:
594 case cInt:
595 // Schlep it over into i0 (where it will become o0 after restore).
596 BuildMI (BB, V8::ORrr, 2, V8::I0).addReg(V8::G0).addReg(RetValReg);
597 break;
598 default:
Brian Gaeke532e60c2004-05-08 04:21:17 +0000599 std::cerr << "Return instruction of this type not handled: " << I;
600 abort ();
Brian Gaeke08f64c32004-03-06 05:32:28 +0000601 }
Chris Lattner1c809c52004-02-29 00:27:00 +0000602 }
Chris Lattner0d538bb2004-04-07 04:36:53 +0000603
Brian Gaeke08f64c32004-03-06 05:32:28 +0000604 // Just emit a 'retl' instruction to return.
605 BuildMI(BB, V8::RETL, 0);
606 return;
Chris Lattner1c809c52004-02-29 00:27:00 +0000607}
608
Brian Gaeke532e60c2004-05-08 04:21:17 +0000609static inline BasicBlock *getBlockAfter(BasicBlock *BB) {
610 Function::iterator I = BB; ++I; // Get iterator to next block
611 return I != BB->getParent()->end() ? &*I : 0;
612}
613
614/// visitBranchInst - Handles conditional and unconditional branches.
615///
616void V8ISel::visitBranchInst(BranchInst &I) {
Brian Gaeke532e60c2004-05-08 04:21:17 +0000617 BasicBlock *takenSucc = I.getSuccessor (0);
Brian Gaeke6c868a42004-06-17 22:34:08 +0000618 MachineBasicBlock *takenSuccMBB = MBBMap[takenSucc];
619 BB->addSuccessor (takenSuccMBB);
620 if (I.isConditional()) { // conditional branch
621 BasicBlock *notTakenSucc = I.getSuccessor (1);
622 MachineBasicBlock *notTakenSuccMBB = MBBMap[notTakenSucc];
623 BB->addSuccessor (notTakenSuccMBB);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000624
Brian Gaeke6c868a42004-06-17 22:34:08 +0000625 // CondReg=(<condition>);
626 // If (CondReg==0) goto notTakenSuccMBB;
627 unsigned CondReg = getReg (I.getCondition ());
628 BuildMI (BB, V8::CMPri, 2).addSImm (0).addReg (CondReg);
629 BuildMI (BB, V8::BE, 1).addMBB (notTakenSuccMBB);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000630 }
Brian Gaeke6c868a42004-06-17 22:34:08 +0000631 // goto takenSuccMBB;
632 BuildMI (BB, V8::BA, 1).addMBB (takenSuccMBB);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000633}
634
635/// emitGEPOperation - Common code shared between visitGetElementPtrInst and
636/// constant expression GEP support.
637///
Brian Gaeke9f564822004-05-08 05:27:20 +0000638void V8ISel::emitGEPOperation (MachineBasicBlock *MBB,
Brian Gaeke532e60c2004-05-08 04:21:17 +0000639 MachineBasicBlock::iterator IP,
640 Value *Src, User::op_iterator IdxBegin,
641 User::op_iterator IdxEnd, unsigned TargetReg) {
Brian Gaeke9f564822004-05-08 05:27:20 +0000642 const TargetData &TD = TM.getTargetData ();
643 const Type *Ty = Src->getType ();
644 unsigned basePtrReg = getReg (Src);
645
646 // GEPs have zero or more indices; we must perform a struct access
647 // or array access for each one.
648 for (GetElementPtrInst::op_iterator oi = IdxBegin, oe = IdxEnd; oi != oe;
649 ++oi) {
650 Value *idx = *oi;
651 unsigned nextBasePtrReg = makeAnotherReg (Type::UIntTy);
652 if (const StructType *StTy = dyn_cast<StructType> (Ty)) {
653 // It's a struct access. idx is the index into the structure,
654 // which names the field. Use the TargetData structure to
655 // pick out what the layout of the structure is in memory.
656 // Use the (constant) structure index's value to find the
657 // right byte offset from the StructLayout class's list of
658 // structure member offsets.
659 unsigned fieldIndex = cast<ConstantUInt> (idx)->getValue ();
660 unsigned memberOffset =
661 TD.getStructLayout (StTy)->MemberOffsets[fieldIndex];
662 // Emit an ADD to add memberOffset to the basePtr.
663 BuildMI (*MBB, IP, V8::ADDri, 2,
664 nextBasePtrReg).addReg (basePtrReg).addZImm (memberOffset);
665 // The next type is the member of the structure selected by the
666 // index.
667 Ty = StTy->getElementType (fieldIndex);
668 } else if (const SequentialType *SqTy = dyn_cast<SequentialType> (Ty)) {
669 // It's an array or pointer access: [ArraySize x ElementType].
670 // We want to add basePtrReg to (idxReg * sizeof ElementType). First, we
671 // must find the size of the pointed-to type (Not coincidentally, the next
672 // type is the type of the elements in the array).
673 Ty = SqTy->getElementType ();
674 unsigned elementSize = TD.getTypeSize (Ty);
675 unsigned idxReg = getReg (idx, MBB, IP);
676 unsigned OffsetReg = makeAnotherReg (Type::IntTy);
677 unsigned elementSizeReg = makeAnotherReg (Type::UIntTy);
678 BuildMI (*MBB, IP, V8::ORri, 2,
679 elementSizeReg).addZImm (elementSize).addReg (V8::G0);
680 // Emit a SMUL to multiply the register holding the index by
681 // elementSize, putting the result in OffsetReg.
682 BuildMI (*MBB, IP, V8::SMULrr, 2,
683 OffsetReg).addReg (elementSizeReg).addReg (idxReg);
684 // Emit an ADD to add OffsetReg to the basePtr.
685 BuildMI (*MBB, IP, V8::ADDrr, 2,
686 nextBasePtrReg).addReg (basePtrReg).addReg (OffsetReg);
687 }
688 basePtrReg = nextBasePtrReg;
689 }
690 // After we have processed all the indices, the result is left in
691 // basePtrReg. Move it to the register where we were expected to
692 // put the answer.
693 BuildMI (BB, V8::ORrr, 1, TargetReg).addReg (V8::G0).addReg (basePtrReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000694}
695
696void V8ISel::visitGetElementPtrInst (GetElementPtrInst &I) {
697 unsigned outputReg = getReg (I);
698 emitGEPOperation (BB, BB->end (), I.getOperand (0),
699 I.op_begin ()+1, I.op_end (), outputReg);
700}
701
Brian Gaeked6a10532004-06-15 21:09:46 +0000702
Chris Lattner4be7ca52004-04-07 04:27:16 +0000703void V8ISel::visitBinaryOperator (Instruction &I) {
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000704 unsigned DestReg = getReg (I);
705 unsigned Op0Reg = getReg (I.getOperand (0));
706 unsigned Op1Reg = getReg (I.getOperand (1));
707
Chris Lattner0d538bb2004-04-07 04:36:53 +0000708 unsigned ResultReg = DestReg;
709 if (getClassB(I.getType()) != cInt)
710 ResultReg = makeAnotherReg (I.getType ());
Chris Lattner22ede702004-04-07 04:06:46 +0000711 unsigned OpCase = ~0;
712
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +0000713 // FIXME: support long, ulong, fp.
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000714 switch (I.getOpcode ()) {
Chris Lattner22ede702004-04-07 04:06:46 +0000715 case Instruction::Add: OpCase = 0; break;
716 case Instruction::Sub: OpCase = 1; break;
717 case Instruction::Mul: OpCase = 2; break;
718 case Instruction::And: OpCase = 3; break;
719 case Instruction::Or: OpCase = 4; break;
720 case Instruction::Xor: OpCase = 5; break;
Chris Lattner4be7ca52004-04-07 04:27:16 +0000721 case Instruction::Shl: OpCase = 6; break;
722 case Instruction::Shr: OpCase = 7+I.getType()->isSigned(); break;
Chris Lattner22ede702004-04-07 04:06:46 +0000723
724 case Instruction::Div:
725 case Instruction::Rem: {
726 unsigned Dest = ResultReg;
727 if (I.getOpcode() == Instruction::Rem)
728 Dest = makeAnotherReg(I.getType());
729
730 // FIXME: this is probably only right for 32 bit operands.
731 if (I.getType ()->isSigned()) {
732 unsigned Tmp = makeAnotherReg (I.getType ());
733 // Sign extend into the Y register
734 BuildMI (BB, V8::SRAri, 2, Tmp).addReg (Op0Reg).addZImm (31);
735 BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (Tmp).addReg (V8::G0);
736 BuildMI (BB, V8::SDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg);
737 } else {
738 // Zero extend into the Y register, ie, just set it to zero
739 BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (V8::G0).addReg (V8::G0);
740 BuildMI (BB, V8::UDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg);
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +0000741 }
Chris Lattner22ede702004-04-07 04:06:46 +0000742
743 if (I.getOpcode() == Instruction::Rem) {
744 unsigned Tmp = makeAnotherReg (I.getType ());
745 BuildMI (BB, V8::SMULrr, 2, Tmp).addReg(Dest).addReg(Op1Reg);
746 BuildMI (BB, V8::SUBrr, 2, ResultReg).addReg(Op0Reg).addReg(Tmp);
Brian Gaekef57e3642004-03-16 22:37:11 +0000747 }
Chris Lattner22ede702004-04-07 04:06:46 +0000748 break;
749 }
750 default:
751 visitInstruction (I);
752 return;
753 }
754
755 if (OpCase != ~0U) {
756 static const unsigned Opcodes[] = {
Chris Lattner4be7ca52004-04-07 04:27:16 +0000757 V8::ADDrr, V8::SUBrr, V8::SMULrr, V8::ANDrr, V8::ORrr, V8::XORrr,
758 V8::SLLrr, V8::SRLrr, V8::SRArr
Chris Lattner22ede702004-04-07 04:06:46 +0000759 };
760 BuildMI (BB, Opcodes[OpCase], 2, ResultReg).addReg (Op0Reg).addReg (Op1Reg);
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000761 }
762
763 switch (getClass (I.getType ())) {
764 case cByte:
Brian Gaeke08f64c32004-03-06 05:32:28 +0000765 if (I.getType ()->isSigned ()) { // add byte
766 BuildMI (BB, V8::ANDri, 2, DestReg).addReg (ResultReg).addZImm (0xff);
767 } else { // add ubyte
768 unsigned TmpReg = makeAnotherReg (I.getType ());
769 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (24);
770 BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (24);
771 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000772 break;
773 case cShort:
Brian Gaeke08f64c32004-03-06 05:32:28 +0000774 if (I.getType ()->isSigned ()) { // add short
775 unsigned TmpReg = makeAnotherReg (I.getType ());
776 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16);
777 BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (16);
778 } else { // add ushort
779 unsigned TmpReg = makeAnotherReg (I.getType ());
Brian Gaeke6d339f92004-03-16 22:45:42 +0000780 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16);
781 BuildMI (BB, V8::SRLri, 2, DestReg).addReg (TmpReg).addZImm (16);
Brian Gaeke08f64c32004-03-06 05:32:28 +0000782 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000783 break;
784 case cInt:
Chris Lattner0d538bb2004-04-07 04:36:53 +0000785 // Nothing todo here.
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000786 break;
787 default:
Brian Gaeke08f64c32004-03-06 05:32:28 +0000788 visitInstruction (I);
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000789 return;
790 }
791}
792
Chris Lattner4d0cda42004-04-07 05:04:51 +0000793void V8ISel::visitSetCondInst(Instruction &I) {
794 unsigned Op0Reg = getReg (I.getOperand (0));
795 unsigned Op1Reg = getReg (I.getOperand (1));
796 unsigned DestReg = getReg (I);
Brian Gaeke429022b2004-05-08 06:36:14 +0000797 const Type *Ty = I.getOperand (0)->getType ();
Chris Lattner4d0cda42004-04-07 05:04:51 +0000798
799 // Compare the two values.
800 BuildMI(BB, V8::SUBCCrr, 2, V8::G0).addReg(Op0Reg).addReg(Op1Reg);
801
Brian Gaeke429022b2004-05-08 06:36:14 +0000802 unsigned BranchIdx;
Chris Lattner4d0cda42004-04-07 05:04:51 +0000803 switch (I.getOpcode()) {
804 default: assert(0 && "Unknown setcc instruction!");
Brian Gaeke429022b2004-05-08 06:36:14 +0000805 case Instruction::SetEQ: BranchIdx = 0; break;
806 case Instruction::SetNE: BranchIdx = 1; break;
807 case Instruction::SetLT: BranchIdx = 2; break;
808 case Instruction::SetGT: BranchIdx = 3; break;
809 case Instruction::SetLE: BranchIdx = 4; break;
810 case Instruction::SetGE: BranchIdx = 5; break;
Chris Lattner4d0cda42004-04-07 05:04:51 +0000811 }
Brian Gaeke429022b2004-05-08 06:36:14 +0000812 static unsigned OpcodeTab[12] = {
813 // LLVM SparcV8
814 // unsigned signed
815 V8::BE, V8::BE, // seteq = be be
816 V8::BNE, V8::BNE, // setne = bne bne
817 V8::BCS, V8::BL, // setlt = bcs bl
818 V8::BGU, V8::BG, // setgt = bgu bg
819 V8::BLEU, V8::BLE, // setle = bleu ble
820 V8::BCC, V8::BGE // setge = bcc bge
821 };
Brian Gaeke6c868a42004-06-17 22:34:08 +0000822 unsigned Opcode = OpcodeTab[2*BranchIdx + (Ty->isSigned() ? 1 : 0)];
823
824 MachineBasicBlock *thisMBB = BB;
825 const BasicBlock *LLVM_BB = BB->getBasicBlock ();
826 // thisMBB:
827 // ...
828 // subcc %reg0, %reg1, %g0
829 // bCC copy1MBB
830 // ba copy0MBB
831
832 // FIXME: we wouldn't need copy0MBB (we could fold it into thisMBB)
833 // if we could insert other, non-terminator instructions after the
834 // bCC. But MBB->getFirstTerminator() can't understand this.
835 MachineBasicBlock *copy1MBB = new MachineBasicBlock (LLVM_BB);
836 F->getBasicBlockList ().push_back (copy1MBB);
837 BuildMI (BB, Opcode, 1).addMBB (copy1MBB);
838 MachineBasicBlock *copy0MBB = new MachineBasicBlock (LLVM_BB);
839 F->getBasicBlockList ().push_back (copy0MBB);
840 BuildMI (BB, V8::BA, 1).addMBB (copy0MBB);
841 // Update machine-CFG edges
842 BB->addSuccessor (copy1MBB);
843 BB->addSuccessor (copy0MBB);
844
845 // copy0MBB:
846 // %FalseValue = or %G0, 0
847 // ba sinkMBB
848 BB = copy0MBB;
849 unsigned FalseValue = makeAnotherReg (I.getType ());
850 BuildMI (BB, V8::ORri, 2, FalseValue).addReg (V8::G0).addZImm (0);
851 MachineBasicBlock *sinkMBB = new MachineBasicBlock (LLVM_BB);
852 F->getBasicBlockList ().push_back (sinkMBB);
853 BuildMI (BB, V8::BA, 1).addMBB (sinkMBB);
854 // Update machine-CFG edges
855 BB->addSuccessor (sinkMBB);
856
857 DEBUG (std::cerr << "thisMBB is at " << (void*)thisMBB << "\n");
858 DEBUG (std::cerr << "copy1MBB is at " << (void*)copy1MBB << "\n");
859 DEBUG (std::cerr << "copy0MBB is at " << (void*)copy0MBB << "\n");
860 DEBUG (std::cerr << "sinkMBB is at " << (void*)sinkMBB << "\n");
861
862 // copy1MBB:
863 // %TrueValue = or %G0, 1
864 // ba sinkMBB
865 BB = copy1MBB;
866 unsigned TrueValue = makeAnotherReg (I.getType ());
867 BuildMI (BB, V8::ORri, 2, TrueValue).addReg (V8::G0).addZImm (1);
868 BuildMI (BB, V8::BA, 1).addMBB (sinkMBB);
869 // Update machine-CFG edges
870 BB->addSuccessor (sinkMBB);
871
872 // sinkMBB:
873 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, copy1MBB ]
874 // ...
875 BB = sinkMBB;
876 BuildMI (BB, V8::PHI, 4, DestReg).addReg (FalseValue)
877 .addMBB (copy0MBB).addReg (TrueValue).addMBB (copy1MBB);
Chris Lattner4d0cda42004-04-07 05:04:51 +0000878}
879
Brian Gaekec93a7522004-06-18 05:19:16 +0000880void V8ISel::visitAllocaInst(AllocaInst &I) {
881 // Find the data size of the alloca inst's getAllocatedType.
882 const Type *Ty = I.getAllocatedType();
883 unsigned TySize = TM.getTargetData().getTypeSize(Ty);
Chris Lattner4d0cda42004-04-07 05:04:51 +0000884
Brian Gaekec93a7522004-06-18 05:19:16 +0000885 unsigned ArraySizeReg = getReg (I.getArraySize ());
886 unsigned TySizeReg = getReg (ConstantUInt::get (Type::UIntTy, TySize));
887 unsigned TmpReg1 = makeAnotherReg (Type::UIntTy);
888 unsigned TmpReg2 = makeAnotherReg (Type::UIntTy);
889 unsigned StackAdjReg = makeAnotherReg (Type::UIntTy);
890 unsigned DestReg = getReg (I);
891
892 // StackAdjReg = (ArraySize * TySize) rounded up to nearest doubleword boundary
893 BuildMI (BB, V8::UMULrr, 2, TmpReg1).addReg (ArraySizeReg).addReg (TySizeReg);
894 // Round up TmpReg1 to nearest doubleword boundary:
895 BuildMI (BB, V8::ADDri, 2, TmpReg2).addReg (TmpReg1).addSImm (7);
896 BuildMI (BB, V8::ANDri, 2, StackAdjReg).addReg (TmpReg2).addSImm (-8);
897 // Adjust stack, push pointer past trap frame space, put result in DestReg
898 BuildMI (BB, V8::SUBrr, 2, V8::SP).addReg (V8::SP).addReg (StackAdjReg);
899 BuildMI (BB, V8::ADDri, 2, DestReg).addReg (V8::SP).addSImm (96);
900}
Chris Lattner1c809c52004-02-29 00:27:00 +0000901
902/// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
903/// function, lowering any calls to unknown intrinsic functions into the
904/// equivalent LLVM code.
905void V8ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) {
906 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
907 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
908 if (CallInst *CI = dyn_cast<CallInst>(I++))
909 if (Function *F = CI->getCalledFunction())
910 switch (F->getIntrinsicID()) {
911 case Intrinsic::not_intrinsic: break;
912 default:
913 // All other intrinsic calls we must lower.
914 Instruction *Before = CI->getPrev();
915 TM.getIntrinsicLowering().LowerIntrinsicCall(CI);
916 if (Before) { // Move iterator to instruction after call
917 I = Before; ++I;
918 } else {
919 I = BB->begin();
920 }
921 }
922}
923
924
925void V8ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) {
926 unsigned TmpReg1, TmpReg2;
927 switch (ID) {
928 default: assert(0 && "Intrinsic not supported!");
929 }
930}