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 | |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame^] | 60 | void visitBinaryOperator(Instruction &I); |
| 61 | void visitShiftInstruction(Instruction &I) { visitBinaryOperator(I); } |
| 62 | void visitCallInst(CallInst &I); |
| 63 | void visitReturnInst(ReturnInst &RI); |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 64 | |
| 65 | void visitInstruction(Instruction &I) { |
| 66 | std::cerr << "Unhandled instruction: " << I; |
| 67 | abort(); |
| 68 | } |
| 69 | |
| 70 | /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the |
| 71 | /// function, lowering any calls to unknown intrinsic functions into the |
| 72 | /// equivalent LLVM code. |
| 73 | void LowerUnknownIntrinsicFunctionCalls(Function &F); |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 74 | void visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI); |
| 75 | |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 76 | /// copyConstantToRegister - Output the instructions required to put the |
| 77 | /// specified constant into the specified register. |
| 78 | /// |
| 79 | void copyConstantToRegister(MachineBasicBlock *MBB, |
| 80 | MachineBasicBlock::iterator IP, |
| 81 | Constant *C, unsigned R); |
| 82 | |
| 83 | /// makeAnotherReg - This method returns the next register number we haven't |
| 84 | /// yet used. |
| 85 | /// |
| 86 | /// Long values are handled somewhat specially. They are always allocated |
| 87 | /// as pairs of 32 bit integer values. The register number returned is the |
| 88 | /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits |
| 89 | /// of the long value. |
| 90 | /// |
| 91 | unsigned makeAnotherReg(const Type *Ty) { |
| 92 | assert(dynamic_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo()) && |
| 93 | "Current target doesn't have SparcV8 reg info??"); |
| 94 | const SparcV8RegisterInfo *MRI = |
| 95 | static_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo()); |
| 96 | if (Ty == Type::LongTy || Ty == Type::ULongTy) { |
| 97 | const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy); |
| 98 | // Create the lower part |
| 99 | F->getSSARegMap()->createVirtualRegister(RC); |
| 100 | // Create the upper part. |
| 101 | return F->getSSARegMap()->createVirtualRegister(RC)-1; |
| 102 | } |
| 103 | |
| 104 | // Add the mapping of regnumber => reg class to MachineFunction |
| 105 | const TargetRegisterClass *RC = MRI->getRegClassForType(Ty); |
| 106 | return F->getSSARegMap()->createVirtualRegister(RC); |
| 107 | } |
| 108 | |
| 109 | unsigned getReg(Value &V) { return getReg (&V); } // allow refs. |
| 110 | unsigned getReg(Value *V) { |
| 111 | // Just append to the end of the current bb. |
| 112 | MachineBasicBlock::iterator It = BB->end(); |
| 113 | return getReg(V, BB, It); |
| 114 | } |
| 115 | unsigned getReg(Value *V, MachineBasicBlock *MBB, |
| 116 | MachineBasicBlock::iterator IPt) { |
| 117 | unsigned &Reg = RegMap[V]; |
| 118 | if (Reg == 0) { |
| 119 | Reg = makeAnotherReg(V->getType()); |
| 120 | RegMap[V] = Reg; |
| 121 | } |
| 122 | // If this operand is a constant, emit the code to copy the constant into |
| 123 | // the register here... |
| 124 | // |
| 125 | if (Constant *C = dyn_cast<Constant>(V)) { |
| 126 | copyConstantToRegister(MBB, IPt, C, Reg); |
| 127 | RegMap.erase(V); // Assign a new name to this constant if ref'd again |
| 128 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 129 | // Move the address of the global into the register |
Brian Gaeke | cf47198 | 2004-03-09 04:49:13 +0000 | [diff] [blame] | 130 | unsigned TmpReg = makeAnotherReg(V->getType()); |
| 131 | BuildMI (*MBB, IPt, V8::SETHIi, 1, TmpReg).addGlobalAddress (GV); |
| 132 | BuildMI (*MBB, IPt, V8::ORri, 2, Reg).addReg (TmpReg) |
| 133 | .addGlobalAddress (GV); |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 134 | RegMap.erase(V); // Assign a new name to this address if ref'd again |
| 135 | } |
| 136 | |
| 137 | return Reg; |
| 138 | } |
| 139 | |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 140 | }; |
| 141 | } |
| 142 | |
| 143 | FunctionPass *llvm::createSparcV8SimpleInstructionSelector(TargetMachine &TM) { |
| 144 | return new V8ISel(TM); |
| 145 | } |
| 146 | |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 147 | enum TypeClass { |
Brian Gaeke | f57e364 | 2004-03-16 22:37:11 +0000 | [diff] [blame] | 148 | cByte, cShort, cInt, cLong, cFloat, cDouble |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | static TypeClass getClass (const Type *T) { |
| 152 | switch (T->getPrimitiveID ()) { |
| 153 | case Type::UByteTyID: case Type::SByteTyID: return cByte; |
| 154 | case Type::UShortTyID: case Type::ShortTyID: return cShort; |
| 155 | case Type::UIntTyID: case Type::IntTyID: return cInt; |
Brian Gaeke | f57e364 | 2004-03-16 22:37:11 +0000 | [diff] [blame] | 156 | case Type::ULongTyID: case Type::LongTyID: return cLong; |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 157 | case Type::FloatTyID: return cFloat; |
| 158 | case Type::DoubleTyID: return cDouble; |
| 159 | default: |
| 160 | assert (0 && "Type of unknown class passed to getClass?"); |
| 161 | return cByte; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /// copyConstantToRegister - Output the instructions required to put the |
| 166 | /// specified constant into the specified register. |
| 167 | /// |
| 168 | void V8ISel::copyConstantToRegister(MachineBasicBlock *MBB, |
| 169 | MachineBasicBlock::iterator IP, |
| 170 | Constant *C, unsigned R) { |
Brian Gaeke | 775158d | 2004-03-04 04:37:45 +0000 | [diff] [blame] | 171 | if (ConstantInt *CI = dyn_cast<ConstantInt> (C)) { |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 172 | unsigned Class = getClass(C->getType()); |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame^] | 173 | uint64_t Val = CI->getRawValue (); |
Brian Gaeke | e806173 | 2004-03-04 00:56:25 +0000 | [diff] [blame] | 174 | switch (Class) { |
| 175 | case cByte: |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame^] | 176 | BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (V8::G0).addImm((uint8_t)Val); |
Brian Gaeke | e806173 | 2004-03-04 00:56:25 +0000 | [diff] [blame] | 177 | return; |
| 178 | case cShort: { |
| 179 | unsigned TmpReg = makeAnotherReg (C->getType ()); |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame^] | 180 | BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg) |
| 181 | .addImm (((uint16_t) Val) >> 10); |
| 182 | BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg) |
| 183 | .addImm (((uint16_t) Val) & 0x03ff); |
Brian Gaeke | e806173 | 2004-03-04 00:56:25 +0000 | [diff] [blame] | 184 | return; |
| 185 | } |
| 186 | case cInt: { |
| 187 | unsigned TmpReg = makeAnotherReg (C->getType ()); |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame^] | 188 | BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addImm(((uint32_t)Val) >> 10); |
| 189 | BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg) |
| 190 | .addImm (((uint32_t) Val) & 0x03ff); |
Brian Gaeke | e806173 | 2004-03-04 00:56:25 +0000 | [diff] [blame] | 191 | return; |
| 192 | } |
Brian Gaeke | 2d4fa8f | 2004-04-07 04:00:49 +0000 | [diff] [blame] | 193 | case cLong: { |
| 194 | unsigned TmpReg = makeAnotherReg (Type::UIntTy); |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame^] | 195 | uint32_t topHalf = (uint32_t) (Val >> 32); |
| 196 | uint32_t bottomHalf = (uint32_t)Val; |
Brian Gaeke | 2d4fa8f | 2004-04-07 04:00:49 +0000 | [diff] [blame] | 197 | BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addImm (topHalf >> 10); |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame^] | 198 | BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg) |
| 199 | .addImm (topHalf & 0x03ff); |
Brian Gaeke | 2d4fa8f | 2004-04-07 04:00:49 +0000 | [diff] [blame] | 200 | BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addImm (bottomHalf >> 10); |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame^] | 201 | BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg) |
| 202 | .addImm (bottomHalf & 0x03ff); |
Brian Gaeke | 2d4fa8f | 2004-04-07 04:00:49 +0000 | [diff] [blame] | 203 | return; |
| 204 | } |
Brian Gaeke | e806173 | 2004-03-04 00:56:25 +0000 | [diff] [blame] | 205 | default: |
Brian Gaeke | 2d4fa8f | 2004-04-07 04:00:49 +0000 | [diff] [blame] | 206 | std::cerr << "Offending constant: " << *C << "\n"; |
Brian Gaeke | 775158d | 2004-03-04 04:37:45 +0000 | [diff] [blame] | 207 | assert (0 && "Can't copy this kind of constant into register yet"); |
Brian Gaeke | e806173 | 2004-03-04 00:56:25 +0000 | [diff] [blame] | 208 | return; |
| 209 | } |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Brian Gaeke | 2d4fa8f | 2004-04-07 04:00:49 +0000 | [diff] [blame] | 212 | std::cerr << "Offending constant: " << *C << "\n"; |
Brian Gaeke | 775158d | 2004-03-04 04:37:45 +0000 | [diff] [blame] | 213 | assert (0 && "Can't copy this kind of constant into register yet"); |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 214 | } |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 215 | |
| 216 | bool V8ISel::runOnFunction(Function &Fn) { |
| 217 | // First pass over the function, lower any unknown intrinsic functions |
| 218 | // with the IntrinsicLowering class. |
| 219 | LowerUnknownIntrinsicFunctionCalls(Fn); |
| 220 | |
| 221 | F = &MachineFunction::construct(&Fn, TM); |
| 222 | |
| 223 | // Create all of the machine basic blocks for the function... |
| 224 | for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) |
| 225 | F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I)); |
| 226 | |
| 227 | BB = &F->front(); |
| 228 | |
| 229 | // Set up a frame object for the return address. This is used by the |
| 230 | // llvm.returnaddress & llvm.frameaddress intrinisics. |
| 231 | //ReturnAddressIndex = F->getFrameInfo()->CreateFixedObject(4, -4); |
| 232 | |
| 233 | // Copy incoming arguments off of the stack and out of fixed registers. |
| 234 | //LoadArgumentsToVirtualRegs(Fn); |
| 235 | |
| 236 | // Instruction select everything except PHI nodes |
| 237 | visit(Fn); |
| 238 | |
| 239 | // Select the PHI nodes |
| 240 | //SelectPHINodes(); |
| 241 | |
| 242 | RegMap.clear(); |
| 243 | MBBMap.clear(); |
| 244 | F = 0; |
| 245 | // We always build a machine code representation for the function |
| 246 | return true; |
| 247 | } |
| 248 | |
Brian Gaeke | f7e44ef | 2004-04-02 20:53:33 +0000 | [diff] [blame] | 249 | void V8ISel::visitCallInst(CallInst &I) { |
| 250 | assert (I.getNumOperands () == 1 && "Can't handle call args yet"); |
Brian Gaeke | ea8494b | 2004-04-06 22:09:23 +0000 | [diff] [blame] | 251 | unsigned DestReg = getReg (I); |
Brian Gaeke | f7e44ef | 2004-04-02 20:53:33 +0000 | [diff] [blame] | 252 | BuildMI (BB, V8::CALL, 1).addPCDisp (I.getOperand (0)); |
Brian Gaeke | ea8494b | 2004-04-06 22:09:23 +0000 | [diff] [blame] | 253 | if (I.getType ()->getPrimitiveID () == Type::VoidTyID) |
| 254 | return; |
| 255 | // Deal w/ return value |
| 256 | switch (getClass (I.getType ())) { |
| 257 | case cByte: |
| 258 | case cShort: |
| 259 | case cInt: |
| 260 | // Schlep it over into the destination register |
| 261 | BuildMI (BB, V8::ORrr, 2, DestReg).addReg(V8::G0).addReg(V8::O0); |
| 262 | break; |
| 263 | default: |
| 264 | visitInstruction (I); |
| 265 | return; |
| 266 | } |
Brian Gaeke | f7e44ef | 2004-04-02 20:53:33 +0000 | [diff] [blame] | 267 | } |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 268 | |
| 269 | void V8ISel::visitReturnInst(ReturnInst &I) { |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 270 | if (I.getNumOperands () == 1) { |
| 271 | unsigned RetValReg = getReg (I.getOperand (0)); |
| 272 | switch (getClass (I.getOperand (0)->getType ())) { |
| 273 | case cByte: |
| 274 | case cShort: |
| 275 | case cInt: |
| 276 | // Schlep it over into i0 (where it will become o0 after restore). |
| 277 | BuildMI (BB, V8::ORrr, 2, V8::I0).addReg(V8::G0).addReg(RetValReg); |
| 278 | break; |
| 279 | default: |
| 280 | visitInstruction (I); |
| 281 | return; |
| 282 | } |
| 283 | } else if (I.getNumOperands () != 1) { |
| 284 | visitInstruction (I); |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 285 | } |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 286 | // Just emit a 'retl' instruction to return. |
| 287 | BuildMI(BB, V8::RETL, 0); |
| 288 | return; |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame^] | 291 | void V8ISel::visitBinaryOperator (Instruction &I) { |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 292 | unsigned DestReg = getReg (I); |
| 293 | unsigned Op0Reg = getReg (I.getOperand (0)); |
| 294 | unsigned Op1Reg = getReg (I.getOperand (1)); |
| 295 | |
| 296 | unsigned ResultReg = makeAnotherReg (I.getType ()); |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 297 | unsigned OpCase = ~0; |
| 298 | |
Brian Gaeke | 2d4fa8f | 2004-04-07 04:00:49 +0000 | [diff] [blame] | 299 | // FIXME: support long, ulong, fp. |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 300 | switch (I.getOpcode ()) { |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 301 | case Instruction::Add: OpCase = 0; break; |
| 302 | case Instruction::Sub: OpCase = 1; break; |
| 303 | case Instruction::Mul: OpCase = 2; break; |
| 304 | case Instruction::And: OpCase = 3; break; |
| 305 | case Instruction::Or: OpCase = 4; break; |
| 306 | case Instruction::Xor: OpCase = 5; break; |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame^] | 307 | case Instruction::Shl: OpCase = 6; break; |
| 308 | case Instruction::Shr: OpCase = 7+I.getType()->isSigned(); break; |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 309 | |
| 310 | case Instruction::Div: |
| 311 | case Instruction::Rem: { |
| 312 | unsigned Dest = ResultReg; |
| 313 | if (I.getOpcode() == Instruction::Rem) |
| 314 | Dest = makeAnotherReg(I.getType()); |
| 315 | |
| 316 | // FIXME: this is probably only right for 32 bit operands. |
| 317 | if (I.getType ()->isSigned()) { |
| 318 | unsigned Tmp = makeAnotherReg (I.getType ()); |
| 319 | // Sign extend into the Y register |
| 320 | BuildMI (BB, V8::SRAri, 2, Tmp).addReg (Op0Reg).addZImm (31); |
| 321 | BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (Tmp).addReg (V8::G0); |
| 322 | BuildMI (BB, V8::SDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg); |
| 323 | } else { |
| 324 | // Zero extend into the Y register, ie, just set it to zero |
| 325 | BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (V8::G0).addReg (V8::G0); |
| 326 | BuildMI (BB, V8::UDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg); |
Brian Gaeke | 2d4fa8f | 2004-04-07 04:00:49 +0000 | [diff] [blame] | 327 | } |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 328 | |
| 329 | if (I.getOpcode() == Instruction::Rem) { |
| 330 | unsigned Tmp = makeAnotherReg (I.getType ()); |
| 331 | BuildMI (BB, V8::SMULrr, 2, Tmp).addReg(Dest).addReg(Op1Reg); |
| 332 | BuildMI (BB, V8::SUBrr, 2, ResultReg).addReg(Op0Reg).addReg(Tmp); |
Brian Gaeke | f57e364 | 2004-03-16 22:37:11 +0000 | [diff] [blame] | 333 | } |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 334 | break; |
| 335 | } |
| 336 | default: |
| 337 | visitInstruction (I); |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | if (OpCase != ~0U) { |
| 342 | static const unsigned Opcodes[] = { |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame^] | 343 | V8::ADDrr, V8::SUBrr, V8::SMULrr, V8::ANDrr, V8::ORrr, V8::XORrr, |
| 344 | V8::SLLrr, V8::SRLrr, V8::SRArr |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 345 | }; |
| 346 | BuildMI (BB, Opcodes[OpCase], 2, ResultReg).addReg (Op0Reg).addReg (Op1Reg); |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | switch (getClass (I.getType ())) { |
| 350 | case cByte: |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 351 | if (I.getType ()->isSigned ()) { // add byte |
| 352 | BuildMI (BB, V8::ANDri, 2, DestReg).addReg (ResultReg).addZImm (0xff); |
| 353 | } else { // add ubyte |
| 354 | unsigned TmpReg = makeAnotherReg (I.getType ()); |
| 355 | BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (24); |
| 356 | BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (24); |
| 357 | } |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 358 | break; |
| 359 | case cShort: |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 360 | if (I.getType ()->isSigned ()) { // add short |
| 361 | unsigned TmpReg = makeAnotherReg (I.getType ()); |
| 362 | BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16); |
| 363 | BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (16); |
| 364 | } else { // add ushort |
| 365 | unsigned TmpReg = makeAnotherReg (I.getType ()); |
Brian Gaeke | 6d339f9 | 2004-03-16 22:45:42 +0000 | [diff] [blame] | 366 | BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16); |
| 367 | BuildMI (BB, V8::SRLri, 2, DestReg).addReg (TmpReg).addZImm (16); |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 368 | } |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 369 | break; |
| 370 | case cInt: |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 371 | BuildMI (BB, V8::ORrr, 2, DestReg).addReg (V8::G0).addReg (ResultReg); |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 372 | break; |
| 373 | default: |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 374 | visitInstruction (I); |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 375 | return; |
| 376 | } |
| 377 | } |
| 378 | |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 379 | |
| 380 | /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the |
| 381 | /// function, lowering any calls to unknown intrinsic functions into the |
| 382 | /// equivalent LLVM code. |
| 383 | void V8ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) { |
| 384 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 385 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) |
| 386 | if (CallInst *CI = dyn_cast<CallInst>(I++)) |
| 387 | if (Function *F = CI->getCalledFunction()) |
| 388 | switch (F->getIntrinsicID()) { |
| 389 | case Intrinsic::not_intrinsic: break; |
| 390 | default: |
| 391 | // All other intrinsic calls we must lower. |
| 392 | Instruction *Before = CI->getPrev(); |
| 393 | TM.getIntrinsicLowering().LowerIntrinsicCall(CI); |
| 394 | if (Before) { // Move iterator to instruction after call |
| 395 | I = Before; ++I; |
| 396 | } else { |
| 397 | I = BB->begin(); |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | |
| 403 | void V8ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) { |
| 404 | unsigned TmpReg1, TmpReg2; |
| 405 | switch (ID) { |
| 406 | default: assert(0 && "Intrinsic not supported!"); |
| 407 | } |
| 408 | } |