1 //===-- InstSelectSimple.cpp - A simple instruction selector for SparcV8 --===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file defines a simple peephole instruction selector for the V8 target
12 //===----------------------------------------------------------------------===//
15 #include "SparcV8InstrInfo.h"
16 #include "llvm/Instructions.h"
17 #include "llvm/IntrinsicLowering.h"
18 #include "llvm/Pass.h"
19 #include "llvm/Constants.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/SSARegMap.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Support/GetElementPtrTypeIterator.h"
25 #include "llvm/Support/InstVisitor.h"
26 #include "llvm/Support/CFG.h"
30 struct V8ISel : public FunctionPass, public InstVisitor<V8ISel> {
32 MachineFunction *F; // The function we are compiling into
33 MachineBasicBlock *BB; // The current MBB we are compiling
35 std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs
37 // MBBMap - Mapping between LLVM BB -> Machine BB
38 std::map<const BasicBlock*, MachineBasicBlock*> MBBMap;
40 V8ISel(TargetMachine &tm) : TM(tm), F(0), BB(0) {}
42 /// runOnFunction - Top level implementation of instruction selection for
43 /// the entire function.
45 bool runOnFunction(Function &Fn);
47 virtual const char *getPassName() const {
48 return "SparcV8 Simple Instruction Selection";
51 /// visitBasicBlock - This method is called when we are visiting a new basic
52 /// block. This simply creates a new MachineBasicBlock to emit code into
53 /// and adds it to the current MachineFunction. Subsequent visit* for
54 /// instructions will be invoked for all instructions in the basic block.
56 void visitBasicBlock(BasicBlock &LLVM_BB) {
57 BB = MBBMap[&LLVM_BB];
60 void visitBinaryOperator(BinaryOperator &I);
61 void visitCallInst(CallInst &I);
62 void visitReturnInst(ReturnInst &RI);
64 void visitInstruction(Instruction &I) {
65 std::cerr << "Unhandled instruction: " << I;
69 /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
70 /// function, lowering any calls to unknown intrinsic functions into the
71 /// equivalent LLVM code.
72 void LowerUnknownIntrinsicFunctionCalls(Function &F);
73 void visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI);
75 /// copyConstantToRegister - Output the instructions required to put the
76 /// specified constant into the specified register.
78 void copyConstantToRegister(MachineBasicBlock *MBB,
79 MachineBasicBlock::iterator IP,
80 Constant *C, unsigned R);
82 /// makeAnotherReg - This method returns the next register number we haven't
85 /// Long values are handled somewhat specially. They are always allocated
86 /// as pairs of 32 bit integer values. The register number returned is the
87 /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits
88 /// of the long value.
90 unsigned makeAnotherReg(const Type *Ty) {
91 assert(dynamic_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo()) &&
92 "Current target doesn't have SparcV8 reg info??");
93 const SparcV8RegisterInfo *MRI =
94 static_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo());
95 if (Ty == Type::LongTy || Ty == Type::ULongTy) {
96 const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy);
97 // Create the lower part
98 F->getSSARegMap()->createVirtualRegister(RC);
99 // Create the upper part.
100 return F->getSSARegMap()->createVirtualRegister(RC)-1;
103 // Add the mapping of regnumber => reg class to MachineFunction
104 const TargetRegisterClass *RC = MRI->getRegClassForType(Ty);
105 return F->getSSARegMap()->createVirtualRegister(RC);
108 unsigned getReg(Value &V) { return getReg (&V); } // allow refs.
109 unsigned getReg(Value *V) {
110 // Just append to the end of the current bb.
111 MachineBasicBlock::iterator It = BB->end();
112 return getReg(V, BB, It);
114 unsigned getReg(Value *V, MachineBasicBlock *MBB,
115 MachineBasicBlock::iterator IPt) {
116 unsigned &Reg = RegMap[V];
118 Reg = makeAnotherReg(V->getType());
121 // If this operand is a constant, emit the code to copy the constant into
122 // the register here...
124 if (Constant *C = dyn_cast<Constant>(V)) {
125 copyConstantToRegister(MBB, IPt, C, Reg);
126 RegMap.erase(V); // Assign a new name to this constant if ref'd again
127 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
128 // Move the address of the global into the register
129 unsigned TmpReg = makeAnotherReg(V->getType());
130 BuildMI (*MBB, IPt, V8::SETHIi, 1, TmpReg).addGlobalAddress (GV);
131 BuildMI (*MBB, IPt, V8::ORri, 2, Reg).addReg (TmpReg)
132 .addGlobalAddress (GV);
133 RegMap.erase(V); // Assign a new name to this address if ref'd again
142 FunctionPass *llvm::createSparcV8SimpleInstructionSelector(TargetMachine &TM) {
143 return new V8ISel(TM);
147 cByte, cShort, cInt, cLong, cFloat, cDouble
150 static TypeClass getClass (const Type *T) {
151 switch (T->getPrimitiveID ()) {
152 case Type::UByteTyID: case Type::SByteTyID: return cByte;
153 case Type::UShortTyID: case Type::ShortTyID: return cShort;
154 case Type::UIntTyID: case Type::IntTyID: return cInt;
155 case Type::ULongTyID: case Type::LongTyID: return cLong;
156 case Type::FloatTyID: return cFloat;
157 case Type::DoubleTyID: return cDouble;
159 assert (0 && "Type of unknown class passed to getClass?");
164 /// copyConstantToRegister - Output the instructions required to put the
165 /// specified constant into the specified register.
167 void V8ISel::copyConstantToRegister(MachineBasicBlock *MBB,
168 MachineBasicBlock::iterator IP,
169 Constant *C, unsigned R) {
170 if (ConstantInt *CI = dyn_cast<ConstantInt> (C)) {
171 unsigned Class = getClass(C->getType());
174 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (V8::G0).addImm ((uint8_t) CI->getRawValue ());
177 unsigned TmpReg = makeAnotherReg (C->getType ());
178 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addImm (((uint16_t) CI->getRawValue ()) >> 10);
179 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg).addImm (((uint16_t) CI->getRawValue ()) & 0x03ff);
183 unsigned TmpReg = makeAnotherReg (C->getType ());
184 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addImm (((uint32_t) CI->getRawValue ()) >> 10);
185 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg).addImm (((uint32_t) CI->getRawValue ()) & 0x03ff);
189 assert (0 && "Can't copy this kind of constant into register yet");
194 assert (0 && "Can't copy this kind of constant into register yet");
197 bool V8ISel::runOnFunction(Function &Fn) {
198 // First pass over the function, lower any unknown intrinsic functions
199 // with the IntrinsicLowering class.
200 LowerUnknownIntrinsicFunctionCalls(Fn);
202 F = &MachineFunction::construct(&Fn, TM);
204 // Create all of the machine basic blocks for the function...
205 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
206 F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I));
210 // Set up a frame object for the return address. This is used by the
211 // llvm.returnaddress & llvm.frameaddress intrinisics.
212 //ReturnAddressIndex = F->getFrameInfo()->CreateFixedObject(4, -4);
214 // Copy incoming arguments off of the stack and out of fixed registers.
215 //LoadArgumentsToVirtualRegs(Fn);
217 // Instruction select everything except PHI nodes
220 // Select the PHI nodes
226 // We always build a machine code representation for the function
230 void V8ISel::visitCallInst(CallInst &I) {
231 assert (I.getNumOperands () == 1 && "Can't handle call args yet");
232 BuildMI (BB, V8::CALL, 1).addPCDisp (I.getOperand (0));
233 BuildMI (BB, V8::NOP, 0); // NOP in delay slot
236 void V8ISel::visitReturnInst(ReturnInst &I) {
237 if (I.getNumOperands () == 1) {
238 unsigned RetValReg = getReg (I.getOperand (0));
239 switch (getClass (I.getOperand (0)->getType ())) {
243 // Schlep it over into i0 (where it will become o0 after restore).
244 BuildMI (BB, V8::ORrr, 2, V8::I0).addReg(V8::G0).addReg(RetValReg);
247 visitInstruction (I);
250 } else if (I.getNumOperands () != 1) {
251 visitInstruction (I);
253 // Just emit a 'retl' instruction to return.
254 BuildMI(BB, V8::RETL, 0);
258 void V8ISel::visitBinaryOperator (BinaryOperator &I) {
259 unsigned DestReg = getReg (I);
260 unsigned Op0Reg = getReg (I.getOperand (0));
261 unsigned Op1Reg = getReg (I.getOperand (1));
263 unsigned ResultReg = makeAnotherReg (I.getType ());
264 switch (I.getOpcode ()) {
265 case Instruction::Add:
266 BuildMI (BB, V8::ADDrr, 2, ResultReg).addReg (Op0Reg).addReg (Op1Reg);
268 case Instruction::Sub:
269 BuildMI (BB, V8::SUBrr, 2, ResultReg).addReg (Op0Reg).addReg (Op1Reg);
271 case Instruction::Mul: {
272 unsigned MulOpcode = I.getType ()->isSigned () ? V8::SMULrr : V8::UMULrr;
273 BuildMI (BB, MulOpcode, 2, ResultReg).addReg (Op0Reg).addReg (Op1Reg);
277 visitInstruction (I);
281 switch (getClass (I.getType ())) {
283 if (I.getType ()->isSigned ()) { // add byte
284 BuildMI (BB, V8::ANDri, 2, DestReg).addReg (ResultReg).addZImm (0xff);
285 } else { // add ubyte
286 unsigned TmpReg = makeAnotherReg (I.getType ());
287 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (24);
288 BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (24);
292 if (I.getType ()->isSigned ()) { // add short
293 unsigned TmpReg = makeAnotherReg (I.getType ());
294 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16);
295 BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (16);
296 } else { // add ushort
297 unsigned TmpReg = makeAnotherReg (I.getType ());
298 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16);
299 BuildMI (BB, V8::SRLri, 2, DestReg).addReg (TmpReg).addZImm (16);
303 BuildMI (BB, V8::ORrr, 2, DestReg).addReg (V8::G0).addReg (ResultReg);
306 visitInstruction (I);
312 /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
313 /// function, lowering any calls to unknown intrinsic functions into the
314 /// equivalent LLVM code.
315 void V8ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) {
316 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
317 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
318 if (CallInst *CI = dyn_cast<CallInst>(I++))
319 if (Function *F = CI->getCalledFunction())
320 switch (F->getIntrinsicID()) {
321 case Intrinsic::not_intrinsic: break;
323 // All other intrinsic calls we must lower.
324 Instruction *Before = CI->getPrev();
325 TM.getIntrinsicLowering().LowerIntrinsicCall(CI);
326 if (Before) { // Move iterator to instruction after call
335 void V8ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) {
336 unsigned TmpReg1, TmpReg2;
338 default: assert(0 && "Intrinsic not supported!");