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" |
Brian Gaeke | 74dfcf1 | 2004-09-02 02:37:43 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Debug.h" |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 17 | #include "llvm/Instructions.h" |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 18 | #include "llvm/Pass.h" |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 19 | #include "llvm/Constants.h" |
Chris Lattner | 3048373 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/IntrinsicLowering.h" |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Brian Gaeke | 9df9282 | 2004-06-15 19:16:07 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Brian Gaeke | c93a752 | 2004-06-18 05:19:16 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineConstantPool.h" |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineFunction.h" |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/SSARegMap.h" |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetMachine.h" |
| 27 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
| 28 | #include "llvm/Support/InstVisitor.h" |
| 29 | #include "llvm/Support/CFG.h" |
| 30 | using namespace llvm; |
| 31 | |
| 32 | namespace { |
| 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 Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 54 | /// emitGEPOperation - Common code shared between visitGetElementPtrInst and |
| 55 | /// constant expression GEP support. |
| 56 | /// |
| 57 | void emitGEPOperation(MachineBasicBlock *BB, MachineBasicBlock::iterator IP, |
| 58 | Value *Src, User::op_iterator IdxBegin, |
| 59 | User::op_iterator IdxEnd, unsigned TargetReg); |
| 60 | |
Brian Gaeke | 00e514e | 2004-06-24 06:33:00 +0000 | [diff] [blame] | 61 | /// emitCastOperation - Common code shared between visitCastInst and |
| 62 | /// constant expression cast support. |
| 63 | /// |
| 64 | void emitCastOperation(MachineBasicBlock *BB,MachineBasicBlock::iterator IP, |
| 65 | Value *Src, const Type *DestTy, unsigned TargetReg); |
| 66 | |
Brian Gaeke | 8b6c1ff | 2004-10-14 19:39:34 +0000 | [diff] [blame] | 67 | /// emitIntegerCast, emitFPToIntegerCast - Helper methods for |
| 68 | /// emitCastOperation. |
| 69 | /// |
| 70 | void emitIntegerCast (MachineBasicBlock *BB, MachineBasicBlock::iterator IP, |
| 71 | const Type *oldTy, unsigned SrcReg, const Type *newTy, |
| 72 | unsigned DestReg); |
| 73 | void emitFPToIntegerCast (MachineBasicBlock *BB, |
| 74 | MachineBasicBlock::iterator IP, const Type *oldTy, |
| 75 | unsigned SrcReg, const Type *newTy, |
| 76 | unsigned DestReg); |
| 77 | |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 78 | /// visitBasicBlock - This method is called when we are visiting a new basic |
| 79 | /// block. This simply creates a new MachineBasicBlock to emit code into |
| 80 | /// and adds it to the current MachineFunction. Subsequent visit* for |
| 81 | /// instructions will be invoked for all instructions in the basic block. |
| 82 | /// |
| 83 | void visitBasicBlock(BasicBlock &LLVM_BB) { |
| 84 | BB = MBBMap[&LLVM_BB]; |
| 85 | } |
| 86 | |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 87 | void visitBinaryOperator(Instruction &I); |
Brian Gaeke | d6a1053 | 2004-06-15 21:09:46 +0000 | [diff] [blame] | 88 | void visitShiftInst (ShiftInst &SI) { visitBinaryOperator (SI); } |
Misha Brukman | ea09126 | 2004-06-30 21:47:40 +0000 | [diff] [blame] | 89 | void visitSetCondInst(SetCondInst &I); |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 90 | void visitCallInst(CallInst &I); |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 91 | void visitReturnInst(ReturnInst &I); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 92 | void visitBranchInst(BranchInst &I); |
Chris Lattner | d14d5b4 | 2004-10-17 02:42:42 +0000 | [diff] [blame] | 93 | void visitUnreachableInst(UnreachableInst &I) {} |
Brian Gaeke | 3d11e8a | 2004-04-13 18:27:46 +0000 | [diff] [blame] | 94 | void visitCastInst(CastInst &I); |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 95 | void visitLoadInst(LoadInst &I); |
| 96 | void visitStoreInst(StoreInst &I); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 97 | void visitPHINode(PHINode &I) {} // PHI nodes handled by second pass |
| 98 | void visitGetElementPtrInst(GetElementPtrInst &I); |
Brian Gaeke | c93a752 | 2004-06-18 05:19:16 +0000 | [diff] [blame] | 99 | void visitAllocaInst(AllocaInst &I); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 100 | |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 101 | void visitInstruction(Instruction &I) { |
| 102 | std::cerr << "Unhandled instruction: " << I; |
| 103 | abort(); |
| 104 | } |
| 105 | |
| 106 | /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the |
| 107 | /// function, lowering any calls to unknown intrinsic functions into the |
| 108 | /// equivalent LLVM code. |
| 109 | void LowerUnknownIntrinsicFunctionCalls(Function &F); |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 110 | void visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI); |
| 111 | |
Brian Gaeke | 562cb16 | 2004-04-07 17:04:09 +0000 | [diff] [blame] | 112 | void LoadArgumentsToVirtualRegs(Function *F); |
| 113 | |
Brian Gaeke | 6c868a4 | 2004-06-17 22:34:08 +0000 | [diff] [blame] | 114 | /// SelectPHINodes - Insert machine code to generate phis. This is tricky |
| 115 | /// because we have to generate our sources into the source basic blocks, |
| 116 | /// not the current one. |
| 117 | /// |
| 118 | void SelectPHINodes(); |
| 119 | |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 120 | /// copyConstantToRegister - Output the instructions required to put the |
| 121 | /// specified constant into the specified register. |
| 122 | /// |
| 123 | void copyConstantToRegister(MachineBasicBlock *MBB, |
| 124 | MachineBasicBlock::iterator IP, |
| 125 | Constant *C, unsigned R); |
| 126 | |
| 127 | /// makeAnotherReg - This method returns the next register number we haven't |
| 128 | /// yet used. |
| 129 | /// |
| 130 | /// Long values are handled somewhat specially. They are always allocated |
| 131 | /// as pairs of 32 bit integer values. The register number returned is the |
| 132 | /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits |
| 133 | /// of the long value. |
| 134 | /// |
| 135 | unsigned makeAnotherReg(const Type *Ty) { |
| 136 | assert(dynamic_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo()) && |
| 137 | "Current target doesn't have SparcV8 reg info??"); |
| 138 | const SparcV8RegisterInfo *MRI = |
| 139 | static_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo()); |
| 140 | if (Ty == Type::LongTy || Ty == Type::ULongTy) { |
| 141 | const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy); |
| 142 | // Create the lower part |
| 143 | F->getSSARegMap()->createVirtualRegister(RC); |
| 144 | // Create the upper part. |
| 145 | return F->getSSARegMap()->createVirtualRegister(RC)-1; |
| 146 | } |
| 147 | |
| 148 | // Add the mapping of regnumber => reg class to MachineFunction |
| 149 | const TargetRegisterClass *RC = MRI->getRegClassForType(Ty); |
| 150 | return F->getSSARegMap()->createVirtualRegister(RC); |
| 151 | } |
| 152 | |
| 153 | unsigned getReg(Value &V) { return getReg (&V); } // allow refs. |
| 154 | unsigned getReg(Value *V) { |
| 155 | // Just append to the end of the current bb. |
| 156 | MachineBasicBlock::iterator It = BB->end(); |
| 157 | return getReg(V, BB, It); |
| 158 | } |
| 159 | unsigned getReg(Value *V, MachineBasicBlock *MBB, |
| 160 | MachineBasicBlock::iterator IPt) { |
| 161 | unsigned &Reg = RegMap[V]; |
| 162 | if (Reg == 0) { |
| 163 | Reg = makeAnotherReg(V->getType()); |
| 164 | RegMap[V] = Reg; |
| 165 | } |
| 166 | // If this operand is a constant, emit the code to copy the constant into |
| 167 | // the register here... |
| 168 | // |
| 169 | if (Constant *C = dyn_cast<Constant>(V)) { |
| 170 | copyConstantToRegister(MBB, IPt, C, Reg); |
| 171 | RegMap.erase(V); // Assign a new name to this constant if ref'd again |
| 172 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 173 | // Move the address of the global into the register |
Brian Gaeke | cf47198 | 2004-03-09 04:49:13 +0000 | [diff] [blame] | 174 | unsigned TmpReg = makeAnotherReg(V->getType()); |
| 175 | BuildMI (*MBB, IPt, V8::SETHIi, 1, TmpReg).addGlobalAddress (GV); |
| 176 | BuildMI (*MBB, IPt, V8::ORri, 2, Reg).addReg (TmpReg) |
| 177 | .addGlobalAddress (GV); |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 178 | RegMap.erase(V); // Assign a new name to this address if ref'd again |
| 179 | } |
| 180 | |
| 181 | return Reg; |
| 182 | } |
| 183 | |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 184 | }; |
| 185 | } |
| 186 | |
| 187 | FunctionPass *llvm::createSparcV8SimpleInstructionSelector(TargetMachine &TM) { |
| 188 | return new V8ISel(TM); |
| 189 | } |
| 190 | |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 191 | enum TypeClass { |
Brian Gaeke | f57e364 | 2004-03-16 22:37:11 +0000 | [diff] [blame] | 192 | cByte, cShort, cInt, cLong, cFloat, cDouble |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 193 | }; |
| 194 | |
| 195 | static TypeClass getClass (const Type *T) { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 196 | switch (T->getTypeID()) { |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 197 | case Type::UByteTyID: case Type::SByteTyID: return cByte; |
| 198 | case Type::UShortTyID: case Type::ShortTyID: return cShort; |
Brian Gaeke | 562cb16 | 2004-04-07 17:04:09 +0000 | [diff] [blame] | 199 | case Type::PointerTyID: |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 200 | case Type::UIntTyID: case Type::IntTyID: return cInt; |
Brian Gaeke | f57e364 | 2004-03-16 22:37:11 +0000 | [diff] [blame] | 201 | case Type::ULongTyID: case Type::LongTyID: return cLong; |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 202 | case Type::FloatTyID: return cFloat; |
| 203 | case Type::DoubleTyID: return cDouble; |
| 204 | default: |
| 205 | assert (0 && "Type of unknown class passed to getClass?"); |
| 206 | return cByte; |
| 207 | } |
| 208 | } |
Brian Gaeke | 50094ed | 2004-10-10 19:57:18 +0000 | [diff] [blame] | 209 | |
Chris Lattner | 0d538bb | 2004-04-07 04:36:53 +0000 | [diff] [blame] | 210 | static TypeClass getClassB(const Type *T) { |
| 211 | if (T == Type::BoolTy) return cByte; |
| 212 | return getClass(T); |
| 213 | } |
| 214 | |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 215 | /// copyConstantToRegister - Output the instructions required to put the |
| 216 | /// specified constant into the specified register. |
| 217 | /// |
| 218 | void V8ISel::copyConstantToRegister(MachineBasicBlock *MBB, |
| 219 | MachineBasicBlock::iterator IP, |
| 220 | Constant *C, unsigned R) { |
Brian Gaeke | 9df9282 | 2004-06-15 19:16:07 +0000 | [diff] [blame] | 221 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 222 | switch (CE->getOpcode()) { |
| 223 | case Instruction::GetElementPtr: |
| 224 | emitGEPOperation(MBB, IP, CE->getOperand(0), |
| 225 | CE->op_begin()+1, CE->op_end(), R); |
| 226 | return; |
Brian Gaeke | 00e514e | 2004-06-24 06:33:00 +0000 | [diff] [blame] | 227 | case Instruction::Cast: |
| 228 | emitCastOperation(MBB, IP, CE->getOperand(0), CE->getType(), R); |
| 229 | return; |
Brian Gaeke | 9df9282 | 2004-06-15 19:16:07 +0000 | [diff] [blame] | 230 | default: |
| 231 | std::cerr << "Copying this constant expr not yet handled: " << *CE; |
| 232 | abort(); |
| 233 | } |
Chris Lattner | d14d5b4 | 2004-10-17 02:42:42 +0000 | [diff] [blame] | 234 | } else if (isa<UndefValue>(C)) { |
| 235 | BuildMI(*MBB, IP, V8::IMPLICIT_DEF, 0, R); |
| 236 | if (getClassB (C->getType ()) == cLong) |
| 237 | BuildMI(*MBB, IP, V8::IMPLICIT_DEF, 0, R+1); |
| 238 | return; |
Brian Gaeke | 9df9282 | 2004-06-15 19:16:07 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Brian Gaeke | e302a7e | 2004-05-07 21:39:30 +0000 | [diff] [blame] | 241 | if (C->getType()->isIntegral ()) { |
| 242 | uint64_t Val; |
Brian Gaeke | 9df9282 | 2004-06-15 19:16:07 +0000 | [diff] [blame] | 243 | unsigned Class = getClassB (C->getType ()); |
| 244 | if (Class == cLong) { |
| 245 | unsigned TmpReg = makeAnotherReg (Type::IntTy); |
| 246 | unsigned TmpReg2 = makeAnotherReg (Type::IntTy); |
| 247 | // Copy the value into the register pair. |
| 248 | // R = top(more-significant) half, R+1 = bottom(less-significant) half |
| 249 | uint64_t Val = cast<ConstantInt>(C)->getRawValue(); |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 250 | copyConstantToRegister(MBB, IP, ConstantUInt::get(Type::UIntTy, |
| 251 | Val >> 32), R); |
| 252 | copyConstantToRegister(MBB, IP, ConstantUInt::get(Type::UIntTy, |
| 253 | Val & 0xffffffffU), R+1); |
Brian Gaeke | 9df9282 | 2004-06-15 19:16:07 +0000 | [diff] [blame] | 254 | return; |
| 255 | } |
| 256 | |
| 257 | assert(Class <= cInt && "Type not handled yet!"); |
| 258 | |
Brian Gaeke | e302a7e | 2004-05-07 21:39:30 +0000 | [diff] [blame] | 259 | if (C->getType() == Type::BoolTy) { |
| 260 | Val = (C == ConstantBool::True); |
| 261 | } else { |
Brian Gaeke | 13dc433 | 2004-06-24 09:17:47 +0000 | [diff] [blame] | 262 | ConstantInt *CI = cast<ConstantInt> (C); |
Brian Gaeke | e302a7e | 2004-05-07 21:39:30 +0000 | [diff] [blame] | 263 | Val = CI->getRawValue (); |
| 264 | } |
Brian Gaeke | 9df9282 | 2004-06-15 19:16:07 +0000 | [diff] [blame] | 265 | switch (Class) { |
Brian Gaeke | 13dc433 | 2004-06-24 09:17:47 +0000 | [diff] [blame] | 266 | case cByte: Val = (int8_t) Val; break; |
| 267 | case cShort: Val = (int16_t) Val; break; |
| 268 | case cInt: Val = (int32_t) Val; break; |
Brian Gaeke | e806173 | 2004-03-04 00:56:25 +0000 | [diff] [blame] | 269 | default: |
Brian Gaeke | 2d4fa8f | 2004-04-07 04:00:49 +0000 | [diff] [blame] | 270 | std::cerr << "Offending constant: " << *C << "\n"; |
Brian Gaeke | 775158d | 2004-03-04 04:37:45 +0000 | [diff] [blame] | 271 | assert (0 && "Can't copy this kind of constant into register yet"); |
Brian Gaeke | e806173 | 2004-03-04 00:56:25 +0000 | [diff] [blame] | 272 | return; |
| 273 | } |
Brian Gaeke | 13dc433 | 2004-06-24 09:17:47 +0000 | [diff] [blame] | 274 | if (Val == 0) { |
| 275 | BuildMI (*MBB, IP, V8::ORrr, 2, R).addReg (V8::G0).addReg(V8::G0); |
| 276 | } else if (((int64_t)Val >= -4096) && ((int64_t)Val <= 4095)) { |
| 277 | BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (V8::G0).addSImm(Val); |
| 278 | } else { |
| 279 | unsigned TmpReg = makeAnotherReg (C->getType ()); |
| 280 | BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg) |
| 281 | .addSImm (((uint32_t) Val) >> 10); |
| 282 | BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg) |
| 283 | .addSImm (((uint32_t) Val) & 0x03ff); |
| 284 | return; |
| 285 | } |
Brian Gaeke | c93a752 | 2004-06-18 05:19:16 +0000 | [diff] [blame] | 286 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
| 287 | // We need to spill the constant to memory... |
| 288 | MachineConstantPool *CP = F->getConstantPool(); |
| 289 | unsigned CPI = CP->getConstantPoolIndex(CFP); |
| 290 | const Type *Ty = CFP->getType(); |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 291 | unsigned TmpReg = makeAnotherReg (Type::UIntTy); |
| 292 | unsigned AddrReg = makeAnotherReg (Type::UIntTy); |
Brian Gaeke | c93a752 | 2004-06-18 05:19:16 +0000 | [diff] [blame] | 293 | |
| 294 | assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!"); |
Brian Gaeke | 4473303 | 2004-06-24 07:36:48 +0000 | [diff] [blame] | 295 | unsigned LoadOpcode = Ty == Type::FloatTy ? V8::LDFri : V8::LDDFri; |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 296 | BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addConstantPoolIndex (CPI); |
Brian Gaeke | 50094ed | 2004-10-10 19:57:18 +0000 | [diff] [blame] | 297 | BuildMI (*MBB, IP, V8::ORri, 2, AddrReg).addReg (TmpReg) |
| 298 | .addConstantPoolIndex (CPI); |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 299 | BuildMI (*MBB, IP, LoadOpcode, 2, R).addReg (AddrReg).addSImm (0); |
Brian Gaeke | 9df9282 | 2004-06-15 19:16:07 +0000 | [diff] [blame] | 300 | } else if (isa<ConstantPointerNull>(C)) { |
| 301 | // Copy zero (null pointer) to the register. |
Brian Gaeke | c7fd0f4 | 2004-06-24 08:55:09 +0000 | [diff] [blame] | 302 | BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (V8::G0).addSImm (0); |
Chris Lattner | 7330248 | 2004-07-18 07:26:17 +0000 | [diff] [blame] | 303 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) { |
Brian Gaeke | 9df9282 | 2004-06-15 19:16:07 +0000 | [diff] [blame] | 304 | // Copy it with a SETHI/OR pair; the JIT + asmwriter should recognize |
| 305 | // that SETHI %reg,global == SETHI %reg,%hi(global) and |
| 306 | // OR %reg,global,%reg == OR %reg,%lo(global),%reg. |
| 307 | unsigned TmpReg = makeAnotherReg (C->getType ()); |
Chris Lattner | 7330248 | 2004-07-18 07:26:17 +0000 | [diff] [blame] | 308 | BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addGlobalAddress(GV); |
| 309 | BuildMI (*MBB, IP, V8::ORri, 2, R).addReg(TmpReg).addGlobalAddress(GV); |
Brian Gaeke | 9df9282 | 2004-06-15 19:16:07 +0000 | [diff] [blame] | 310 | } else { |
| 311 | std::cerr << "Offending constant: " << *C << "\n"; |
| 312 | assert (0 && "Can't copy this kind of constant into register yet"); |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 313 | } |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 314 | } |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 315 | |
Brian Gaeke | 812c488 | 2004-07-16 10:31:25 +0000 | [diff] [blame] | 316 | void V8ISel::LoadArgumentsToVirtualRegs (Function *LF) { |
| 317 | unsigned ArgOffset; |
Brian Gaeke | 562cb16 | 2004-04-07 17:04:09 +0000 | [diff] [blame] | 318 | static const unsigned IncomingArgRegs[] = { V8::I0, V8::I1, V8::I2, |
| 319 | V8::I3, V8::I4, V8::I5 }; |
Brian Gaeke | 812c488 | 2004-07-16 10:31:25 +0000 | [diff] [blame] | 320 | // Add IMPLICIT_DEFs of input regs. |
| 321 | ArgOffset = 0; |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 322 | for (Function::aiterator I = LF->abegin(), E = LF->aend(); |
| 323 | I != E && ArgOffset < 6; ++I, ++ArgOffset) { |
Brian Gaeke | 812c488 | 2004-07-16 10:31:25 +0000 | [diff] [blame] | 324 | unsigned Reg = getReg(*I); |
| 325 | switch (getClassB(I->getType())) { |
| 326 | case cByte: |
| 327 | case cShort: |
| 328 | case cInt: |
| 329 | case cFloat: |
| 330 | BuildMI(BB, V8::IMPLICIT_DEF, 0, IncomingArgRegs[ArgOffset]); |
| 331 | break; |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 332 | case cDouble: |
| 333 | case cLong: |
| 334 | // Double and Long use register pairs. |
| 335 | BuildMI(BB, V8::IMPLICIT_DEF, 0, IncomingArgRegs[ArgOffset]); |
| 336 | ++ArgOffset; |
| 337 | if (ArgOffset < 6) |
| 338 | BuildMI(BB, V8::IMPLICIT_DEF, 0, IncomingArgRegs[ArgOffset]); |
| 339 | break; |
Brian Gaeke | 812c488 | 2004-07-16 10:31:25 +0000 | [diff] [blame] | 340 | default: |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 341 | assert (0 && "type not handled"); |
Brian Gaeke | 812c488 | 2004-07-16 10:31:25 +0000 | [diff] [blame] | 342 | return; |
| 343 | } |
Brian Gaeke | 812c488 | 2004-07-16 10:31:25 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | ArgOffset = 0; |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 347 | for (Function::aiterator I = LF->abegin(), E = LF->aend(); I != E; |
| 348 | ++I, ++ArgOffset) { |
Brian Gaeke | 562cb16 | 2004-04-07 17:04:09 +0000 | [diff] [blame] | 349 | unsigned Reg = getReg(*I); |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 350 | if (ArgOffset < 6) { |
| 351 | |
| 352 | switch (getClassB(I->getType())) { |
| 353 | case cByte: |
| 354 | case cShort: |
| 355 | case cInt: |
| 356 | BuildMI(BB, V8::ORrr, 2, Reg).addReg (V8::G0) |
| 357 | .addReg (IncomingArgRegs[ArgOffset]); |
| 358 | break; |
| 359 | case cFloat: { |
| 360 | // Single-fp args are passed in integer registers; go through |
| 361 | // memory to get them into FP registers. (Bleh!) |
| 362 | unsigned FltAlign = TM.getTargetData().getFloatAlignment(); |
| 363 | int FI = F->getFrameInfo()->CreateStackObject(4, FltAlign); |
| 364 | BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (0) |
| 365 | .addReg (IncomingArgRegs[ArgOffset]); |
| 366 | BuildMI (BB, V8::LDFri, 2, Reg).addFrameIndex (FI).addSImm (0); |
| 367 | break; |
| 368 | } |
Brian Gaeke | 6672f86 | 2004-09-30 19:44:32 +0000 | [diff] [blame] | 369 | case cDouble: { |
| 370 | // Double-fp args are passed in pairs of integer registers; go through |
| 371 | // memory to get them into FP registers. (Double bleh!) |
| 372 | unsigned DblAlign = TM.getTargetData().getDoubleAlignment(); |
| 373 | int FI = F->getFrameInfo()->CreateStackObject(8, DblAlign); |
| 374 | BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (0) |
| 375 | .addReg (IncomingArgRegs[ArgOffset]); |
| 376 | ++ArgOffset; |
| 377 | BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (4) |
| 378 | .addReg (IncomingArgRegs[ArgOffset]); |
| 379 | BuildMI (BB, V8::LDDFri, 2, Reg).addFrameIndex (FI).addSImm (0); |
| 380 | break; |
| 381 | } |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 382 | default: |
Brian Gaeke | 6672f86 | 2004-09-30 19:44:32 +0000 | [diff] [blame] | 383 | // FIXME: handle cLong |
| 384 | assert (0 && "64-bit int (long/ulong) function args not handled"); |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 385 | return; |
| 386 | } |
| 387 | |
| 388 | } else { |
| 389 | |
| 390 | switch (getClassB(I->getType())) { |
| 391 | case cByte: |
| 392 | case cShort: |
| 393 | case cInt: { |
| 394 | int FI = F->getFrameInfo()->CreateFixedObject(4, 68 + (4 * ArgOffset)); |
| 395 | BuildMI (BB, V8::LD, 2, Reg).addFrameIndex (FI).addSImm(0); |
| 396 | break; |
| 397 | } |
| 398 | case cFloat: { |
| 399 | int FI = F->getFrameInfo()->CreateFixedObject(4, 68 + (4 * ArgOffset)); |
| 400 | BuildMI (BB, V8::LDFri, 2, Reg).addFrameIndex (FI).addSImm(0); |
| 401 | break; |
| 402 | } |
| 403 | case cDouble: { |
| 404 | int FI = F->getFrameInfo()->CreateFixedObject(8, 68 + (4 * ArgOffset)); |
| 405 | BuildMI (BB, V8::LDDFri, 2, Reg).addFrameIndex (FI).addSImm(0); |
| 406 | break; |
| 407 | } |
| 408 | default: |
| 409 | // FIXME: handle cLong |
| 410 | assert (0 && "64-bit integer (long/ulong) function args not handled"); |
| 411 | return; |
| 412 | } |
Brian Gaeke | 812c488 | 2004-07-16 10:31:25 +0000 | [diff] [blame] | 413 | } |
Brian Gaeke | 562cb16 | 2004-04-07 17:04:09 +0000 | [diff] [blame] | 414 | } |
Brian Gaeke | 812c488 | 2004-07-16 10:31:25 +0000 | [diff] [blame] | 415 | |
Brian Gaeke | 562cb16 | 2004-04-07 17:04:09 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Brian Gaeke | 6c868a4 | 2004-06-17 22:34:08 +0000 | [diff] [blame] | 418 | void V8ISel::SelectPHINodes() { |
| 419 | const TargetInstrInfo &TII = *TM.getInstrInfo(); |
| 420 | const Function &LF = *F->getFunction(); // The LLVM function... |
| 421 | for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) { |
| 422 | const BasicBlock *BB = I; |
| 423 | MachineBasicBlock &MBB = *MBBMap[I]; |
| 424 | |
| 425 | // Loop over all of the PHI nodes in the LLVM basic block... |
| 426 | MachineBasicBlock::iterator PHIInsertPoint = MBB.begin(); |
| 427 | for (BasicBlock::const_iterator I = BB->begin(); |
| 428 | PHINode *PN = const_cast<PHINode*>(dyn_cast<PHINode>(I)); ++I) { |
| 429 | |
| 430 | // Create a new machine instr PHI node, and insert it. |
| 431 | unsigned PHIReg = getReg(*PN); |
| 432 | MachineInstr *PhiMI = BuildMI(MBB, PHIInsertPoint, |
| 433 | V8::PHI, PN->getNumOperands(), PHIReg); |
| 434 | |
| 435 | MachineInstr *LongPhiMI = 0; |
| 436 | if (PN->getType() == Type::LongTy || PN->getType() == Type::ULongTy) |
| 437 | LongPhiMI = BuildMI(MBB, PHIInsertPoint, |
| 438 | V8::PHI, PN->getNumOperands(), PHIReg+1); |
| 439 | |
| 440 | // PHIValues - Map of blocks to incoming virtual registers. We use this |
| 441 | // so that we only initialize one incoming value for a particular block, |
| 442 | // even if the block has multiple entries in the PHI node. |
| 443 | // |
| 444 | std::map<MachineBasicBlock*, unsigned> PHIValues; |
| 445 | |
| 446 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 447 | MachineBasicBlock *PredMBB = 0; |
| 448 | for (MachineBasicBlock::pred_iterator PI = MBB.pred_begin (), |
| 449 | PE = MBB.pred_end (); PI != PE; ++PI) |
| 450 | if (PN->getIncomingBlock(i) == (*PI)->getBasicBlock()) { |
| 451 | PredMBB = *PI; |
| 452 | break; |
| 453 | } |
| 454 | assert (PredMBB && "Couldn't find incoming machine-cfg edge for phi"); |
| 455 | |
| 456 | unsigned ValReg; |
| 457 | std::map<MachineBasicBlock*, unsigned>::iterator EntryIt = |
| 458 | PHIValues.lower_bound(PredMBB); |
| 459 | |
| 460 | if (EntryIt != PHIValues.end() && EntryIt->first == PredMBB) { |
| 461 | // We already inserted an initialization of the register for this |
| 462 | // predecessor. Recycle it. |
| 463 | ValReg = EntryIt->second; |
| 464 | |
| 465 | } else { |
| 466 | // Get the incoming value into a virtual register. |
| 467 | // |
| 468 | Value *Val = PN->getIncomingValue(i); |
| 469 | |
| 470 | // If this is a constant or GlobalValue, we may have to insert code |
| 471 | // into the basic block to compute it into a virtual register. |
| 472 | if ((isa<Constant>(Val) && !isa<ConstantExpr>(Val)) || |
| 473 | isa<GlobalValue>(Val)) { |
| 474 | // Simple constants get emitted at the end of the basic block, |
| 475 | // before any terminator instructions. We "know" that the code to |
| 476 | // move a constant into a register will never clobber any flags. |
| 477 | ValReg = getReg(Val, PredMBB, PredMBB->getFirstTerminator()); |
| 478 | } else { |
| 479 | // Because we don't want to clobber any values which might be in |
| 480 | // physical registers with the computation of this constant (which |
| 481 | // might be arbitrarily complex if it is a constant expression), |
| 482 | // just insert the computation at the top of the basic block. |
| 483 | MachineBasicBlock::iterator PI = PredMBB->begin(); |
| 484 | |
| 485 | // Skip over any PHI nodes though! |
| 486 | while (PI != PredMBB->end() && PI->getOpcode() == V8::PHI) |
| 487 | ++PI; |
| 488 | |
| 489 | ValReg = getReg(Val, PredMBB, PI); |
| 490 | } |
| 491 | |
| 492 | // Remember that we inserted a value for this PHI for this predecessor |
| 493 | PHIValues.insert(EntryIt, std::make_pair(PredMBB, ValReg)); |
| 494 | } |
| 495 | |
| 496 | PhiMI->addRegOperand(ValReg); |
| 497 | PhiMI->addMachineBasicBlockOperand(PredMBB); |
| 498 | if (LongPhiMI) { |
| 499 | LongPhiMI->addRegOperand(ValReg+1); |
| 500 | LongPhiMI->addMachineBasicBlockOperand(PredMBB); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | // Now that we emitted all of the incoming values for the PHI node, make |
| 505 | // sure to reposition the InsertPoint after the PHI that we just added. |
| 506 | // This is needed because we might have inserted a constant into this |
| 507 | // block, right after the PHI's which is before the old insert point! |
| 508 | PHIInsertPoint = LongPhiMI ? LongPhiMI : PhiMI; |
| 509 | ++PHIInsertPoint; |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 514 | bool V8ISel::runOnFunction(Function &Fn) { |
| 515 | // First pass over the function, lower any unknown intrinsic functions |
| 516 | // with the IntrinsicLowering class. |
| 517 | LowerUnknownIntrinsicFunctionCalls(Fn); |
| 518 | |
| 519 | F = &MachineFunction::construct(&Fn, TM); |
| 520 | |
| 521 | // Create all of the machine basic blocks for the function... |
| 522 | for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) |
| 523 | F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I)); |
| 524 | |
| 525 | BB = &F->front(); |
| 526 | |
| 527 | // Set up a frame object for the return address. This is used by the |
| 528 | // llvm.returnaddress & llvm.frameaddress intrinisics. |
| 529 | //ReturnAddressIndex = F->getFrameInfo()->CreateFixedObject(4, -4); |
| 530 | |
| 531 | // Copy incoming arguments off of the stack and out of fixed registers. |
Brian Gaeke | 562cb16 | 2004-04-07 17:04:09 +0000 | [diff] [blame] | 532 | LoadArgumentsToVirtualRegs(&Fn); |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 533 | |
| 534 | // Instruction select everything except PHI nodes |
| 535 | visit(Fn); |
| 536 | |
| 537 | // Select the PHI nodes |
Brian Gaeke | 6c868a4 | 2004-06-17 22:34:08 +0000 | [diff] [blame] | 538 | SelectPHINodes(); |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 539 | |
| 540 | RegMap.clear(); |
| 541 | MBBMap.clear(); |
| 542 | F = 0; |
| 543 | // We always build a machine code representation for the function |
| 544 | return true; |
| 545 | } |
| 546 | |
Brian Gaeke | 3d11e8a | 2004-04-13 18:27:46 +0000 | [diff] [blame] | 547 | void V8ISel::visitCastInst(CastInst &I) { |
Brian Gaeke | 00e514e | 2004-06-24 06:33:00 +0000 | [diff] [blame] | 548 | Value *Op = I.getOperand(0); |
| 549 | unsigned DestReg = getReg(I); |
| 550 | MachineBasicBlock::iterator MI = BB->end(); |
| 551 | emitCastOperation(BB, MI, Op, I.getType(), DestReg); |
| 552 | } |
| 553 | |
Brian Gaeke | 8b6c1ff | 2004-10-14 19:39:34 +0000 | [diff] [blame] | 554 | |
| 555 | void V8ISel::emitIntegerCast (MachineBasicBlock *BB, |
| 556 | MachineBasicBlock::iterator IP, const Type *oldTy, |
| 557 | unsigned SrcReg, const Type *newTy, |
| 558 | unsigned DestReg) { |
| 559 | if (oldTy == newTy) { |
| 560 | // No-op cast - just emit a copy; assume the reg. allocator will zap it. |
| 561 | BuildMI (*BB, IP, V8::ORrr, 2, DestReg).addReg (V8::G0).addReg(SrcReg); |
| 562 | return; |
| 563 | } |
| 564 | // Emit left-shift, then right-shift to sign- or zero-extend. |
| 565 | unsigned TmpReg = makeAnotherReg (newTy); |
| 566 | unsigned shiftWidth = 32 - (8 * TM.getTargetData ().getTypeSize (newTy)); |
| 567 | BuildMI (*BB, IP, V8::SLLri, 2, TmpReg).addZImm (shiftWidth).addReg(SrcReg); |
| 568 | if (newTy->isSigned ()) { // sign-extend with SRA |
| 569 | BuildMI(*BB, IP, V8::SRAri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg); |
| 570 | } else { // zero-extend with SRL |
| 571 | BuildMI(*BB, IP, V8::SRLri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | void V8ISel::emitFPToIntegerCast (MachineBasicBlock *BB, |
| 576 | MachineBasicBlock::iterator IP, |
| 577 | const Type *oldTy, unsigned SrcReg, |
| 578 | const Type *newTy, unsigned DestReg) { |
| 579 | unsigned FPCastOpcode, FPStoreOpcode, FPSize, FPAlign; |
| 580 | unsigned oldTyClass = getClassB(oldTy); |
| 581 | if (oldTyClass == cFloat) { |
| 582 | FPCastOpcode = V8::FSTOI; FPStoreOpcode = V8::STFri; FPSize = 4; |
| 583 | FPAlign = TM.getTargetData().getFloatAlignment(); |
| 584 | } else { // it's a double |
| 585 | FPCastOpcode = V8::FDTOI; FPStoreOpcode = V8::STDFri; FPSize = 8; |
| 586 | FPAlign = TM.getTargetData().getDoubleAlignment(); |
| 587 | } |
| 588 | unsigned TempReg = makeAnotherReg (oldTy); |
| 589 | BuildMI (*BB, IP, FPCastOpcode, 1, TempReg).addReg (SrcReg); |
| 590 | int FI = F->getFrameInfo()->CreateStackObject(FPSize, FPAlign); |
| 591 | BuildMI (*BB, IP, FPStoreOpcode, 3).addFrameIndex (FI).addSImm (0) |
| 592 | .addReg (TempReg); |
| 593 | unsigned TempReg2 = makeAnotherReg (newTy); |
| 594 | BuildMI (*BB, IP, V8::LD, 3, TempReg2).addFrameIndex (FI).addSImm (0); |
| 595 | emitIntegerCast (BB, IP, Type::IntTy, TempReg2, newTy, DestReg); |
| 596 | } |
| 597 | |
Brian Gaeke | 00e514e | 2004-06-24 06:33:00 +0000 | [diff] [blame] | 598 | /// emitCastOperation - Common code shared between visitCastInst and constant |
| 599 | /// expression cast support. |
| 600 | /// |
| 601 | void V8ISel::emitCastOperation(MachineBasicBlock *BB, |
Brian Gaeke | 8b6c1ff | 2004-10-14 19:39:34 +0000 | [diff] [blame] | 602 | MachineBasicBlock::iterator IP, Value *Src, |
| 603 | const Type *DestTy, unsigned DestReg) { |
Brian Gaeke | 00e514e | 2004-06-24 06:33:00 +0000 | [diff] [blame] | 604 | const Type *SrcTy = Src->getType(); |
| 605 | unsigned SrcClass = getClassB(SrcTy); |
| 606 | unsigned DestClass = getClassB(DestTy); |
| 607 | unsigned SrcReg = getReg(Src, BB, IP); |
| 608 | |
| 609 | const Type *oldTy = SrcTy; |
| 610 | const Type *newTy = DestTy; |
| 611 | unsigned oldTyClass = SrcClass; |
| 612 | unsigned newTyClass = DestClass; |
Brian Gaeke | 3d11e8a | 2004-04-13 18:27:46 +0000 | [diff] [blame] | 613 | |
Brian Gaeke | 429022b | 2004-05-08 06:36:14 +0000 | [diff] [blame] | 614 | if (oldTyClass < cLong && newTyClass < cLong) { |
Brian Gaeke | 8b6c1ff | 2004-10-14 19:39:34 +0000 | [diff] [blame] | 615 | emitIntegerCast (BB, IP, oldTy, SrcReg, newTy, DestReg); |
| 616 | } else switch (newTyClass) { |
| 617 | case cByte: |
| 618 | case cShort: |
| 619 | case cInt: |
Brian Gaeke | 495a097 | 2004-06-24 21:22:08 +0000 | [diff] [blame] | 620 | switch (oldTyClass) { |
Brian Gaeke | 8b6c1ff | 2004-10-14 19:39:34 +0000 | [diff] [blame] | 621 | case cFloat: |
| 622 | case cDouble: |
| 623 | emitFPToIntegerCast (BB, IP, oldTy, SrcReg, newTy, DestReg); |
| 624 | break; |
| 625 | default: goto not_yet; |
| 626 | } |
| 627 | return; |
| 628 | |
| 629 | case cFloat: |
| 630 | switch (oldTyClass) { |
| 631 | case cLong: goto not_yet; |
Brian Gaeke | 495a097 | 2004-06-24 21:22:08 +0000 | [diff] [blame] | 632 | case cFloat: |
| 633 | BuildMI (*BB, IP, V8::FMOVS, 1, DestReg).addReg (SrcReg); |
| 634 | break; |
| 635 | case cDouble: |
| 636 | BuildMI (*BB, IP, V8::FDTOS, 1, DestReg).addReg (SrcReg); |
| 637 | break; |
Brian Gaeke | ec3227f | 2004-06-27 22:47:33 +0000 | [diff] [blame] | 638 | default: { |
| 639 | unsigned FltAlign = TM.getTargetData().getFloatAlignment(); |
Brian Gaeke | 8b6c1ff | 2004-10-14 19:39:34 +0000 | [diff] [blame] | 640 | // cast integer type to float. Store it to a stack slot and then load |
Brian Gaeke | 495a097 | 2004-06-24 21:22:08 +0000 | [diff] [blame] | 641 | // it using ldf into a floating point register. then do fitos. |
Brian Gaeke | ec3227f | 2004-06-27 22:47:33 +0000 | [diff] [blame] | 642 | unsigned TmpReg = makeAnotherReg (newTy); |
| 643 | int FI = F->getFrameInfo()->CreateStackObject(4, FltAlign); |
| 644 | BuildMI (*BB, IP, V8::ST, 3).addFrameIndex (FI).addSImm (0) |
| 645 | .addReg (SrcReg); |
| 646 | BuildMI (*BB, IP, V8::LDFri, 2, TmpReg).addFrameIndex (FI).addSImm (0); |
| 647 | BuildMI (*BB, IP, V8::FITOS, 1, DestReg).addReg(TmpReg); |
Brian Gaeke | 495a097 | 2004-06-24 21:22:08 +0000 | [diff] [blame] | 648 | break; |
| 649 | } |
Brian Gaeke | ec3227f | 2004-06-27 22:47:33 +0000 | [diff] [blame] | 650 | } |
Brian Gaeke | 8b6c1ff | 2004-10-14 19:39:34 +0000 | [diff] [blame] | 651 | return; |
| 652 | |
| 653 | case cDouble: |
Brian Gaeke | 495a097 | 2004-06-24 21:22:08 +0000 | [diff] [blame] | 654 | switch (oldTyClass) { |
Brian Gaeke | 8b6c1ff | 2004-10-14 19:39:34 +0000 | [diff] [blame] | 655 | case cLong: goto not_yet; |
Brian Gaeke | 495a097 | 2004-06-24 21:22:08 +0000 | [diff] [blame] | 656 | case cFloat: |
| 657 | BuildMI (*BB, IP, V8::FSTOD, 1, DestReg).addReg (SrcReg); |
| 658 | break; |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 659 | case cDouble: // use double move pseudo-instr |
| 660 | BuildMI (*BB, IP, V8::FpMOVD, 1, DestReg).addReg (SrcReg); |
Brian Gaeke | 495a097 | 2004-06-24 21:22:08 +0000 | [diff] [blame] | 661 | break; |
Brian Gaeke | ec3227f | 2004-06-27 22:47:33 +0000 | [diff] [blame] | 662 | default: { |
| 663 | unsigned DoubleAlignment = TM.getTargetData().getDoubleAlignment(); |
| 664 | unsigned TmpReg = makeAnotherReg (newTy); |
| 665 | int FI = F->getFrameInfo()->CreateStackObject(8, DoubleAlignment); |
| 666 | BuildMI (*BB, IP, V8::ST, 3).addFrameIndex (FI).addSImm (0) |
| 667 | .addReg (SrcReg); |
| 668 | BuildMI (*BB, IP, V8::LDDFri, 2, TmpReg).addFrameIndex (FI).addSImm (0); |
| 669 | BuildMI (*BB, IP, V8::FITOD, 1, DestReg).addReg(TmpReg); |
| 670 | break; |
| 671 | } |
| 672 | } |
Brian Gaeke | 8b6c1ff | 2004-10-14 19:39:34 +0000 | [diff] [blame] | 673 | return; |
| 674 | |
| 675 | case cLong: |
| 676 | switch (oldTyClass) { |
| 677 | case cLong: |
Brian Gaeke | 2a9f539 | 2004-07-08 07:52:13 +0000 | [diff] [blame] | 678 | // Just copy it |
| 679 | BuildMI (*BB, IP, V8::ORrr, 2, DestReg).addReg (V8::G0).addReg (SrcReg); |
| 680 | BuildMI (*BB, IP, V8::ORrr, 2, DestReg+1).addReg (V8::G0) |
| 681 | .addReg (SrcReg+1); |
Brian Gaeke | 8b6c1ff | 2004-10-14 19:39:34 +0000 | [diff] [blame] | 682 | break; |
| 683 | default: goto not_yet; |
Brian Gaeke | 2a9f539 | 2004-07-08 07:52:13 +0000 | [diff] [blame] | 684 | } |
Brian Gaeke | 8b6c1ff | 2004-10-14 19:39:34 +0000 | [diff] [blame] | 685 | return; |
| 686 | |
| 687 | default: goto not_yet; |
Brian Gaeke | e302a7e | 2004-05-07 21:39:30 +0000 | [diff] [blame] | 688 | } |
Brian Gaeke | 8b6c1ff | 2004-10-14 19:39:34 +0000 | [diff] [blame] | 689 | return; |
| 690 | not_yet: |
| 691 | std::cerr << "Sorry, cast still unsupported: SrcTy = " << *SrcTy |
| 692 | << ", DestTy = " << *DestTy << "\n"; |
| 693 | abort (); |
Brian Gaeke | 3d11e8a | 2004-04-13 18:27:46 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 696 | void V8ISel::visitLoadInst(LoadInst &I) { |
| 697 | unsigned DestReg = getReg (I); |
| 698 | unsigned PtrReg = getReg (I.getOperand (0)); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 699 | switch (getClassB (I.getType ())) { |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 700 | case cByte: |
| 701 | if (I.getType ()->isSigned ()) |
Brian Gaeke | 4473303 | 2004-06-24 07:36:48 +0000 | [diff] [blame] | 702 | BuildMI (BB, V8::LDSB, 2, DestReg).addReg (PtrReg).addSImm(0); |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 703 | else |
Brian Gaeke | 4473303 | 2004-06-24 07:36:48 +0000 | [diff] [blame] | 704 | BuildMI (BB, V8::LDUB, 2, DestReg).addReg (PtrReg).addSImm(0); |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 705 | return; |
| 706 | case cShort: |
| 707 | if (I.getType ()->isSigned ()) |
Brian Gaeke | 4473303 | 2004-06-24 07:36:48 +0000 | [diff] [blame] | 708 | BuildMI (BB, V8::LDSH, 2, DestReg).addReg (PtrReg).addSImm(0); |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 709 | else |
Brian Gaeke | 4473303 | 2004-06-24 07:36:48 +0000 | [diff] [blame] | 710 | BuildMI (BB, V8::LDUH, 2, DestReg).addReg (PtrReg).addSImm(0); |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 711 | return; |
| 712 | case cInt: |
Brian Gaeke | 4473303 | 2004-06-24 07:36:48 +0000 | [diff] [blame] | 713 | BuildMI (BB, V8::LD, 2, DestReg).addReg (PtrReg).addSImm(0); |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 714 | return; |
| 715 | case cLong: |
Brian Gaeke | 4473303 | 2004-06-24 07:36:48 +0000 | [diff] [blame] | 716 | BuildMI (BB, V8::LD, 2, DestReg).addReg (PtrReg).addSImm(0); |
| 717 | BuildMI (BB, V8::LD, 2, DestReg+1).addReg (PtrReg).addSImm(4); |
| 718 | return; |
| 719 | case cFloat: |
| 720 | BuildMI (BB, V8::LDFri, 2, DestReg).addReg (PtrReg).addSImm(0); |
| 721 | return; |
| 722 | case cDouble: |
| 723 | BuildMI (BB, V8::LDDFri, 2, DestReg).addReg (PtrReg).addSImm(0); |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 724 | return; |
| 725 | default: |
| 726 | std::cerr << "Load instruction not handled: " << I; |
| 727 | abort (); |
| 728 | return; |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | void V8ISel::visitStoreInst(StoreInst &I) { |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 733 | Value *SrcVal = I.getOperand (0); |
| 734 | unsigned SrcReg = getReg (SrcVal); |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 735 | unsigned PtrReg = getReg (I.getOperand (1)); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 736 | switch (getClassB (SrcVal->getType ())) { |
| 737 | case cByte: |
Brian Gaeke | 4473303 | 2004-06-24 07:36:48 +0000 | [diff] [blame] | 738 | BuildMI (BB, V8::STB, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 739 | return; |
| 740 | case cShort: |
Brian Gaeke | 4473303 | 2004-06-24 07:36:48 +0000 | [diff] [blame] | 741 | BuildMI (BB, V8::STH, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 742 | return; |
| 743 | case cInt: |
Brian Gaeke | 4473303 | 2004-06-24 07:36:48 +0000 | [diff] [blame] | 744 | BuildMI (BB, V8::ST, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 745 | return; |
| 746 | case cLong: |
Brian Gaeke | 4473303 | 2004-06-24 07:36:48 +0000 | [diff] [blame] | 747 | BuildMI (BB, V8::ST, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg); |
| 748 | BuildMI (BB, V8::ST, 3).addReg (PtrReg).addSImm (4).addReg (SrcReg+1); |
| 749 | return; |
| 750 | case cFloat: |
| 751 | BuildMI (BB, V8::STFri, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg); |
| 752 | return; |
| 753 | case cDouble: |
| 754 | BuildMI (BB, V8::STDFri, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 755 | return; |
| 756 | default: |
| 757 | std::cerr << "Store instruction not handled: " << I; |
| 758 | abort (); |
| 759 | return; |
| 760 | } |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Brian Gaeke | f7e44ef | 2004-04-02 20:53:33 +0000 | [diff] [blame] | 763 | void V8ISel::visitCallInst(CallInst &I) { |
Brian Gaeke | 9d67ea0 | 2004-06-18 06:27:48 +0000 | [diff] [blame] | 764 | MachineInstr *TheCall; |
| 765 | // Is it an intrinsic function call? |
| 766 | if (Function *F = I.getCalledFunction()) { |
| 767 | if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) { |
| 768 | visitIntrinsicCall(ID, I); // Special intrinsics are not handled here |
| 769 | return; |
| 770 | } |
| 771 | } |
| 772 | |
Brian Gaeke | 50094ed | 2004-10-10 19:57:18 +0000 | [diff] [blame] | 773 | unsigned extraStack = 0; |
| 774 | // How much extra call stack will we need? |
| 775 | for (unsigned i = 7; i < I.getNumOperands (); ++i) { |
| 776 | switch (getClassB (I.getOperand (i)->getType ())) { |
| 777 | case cLong: extraStack += 8; break; |
| 778 | case cFloat: extraStack += 4; break; |
| 779 | case cDouble: extraStack += 8; break; |
| 780 | default: extraStack += 4; break; |
| 781 | } |
| 782 | } |
| 783 | |
Brian Gaeke | 9d67ea0 | 2004-06-18 06:27:48 +0000 | [diff] [blame] | 784 | // Deal with args |
Brian Gaeke | 562cb16 | 2004-04-07 17:04:09 +0000 | [diff] [blame] | 785 | static const unsigned OutgoingArgRegs[] = { V8::O0, V8::O1, V8::O2, V8::O3, |
Brian Gaeke | d54c38b | 2004-04-07 16:41:22 +0000 | [diff] [blame] | 786 | V8::O4, V8::O5 }; |
Brian Gaeke | 24b90c3 | 2004-11-14 03:22:07 +0000 | [diff] [blame^] | 787 | const unsigned *OAREnd = &OutgoingArgRegs[6]; |
Brian Gaeke | 6931fd6 | 2004-11-04 00:27:04 +0000 | [diff] [blame] | 788 | const unsigned *OAR = &OutgoingArgRegs[0]; |
Brian Gaeke | 24b90c3 | 2004-11-14 03:22:07 +0000 | [diff] [blame^] | 789 | unsigned ArgOffset = 68; |
Brian Gaeke | 50094ed | 2004-10-10 19:57:18 +0000 | [diff] [blame] | 790 | for (unsigned i = 1; i < I.getNumOperands (); ++i) { |
| 791 | unsigned ArgReg = getReg (I.getOperand (i)); |
Brian Gaeke | 24b90c3 | 2004-11-14 03:22:07 +0000 | [diff] [blame^] | 792 | if (i == 7 && extraStack) |
| 793 | BuildMI (BB, V8::ADJCALLSTACKDOWN, 1).addImm (extraStack); |
| 794 | if (getClassB (I.getOperand (i)->getType ()) < cLong) { |
| 795 | // Schlep it over into the incoming arg register |
| 796 | if (ArgOffset < 92) { |
| 797 | assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs"); |
| 798 | BuildMI (BB, V8::ORrr, 2, *OAR++).addReg (V8::G0).addReg (ArgReg); |
Brian Gaeke | 812c488 | 2004-07-16 10:31:25 +0000 | [diff] [blame] | 799 | } else { |
Brian Gaeke | 24b90c3 | 2004-11-14 03:22:07 +0000 | [diff] [blame^] | 800 | BuildMI (BB, V8::ST, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (ArgReg); |
Brian Gaeke | 812c488 | 2004-07-16 10:31:25 +0000 | [diff] [blame] | 801 | } |
Brian Gaeke | 24b90c3 | 2004-11-14 03:22:07 +0000 | [diff] [blame^] | 802 | ArgOffset += 4; |
| 803 | } else if (getClassB (I.getOperand (i)->getType ()) == cFloat) { |
| 804 | if (ArgOffset < 92) { |
| 805 | // Single-fp args are passed in integer registers; go through |
| 806 | // memory to get them out of FP registers. (Bleh!) |
| 807 | unsigned FltAlign = TM.getTargetData().getFloatAlignment(); |
| 808 | int FI = F->getFrameInfo()->CreateStackObject(4, FltAlign); |
| 809 | BuildMI (BB, V8::STFri, 3).addFrameIndex (FI).addSImm (0).addReg (ArgReg); |
| 810 | assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs"); |
| 811 | BuildMI (BB, V8::LD, 2, *OAR++).addFrameIndex (FI).addSImm (0); |
| 812 | } else { |
| 813 | BuildMI (BB, V8::STFri, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (ArgReg); |
| 814 | } |
| 815 | ArgOffset += 4; |
| 816 | } else if (getClassB (I.getOperand (i)->getType ()) == cDouble) { |
| 817 | // Double-fp args are passed in pairs of integer registers; go through |
| 818 | // memory to get them out of FP registers. (Bleh!) |
| 819 | // We'd like to 'std' these right onto the outgoing-args area, but it might |
| 820 | // not be 8-byte aligned (e.g., call x(int x, double d)). sigh. |
| 821 | unsigned DblAlign = TM.getTargetData().getDoubleAlignment(); |
| 822 | int FI = F->getFrameInfo()->CreateStackObject(8, DblAlign); |
| 823 | BuildMI (BB, V8::STDFri, 3).addFrameIndex (FI).addSImm (0).addReg (ArgReg); |
| 824 | if (ArgOffset < 92 && OAR != OAREnd) { |
| 825 | assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs"); |
| 826 | BuildMI (BB, V8::LD, 2, *OAR++).addFrameIndex (FI).addSImm (0); |
| 827 | } else { |
| 828 | unsigned TempReg = makeAnotherReg (Type::IntTy); |
| 829 | BuildMI (BB, V8::LD, 2, TempReg).addFrameIndex (FI).addSImm (0); |
| 830 | BuildMI (BB, V8::ST, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (TempReg); |
| 831 | } |
| 832 | ArgOffset += 4; |
| 833 | if (ArgOffset < 92 && OAR != OAREnd) { |
| 834 | assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs"); |
| 835 | BuildMI (BB, V8::LD, 2, *OAR++).addFrameIndex (FI).addSImm (4); |
| 836 | } else { |
| 837 | unsigned TempReg = makeAnotherReg (Type::IntTy); |
| 838 | BuildMI (BB, V8::LD, 2, TempReg).addFrameIndex (FI).addSImm (4); |
| 839 | BuildMI (BB, V8::ST, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (TempReg); |
| 840 | } |
| 841 | ArgOffset += 4; |
| 842 | } else if (getClassB (I.getOperand (i)->getType ()) == cLong) { |
| 843 | // do the first half... |
| 844 | if (ArgOffset < 92) { |
| 845 | assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs"); |
| 846 | BuildMI (BB, V8::ORrr, 2, *OAR++).addReg (V8::G0).addReg (ArgReg); |
| 847 | } else { |
| 848 | BuildMI (BB, V8::ST, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (ArgReg); |
| 849 | } |
| 850 | ArgOffset += 4; |
| 851 | // ...then do the second half |
| 852 | if (ArgOffset < 92) { |
| 853 | assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs"); |
| 854 | BuildMI (BB, V8::ORrr, 2, *OAR++).addReg (V8::G0).addReg (ArgReg+1); |
| 855 | } else { |
| 856 | BuildMI (BB, V8::ST, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (ArgReg+1); |
| 857 | } |
| 858 | ArgOffset += 4; |
Brian Gaeke | 50094ed | 2004-10-10 19:57:18 +0000 | [diff] [blame] | 859 | } else { |
Brian Gaeke | 24b90c3 | 2004-11-14 03:22:07 +0000 | [diff] [blame^] | 860 | assert (0 && "Unknown class?!"); |
Brian Gaeke | d54c38b | 2004-04-07 16:41:22 +0000 | [diff] [blame] | 861 | } |
Brian Gaeke | 50094ed | 2004-10-10 19:57:18 +0000 | [diff] [blame] | 862 | } |
Brian Gaeke | d54c38b | 2004-04-07 16:41:22 +0000 | [diff] [blame] | 863 | |
Brian Gaeke | 9d67ea0 | 2004-06-18 06:27:48 +0000 | [diff] [blame] | 864 | // Emit call instruction |
| 865 | if (Function *F = I.getCalledFunction ()) { |
| 866 | BuildMI (BB, V8::CALL, 1).addGlobalAddress (F, true); |
| 867 | } else { // Emit an indirect call... |
| 868 | unsigned Reg = getReg (I.getCalledValue ()); |
| 869 | BuildMI (BB, V8::JMPLrr, 3, V8::O7).addReg (Reg).addReg (V8::G0); |
| 870 | } |
| 871 | |
Brian Gaeke | 50094ed | 2004-10-10 19:57:18 +0000 | [diff] [blame] | 872 | if (extraStack) BuildMI (BB, V8::ADJCALLSTACKUP, 1).addImm (extraStack); |
| 873 | |
Brian Gaeke | 9d67ea0 | 2004-06-18 06:27:48 +0000 | [diff] [blame] | 874 | // Deal w/ return value: schlep it over into the destination register |
Brian Gaeke | e14e338 | 2004-06-15 20:06:32 +0000 | [diff] [blame] | 875 | if (I.getType () == Type::VoidTy) |
Brian Gaeke | ea8494b | 2004-04-06 22:09:23 +0000 | [diff] [blame] | 876 | return; |
Brian Gaeke | e14e338 | 2004-06-15 20:06:32 +0000 | [diff] [blame] | 877 | unsigned DestReg = getReg (I); |
Brian Gaeke | 299b39d | 2004-10-10 20:34:17 +0000 | [diff] [blame] | 878 | switch (getClassB (I.getType ())) { |
Brian Gaeke | ea8494b | 2004-04-06 22:09:23 +0000 | [diff] [blame] | 879 | case cByte: |
| 880 | case cShort: |
| 881 | case cInt: |
Brian Gaeke | ea8494b | 2004-04-06 22:09:23 +0000 | [diff] [blame] | 882 | BuildMI (BB, V8::ORrr, 2, DestReg).addReg(V8::G0).addReg(V8::O0); |
| 883 | break; |
Brian Gaeke | 9d67ea0 | 2004-06-18 06:27:48 +0000 | [diff] [blame] | 884 | case cFloat: |
| 885 | BuildMI (BB, V8::FMOVS, 2, DestReg).addReg(V8::F0); |
| 886 | break; |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 887 | case cDouble: |
| 888 | BuildMI (BB, V8::FpMOVD, 2, DestReg).addReg(V8::D0); |
| 889 | break; |
| 890 | case cLong: |
| 891 | BuildMI (BB, V8::ORrr, 2, DestReg).addReg(V8::G0).addReg(V8::O0); |
| 892 | BuildMI (BB, V8::ORrr, 2, DestReg+1).addReg(V8::G0).addReg(V8::O1); |
| 893 | break; |
Brian Gaeke | ea8494b | 2004-04-06 22:09:23 +0000 | [diff] [blame] | 894 | default: |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 895 | std::cerr << "Return type of call instruction not handled: " << I; |
| 896 | abort (); |
Brian Gaeke | ea8494b | 2004-04-06 22:09:23 +0000 | [diff] [blame] | 897 | } |
Brian Gaeke | f7e44ef | 2004-04-02 20:53:33 +0000 | [diff] [blame] | 898 | } |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 899 | |
| 900 | void V8ISel::visitReturnInst(ReturnInst &I) { |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 901 | if (I.getNumOperands () == 1) { |
| 902 | unsigned RetValReg = getReg (I.getOperand (0)); |
Brian Gaeke | 299b39d | 2004-10-10 20:34:17 +0000 | [diff] [blame] | 903 | switch (getClassB (I.getOperand (0)->getType ())) { |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 904 | case cByte: |
| 905 | case cShort: |
| 906 | case cInt: |
| 907 | // Schlep it over into i0 (where it will become o0 after restore). |
| 908 | BuildMI (BB, V8::ORrr, 2, V8::I0).addReg(V8::G0).addReg(RetValReg); |
| 909 | break; |
Brian Gaeke | f9a7546 | 2004-07-08 07:22:27 +0000 | [diff] [blame] | 910 | case cFloat: |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 911 | BuildMI (BB, V8::FMOVS, 1, V8::F0).addReg(RetValReg); |
Brian Gaeke | f9a7546 | 2004-07-08 07:22:27 +0000 | [diff] [blame] | 912 | break; |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 913 | case cDouble: |
| 914 | BuildMI (BB, V8::FpMOVD, 1, V8::D0).addReg(RetValReg); |
Brian Gaeke | 812c488 | 2004-07-16 10:31:25 +0000 | [diff] [blame] | 915 | break; |
Brian Gaeke | 2a9f539 | 2004-07-08 07:52:13 +0000 | [diff] [blame] | 916 | case cLong: |
| 917 | BuildMI (BB, V8::ORrr, 2, V8::I0).addReg(V8::G0).addReg(RetValReg); |
| 918 | BuildMI (BB, V8::ORrr, 2, V8::I1).addReg(V8::G0).addReg(RetValReg+1); |
| 919 | break; |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 920 | default: |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 921 | std::cerr << "Return instruction of this type not handled: " << I; |
| 922 | abort (); |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 923 | } |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 924 | } |
Chris Lattner | 0d538bb | 2004-04-07 04:36:53 +0000 | [diff] [blame] | 925 | |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 926 | // Just emit a 'retl' instruction to return. |
| 927 | BuildMI(BB, V8::RETL, 0); |
| 928 | return; |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 929 | } |
| 930 | |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 931 | static inline BasicBlock *getBlockAfter(BasicBlock *BB) { |
| 932 | Function::iterator I = BB; ++I; // Get iterator to next block |
| 933 | return I != BB->getParent()->end() ? &*I : 0; |
| 934 | } |
| 935 | |
| 936 | /// visitBranchInst - Handles conditional and unconditional branches. |
| 937 | /// |
| 938 | void V8ISel::visitBranchInst(BranchInst &I) { |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 939 | BasicBlock *takenSucc = I.getSuccessor (0); |
Brian Gaeke | 6c868a4 | 2004-06-17 22:34:08 +0000 | [diff] [blame] | 940 | MachineBasicBlock *takenSuccMBB = MBBMap[takenSucc]; |
| 941 | BB->addSuccessor (takenSuccMBB); |
| 942 | if (I.isConditional()) { // conditional branch |
| 943 | BasicBlock *notTakenSucc = I.getSuccessor (1); |
| 944 | MachineBasicBlock *notTakenSuccMBB = MBBMap[notTakenSucc]; |
| 945 | BB->addSuccessor (notTakenSuccMBB); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 946 | |
Brian Gaeke | 6c868a4 | 2004-06-17 22:34:08 +0000 | [diff] [blame] | 947 | // CondReg=(<condition>); |
| 948 | // If (CondReg==0) goto notTakenSuccMBB; |
| 949 | unsigned CondReg = getReg (I.getCondition ()); |
| 950 | BuildMI (BB, V8::CMPri, 2).addSImm (0).addReg (CondReg); |
| 951 | BuildMI (BB, V8::BE, 1).addMBB (notTakenSuccMBB); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 952 | } |
Brian Gaeke | 6c868a4 | 2004-06-17 22:34:08 +0000 | [diff] [blame] | 953 | // goto takenSuccMBB; |
| 954 | BuildMI (BB, V8::BA, 1).addMBB (takenSuccMBB); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | /// emitGEPOperation - Common code shared between visitGetElementPtrInst and |
| 958 | /// constant expression GEP support. |
| 959 | /// |
Brian Gaeke | 9f56482 | 2004-05-08 05:27:20 +0000 | [diff] [blame] | 960 | void V8ISel::emitGEPOperation (MachineBasicBlock *MBB, |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 961 | MachineBasicBlock::iterator IP, |
| 962 | Value *Src, User::op_iterator IdxBegin, |
| 963 | User::op_iterator IdxEnd, unsigned TargetReg) { |
Brian Gaeke | 9f56482 | 2004-05-08 05:27:20 +0000 | [diff] [blame] | 964 | const TargetData &TD = TM.getTargetData (); |
| 965 | const Type *Ty = Src->getType (); |
Brian Gaeke | c7fd0f4 | 2004-06-24 08:55:09 +0000 | [diff] [blame] | 966 | unsigned basePtrReg = getReg (Src, MBB, IP); |
Brian Gaeke | 9f56482 | 2004-05-08 05:27:20 +0000 | [diff] [blame] | 967 | |
| 968 | // GEPs have zero or more indices; we must perform a struct access |
| 969 | // or array access for each one. |
| 970 | for (GetElementPtrInst::op_iterator oi = IdxBegin, oe = IdxEnd; oi != oe; |
| 971 | ++oi) { |
| 972 | Value *idx = *oi; |
| 973 | unsigned nextBasePtrReg = makeAnotherReg (Type::UIntTy); |
| 974 | if (const StructType *StTy = dyn_cast<StructType> (Ty)) { |
| 975 | // It's a struct access. idx is the index into the structure, |
| 976 | // which names the field. Use the TargetData structure to |
| 977 | // pick out what the layout of the structure is in memory. |
| 978 | // Use the (constant) structure index's value to find the |
| 979 | // right byte offset from the StructLayout class's list of |
| 980 | // structure member offsets. |
| 981 | unsigned fieldIndex = cast<ConstantUInt> (idx)->getValue (); |
| 982 | unsigned memberOffset = |
| 983 | TD.getStructLayout (StTy)->MemberOffsets[fieldIndex]; |
| 984 | // Emit an ADD to add memberOffset to the basePtr. |
| 985 | BuildMI (*MBB, IP, V8::ADDri, 2, |
| 986 | nextBasePtrReg).addReg (basePtrReg).addZImm (memberOffset); |
| 987 | // The next type is the member of the structure selected by the |
| 988 | // index. |
| 989 | Ty = StTy->getElementType (fieldIndex); |
| 990 | } else if (const SequentialType *SqTy = dyn_cast<SequentialType> (Ty)) { |
| 991 | // It's an array or pointer access: [ArraySize x ElementType]. |
| 992 | // We want to add basePtrReg to (idxReg * sizeof ElementType). First, we |
| 993 | // must find the size of the pointed-to type (Not coincidentally, the next |
| 994 | // type is the type of the elements in the array). |
| 995 | Ty = SqTy->getElementType (); |
| 996 | unsigned elementSize = TD.getTypeSize (Ty); |
| 997 | unsigned idxReg = getReg (idx, MBB, IP); |
| 998 | unsigned OffsetReg = makeAnotherReg (Type::IntTy); |
| 999 | unsigned elementSizeReg = makeAnotherReg (Type::UIntTy); |
Brian Gaeke | c7fd0f4 | 2004-06-24 08:55:09 +0000 | [diff] [blame] | 1000 | copyConstantToRegister (MBB, IP, |
| 1001 | ConstantUInt::get(Type::UIntTy, elementSize), elementSizeReg); |
Brian Gaeke | 9f56482 | 2004-05-08 05:27:20 +0000 | [diff] [blame] | 1002 | // Emit a SMUL to multiply the register holding the index by |
| 1003 | // elementSize, putting the result in OffsetReg. |
| 1004 | BuildMI (*MBB, IP, V8::SMULrr, 2, |
| 1005 | OffsetReg).addReg (elementSizeReg).addReg (idxReg); |
| 1006 | // Emit an ADD to add OffsetReg to the basePtr. |
| 1007 | BuildMI (*MBB, IP, V8::ADDrr, 2, |
| 1008 | nextBasePtrReg).addReg (basePtrReg).addReg (OffsetReg); |
| 1009 | } |
| 1010 | basePtrReg = nextBasePtrReg; |
| 1011 | } |
| 1012 | // After we have processed all the indices, the result is left in |
| 1013 | // basePtrReg. Move it to the register where we were expected to |
| 1014 | // put the answer. |
| 1015 | BuildMI (BB, V8::ORrr, 1, TargetReg).addReg (V8::G0).addReg (basePtrReg); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | void V8ISel::visitGetElementPtrInst (GetElementPtrInst &I) { |
| 1019 | unsigned outputReg = getReg (I); |
| 1020 | emitGEPOperation (BB, BB->end (), I.getOperand (0), |
| 1021 | I.op_begin ()+1, I.op_end (), outputReg); |
| 1022 | } |
| 1023 | |
Brian Gaeke | d6a1053 | 2004-06-15 21:09:46 +0000 | [diff] [blame] | 1024 | |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 1025 | void V8ISel::visitBinaryOperator (Instruction &I) { |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 1026 | unsigned DestReg = getReg (I); |
| 1027 | unsigned Op0Reg = getReg (I.getOperand (0)); |
| 1028 | unsigned Op1Reg = getReg (I.getOperand (1)); |
| 1029 | |
Brian Gaeke | ec3227f | 2004-06-27 22:47:33 +0000 | [diff] [blame] | 1030 | unsigned Class = getClassB (I.getType()); |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 1031 | unsigned OpCase = ~0; |
| 1032 | |
Brian Gaeke | ec3227f | 2004-06-27 22:47:33 +0000 | [diff] [blame] | 1033 | if (Class > cLong) { |
| 1034 | switch (I.getOpcode ()) { |
| 1035 | case Instruction::Add: OpCase = 0; break; |
| 1036 | case Instruction::Sub: OpCase = 1; break; |
| 1037 | case Instruction::Mul: OpCase = 2; break; |
| 1038 | case Instruction::Div: OpCase = 3; break; |
| 1039 | default: visitInstruction (I); return; |
| 1040 | } |
| 1041 | static unsigned Opcodes[] = { V8::FADDS, V8::FADDD, |
| 1042 | V8::FSUBS, V8::FSUBD, |
| 1043 | V8::FMULS, V8::FMULD, |
| 1044 | V8::FDIVS, V8::FDIVD }; |
| 1045 | BuildMI (BB, Opcodes[2*OpCase + (Class - cFloat)], 2, DestReg) |
| 1046 | .addReg (Op0Reg).addReg (Op1Reg); |
| 1047 | return; |
| 1048 | } |
| 1049 | |
| 1050 | unsigned ResultReg = DestReg; |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 1051 | if (Class != cInt && Class != cLong) |
Brian Gaeke | ec3227f | 2004-06-27 22:47:33 +0000 | [diff] [blame] | 1052 | ResultReg = makeAnotherReg (I.getType ()); |
| 1053 | |
Brian Gaeke | 1df468e | 2004-09-29 03:34:41 +0000 | [diff] [blame] | 1054 | if (Class == cLong) { |
| 1055 | DEBUG (std::cerr << "Class = cLong\n"); |
| 1056 | DEBUG (std::cerr << "Op0Reg = " << Op0Reg << ", " << Op0Reg+1 << "\n"); |
| 1057 | DEBUG (std::cerr << "Op1Reg = " << Op1Reg << ", " << Op1Reg+1 << "\n"); |
| 1058 | DEBUG (std::cerr << "ResultReg = " << ResultReg << ", " << ResultReg+1 << "\n"); |
| 1059 | DEBUG (std::cerr << "DestReg = " << DestReg << ", " << DestReg+1 << "\n"); |
| 1060 | } |
| 1061 | |
| 1062 | // FIXME: support long, ulong. |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 1063 | switch (I.getOpcode ()) { |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 1064 | case Instruction::Add: OpCase = 0; break; |
| 1065 | case Instruction::Sub: OpCase = 1; break; |
| 1066 | case Instruction::Mul: OpCase = 2; break; |
| 1067 | case Instruction::And: OpCase = 3; break; |
| 1068 | case Instruction::Or: OpCase = 4; break; |
| 1069 | case Instruction::Xor: OpCase = 5; break; |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 1070 | case Instruction::Shl: OpCase = 6; break; |
| 1071 | case Instruction::Shr: OpCase = 7+I.getType()->isSigned(); break; |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 1072 | |
| 1073 | case Instruction::Div: |
| 1074 | case Instruction::Rem: { |
| 1075 | unsigned Dest = ResultReg; |
| 1076 | if (I.getOpcode() == Instruction::Rem) |
| 1077 | Dest = makeAnotherReg(I.getType()); |
| 1078 | |
| 1079 | // FIXME: this is probably only right for 32 bit operands. |
| 1080 | if (I.getType ()->isSigned()) { |
| 1081 | unsigned Tmp = makeAnotherReg (I.getType ()); |
| 1082 | // Sign extend into the Y register |
| 1083 | BuildMI (BB, V8::SRAri, 2, Tmp).addReg (Op0Reg).addZImm (31); |
| 1084 | BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (Tmp).addReg (V8::G0); |
| 1085 | BuildMI (BB, V8::SDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg); |
| 1086 | } else { |
| 1087 | // Zero extend into the Y register, ie, just set it to zero |
| 1088 | BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (V8::G0).addReg (V8::G0); |
| 1089 | BuildMI (BB, V8::UDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg); |
Brian Gaeke | 2d4fa8f | 2004-04-07 04:00:49 +0000 | [diff] [blame] | 1090 | } |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 1091 | |
| 1092 | if (I.getOpcode() == Instruction::Rem) { |
| 1093 | unsigned Tmp = makeAnotherReg (I.getType ()); |
| 1094 | BuildMI (BB, V8::SMULrr, 2, Tmp).addReg(Dest).addReg(Op1Reg); |
| 1095 | BuildMI (BB, V8::SUBrr, 2, ResultReg).addReg(Op0Reg).addReg(Tmp); |
Brian Gaeke | f57e364 | 2004-03-16 22:37:11 +0000 | [diff] [blame] | 1096 | } |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 1097 | break; |
| 1098 | } |
| 1099 | default: |
| 1100 | visitInstruction (I); |
| 1101 | return; |
| 1102 | } |
| 1103 | |
Brian Gaeke | c7fd0f4 | 2004-06-24 08:55:09 +0000 | [diff] [blame] | 1104 | static const unsigned Opcodes[] = { |
| 1105 | V8::ADDrr, V8::SUBrr, V8::SMULrr, V8::ANDrr, V8::ORrr, V8::XORrr, |
| 1106 | V8::SLLrr, V8::SRLrr, V8::SRArr |
| 1107 | }; |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 1108 | if (OpCase != ~0U) { |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 1109 | BuildMI (BB, Opcodes[OpCase], 2, ResultReg).addReg (Op0Reg).addReg (Op1Reg); |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
Brian Gaeke | ccdd70a | 2004-07-08 08:08:10 +0000 | [diff] [blame] | 1112 | switch (getClassB (I.getType ())) { |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 1113 | case cByte: |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 1114 | if (I.getType ()->isSigned ()) { // add byte |
| 1115 | BuildMI (BB, V8::ANDri, 2, DestReg).addReg (ResultReg).addZImm (0xff); |
| 1116 | } else { // add ubyte |
| 1117 | unsigned TmpReg = makeAnotherReg (I.getType ()); |
| 1118 | BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (24); |
| 1119 | BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (24); |
| 1120 | } |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 1121 | break; |
| 1122 | case cShort: |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 1123 | if (I.getType ()->isSigned ()) { // add short |
| 1124 | unsigned TmpReg = makeAnotherReg (I.getType ()); |
| 1125 | BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16); |
| 1126 | BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (16); |
| 1127 | } else { // add ushort |
| 1128 | unsigned TmpReg = makeAnotherReg (I.getType ()); |
Brian Gaeke | 6d339f9 | 2004-03-16 22:45:42 +0000 | [diff] [blame] | 1129 | BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16); |
| 1130 | BuildMI (BB, V8::SRLri, 2, DestReg).addReg (TmpReg).addZImm (16); |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 1131 | } |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 1132 | break; |
| 1133 | case cInt: |
Brian Gaeke | ccdd70a | 2004-07-08 08:08:10 +0000 | [diff] [blame] | 1134 | // Nothing to do here. |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 1135 | break; |
Brian Gaeke | c7fd0f4 | 2004-06-24 08:55:09 +0000 | [diff] [blame] | 1136 | case cLong: |
| 1137 | // Only support and, or, xor. |
| 1138 | if (OpCase < 3 || OpCase > 5) { |
| 1139 | visitInstruction (I); |
| 1140 | return; |
| 1141 | } |
| 1142 | // Do the other half of the value: |
Brian Gaeke | ec3227f | 2004-06-27 22:47:33 +0000 | [diff] [blame] | 1143 | BuildMI (BB, Opcodes[OpCase], 2, ResultReg+1).addReg (Op0Reg+1) |
| 1144 | .addReg (Op1Reg+1); |
Brian Gaeke | c7fd0f4 | 2004-06-24 08:55:09 +0000 | [diff] [blame] | 1145 | break; |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 1146 | default: |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 1147 | visitInstruction (I); |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 1148 | } |
| 1149 | } |
| 1150 | |
Misha Brukman | ea09126 | 2004-06-30 21:47:40 +0000 | [diff] [blame] | 1151 | void V8ISel::visitSetCondInst(SetCondInst &I) { |
Chris Lattner | 4d0cda4 | 2004-04-07 05:04:51 +0000 | [diff] [blame] | 1152 | unsigned Op0Reg = getReg (I.getOperand (0)); |
| 1153 | unsigned Op1Reg = getReg (I.getOperand (1)); |
| 1154 | unsigned DestReg = getReg (I); |
Brian Gaeke | 429022b | 2004-05-08 06:36:14 +0000 | [diff] [blame] | 1155 | const Type *Ty = I.getOperand (0)->getType (); |
Chris Lattner | 4d0cda4 | 2004-04-07 05:04:51 +0000 | [diff] [blame] | 1156 | |
| 1157 | // Compare the two values. |
Brian Gaeke | 3a08589 | 2004-07-08 09:08:35 +0000 | [diff] [blame] | 1158 | assert (getClass (Ty) != cLong && "can't setcc on longs yet"); |
| 1159 | if (getClass (Ty) < cLong) { |
| 1160 | BuildMI(BB, V8::SUBCCrr, 2, V8::G0).addReg(Op0Reg).addReg(Op1Reg); |
| 1161 | } else if (getClass (Ty) == cFloat) { |
| 1162 | BuildMI(BB, V8::FCMPS, 2).addReg(Op0Reg).addReg(Op1Reg); |
| 1163 | } else if (getClass (Ty) == cDouble) { |
| 1164 | BuildMI(BB, V8::FCMPD, 2).addReg(Op0Reg).addReg(Op1Reg); |
| 1165 | } |
Chris Lattner | 4d0cda4 | 2004-04-07 05:04:51 +0000 | [diff] [blame] | 1166 | |
Brian Gaeke | 429022b | 2004-05-08 06:36:14 +0000 | [diff] [blame] | 1167 | unsigned BranchIdx; |
Chris Lattner | 4d0cda4 | 2004-04-07 05:04:51 +0000 | [diff] [blame] | 1168 | switch (I.getOpcode()) { |
| 1169 | default: assert(0 && "Unknown setcc instruction!"); |
Brian Gaeke | 429022b | 2004-05-08 06:36:14 +0000 | [diff] [blame] | 1170 | case Instruction::SetEQ: BranchIdx = 0; break; |
| 1171 | case Instruction::SetNE: BranchIdx = 1; break; |
| 1172 | case Instruction::SetLT: BranchIdx = 2; break; |
| 1173 | case Instruction::SetGT: BranchIdx = 3; break; |
| 1174 | case Instruction::SetLE: BranchIdx = 4; break; |
| 1175 | case Instruction::SetGE: BranchIdx = 5; break; |
Chris Lattner | 4d0cda4 | 2004-04-07 05:04:51 +0000 | [diff] [blame] | 1176 | } |
Brian Gaeke | 3a08589 | 2004-07-08 09:08:35 +0000 | [diff] [blame] | 1177 | unsigned Column = 0; |
| 1178 | if (Ty->isSigned()) ++Column; |
| 1179 | if (Ty->isFloatingPoint()) ++Column; |
| 1180 | static unsigned OpcodeTab[3*6] = { |
| 1181 | // LLVM SparcV8 |
| 1182 | // unsigned signed fp |
| 1183 | V8::BE, V8::BE, V8::FBE, // seteq = be be fbe |
| 1184 | V8::BNE, V8::BNE, V8::FBNE, // setne = bne bne fbne |
| 1185 | V8::BCS, V8::BL, V8::FBL, // setlt = bcs bl fbl |
| 1186 | V8::BGU, V8::BG, V8::FBG, // setgt = bgu bg fbg |
| 1187 | V8::BLEU, V8::BLE, V8::FBLE, // setle = bleu ble fble |
| 1188 | V8::BCC, V8::BGE, V8::FBGE // setge = bcc bge fbge |
Brian Gaeke | 429022b | 2004-05-08 06:36:14 +0000 | [diff] [blame] | 1189 | }; |
Brian Gaeke | 3a08589 | 2004-07-08 09:08:35 +0000 | [diff] [blame] | 1190 | unsigned Opcode = OpcodeTab[3*BranchIdx + Column]; |
Brian Gaeke | 6c868a4 | 2004-06-17 22:34:08 +0000 | [diff] [blame] | 1191 | |
| 1192 | MachineBasicBlock *thisMBB = BB; |
| 1193 | const BasicBlock *LLVM_BB = BB->getBasicBlock (); |
| 1194 | // thisMBB: |
| 1195 | // ... |
| 1196 | // subcc %reg0, %reg1, %g0 |
| 1197 | // bCC copy1MBB |
| 1198 | // ba copy0MBB |
| 1199 | |
| 1200 | // FIXME: we wouldn't need copy0MBB (we could fold it into thisMBB) |
| 1201 | // if we could insert other, non-terminator instructions after the |
| 1202 | // bCC. But MBB->getFirstTerminator() can't understand this. |
| 1203 | MachineBasicBlock *copy1MBB = new MachineBasicBlock (LLVM_BB); |
| 1204 | F->getBasicBlockList ().push_back (copy1MBB); |
| 1205 | BuildMI (BB, Opcode, 1).addMBB (copy1MBB); |
| 1206 | MachineBasicBlock *copy0MBB = new MachineBasicBlock (LLVM_BB); |
| 1207 | F->getBasicBlockList ().push_back (copy0MBB); |
| 1208 | BuildMI (BB, V8::BA, 1).addMBB (copy0MBB); |
| 1209 | // Update machine-CFG edges |
| 1210 | BB->addSuccessor (copy1MBB); |
| 1211 | BB->addSuccessor (copy0MBB); |
| 1212 | |
| 1213 | // copy0MBB: |
| 1214 | // %FalseValue = or %G0, 0 |
| 1215 | // ba sinkMBB |
| 1216 | BB = copy0MBB; |
| 1217 | unsigned FalseValue = makeAnotherReg (I.getType ()); |
| 1218 | BuildMI (BB, V8::ORri, 2, FalseValue).addReg (V8::G0).addZImm (0); |
| 1219 | MachineBasicBlock *sinkMBB = new MachineBasicBlock (LLVM_BB); |
| 1220 | F->getBasicBlockList ().push_back (sinkMBB); |
| 1221 | BuildMI (BB, V8::BA, 1).addMBB (sinkMBB); |
| 1222 | // Update machine-CFG edges |
| 1223 | BB->addSuccessor (sinkMBB); |
| 1224 | |
| 1225 | DEBUG (std::cerr << "thisMBB is at " << (void*)thisMBB << "\n"); |
| 1226 | DEBUG (std::cerr << "copy1MBB is at " << (void*)copy1MBB << "\n"); |
| 1227 | DEBUG (std::cerr << "copy0MBB is at " << (void*)copy0MBB << "\n"); |
| 1228 | DEBUG (std::cerr << "sinkMBB is at " << (void*)sinkMBB << "\n"); |
| 1229 | |
| 1230 | // copy1MBB: |
| 1231 | // %TrueValue = or %G0, 1 |
| 1232 | // ba sinkMBB |
| 1233 | BB = copy1MBB; |
| 1234 | unsigned TrueValue = makeAnotherReg (I.getType ()); |
| 1235 | BuildMI (BB, V8::ORri, 2, TrueValue).addReg (V8::G0).addZImm (1); |
| 1236 | BuildMI (BB, V8::BA, 1).addMBB (sinkMBB); |
| 1237 | // Update machine-CFG edges |
| 1238 | BB->addSuccessor (sinkMBB); |
| 1239 | |
| 1240 | // sinkMBB: |
| 1241 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, copy1MBB ] |
| 1242 | // ... |
| 1243 | BB = sinkMBB; |
| 1244 | BuildMI (BB, V8::PHI, 4, DestReg).addReg (FalseValue) |
| 1245 | .addMBB (copy0MBB).addReg (TrueValue).addMBB (copy1MBB); |
Chris Lattner | 4d0cda4 | 2004-04-07 05:04:51 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
Brian Gaeke | c93a752 | 2004-06-18 05:19:16 +0000 | [diff] [blame] | 1248 | void V8ISel::visitAllocaInst(AllocaInst &I) { |
| 1249 | // Find the data size of the alloca inst's getAllocatedType. |
| 1250 | const Type *Ty = I.getAllocatedType(); |
| 1251 | unsigned TySize = TM.getTargetData().getTypeSize(Ty); |
Chris Lattner | 4d0cda4 | 2004-04-07 05:04:51 +0000 | [diff] [blame] | 1252 | |
Brian Gaeke | c93a752 | 2004-06-18 05:19:16 +0000 | [diff] [blame] | 1253 | unsigned ArraySizeReg = getReg (I.getArraySize ()); |
| 1254 | unsigned TySizeReg = getReg (ConstantUInt::get (Type::UIntTy, TySize)); |
| 1255 | unsigned TmpReg1 = makeAnotherReg (Type::UIntTy); |
| 1256 | unsigned TmpReg2 = makeAnotherReg (Type::UIntTy); |
| 1257 | unsigned StackAdjReg = makeAnotherReg (Type::UIntTy); |
Brian Gaeke | c93a752 | 2004-06-18 05:19:16 +0000 | [diff] [blame] | 1258 | |
| 1259 | // StackAdjReg = (ArraySize * TySize) rounded up to nearest doubleword boundary |
| 1260 | BuildMI (BB, V8::UMULrr, 2, TmpReg1).addReg (ArraySizeReg).addReg (TySizeReg); |
Brian Gaeke | cfaf224 | 2004-06-18 08:45:52 +0000 | [diff] [blame] | 1261 | |
Brian Gaeke | c93a752 | 2004-06-18 05:19:16 +0000 | [diff] [blame] | 1262 | // Round up TmpReg1 to nearest doubleword boundary: |
| 1263 | BuildMI (BB, V8::ADDri, 2, TmpReg2).addReg (TmpReg1).addSImm (7); |
| 1264 | BuildMI (BB, V8::ANDri, 2, StackAdjReg).addReg (TmpReg2).addSImm (-8); |
Brian Gaeke | cfaf224 | 2004-06-18 08:45:52 +0000 | [diff] [blame] | 1265 | |
| 1266 | // Subtract size from stack pointer, thereby allocating some space. |
Brian Gaeke | c93a752 | 2004-06-18 05:19:16 +0000 | [diff] [blame] | 1267 | BuildMI (BB, V8::SUBrr, 2, V8::SP).addReg (V8::SP).addReg (StackAdjReg); |
Brian Gaeke | cfaf224 | 2004-06-18 08:45:52 +0000 | [diff] [blame] | 1268 | |
| 1269 | // Put a pointer to the space into the result register, by copying |
| 1270 | // the stack pointer. |
| 1271 | BuildMI (BB, V8::ADDri, 2, getReg(I)).addReg (V8::SP).addSImm (96); |
| 1272 | |
| 1273 | // Inform the Frame Information that we have just allocated a variable-sized |
| 1274 | // object. |
| 1275 | F->getFrameInfo()->CreateVariableSizedObject(); |
Brian Gaeke | c93a752 | 2004-06-18 05:19:16 +0000 | [diff] [blame] | 1276 | } |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 1277 | |
| 1278 | /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the |
| 1279 | /// function, lowering any calls to unknown intrinsic functions into the |
| 1280 | /// equivalent LLVM code. |
| 1281 | void V8ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) { |
| 1282 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 1283 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) |
| 1284 | if (CallInst *CI = dyn_cast<CallInst>(I++)) |
| 1285 | if (Function *F = CI->getCalledFunction()) |
| 1286 | switch (F->getIntrinsicID()) { |
| 1287 | case Intrinsic::not_intrinsic: break; |
| 1288 | default: |
| 1289 | // All other intrinsic calls we must lower. |
| 1290 | Instruction *Before = CI->getPrev(); |
| 1291 | TM.getIntrinsicLowering().LowerIntrinsicCall(CI); |
| 1292 | if (Before) { // Move iterator to instruction after call |
| 1293 | I = Before; ++I; |
| 1294 | } else { |
| 1295 | I = BB->begin(); |
| 1296 | } |
| 1297 | } |
| 1298 | } |
| 1299 | |
| 1300 | |
| 1301 | void V8ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) { |
| 1302 | unsigned TmpReg1, TmpReg2; |
| 1303 | switch (ID) { |
| 1304 | default: assert(0 && "Intrinsic not supported!"); |
| 1305 | } |
| 1306 | } |