Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 1 | //===-- 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 Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 15 | #include "SparcV8InstrInfo.h" |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 16 | #include "llvm/Instructions.h" |
| 17 | #include "llvm/IntrinsicLowering.h" |
| 18 | #include "llvm/Pass.h" |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 19 | #include "llvm/Constants.h" |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 21 | #include "llvm/CodeGen/MachineFunction.h" |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/SSARegMap.h" |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetMachine.h" |
| 24 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
| 25 | #include "llvm/Support/InstVisitor.h" |
| 26 | #include "llvm/Support/CFG.h" |
| 27 | using namespace llvm; |
| 28 | |
| 29 | namespace { |
| 30 | struct V8ISel : public FunctionPass, public InstVisitor<V8ISel> { |
| 31 | TargetMachine &TM; |
| 32 | MachineFunction *F; // The function we are compiling into |
| 33 | MachineBasicBlock *BB; // The current MBB we are compiling |
| 34 | |
| 35 | std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs |
| 36 | |
| 37 | // MBBMap - Mapping between LLVM BB -> Machine BB |
| 38 | std::map<const BasicBlock*, MachineBasicBlock*> MBBMap; |
| 39 | |
| 40 | V8ISel(TargetMachine &tm) : TM(tm), F(0), BB(0) {} |
| 41 | |
| 42 | /// runOnFunction - Top level implementation of instruction selection for |
| 43 | /// the entire function. |
| 44 | /// |
| 45 | bool runOnFunction(Function &Fn); |
| 46 | |
| 47 | virtual const char *getPassName() const { |
| 48 | return "SparcV8 Simple Instruction Selection"; |
| 49 | } |
| 50 | |
| 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. |
| 55 | /// |
| 56 | void visitBasicBlock(BasicBlock &LLVM_BB) { |
| 57 | BB = MBBMap[&LLVM_BB]; |
| 58 | } |
| 59 | |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 60 | void visitBinaryOperator(BinaryOperator &I); |
Brian Gaeke | f7e44ef | 2004-04-02 20:53:33 +0000 | [diff] [blame] | 61 | void visitCallInst(CallInst &I); |
| 62 | void visitReturnInst(ReturnInst &RI); |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 63 | |
| 64 | void visitInstruction(Instruction &I) { |
| 65 | std::cerr << "Unhandled instruction: " << I; |
| 66 | abort(); |
| 67 | } |
| 68 | |
| 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); |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 73 | void visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI); |
| 74 | |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 75 | /// copyConstantToRegister - Output the instructions required to put the |
| 76 | /// specified constant into the specified register. |
| 77 | /// |
| 78 | void copyConstantToRegister(MachineBasicBlock *MBB, |
| 79 | MachineBasicBlock::iterator IP, |
| 80 | Constant *C, unsigned R); |
| 81 | |
| 82 | /// makeAnotherReg - This method returns the next register number we haven't |
| 83 | /// yet used. |
| 84 | /// |
| 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. |
| 89 | /// |
| 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; |
| 101 | } |
| 102 | |
| 103 | // Add the mapping of regnumber => reg class to MachineFunction |
| 104 | const TargetRegisterClass *RC = MRI->getRegClassForType(Ty); |
| 105 | return F->getSSARegMap()->createVirtualRegister(RC); |
| 106 | } |
| 107 | |
| 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); |
| 113 | } |
| 114 | unsigned getReg(Value *V, MachineBasicBlock *MBB, |
| 115 | MachineBasicBlock::iterator IPt) { |
| 116 | unsigned &Reg = RegMap[V]; |
| 117 | if (Reg == 0) { |
| 118 | Reg = makeAnotherReg(V->getType()); |
| 119 | RegMap[V] = Reg; |
| 120 | } |
| 121 | // If this operand is a constant, emit the code to copy the constant into |
| 122 | // the register here... |
| 123 | // |
| 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 |
Brian Gaeke | cf47198 | 2004-03-09 04:49:13 +0000 | [diff] [blame] | 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); |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 133 | RegMap.erase(V); // Assign a new name to this address if ref'd again |
| 134 | } |
| 135 | |
| 136 | return Reg; |
| 137 | } |
| 138 | |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 139 | }; |
| 140 | } |
| 141 | |
| 142 | FunctionPass *llvm::createSparcV8SimpleInstructionSelector(TargetMachine &TM) { |
| 143 | return new V8ISel(TM); |
| 144 | } |
| 145 | |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 146 | enum TypeClass { |
Brian Gaeke | f57e364 | 2004-03-16 22:37:11 +0000 | [diff] [blame] | 147 | cByte, cShort, cInt, cLong, cFloat, cDouble |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 148 | }; |
| 149 | |
| 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; |
Brian Gaeke | f57e364 | 2004-03-16 22:37:11 +0000 | [diff] [blame] | 155 | case Type::ULongTyID: case Type::LongTyID: return cLong; |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 156 | case Type::FloatTyID: return cFloat; |
| 157 | case Type::DoubleTyID: return cDouble; |
| 158 | default: |
| 159 | assert (0 && "Type of unknown class passed to getClass?"); |
| 160 | return cByte; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /// copyConstantToRegister - Output the instructions required to put the |
| 165 | /// specified constant into the specified register. |
| 166 | /// |
| 167 | void V8ISel::copyConstantToRegister(MachineBasicBlock *MBB, |
| 168 | MachineBasicBlock::iterator IP, |
| 169 | Constant *C, unsigned R) { |
Brian Gaeke | 775158d | 2004-03-04 04:37:45 +0000 | [diff] [blame] | 170 | if (ConstantInt *CI = dyn_cast<ConstantInt> (C)) { |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 171 | unsigned Class = getClass(C->getType()); |
Brian Gaeke | e806173 | 2004-03-04 00:56:25 +0000 | [diff] [blame] | 172 | switch (Class) { |
| 173 | case cByte: |
| 174 | BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (V8::G0).addImm ((uint8_t) CI->getRawValue ()); |
| 175 | return; |
| 176 | case cShort: { |
| 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); |
| 180 | return; |
| 181 | } |
| 182 | case cInt: { |
| 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); |
| 186 | return; |
| 187 | } |
| 188 | default: |
Brian Gaeke | 775158d | 2004-03-04 04:37:45 +0000 | [diff] [blame] | 189 | assert (0 && "Can't copy this kind of constant into register yet"); |
Brian Gaeke | e806173 | 2004-03-04 00:56:25 +0000 | [diff] [blame] | 190 | return; |
| 191 | } |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Brian Gaeke | 775158d | 2004-03-04 04:37:45 +0000 | [diff] [blame] | 194 | assert (0 && "Can't copy this kind of constant into register yet"); |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 195 | } |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 196 | |
| 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); |
| 201 | |
| 202 | F = &MachineFunction::construct(&Fn, TM); |
| 203 | |
| 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)); |
| 207 | |
| 208 | BB = &F->front(); |
| 209 | |
| 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); |
| 213 | |
| 214 | // Copy incoming arguments off of the stack and out of fixed registers. |
| 215 | //LoadArgumentsToVirtualRegs(Fn); |
| 216 | |
| 217 | // Instruction select everything except PHI nodes |
| 218 | visit(Fn); |
| 219 | |
| 220 | // Select the PHI nodes |
| 221 | //SelectPHINodes(); |
| 222 | |
| 223 | RegMap.clear(); |
| 224 | MBBMap.clear(); |
| 225 | F = 0; |
| 226 | // We always build a machine code representation for the function |
| 227 | return true; |
| 228 | } |
| 229 | |
Brian Gaeke | f7e44ef | 2004-04-02 20:53:33 +0000 | [diff] [blame] | 230 | void V8ISel::visitCallInst(CallInst &I) { |
| 231 | assert (I.getNumOperands () == 1 && "Can't handle call args yet"); |
Brian Gaeke | ea8494b | 2004-04-06 22:09:23 +0000 | [diff] [blame^] | 232 | unsigned DestReg = getReg (I); |
Brian Gaeke | f7e44ef | 2004-04-02 20:53:33 +0000 | [diff] [blame] | 233 | BuildMI (BB, V8::CALL, 1).addPCDisp (I.getOperand (0)); |
Brian Gaeke | ea8494b | 2004-04-06 22:09:23 +0000 | [diff] [blame^] | 234 | if (I.getType ()->getPrimitiveID () == Type::VoidTyID) |
| 235 | return; |
| 236 | // Deal w/ return value |
| 237 | switch (getClass (I.getType ())) { |
| 238 | case cByte: |
| 239 | case cShort: |
| 240 | case cInt: |
| 241 | // Schlep it over into the destination register |
| 242 | BuildMI (BB, V8::ORrr, 2, DestReg).addReg(V8::G0).addReg(V8::O0); |
| 243 | break; |
| 244 | default: |
| 245 | visitInstruction (I); |
| 246 | return; |
| 247 | } |
Brian Gaeke | f7e44ef | 2004-04-02 20:53:33 +0000 | [diff] [blame] | 248 | } |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 249 | |
| 250 | void V8ISel::visitReturnInst(ReturnInst &I) { |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 251 | if (I.getNumOperands () == 1) { |
| 252 | unsigned RetValReg = getReg (I.getOperand (0)); |
| 253 | switch (getClass (I.getOperand (0)->getType ())) { |
| 254 | case cByte: |
| 255 | case cShort: |
| 256 | case cInt: |
| 257 | // Schlep it over into i0 (where it will become o0 after restore). |
| 258 | BuildMI (BB, V8::ORrr, 2, V8::I0).addReg(V8::G0).addReg(RetValReg); |
| 259 | break; |
| 260 | default: |
| 261 | visitInstruction (I); |
| 262 | return; |
| 263 | } |
| 264 | } else if (I.getNumOperands () != 1) { |
| 265 | visitInstruction (I); |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 266 | } |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 267 | // Just emit a 'retl' instruction to return. |
| 268 | BuildMI(BB, V8::RETL, 0); |
| 269 | return; |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 272 | void V8ISel::visitBinaryOperator (BinaryOperator &I) { |
| 273 | unsigned DestReg = getReg (I); |
| 274 | unsigned Op0Reg = getReg (I.getOperand (0)); |
| 275 | unsigned Op1Reg = getReg (I.getOperand (1)); |
| 276 | |
| 277 | unsigned ResultReg = makeAnotherReg (I.getType ()); |
| 278 | switch (I.getOpcode ()) { |
| 279 | case Instruction::Add: |
| 280 | BuildMI (BB, V8::ADDrr, 2, ResultReg).addReg (Op0Reg).addReg (Op1Reg); |
| 281 | break; |
Brian Gaeke | 775158d | 2004-03-04 04:37:45 +0000 | [diff] [blame] | 282 | case Instruction::Sub: |
| 283 | BuildMI (BB, V8::SUBrr, 2, ResultReg).addReg (Op0Reg).addReg (Op1Reg); |
| 284 | break; |
Brian Gaeke | f57e364 | 2004-03-16 22:37:11 +0000 | [diff] [blame] | 285 | case Instruction::Mul: { |
| 286 | unsigned MulOpcode = I.getType ()->isSigned () ? V8::SMULrr : V8::UMULrr; |
| 287 | BuildMI (BB, MulOpcode, 2, ResultReg).addReg (Op0Reg).addReg (Op1Reg); |
| 288 | break; |
| 289 | } |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 290 | default: |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 291 | visitInstruction (I); |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 292 | return; |
| 293 | } |
| 294 | |
| 295 | switch (getClass (I.getType ())) { |
| 296 | case cByte: |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 297 | if (I.getType ()->isSigned ()) { // add byte |
| 298 | BuildMI (BB, V8::ANDri, 2, DestReg).addReg (ResultReg).addZImm (0xff); |
| 299 | } else { // add ubyte |
| 300 | unsigned TmpReg = makeAnotherReg (I.getType ()); |
| 301 | BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (24); |
| 302 | BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (24); |
| 303 | } |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 304 | break; |
| 305 | case cShort: |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 306 | if (I.getType ()->isSigned ()) { // add short |
| 307 | unsigned TmpReg = makeAnotherReg (I.getType ()); |
| 308 | BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16); |
| 309 | BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (16); |
| 310 | } else { // add ushort |
| 311 | unsigned TmpReg = makeAnotherReg (I.getType ()); |
Brian Gaeke | 6d339f9 | 2004-03-16 22:45:42 +0000 | [diff] [blame] | 312 | BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16); |
| 313 | BuildMI (BB, V8::SRLri, 2, DestReg).addReg (TmpReg).addZImm (16); |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 314 | } |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 315 | break; |
| 316 | case cInt: |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 317 | BuildMI (BB, V8::ORrr, 2, DestReg).addReg (V8::G0).addReg (ResultReg); |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 318 | break; |
| 319 | default: |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 320 | visitInstruction (I); |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 321 | return; |
| 322 | } |
| 323 | } |
| 324 | |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 325 | |
| 326 | /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the |
| 327 | /// function, lowering any calls to unknown intrinsic functions into the |
| 328 | /// equivalent LLVM code. |
| 329 | void V8ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) { |
| 330 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 331 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) |
| 332 | if (CallInst *CI = dyn_cast<CallInst>(I++)) |
| 333 | if (Function *F = CI->getCalledFunction()) |
| 334 | switch (F->getIntrinsicID()) { |
| 335 | case Intrinsic::not_intrinsic: break; |
| 336 | default: |
| 337 | // All other intrinsic calls we must lower. |
| 338 | Instruction *Before = CI->getPrev(); |
| 339 | TM.getIntrinsicLowering().LowerIntrinsicCall(CI); |
| 340 | if (Before) { // Move iterator to instruction after call |
| 341 | I = Before; ++I; |
| 342 | } else { |
| 343 | I = BB->begin(); |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | |
| 349 | void V8ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) { |
| 350 | unsigned TmpReg1, TmpReg2; |
| 351 | switch (ID) { |
| 352 | default: assert(0 && "Intrinsic not supported!"); |
| 353 | } |
| 354 | } |