LLVM API Documentation
00001 // 00002 // The LLVM Compiler Infrastructure 00003 // 00004 // This file is distributed under the University of Illinois Open Source 00005 // License. See LICENSE.TXT for details. 00006 // 00007 //===----------------------------------------------------------------------===// 00008 // 00009 // This file defines the interfaces that PIC16 uses to lower LLVM code into a 00010 // selection DAG. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #define DEBUG_TYPE "pic16-lower" 00015 #include "PIC16ISelLowering.h" 00016 #include "PIC16TargetObjectFile.h" 00017 #include "PIC16TargetMachine.h" 00018 #include "llvm/DerivedTypes.h" 00019 #include "llvm/GlobalValue.h" 00020 #include "llvm/Function.h" 00021 #include "llvm/CallingConv.h" 00022 #include "llvm/CodeGen/MachineFrameInfo.h" 00023 #include "llvm/CodeGen/MachineFunction.h" 00024 #include "llvm/CodeGen/MachineInstrBuilder.h" 00025 #include "llvm/CodeGen/MachineRegisterInfo.h" 00026 #include "llvm/Support/ErrorHandling.h" 00027 00028 00029 using namespace llvm; 00030 00031 static const char *getIntrinsicName(unsigned opcode) { 00032 std::string Basename; 00033 switch(opcode) { 00034 default: llvm_unreachable("do not know intrinsic name"); 00035 // Arithmetic Right shift for integer types. 00036 case PIC16ISD::SRA_I8: Basename = "sra.i8"; break; 00037 case RTLIB::SRA_I16: Basename = "sra.i16"; break; 00038 case RTLIB::SRA_I32: Basename = "sra.i32"; break; 00039 00040 // Left shift for integer types. 00041 case PIC16ISD::SLL_I8: Basename = "sll.i8"; break; 00042 case RTLIB::SHL_I16: Basename = "sll.i16"; break; 00043 case RTLIB::SHL_I32: Basename = "sll.i32"; break; 00044 00045 // Logical Right Shift for integer types. 00046 case PIC16ISD::SRL_I8: Basename = "srl.i8"; break; 00047 case RTLIB::SRL_I16: Basename = "srl.i16"; break; 00048 case RTLIB::SRL_I32: Basename = "srl.i32"; break; 00049 00050 // Multiply for integer types. 00051 case PIC16ISD::MUL_I8: Basename = "mul.i8"; break; 00052 case RTLIB::MUL_I16: Basename = "mul.i16"; break; 00053 case RTLIB::MUL_I32: Basename = "mul.i32"; break; 00054 00055 // Signed division for integers. 00056 case RTLIB::SDIV_I16: Basename = "sdiv.i16"; break; 00057 case RTLIB::SDIV_I32: Basename = "sdiv.i32"; break; 00058 00059 // Unsigned division for integers. 00060 case RTLIB::UDIV_I16: Basename = "udiv.i16"; break; 00061 case RTLIB::UDIV_I32: Basename = "udiv.i32"; break; 00062 00063 // Signed Modulas for integers. 00064 case RTLIB::SREM_I16: Basename = "srem.i16"; break; 00065 case RTLIB::SREM_I32: Basename = "srem.i32"; break; 00066 00067 // Unsigned Modulas for integers. 00068 case RTLIB::UREM_I16: Basename = "urem.i16"; break; 00069 case RTLIB::UREM_I32: Basename = "urem.i32"; break; 00070 00071 ////////////////////// 00072 // LIBCALLS FOR FLOATS 00073 ////////////////////// 00074 00075 // Float to signed integrals 00076 case RTLIB::FPTOSINT_F32_I8: Basename = "f32_to_si32"; break; 00077 case RTLIB::FPTOSINT_F32_I16: Basename = "f32_to_si32"; break; 00078 case RTLIB::FPTOSINT_F32_I32: Basename = "f32_to_si32"; break; 00079 00080 // Signed integrals to float. char and int are first sign extended to i32 00081 // before being converted to float, so an I8_F32 or I16_F32 isn't required. 00082 case RTLIB::SINTTOFP_I32_F32: Basename = "si32_to_f32"; break; 00083 00084 // Float to Unsigned conversions. 00085 // Signed conversion can be used for unsigned conversion as well. 00086 // In signed and unsigned versions only the interpretation of the 00087 // MSB is different. Bit representation remains the same. 00088 case RTLIB::FPTOUINT_F32_I8: Basename = "f32_to_si32"; break; 00089 case RTLIB::FPTOUINT_F32_I16: Basename = "f32_to_si32"; break; 00090 case RTLIB::FPTOUINT_F32_I32: Basename = "f32_to_si32"; break; 00091 00092 // Unsigned to Float conversions. char and int are first zero extended 00093 // before being converted to float. 00094 case RTLIB::UINTTOFP_I32_F32: Basename = "ui32_to_f32"; break; 00095 00096 // Floating point add, sub, mul, div. 00097 case RTLIB::ADD_F32: Basename = "add.f32"; break; 00098 case RTLIB::SUB_F32: Basename = "sub.f32"; break; 00099 case RTLIB::MUL_F32: Basename = "mul.f32"; break; 00100 case RTLIB::DIV_F32: Basename = "div.f32"; break; 00101 00102 // Floating point comparison 00103 case RTLIB::O_F32: Basename = "unordered.f32"; break; 00104 case RTLIB::UO_F32: Basename = "unordered.f32"; break; 00105 case RTLIB::OLE_F32: Basename = "le.f32"; break; 00106 case RTLIB::OGE_F32: Basename = "ge.f32"; break; 00107 case RTLIB::OLT_F32: Basename = "lt.f32"; break; 00108 case RTLIB::OGT_F32: Basename = "gt.f32"; break; 00109 case RTLIB::OEQ_F32: Basename = "eq.f32"; break; 00110 case RTLIB::UNE_F32: Basename = "neq.f32"; break; 00111 } 00112 00113 std::string prefix = PAN::getTagName(PAN::PREFIX_SYMBOL); 00114 std::string tagname = PAN::getTagName(PAN::LIBCALL); 00115 std::string Fullname = prefix + tagname + Basename; 00116 00117 // The name has to live through program life. 00118 return createESName(Fullname); 00119 } 00120 00121 // getStdLibCallName - Get the name for the standard library function. 00122 static const char *getStdLibCallName(unsigned opcode) { 00123 std::string BaseName; 00124 switch(opcode) { 00125 case RTLIB::COS_F32: BaseName = "cos"; 00126 break; 00127 case RTLIB::SIN_F32: BaseName = "sin"; 00128 break; 00129 case RTLIB::MEMCPY: BaseName = "memcpy"; 00130 break; 00131 case RTLIB::MEMSET: BaseName = "memset"; 00132 break; 00133 case RTLIB::MEMMOVE: BaseName = "memmove"; 00134 break; 00135 default: llvm_unreachable("do not know std lib call name"); 00136 } 00137 std::string prefix = PAN::getTagName(PAN::PREFIX_SYMBOL); 00138 std::string LibCallName = prefix + BaseName; 00139 00140 // The name has to live through program life. 00141 return createESName(LibCallName); 00142 } 00143 00144 // PIC16TargetLowering Constructor. 00145 PIC16TargetLowering::PIC16TargetLowering(PIC16TargetMachine &TM) 00146 : TargetLowering(TM, new PIC16TargetObjectFile()), TmpSize(0) { 00147 00148 Subtarget = &TM.getSubtarget<PIC16Subtarget>(); 00149 00150 addRegisterClass(MVT::i8, PIC16::GPRRegisterClass); 00151 00152 setShiftAmountType(MVT::i8); 00153 00154 // Std lib call names 00155 setLibcallName(RTLIB::COS_F32, getStdLibCallName(RTLIB::COS_F32)); 00156 setLibcallName(RTLIB::SIN_F32, getStdLibCallName(RTLIB::SIN_F32)); 00157 setLibcallName(RTLIB::MEMCPY, getStdLibCallName(RTLIB::MEMCPY)); 00158 setLibcallName(RTLIB::MEMSET, getStdLibCallName(RTLIB::MEMSET)); 00159 setLibcallName(RTLIB::MEMMOVE, getStdLibCallName(RTLIB::MEMMOVE)); 00160 00161 // SRA library call names 00162 setPIC16LibcallName(PIC16ISD::SRA_I8, getIntrinsicName(PIC16ISD::SRA_I8)); 00163 setLibcallName(RTLIB::SRA_I16, getIntrinsicName(RTLIB::SRA_I16)); 00164 setLibcallName(RTLIB::SRA_I32, getIntrinsicName(RTLIB::SRA_I32)); 00165 00166 // SHL library call names 00167 setPIC16LibcallName(PIC16ISD::SLL_I8, getIntrinsicName(PIC16ISD::SLL_I8)); 00168 setLibcallName(RTLIB::SHL_I16, getIntrinsicName(RTLIB::SHL_I16)); 00169 setLibcallName(RTLIB::SHL_I32, getIntrinsicName(RTLIB::SHL_I32)); 00170 00171 // SRL library call names 00172 setPIC16LibcallName(PIC16ISD::SRL_I8, getIntrinsicName(PIC16ISD::SRL_I8)); 00173 setLibcallName(RTLIB::SRL_I16, getIntrinsicName(RTLIB::SRL_I16)); 00174 setLibcallName(RTLIB::SRL_I32, getIntrinsicName(RTLIB::SRL_I32)); 00175 00176 // MUL Library call names 00177 setPIC16LibcallName(PIC16ISD::MUL_I8, getIntrinsicName(PIC16ISD::MUL_I8)); 00178 setLibcallName(RTLIB::MUL_I16, getIntrinsicName(RTLIB::MUL_I16)); 00179 setLibcallName(RTLIB::MUL_I32, getIntrinsicName(RTLIB::MUL_I32)); 00180 00181 // Signed division lib call names 00182 setLibcallName(RTLIB::SDIV_I16, getIntrinsicName(RTLIB::SDIV_I16)); 00183 setLibcallName(RTLIB::SDIV_I32, getIntrinsicName(RTLIB::SDIV_I32)); 00184 00185 // Unsigned division lib call names 00186 setLibcallName(RTLIB::UDIV_I16, getIntrinsicName(RTLIB::UDIV_I16)); 00187 setLibcallName(RTLIB::UDIV_I32, getIntrinsicName(RTLIB::UDIV_I32)); 00188 00189 // Signed remainder lib call names 00190 setLibcallName(RTLIB::SREM_I16, getIntrinsicName(RTLIB::SREM_I16)); 00191 setLibcallName(RTLIB::SREM_I32, getIntrinsicName(RTLIB::SREM_I32)); 00192 00193 // Unsigned remainder lib call names 00194 setLibcallName(RTLIB::UREM_I16, getIntrinsicName(RTLIB::UREM_I16)); 00195 setLibcallName(RTLIB::UREM_I32, getIntrinsicName(RTLIB::UREM_I32)); 00196 00197 // Floating point to signed int conversions. 00198 setLibcallName(RTLIB::FPTOSINT_F32_I8, 00199 getIntrinsicName(RTLIB::FPTOSINT_F32_I8)); 00200 setLibcallName(RTLIB::FPTOSINT_F32_I16, 00201 getIntrinsicName(RTLIB::FPTOSINT_F32_I16)); 00202 setLibcallName(RTLIB::FPTOSINT_F32_I32, 00203 getIntrinsicName(RTLIB::FPTOSINT_F32_I32)); 00204 00205 // Signed int to floats. 00206 setLibcallName(RTLIB::SINTTOFP_I32_F32, 00207 getIntrinsicName(RTLIB::SINTTOFP_I32_F32)); 00208 00209 // Floating points to unsigned ints. 00210 setLibcallName(RTLIB::FPTOUINT_F32_I8, 00211 getIntrinsicName(RTLIB::FPTOUINT_F32_I8)); 00212 setLibcallName(RTLIB::FPTOUINT_F32_I16, 00213 getIntrinsicName(RTLIB::FPTOUINT_F32_I16)); 00214 setLibcallName(RTLIB::FPTOUINT_F32_I32, 00215 getIntrinsicName(RTLIB::FPTOUINT_F32_I32)); 00216 00217 // Unsigned int to floats. 00218 setLibcallName(RTLIB::UINTTOFP_I32_F32, 00219 getIntrinsicName(RTLIB::UINTTOFP_I32_F32)); 00220 00221 // Floating point add, sub, mul ,div. 00222 setLibcallName(RTLIB::ADD_F32, getIntrinsicName(RTLIB::ADD_F32)); 00223 setLibcallName(RTLIB::SUB_F32, getIntrinsicName(RTLIB::SUB_F32)); 00224 setLibcallName(RTLIB::MUL_F32, getIntrinsicName(RTLIB::MUL_F32)); 00225 setLibcallName(RTLIB::DIV_F32, getIntrinsicName(RTLIB::DIV_F32)); 00226 00227 // Floationg point comparison 00228 setLibcallName(RTLIB::UO_F32, getIntrinsicName(RTLIB::UO_F32)); 00229 setLibcallName(RTLIB::OLE_F32, getIntrinsicName(RTLIB::OLE_F32)); 00230 setLibcallName(RTLIB::OGE_F32, getIntrinsicName(RTLIB::OGE_F32)); 00231 setLibcallName(RTLIB::OLT_F32, getIntrinsicName(RTLIB::OLT_F32)); 00232 setLibcallName(RTLIB::OGT_F32, getIntrinsicName(RTLIB::OGT_F32)); 00233 setLibcallName(RTLIB::OEQ_F32, getIntrinsicName(RTLIB::OEQ_F32)); 00234 setLibcallName(RTLIB::UNE_F32, getIntrinsicName(RTLIB::UNE_F32)); 00235 00236 // Return value comparisons of floating point calls. 00237 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE); 00238 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE); 00239 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE); 00240 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE); 00241 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE); 00242 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE); 00243 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE); 00244 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ); 00245 00246 setOperationAction(ISD::GlobalAddress, MVT::i16, Custom); 00247 setOperationAction(ISD::ExternalSymbol, MVT::i16, Custom); 00248 00249 setOperationAction(ISD::LOAD, MVT::i8, Legal); 00250 setOperationAction(ISD::LOAD, MVT::i16, Custom); 00251 setOperationAction(ISD::LOAD, MVT::i32, Custom); 00252 00253 setOperationAction(ISD::STORE, MVT::i8, Legal); 00254 setOperationAction(ISD::STORE, MVT::i16, Custom); 00255 setOperationAction(ISD::STORE, MVT::i32, Custom); 00256 setOperationAction(ISD::STORE, MVT::i64, Custom); 00257 00258 setOperationAction(ISD::ADDE, MVT::i8, Custom); 00259 setOperationAction(ISD::ADDC, MVT::i8, Custom); 00260 setOperationAction(ISD::SUBE, MVT::i8, Custom); 00261 setOperationAction(ISD::SUBC, MVT::i8, Custom); 00262 setOperationAction(ISD::SUB, MVT::i8, Custom); 00263 setOperationAction(ISD::ADD, MVT::i8, Custom); 00264 setOperationAction(ISD::ADD, MVT::i16, Custom); 00265 00266 setOperationAction(ISD::OR, MVT::i8, Custom); 00267 setOperationAction(ISD::AND, MVT::i8, Custom); 00268 setOperationAction(ISD::XOR, MVT::i8, Custom); 00269 00270 setOperationAction(ISD::FrameIndex, MVT::i16, Custom); 00271 00272 setOperationAction(ISD::MUL, MVT::i8, Custom); 00273 00274 setOperationAction(ISD::SMUL_LOHI, MVT::i8, Expand); 00275 setOperationAction(ISD::UMUL_LOHI, MVT::i8, Expand); 00276 setOperationAction(ISD::MULHU, MVT::i8, Expand); 00277 setOperationAction(ISD::MULHS, MVT::i8, Expand); 00278 00279 setOperationAction(ISD::SRA, MVT::i8, Custom); 00280 setOperationAction(ISD::SHL, MVT::i8, Custom); 00281 setOperationAction(ISD::SRL, MVT::i8, Custom); 00282 00283 setOperationAction(ISD::ROTL, MVT::i8, Expand); 00284 setOperationAction(ISD::ROTR, MVT::i8, Expand); 00285 00286 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); 00287 00288 // PIC16 does not support shift parts 00289 setOperationAction(ISD::SRA_PARTS, MVT::i8, Expand); 00290 setOperationAction(ISD::SHL_PARTS, MVT::i8, Expand); 00291 setOperationAction(ISD::SRL_PARTS, MVT::i8, Expand); 00292 00293 00294 // PIC16 does not have a SETCC, expand it to SELECT_CC. 00295 setOperationAction(ISD::SETCC, MVT::i8, Expand); 00296 setOperationAction(ISD::SELECT, MVT::i8, Expand); 00297 setOperationAction(ISD::BRCOND, MVT::Other, Expand); 00298 setOperationAction(ISD::BRIND, MVT::Other, Expand); 00299 00300 setOperationAction(ISD::SELECT_CC, MVT::i8, Custom); 00301 setOperationAction(ISD::BR_CC, MVT::i8, Custom); 00302 00303 //setOperationAction(ISD::TRUNCATE, MVT::i16, Custom); 00304 setTruncStoreAction(MVT::i16, MVT::i8, Custom); 00305 00306 // Now deduce the information based on the above mentioned 00307 // actions 00308 computeRegisterProperties(); 00309 } 00310 00311 // getOutFlag - Extract the flag result if the Op has it. 00312 static SDValue getOutFlag(SDValue &Op) { 00313 // Flag is the last value of the node. 00314 SDValue Flag = Op.getValue(Op.getNode()->getNumValues() - 1); 00315 00316 assert (Flag.getValueType() == MVT::Flag 00317 && "Node does not have an out Flag"); 00318 00319 return Flag; 00320 } 00321 // Get the TmpOffset for FrameIndex 00322 unsigned PIC16TargetLowering::GetTmpOffsetForFI(unsigned FI, unsigned size) { 00323 std::map<unsigned, unsigned>::iterator 00324 MapIt = FiTmpOffsetMap.find(FI); 00325 if (MapIt != FiTmpOffsetMap.end()) 00326 return MapIt->second; 00327 00328 // This FI (FrameIndex) is not yet mapped, so map it 00329 FiTmpOffsetMap[FI] = TmpSize; 00330 TmpSize += size; 00331 return FiTmpOffsetMap[FI]; 00332 } 00333 00334 // To extract chain value from the SDValue Nodes 00335 // This function will help to maintain the chain extracting 00336 // code at one place. In case of any change in future it will 00337 // help maintain the code. 00338 static SDValue getChain(SDValue &Op) { 00339 SDValue Chain = Op.getValue(Op.getNode()->getNumValues() - 1); 00340 00341 // If the last value returned in Flag then the chain is 00342 // second last value returned. 00343 if (Chain.getValueType() == MVT::Flag) 00344 Chain = Op.getValue(Op.getNode()->getNumValues() - 2); 00345 00346 // All nodes may not produce a chain. Therefore following assert 00347 // verifies that the node is returning a chain only. 00348 assert (Chain.getValueType() == MVT::Other 00349 && "Node does not have a chain"); 00350 00351 return Chain; 00352 } 00353 00354 /// PopulateResults - Helper function to LowerOperation. 00355 /// If a node wants to return multiple results after lowering, 00356 /// it stuffs them into an array of SDValue called Results. 00357 00358 static void PopulateResults(SDValue N, SmallVectorImpl<SDValue>&Results) { 00359 if (N.getOpcode() == ISD::MERGE_VALUES) { 00360 int NumResults = N.getNumOperands(); 00361 for( int i = 0; i < NumResults; i++) 00362 Results.push_back(N.getOperand(i)); 00363 } 00364 else 00365 Results.push_back(N); 00366 } 00367 00368 MVT::SimpleValueType 00369 PIC16TargetLowering::getSetCCResultType(EVT ValType) const { 00370 return MVT::i8; 00371 } 00372 00373 /// The type legalizer framework of generating legalizer can generate libcalls 00374 /// only when the operand/result types are illegal. 00375 /// PIC16 needs to generate libcalls even for the legal types (i8) for some ops. 00376 /// For example an arithmetic right shift. These functions are used to lower 00377 /// such operations that generate libcall for legal types. 00378 00379 void 00380 PIC16TargetLowering::setPIC16LibcallName(PIC16ISD::PIC16Libcall Call, 00381 const char *Name) { 00382 PIC16LibcallNames[Call] = Name; 00383 } 00384 00385 const char * 00386 PIC16TargetLowering::getPIC16LibcallName(PIC16ISD::PIC16Libcall Call) { 00387 return PIC16LibcallNames[Call]; 00388 } 00389 00390 SDValue 00391 PIC16TargetLowering::MakePIC16Libcall(PIC16ISD::PIC16Libcall Call, 00392 EVT RetVT, const SDValue *Ops, 00393 unsigned NumOps, bool isSigned, 00394 SelectionDAG &DAG, DebugLoc dl) { 00395 00396 TargetLowering::ArgListTy Args; 00397 Args.reserve(NumOps); 00398 00399 TargetLowering::ArgListEntry Entry; 00400 for (unsigned i = 0; i != NumOps; ++i) { 00401 Entry.Node = Ops[i]; 00402 Entry.Ty = Entry.Node.getValueType().getTypeForEVT(*DAG.getContext()); 00403 Entry.isSExt = isSigned; 00404 Entry.isZExt = !isSigned; 00405 Args.push_back(Entry); 00406 } 00407 00408 SDValue Callee = DAG.getExternalSymbol(getPIC16LibcallName(Call), MVT::i16); 00409 00410 const Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext()); 00411 std::pair<SDValue,SDValue> CallInfo = 00412 LowerCallTo(DAG.getEntryNode(), RetTy, isSigned, !isSigned, false, 00413 false, 0, CallingConv::C, false, 00414 /*isReturnValueUsed=*/true, 00415 Callee, Args, DAG, dl); 00416 00417 return CallInfo.first; 00418 } 00419 00420 const char *PIC16TargetLowering::getTargetNodeName(unsigned Opcode) const { 00421 switch (Opcode) { 00422 default: return NULL; 00423 case PIC16ISD::Lo: return "PIC16ISD::Lo"; 00424 case PIC16ISD::Hi: return "PIC16ISD::Hi"; 00425 case PIC16ISD::MTLO: return "PIC16ISD::MTLO"; 00426 case PIC16ISD::MTHI: return "PIC16ISD::MTHI"; 00427 case PIC16ISD::MTPCLATH: return "PIC16ISD::MTPCLATH"; 00428 case PIC16ISD::PIC16Connect: return "PIC16ISD::PIC16Connect"; 00429 case PIC16ISD::Banksel: return "PIC16ISD::Banksel"; 00430 case PIC16ISD::PIC16Load: return "PIC16ISD::PIC16Load"; 00431 case PIC16ISD::PIC16LdArg: return "PIC16ISD::PIC16LdArg"; 00432 case PIC16ISD::PIC16LdWF: return "PIC16ISD::PIC16LdWF"; 00433 case PIC16ISD::PIC16Store: return "PIC16ISD::PIC16Store"; 00434 case PIC16ISD::PIC16StWF: return "PIC16ISD::PIC16StWF"; 00435 case PIC16ISD::BCF: return "PIC16ISD::BCF"; 00436 case PIC16ISD::LSLF: return "PIC16ISD::LSLF"; 00437 case PIC16ISD::LRLF: return "PIC16ISD::LRLF"; 00438 case PIC16ISD::RLF: return "PIC16ISD::RLF"; 00439 case PIC16ISD::RRF: return "PIC16ISD::RRF"; 00440 case PIC16ISD::CALL: return "PIC16ISD::CALL"; 00441 case PIC16ISD::CALLW: return "PIC16ISD::CALLW"; 00442 case PIC16ISD::SUBCC: return "PIC16ISD::SUBCC"; 00443 case PIC16ISD::SELECT_ICC: return "PIC16ISD::SELECT_ICC"; 00444 case PIC16ISD::BRCOND: return "PIC16ISD::BRCOND"; 00445 case PIC16ISD::RET: return "PIC16ISD::RET"; 00446 case PIC16ISD::Dummy: return "PIC16ISD::Dummy"; 00447 } 00448 } 00449 00450 void PIC16TargetLowering::ReplaceNodeResults(SDNode *N, 00451 SmallVectorImpl<SDValue>&Results, 00452 SelectionDAG &DAG) { 00453 00454 switch (N->getOpcode()) { 00455 case ISD::GlobalAddress: 00456 Results.push_back(ExpandGlobalAddress(N, DAG)); 00457 return; 00458 case ISD::ExternalSymbol: 00459 Results.push_back(ExpandExternalSymbol(N, DAG)); 00460 return; 00461 case ISD::STORE: 00462 Results.push_back(ExpandStore(N, DAG)); 00463 return; 00464 case ISD::LOAD: 00465 PopulateResults(ExpandLoad(N, DAG), Results); 00466 return; 00467 case ISD::ADD: 00468 // Results.push_back(ExpandAdd(N, DAG)); 00469 return; 00470 case ISD::FrameIndex: 00471 Results.push_back(ExpandFrameIndex(N, DAG)); 00472 return; 00473 default: 00474 assert (0 && "not implemented"); 00475 return; 00476 } 00477 } 00478 00479 SDValue PIC16TargetLowering::ExpandFrameIndex(SDNode *N, SelectionDAG &DAG) { 00480 00481 // Currently handling FrameIndex of size MVT::i16 only 00482 // One example of this scenario is when return value is written on 00483 // FrameIndex#0 00484 00485 if (N->getValueType(0) != MVT::i16) 00486 return SDValue(); 00487 00488 // Expand the FrameIndex into ExternalSymbol and a Constant node 00489 // The constant will represent the frame index number 00490 // Get the current function frame 00491 MachineFunction &MF = DAG.getMachineFunction(); 00492 const Function *Func = MF.getFunction(); 00493 const std::string Name = Func->getName(); 00494 00495 FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(SDValue(N,0)); 00496 // FIXME there isn't really debug info here 00497 DebugLoc dl = FR->getDebugLoc(); 00498 00499 // Expand FrameIndex like GlobalAddress and ExternalSymbol 00500 // Also use Offset field for lo and hi parts. The default 00501 // offset is zero. 00502 00503 SDValue ES; 00504 int FrameOffset; 00505 SDValue FI = SDValue(N,0); 00506 LegalizeFrameIndex(FI, DAG, ES, FrameOffset); 00507 SDValue Offset = DAG.getConstant(FrameOffset, MVT::i8); 00508 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, ES, Offset); 00509 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, ES, Offset); 00510 return DAG.getNode(ISD::BUILD_PAIR, dl, N->getValueType(0), Lo, Hi); 00511 } 00512 00513 00514 SDValue PIC16TargetLowering::ExpandStore(SDNode *N, SelectionDAG &DAG) { 00515 StoreSDNode *St = cast<StoreSDNode>(N); 00516 SDValue Chain = St->getChain(); 00517 SDValue Src = St->getValue(); 00518 SDValue Ptr = St->getBasePtr(); 00519 EVT ValueType = Src.getValueType(); 00520 unsigned StoreOffset = 0; 00521 DebugLoc dl = N->getDebugLoc(); 00522 00523 SDValue PtrLo, PtrHi; 00524 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, StoreOffset, dl); 00525 00526 if (ValueType == MVT::i8) { 00527 return DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other, Chain, Src, 00528 PtrLo, PtrHi, 00529 DAG.getConstant (0 + StoreOffset, MVT::i8)); 00530 } 00531 else if (ValueType == MVT::i16) { 00532 // Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR. 00533 SDValue SrcLo, SrcHi; 00534 GetExpandedParts(Src, DAG, SrcLo, SrcHi); 00535 SDValue ChainLo = Chain, ChainHi = Chain; 00536 if (Chain.getOpcode() == ISD::TokenFactor) { 00537 ChainLo = Chain.getOperand(0); 00538 ChainHi = Chain.getOperand(1); 00539 } 00540 SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, 00541 ChainLo, 00542 SrcLo, PtrLo, PtrHi, 00543 DAG.getConstant (0 + StoreOffset, MVT::i8)); 00544 00545 SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi, 00546 SrcHi, PtrLo, PtrHi, 00547 DAG.getConstant (1 + StoreOffset, MVT::i8)); 00548 00549 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, getChain(Store1), 00550 getChain(Store2)); 00551 } 00552 else if (ValueType == MVT::i32) { 00553 // Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR. 00554 SDValue SrcLo, SrcHi; 00555 GetExpandedParts(Src, DAG, SrcLo, SrcHi); 00556 00557 // Get the expanded parts of each of SrcLo and SrcHi. 00558 SDValue SrcLo1, SrcLo2, SrcHi1, SrcHi2; 00559 GetExpandedParts(SrcLo, DAG, SrcLo1, SrcLo2); 00560 GetExpandedParts(SrcHi, DAG, SrcHi1, SrcHi2); 00561 00562 SDValue ChainLo = Chain, ChainHi = Chain; 00563 if (Chain.getOpcode() == ISD::TokenFactor) { 00564 ChainLo = Chain.getOperand(0); 00565 ChainHi = Chain.getOperand(1); 00566 } 00567 SDValue ChainLo1 = ChainLo, ChainLo2 = ChainLo, ChainHi1 = ChainHi, 00568 ChainHi2 = ChainHi; 00569 if (ChainLo.getOpcode() == ISD::TokenFactor) { 00570 ChainLo1 = ChainLo.getOperand(0); 00571 ChainLo2 = ChainLo.getOperand(1); 00572 } 00573 if (ChainHi.getOpcode() == ISD::TokenFactor) { 00574 ChainHi1 = ChainHi.getOperand(0); 00575 ChainHi2 = ChainHi.getOperand(1); 00576 } 00577 SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, 00578 ChainLo1, 00579 SrcLo1, PtrLo, PtrHi, 00580 DAG.getConstant (0 + StoreOffset, MVT::i8)); 00581 00582 SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainLo2, 00583 SrcLo2, PtrLo, PtrHi, 00584 DAG.getConstant (1 + StoreOffset, MVT::i8)); 00585 00586 SDValue Store3 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi1, 00587 SrcHi1, PtrLo, PtrHi, 00588 DAG.getConstant (2 + StoreOffset, MVT::i8)); 00589 00590 SDValue Store4 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi2, 00591 SrcHi2, PtrLo, PtrHi, 00592 DAG.getConstant (3 + StoreOffset, MVT::i8)); 00593 00594 SDValue RetLo = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 00595 getChain(Store1), getChain(Store2)); 00596 SDValue RetHi = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 00597 getChain(Store3), getChain(Store4)); 00598 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, RetLo, RetHi); 00599 00600 } else if (ValueType == MVT::i64) { 00601 SDValue SrcLo, SrcHi; 00602 GetExpandedParts(Src, DAG, SrcLo, SrcHi); 00603 SDValue ChainLo = Chain, ChainHi = Chain; 00604 if (Chain.getOpcode() == ISD::TokenFactor) { 00605 ChainLo = Chain.getOperand(0); 00606 ChainHi = Chain.getOperand(1); 00607 } 00608 SDValue Store1 = DAG.getStore(ChainLo, dl, SrcLo, Ptr, NULL, 00609 0 + StoreOffset); 00610 00611 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, 00612 DAG.getConstant(4, Ptr.getValueType())); 00613 SDValue Store2 = DAG.getStore(ChainHi, dl, SrcHi, Ptr, NULL, 00614 1 + StoreOffset); 00615 00616 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, 00617 Store2); 00618 } else { 00619 assert (0 && "value type not supported"); 00620 return SDValue(); 00621 } 00622 } 00623 00624 SDValue PIC16TargetLowering::ExpandExternalSymbol(SDNode *N, SelectionDAG &DAG) 00625 { 00626 ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(SDValue(N, 0)); 00627 // FIXME there isn't really debug info here 00628 DebugLoc dl = ES->getDebugLoc(); 00629 00630 SDValue TES = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8); 00631 SDValue Offset = DAG.getConstant(0, MVT::i8); 00632 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, TES, Offset); 00633 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, TES, Offset); 00634 00635 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, Lo, Hi); 00636 } 00637 00638 // ExpandGlobalAddress - 00639 SDValue PIC16TargetLowering::ExpandGlobalAddress(SDNode *N, SelectionDAG &DAG) { 00640 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(SDValue(N, 0)); 00641 // FIXME there isn't really debug info here 00642 DebugLoc dl = G->getDebugLoc(); 00643 00644 SDValue TGA = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i8, 00645 G->getOffset()); 00646 00647 SDValue Offset = DAG.getConstant(0, MVT::i8); 00648 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, TGA, Offset); 00649 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, TGA, Offset); 00650 00651 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, Lo, Hi); 00652 } 00653 00654 bool PIC16TargetLowering::isDirectAddress(const SDValue &Op) { 00655 assert (Op.getNode() != NULL && "Can't operate on NULL SDNode!!"); 00656 00657 if (Op.getOpcode() == ISD::BUILD_PAIR) { 00658 if (Op.getOperand(0).getOpcode() == PIC16ISD::Lo) 00659 return true; 00660 } 00661 return false; 00662 } 00663 00664 // Return true if DirectAddress is in ROM_SPACE 00665 bool PIC16TargetLowering::isRomAddress(const SDValue &Op) { 00666 00667 // RomAddress is a GlobalAddress in ROM_SPACE_ 00668 // If the Op is not a GlobalAddress return NULL without checking 00669 // anything further. 00670 if (!isDirectAddress(Op)) 00671 return false; 00672 00673 // Its a GlobalAddress. 00674 // It is BUILD_PAIR((PIC16Lo TGA), (PIC16Hi TGA)) and Op is BUILD_PAIR 00675 SDValue TGA = Op.getOperand(0).getOperand(0); 00676 GlobalAddressSDNode *GSDN = dyn_cast<GlobalAddressSDNode>(TGA); 00677 00678 if (GSDN->getAddressSpace() == PIC16ISD::ROM_SPACE) 00679 return true; 00680 00681 // Any other address space return it false 00682 return false; 00683 } 00684 00685 00686 // GetExpandedParts - This function is on the similiar lines as 00687 // the GetExpandedInteger in type legalizer is. This returns expanded 00688 // parts of Op in Lo and Hi. 00689 00690 void PIC16TargetLowering::GetExpandedParts(SDValue Op, SelectionDAG &DAG, 00691 SDValue &Lo, SDValue &Hi) { 00692 SDNode *N = Op.getNode(); 00693 DebugLoc dl = N->getDebugLoc(); 00694 EVT NewVT = getTypeToTransformTo(*DAG.getContext(), N->getValueType(0)); 00695 00696 // Extract the lo component. 00697 Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NewVT, Op, 00698 DAG.getConstant(0, MVT::i8)); 00699 00700 // extract the hi component 00701 Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NewVT, Op, 00702 DAG.getConstant(1, MVT::i8)); 00703 } 00704 00705 // Legalize FrameIndex into ExternalSymbol and offset. 00706 void 00707 PIC16TargetLowering::LegalizeFrameIndex(SDValue Op, SelectionDAG &DAG, 00708 SDValue &ES, int &Offset) { 00709 00710 MachineFunction &MF = DAG.getMachineFunction(); 00711 const Function *Func = MF.getFunction(); 00712 MachineFrameInfo *MFI = MF.getFrameInfo(); 00713 const std::string Name = Func->getName(); 00714 00715 FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(Op); 00716 00717 // FrameIndices are not stack offsets. But they represent the request 00718 // for space on stack. That space requested may be more than one byte. 00719 // Therefore, to calculate the stack offset that a FrameIndex aligns 00720 // with, we need to traverse all the FrameIndices available earlier in 00721 // the list and add their requested size. 00722 unsigned FIndex = FR->getIndex(); 00723 const char *tmpName; 00724 if (FIndex < ReservedFrameCount) { 00725 tmpName = createESName(PAN::getFrameLabel(Name)); 00726 ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8); 00727 Offset = 0; 00728 for (unsigned i=0; i<FIndex ; ++i) { 00729 Offset += MFI->getObjectSize(i); 00730 } 00731 } else { 00732 // FrameIndex has been made for some temporary storage 00733 tmpName = createESName(PAN::getTempdataLabel(Name)); 00734 ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8); 00735 Offset = GetTmpOffsetForFI(FIndex, MFI->getObjectSize(FIndex)); 00736 } 00737 00738 return; 00739 } 00740 00741 // This function legalizes the PIC16 Addresses. If the Pointer is 00742 // -- Direct address variable residing 00743 // --> then a Banksel for that variable will be created. 00744 // -- Rom variable 00745 // --> then it will be treated as an indirect address. 00746 // -- Indirect address 00747 // --> then the address will be loaded into FSR 00748 // -- ADD with constant operand 00749 // --> then constant operand of ADD will be returned as Offset 00750 // and non-constant operand of ADD will be treated as pointer. 00751 // Returns the high and lo part of the address, and the offset(in case of ADD). 00752 00753 void PIC16TargetLowering::LegalizeAddress(SDValue Ptr, SelectionDAG &DAG, 00754 SDValue &Lo, SDValue &Hi, 00755 unsigned &Offset, DebugLoc dl) { 00756 00757 // Offset, by default, should be 0 00758 Offset = 0; 00759 00760 // If the pointer is ADD with constant, 00761 // return the constant value as the offset 00762 if (Ptr.getOpcode() == ISD::ADD) { 00763 SDValue OperLeft = Ptr.getOperand(0); 00764 SDValue OperRight = Ptr.getOperand(1); 00765 if ((OperLeft.getOpcode() == ISD::Constant) && 00766 (dyn_cast<ConstantSDNode>(OperLeft)->getZExtValue() < 32 )) { 00767 Offset = dyn_cast<ConstantSDNode>(OperLeft)->getZExtValue(); 00768 Ptr = OperRight; 00769 } else if ((OperRight.getOpcode() == ISD::Constant) && 00770 (dyn_cast<ConstantSDNode>(OperRight)->getZExtValue() < 32 )){ 00771 Offset = dyn_cast<ConstantSDNode>(OperRight)->getZExtValue(); 00772 Ptr = OperLeft; 00773 } 00774 } 00775 00776 // If the pointer is Type i8 and an external symbol 00777 // then treat it as direct address. 00778 // One example for such case is storing and loading 00779 // from function frame during a call 00780 if (Ptr.getValueType() == MVT::i8) { 00781 switch (Ptr.getOpcode()) { 00782 case ISD::TargetExternalSymbol: 00783 Lo = Ptr; 00784 Hi = DAG.getConstant(1, MVT::i8); 00785 return; 00786 } 00787 } 00788 00789 // Expansion of FrameIndex has Lo/Hi parts 00790 if (isDirectAddress(Ptr)) { 00791 SDValue TFI = Ptr.getOperand(0).getOperand(0); 00792 int FrameOffset; 00793 if (TFI.getOpcode() == ISD::TargetFrameIndex) { 00794 LegalizeFrameIndex(TFI, DAG, Lo, FrameOffset); 00795 Hi = DAG.getConstant(1, MVT::i8); 00796 Offset += FrameOffset; 00797 return; 00798 } else if (TFI.getOpcode() == ISD::TargetExternalSymbol) { 00799 // FrameIndex has already been expanded. 00800 // Now just make use of its expansion 00801 Lo = TFI; 00802 Hi = DAG.getConstant(1, MVT::i8); 00803 SDValue FOffset = Ptr.getOperand(0).getOperand(1); 00804 assert (FOffset.getOpcode() == ISD::Constant && 00805 "Invalid operand of PIC16ISD::Lo"); 00806 Offset += dyn_cast<ConstantSDNode>(FOffset)->getZExtValue(); 00807 return; 00808 } 00809 } 00810 00811 if (isDirectAddress(Ptr) && !isRomAddress(Ptr)) { 00812 // Direct addressing case for RAM variables. The Hi part is constant 00813 // and the Lo part is the TGA itself. 00814 Lo = Ptr.getOperand(0).getOperand(0); 00815 00816 // For direct addresses Hi is a constant. Value 1 for the constant 00817 // signifies that banksel needs to generated for it. Value 0 for 00818 // the constant signifies that banksel does not need to be generated 00819 // for it. Mark it as 1 now and optimize later. 00820 Hi = DAG.getConstant(1, MVT::i8); 00821 return; 00822 } 00823 00824 // Indirect addresses. Get the hi and lo parts of ptr. 00825 GetExpandedParts(Ptr, DAG, Lo, Hi); 00826 00827 // Put the hi and lo parts into FSR. 00828 Lo = DAG.getNode(PIC16ISD::MTLO, dl, MVT::i8, Lo); 00829 Hi = DAG.getNode(PIC16ISD::MTHI, dl, MVT::i8, Hi); 00830 00831 return; 00832 } 00833 00834 SDValue PIC16TargetLowering::ExpandLoad(SDNode *N, SelectionDAG &DAG) { 00835 LoadSDNode *LD = dyn_cast<LoadSDNode>(SDValue(N, 0)); 00836 SDValue Chain = LD->getChain(); 00837 SDValue Ptr = LD->getBasePtr(); 00838 DebugLoc dl = LD->getDebugLoc(); 00839 00840 SDValue Load, Offset; 00841 SDVTList Tys; 00842 EVT VT, NewVT; 00843 SDValue PtrLo, PtrHi; 00844 unsigned LoadOffset; 00845 00846 // Legalize direct/indirect addresses. This will give the lo and hi parts 00847 // of the address and the offset. 00848 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, LoadOffset, dl); 00849 00850 // Load from the pointer (direct address or FSR) 00851 VT = N->getValueType(0); 00852 unsigned NumLoads = VT.getSizeInBits() / 8; 00853 std::vector<SDValue> PICLoads; 00854 unsigned iter; 00855 EVT MemVT = LD->getMemoryVT(); 00856 if(ISD::isNON_EXTLoad(N)) { 00857 for (iter=0; iter<NumLoads ; ++iter) { 00858 // Add the pointer offset if any 00859 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8); 00860 Tys = DAG.getVTList(MVT::i8, MVT::Other); 00861 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi, 00862 Offset); 00863 PICLoads.push_back(Load); 00864 } 00865 } else { 00866 // If it is extended load then use PIC16Load for Memory Bytes 00867 // and for all extended bytes perform action based on type of 00868 // extention - i.e. SignExtendedLoad or ZeroExtendedLoad 00869 00870 00871 // For extended loads this is the memory value type 00872 // i.e. without any extension 00873 EVT MemVT = LD->getMemoryVT(); 00874 unsigned MemBytes = MemVT.getSizeInBits() / 8; 00875 // if MVT::i1 is extended to MVT::i8 then MemBytes will be zero 00876 // So set it to one 00877 if (MemBytes == 0) MemBytes = 1; 00878 00879 unsigned ExtdBytes = VT.getSizeInBits() / 8; 00880 Offset = DAG.getConstant(LoadOffset, MVT::i8); 00881 00882 Tys = DAG.getVTList(MVT::i8, MVT::Other); 00883 // For MemBytes generate PIC16Load with proper offset 00884 for (iter=0; iter < MemBytes; ++iter) { 00885 // Add the pointer offset if any 00886 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8); 00887 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi, 00888 Offset); 00889 PICLoads.push_back(Load); 00890 } 00891 00892 // For SignExtendedLoad 00893 if (ISD::isSEXTLoad(N)) { 00894 // For all ExtdBytes use the Right Shifted(Arithmetic) Value of the 00895 // highest MemByte 00896 SDValue SRA = DAG.getNode(ISD::SRA, dl, MVT::i8, Load, 00897 DAG.getConstant(7, MVT::i8)); 00898 for (iter=MemBytes; iter<ExtdBytes; ++iter) { 00899 PICLoads.push_back(SRA); 00900 } 00901 } else if (ISD::isZEXTLoad(N) || ISD::isEXTLoad(N)) { 00902 //} else if (ISD::isZEXTLoad(N)) { 00903 // ZeroExtendedLoad -- For all ExtdBytes use constant 0 00904 SDValue ConstZero = DAG.getConstant(0, MVT::i8); 00905 for (iter=MemBytes; iter<ExtdBytes; ++iter) { 00906 PICLoads.push_back(ConstZero); 00907 } 00908 } 00909 } 00910 SDValue BP; 00911 00912 if (VT == MVT::i8) { 00913 // Operand of Load is illegal -- Load itself is legal 00914 return PICLoads[0]; 00915 } 00916 else if (VT == MVT::i16) { 00917 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, PICLoads[0], PICLoads[1]); 00918 if (MemVT == MVT::i8) 00919 Chain = getChain(PICLoads[0]); 00920 else 00921 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 00922 getChain(PICLoads[0]), getChain(PICLoads[1])); 00923 } else if (VT == MVT::i32) { 00924 SDValue BPs[2]; 00925 BPs[0] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, 00926 PICLoads[0], PICLoads[1]); 00927 BPs[1] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, 00928 PICLoads[2], PICLoads[3]); 00929 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, BPs[0], BPs[1]); 00930 if (MemVT == MVT::i8) 00931 Chain = getChain(PICLoads[0]); 00932 else if (MemVT == MVT::i16) 00933 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 00934 getChain(PICLoads[0]), getChain(PICLoads[1])); 00935 else { 00936 SDValue Chains[2]; 00937 Chains[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 00938 getChain(PICLoads[0]), getChain(PICLoads[1])); 00939 Chains[1] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 00940 getChain(PICLoads[2]), getChain(PICLoads[3])); 00941 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 00942 Chains[0], Chains[1]); 00943 } 00944 } 00945 Tys = DAG.getVTList(VT, MVT::Other); 00946 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys, BP, Chain); 00947 } 00948 00949 SDValue PIC16TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) { 00950 // We should have handled larger operands in type legalizer itself. 00951 assert (Op.getValueType() == MVT::i8 && "illegal shift to lower"); 00952 00953 SDNode *N = Op.getNode(); 00954 SDValue Value = N->getOperand(0); 00955 SDValue Amt = N->getOperand(1); 00956 PIC16ISD::PIC16Libcall CallCode; 00957 switch (N->getOpcode()) { 00958 case ISD::SRA: 00959 CallCode = PIC16ISD::SRA_I8; 00960 break; 00961 case ISD::SHL: 00962 CallCode = PIC16ISD::SLL_I8; 00963 break; 00964 case ISD::SRL: 00965 CallCode = PIC16ISD::SRL_I8; 00966 break; 00967 default: 00968 assert ( 0 && "This shift is not implemented yet."); 00969 return SDValue(); 00970 } 00971 SmallVector<SDValue, 2> Ops(2); 00972 Ops[0] = Value; 00973 Ops[1] = Amt; 00974 SDValue Call = MakePIC16Libcall(CallCode, N->getValueType(0), &Ops[0], 2, 00975 true, DAG, N->getDebugLoc()); 00976 return Call; 00977 } 00978 00979 SDValue PIC16TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) { 00980 // We should have handled larger operands in type legalizer itself. 00981 assert (Op.getValueType() == MVT::i8 && "illegal multiply to lower"); 00982 00983 SDNode *N = Op.getNode(); 00984 SmallVector<SDValue, 2> Ops(2); 00985 Ops[0] = N->getOperand(0); 00986 Ops[1] = N->getOperand(1); 00987 SDValue Call = MakePIC16Libcall(PIC16ISD::MUL_I8, N->getValueType(0), 00988 &Ops[0], 2, true, DAG, N->getDebugLoc()); 00989 return Call; 00990 } 00991 00992 void 00993 PIC16TargetLowering::LowerOperationWrapper(SDNode *N, 00994 SmallVectorImpl<SDValue>&Results, 00995 SelectionDAG &DAG) { 00996 SDValue Op = SDValue(N, 0); 00997 SDValue Res; 00998 unsigned i; 00999 switch (Op.getOpcode()) { 01000 case ISD::LOAD: 01001 Res = ExpandLoad(Op.getNode(), DAG); break; 01002 default: { 01003 // All other operations are handled in LowerOperation. 01004 Res = LowerOperation(Op, DAG); 01005 if (Res.getNode()) 01006 Results.push_back(Res); 01007 01008 return; 01009 } 01010 } 01011 01012 N = Res.getNode(); 01013 unsigned NumValues = N->getNumValues(); 01014 for (i = 0; i < NumValues ; i++) { 01015 Results.push_back(SDValue(N, i)); 01016 } 01017 } 01018 01019 SDValue PIC16TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) { 01020 switch (Op.getOpcode()) { 01021 case ISD::ADD: 01022 case ISD::ADDC: 01023 case ISD::ADDE: 01024 return LowerADD(Op, DAG); 01025 case ISD::SUB: 01026 case ISD::SUBC: 01027 case ISD::SUBE: 01028 return LowerSUB(Op, DAG); 01029 case ISD::LOAD: 01030 return ExpandLoad(Op.getNode(), DAG); 01031 case ISD::STORE: 01032 return ExpandStore(Op.getNode(), DAG); 01033 case ISD::MUL: 01034 return LowerMUL(Op, DAG); 01035 case ISD::SHL: 01036 case ISD::SRA: 01037 case ISD::SRL: 01038 return LowerShift(Op, DAG); 01039 case ISD::OR: 01040 case ISD::AND: 01041 case ISD::XOR: 01042 return LowerBinOp(Op, DAG); 01043 case ISD::BR_CC: 01044 return LowerBR_CC(Op, DAG); 01045 case ISD::SELECT_CC: 01046 return LowerSELECT_CC(Op, DAG); 01047 } 01048 return SDValue(); 01049 } 01050 01051 SDValue PIC16TargetLowering::ConvertToMemOperand(SDValue Op, 01052 SelectionDAG &DAG, 01053 DebugLoc dl) { 01054 assert (Op.getValueType() == MVT::i8 01055 && "illegal value type to store on stack."); 01056 01057 MachineFunction &MF = DAG.getMachineFunction(); 01058 const Function *Func = MF.getFunction(); 01059 const std::string FuncName = Func->getName(); 01060 01061 01062 // Put the value on stack. 01063 // Get a stack slot index and convert to es. 01064 int FI = MF.getFrameInfo()->CreateStackObject(1, 1); 01065 const char *tmpName = createESName(PAN::getTempdataLabel(FuncName)); 01066 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8); 01067 01068 // Store the value to ES. 01069 SDValue Store = DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other, 01070 DAG.getEntryNode(), 01071 Op, ES, 01072 DAG.getConstant (1, MVT::i8), // Banksel. 01073 DAG.getConstant (GetTmpOffsetForFI(FI, 1), 01074 MVT::i8)); 01075 01076 // Load the value from ES. 01077 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other); 01078 SDValue Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Store, 01079 ES, DAG.getConstant (1, MVT::i8), 01080 DAG.getConstant (GetTmpOffsetForFI(FI, 1), 01081 MVT::i8)); 01082 01083 return Load.getValue(0); 01084 } 01085 01086 SDValue PIC16TargetLowering:: 01087 LowerIndirectCallArguments(SDValue Chain, SDValue InFlag, 01088 SDValue DataAddr_Lo, SDValue DataAddr_Hi, 01089 const SmallVectorImpl<ISD::OutputArg> &Outs, 01090 const SmallVectorImpl<ISD::InputArg> &Ins, 01091 DebugLoc dl, SelectionDAG &DAG) { 01092 unsigned NumOps = Outs.size(); 01093 01094 // If call has no arguments then do nothing and return. 01095 if (NumOps == 0) 01096 return Chain; 01097 01098 std::vector<SDValue> Ops; 01099 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag); 01100 SDValue Arg, StoreRet; 01101 01102 // For PIC16 ABI the arguments come after the return value. 01103 unsigned RetVals = Ins.size(); 01104 for (unsigned i = 0, ArgOffset = RetVals; i < NumOps; i++) { 01105 // Get the arguments 01106 Arg = Outs[i].Val; 01107 01108 Ops.clear(); 01109 Ops.push_back(Chain); 01110 Ops.push_back(Arg); 01111 Ops.push_back(DataAddr_Lo); 01112 Ops.push_back(DataAddr_Hi); 01113 Ops.push_back(DAG.getConstant(ArgOffset, MVT::i8)); 01114 Ops.push_back(InFlag); 01115 01116 StoreRet = DAG.getNode (PIC16ISD::PIC16StWF, dl, Tys, &Ops[0], Ops.size()); 01117 01118 Chain = getChain(StoreRet); 01119 InFlag = getOutFlag(StoreRet); 01120 ArgOffset++; 01121 } 01122 return Chain; 01123 } 01124 01125 SDValue PIC16TargetLowering:: 01126 LowerDirectCallArguments(SDValue ArgLabel, SDValue Chain, SDValue InFlag, 01127 const SmallVectorImpl<ISD::OutputArg> &Outs, 01128 DebugLoc dl, SelectionDAG &DAG) { 01129 unsigned NumOps = Outs.size(); 01130 std::string Name; 01131 SDValue Arg, StoreAt; 01132 EVT ArgVT; 01133 unsigned Size=0; 01134 01135 // If call has no arguments then do nothing and return. 01136 if (NumOps == 0) 01137 return Chain; 01138 01139 // FIXME: This portion of code currently assumes only 01140 // primitive types being passed as arguments. 01141 01142 // Legalize the address before use 01143 SDValue PtrLo, PtrHi; 01144 unsigned AddressOffset; 01145 int StoreOffset = 0; 01146 LegalizeAddress(ArgLabel, DAG, PtrLo, PtrHi, AddressOffset, dl); 01147 SDValue StoreRet; 01148 01149 std::vector<SDValue> Ops; 01150 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag); 01151 for (unsigned i=0, Offset = 0; i<NumOps; i++) { 01152 // Get the argument 01153 Arg = Outs[i].Val; 01154 StoreOffset = (Offset + AddressOffset); 01155 01156 // Store the argument on frame 01157 01158 Ops.clear(); 01159 Ops.push_back(Chain); 01160 Ops.push_back(Arg); 01161 Ops.push_back(PtrLo); 01162 Ops.push_back(PtrHi); 01163 Ops.push_back(DAG.getConstant(StoreOffset, MVT::i8)); 01164 Ops.push_back(InFlag); 01165 01166 StoreRet = DAG.getNode (PIC16ISD::PIC16StWF, dl, Tys, &Ops[0], Ops.size()); 01167 01168 Chain = getChain(StoreRet); 01169 InFlag = getOutFlag(StoreRet); 01170 01171 // Update the frame offset to be used for next argument 01172 ArgVT = Arg.getValueType(); 01173 Size = ArgVT.getSizeInBits(); 01174 Size = Size/8; // Calculate size in bytes 01175 Offset += Size; // Increase the frame offset 01176 } 01177 return Chain; 01178 } 01179 01180 SDValue PIC16TargetLowering:: 01181 LowerIndirectCallReturn(SDValue Chain, SDValue InFlag, 01182 SDValue DataAddr_Lo, SDValue DataAddr_Hi, 01183 const SmallVectorImpl<ISD::InputArg> &Ins, 01184 DebugLoc dl, SelectionDAG &DAG, 01185 SmallVectorImpl<SDValue> &InVals) { 01186 unsigned RetVals = Ins.size(); 01187 01188 // If call does not have anything to return 01189 // then do nothing and go back. 01190 if (RetVals == 0) 01191 return Chain; 01192 01193 // Call has something to return 01194 SDValue LoadRet; 01195 01196 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag); 01197 for(unsigned i=0;i<RetVals;i++) { 01198 LoadRet = DAG.getNode(PIC16ISD::PIC16LdWF, dl, Tys, Chain, DataAddr_Lo, 01199 DataAddr_Hi, DAG.getConstant(i, MVT::i8), 01200 InFlag); 01201 InFlag = getOutFlag(LoadRet); 01202 Chain = getChain(LoadRet); 01203 InVals.push_back(LoadRet); 01204 } 01205 return Chain; 01206 } 01207 01208 SDValue PIC16TargetLowering:: 01209 LowerDirectCallReturn(SDValue RetLabel, SDValue Chain, SDValue InFlag, 01210 const SmallVectorImpl<ISD::InputArg> &Ins, 01211 DebugLoc dl, SelectionDAG &DAG, 01212 SmallVectorImpl<SDValue> &InVals) { 01213 01214 // Currently handling primitive types only. They will come in 01215 // i8 parts 01216 unsigned RetVals = Ins.size(); 01217 01218 // Return immediately if the return type is void 01219 if (RetVals == 0) 01220 return Chain; 01221 01222 // Call has something to return 01223 01224 // Legalize the address before use 01225 SDValue LdLo, LdHi; 01226 unsigned LdOffset; 01227 LegalizeAddress(RetLabel, DAG, LdLo, LdHi, LdOffset, dl); 01228 01229 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag); 01230 SDValue LoadRet; 01231 01232 for(unsigned i=0, Offset=0;i<RetVals;i++) { 01233 01234 LoadRet = DAG.getNode(PIC16ISD::PIC16LdWF, dl, Tys, Chain, LdLo, LdHi, 01235 DAG.getConstant(LdOffset + Offset, MVT::i8), 01236 InFlag); 01237 01238 InFlag = getOutFlag(LoadRet); 01239 01240 Chain = getChain(LoadRet); 01241 Offset++; 01242 InVals.push_back(LoadRet); 01243 } 01244 01245 return Chain; 01246 } 01247 01248 SDValue 01249 PIC16TargetLowering::LowerReturn(SDValue Chain, 01250 unsigned CallConv, bool isVarArg, 01251 const SmallVectorImpl<ISD::OutputArg> &Outs, 01252 DebugLoc dl, SelectionDAG &DAG) { 01253 01254 // Number of values to return 01255 unsigned NumRet = Outs.size(); 01256 01257 // Function returns value always on stack with the offset starting 01258 // from 0 01259 MachineFunction &MF = DAG.getMachineFunction(); 01260 const Function *F = MF.getFunction(); 01261 std::string FuncName = F->getName(); 01262 01263 const char *tmpName = createESName(PAN::getFrameLabel(FuncName)); 01264 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Other); 01265 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8); 01266 SDValue BS = DAG.getConstant(1, MVT::i8); 01267 SDValue RetVal; 01268 for(unsigned i=0;i<NumRet; ++i) { 01269 RetVal = Outs[i].Val; 01270 Chain = DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other, Chain, RetVal, 01271 ES, BS, 01272 DAG.getConstant (i, MVT::i8)); 01273 01274 } 01275 return DAG.getNode(PIC16ISD::RET, dl, MVT::Other, Chain); 01276 } 01277 01278 void PIC16TargetLowering:: 01279 GetDataAddress(DebugLoc dl, SDValue Callee, SDValue &Chain, 01280 SDValue &DataAddr_Lo, SDValue &DataAddr_Hi, 01281 SelectionDAG &DAG) { 01282 assert (Callee.getOpcode() == PIC16ISD::PIC16Connect 01283 && "Don't know what to do of such callee!!"); 01284 SDValue ZeroOperand = DAG.getConstant(0, MVT::i8); 01285 SDValue SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand); 01286 Chain = getChain(SeqStart); 01287 SDValue OperFlag = getOutFlag(SeqStart); // To manage the data dependency 01288 01289 // Get the Lo and Hi part of code address 01290 SDValue Lo = Callee.getOperand(0); 01291 SDValue Hi = Callee.getOperand(1); 01292 01293 SDValue Data_Lo, Data_Hi; 01294 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag); 01295 // Subtract 2 from Address to get the Lower part of DataAddress. 01296 SDVTList VTList = DAG.getVTList(MVT::i8, MVT::Flag); 01297 Data_Lo = DAG.getNode(ISD::SUBC, dl, VTList, Lo, 01298 DAG.getConstant(2, MVT::i8)); 01299 SDValue Ops[3] = { Hi, DAG.getConstant(0, MVT::i8), Data_Lo.getValue(1)}; 01300 Data_Hi = DAG.getNode(ISD::SUBE, dl, VTList, Ops, 3); 01301 SDValue PCLATH = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, Data_Hi); 01302 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Data_Lo, PCLATH); 01303 SDValue Call = DAG.getNode(PIC16ISD::CALLW, dl, Tys, Chain, Callee, 01304 OperFlag); 01305 Chain = getChain(Call); 01306 OperFlag = getOutFlag(Call); 01307 SDValue SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand, 01308 OperFlag); 01309 Chain = getChain(SeqEnd); 01310 OperFlag = getOutFlag(SeqEnd); 01311 01312 // Low part of Data Address 01313 DataAddr_Lo = DAG.getNode(PIC16ISD::MTLO, dl, MVT::i8, Call, OperFlag); 01314 01315 // Make the second call. 01316 SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand); 01317 Chain = getChain(SeqStart); 01318 OperFlag = getOutFlag(SeqStart); // To manage the data dependency 01319 01320 // Subtract 1 from Address to get high part of data address. 01321 Data_Lo = DAG.getNode(ISD::SUBC, dl, VTList, Lo, 01322 DAG.getConstant(1, MVT::i8)); 01323 SDValue HiOps[3] = { Hi, DAG.getConstant(0, MVT::i8), Data_Lo.getValue(1)}; 01324 Data_Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps, 3); 01325 PCLATH = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, Data_Hi); 01326 01327 // Use new Lo to make another CALLW 01328 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Data_Lo, PCLATH); 01329 Call = DAG.getNode(PIC16ISD::CALLW, dl, Tys, Chain, Callee, OperFlag); 01330 Chain = getChain(Call); 01331 OperFlag = getOutFlag(Call); 01332 SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand, 01333 OperFlag); 01334 Chain = getChain(SeqEnd); 01335 OperFlag = getOutFlag(SeqEnd); 01336 // Hi part of Data Address 01337 DataAddr_Hi = DAG.getNode(PIC16ISD::MTHI, dl, MVT::i8, Call, OperFlag); 01338 } 01339 01340 SDValue 01341 PIC16TargetLowering::LowerCall(SDValue Chain, SDValue Callee, 01342 unsigned CallConv, bool isVarArg, 01343 bool isTailCall, 01344 const SmallVectorImpl<ISD::OutputArg> &Outs, 01345 const SmallVectorImpl<ISD::InputArg> &Ins, 01346 DebugLoc dl, SelectionDAG &DAG, 01347 SmallVectorImpl<SDValue> &InVals) { 01348 01349 assert(Callee.getValueType() == MVT::i16 && 01350 "Don't know how to legalize this call node!!!"); 01351 01352 // The flag to track if this is a direct or indirect call. 01353 bool IsDirectCall = true; 01354 unsigned RetVals = Ins.size(); 01355 unsigned NumArgs = Outs.size(); 01356 01357 SDValue DataAddr_Lo, DataAddr_Hi; 01358 if (!isa<GlobalAddressSDNode>(Callee) && 01359 !isa<ExternalSymbolSDNode>(Callee)) { 01360 IsDirectCall = false; // This is indirect call 01361 01362 // If this is an indirect call then to pass the arguments 01363 // and read the return value back, we need the data address 01364 // of the function being called. 01365 // To get the data address two more calls need to be made. 01366 01367 // Come here for indirect calls 01368 SDValue Lo, Hi; 01369 // Indirect addresses. Get the hi and lo parts of ptr. 01370 GetExpandedParts(Callee, DAG, Lo, Hi); 01371 // Connect Lo and Hi parts of the callee with the PIC16Connect 01372 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Lo, Hi); 01373 01374 // Read DataAddress only if we have to pass arguments or 01375 // read return value. 01376 if ((RetVals > 0) || (NumArgs > 0)) 01377 GetDataAddress(dl, Callee, Chain, DataAddr_Lo, DataAddr_Hi, DAG); 01378 } 01379 01380 SDValue ZeroOperand = DAG.getConstant(0, MVT::i8); 01381 01382 // Start the call sequence. 01383 // Carring the Constant 0 along the CALLSEQSTART 01384 // because there is nothing else to carry. 01385 SDValue SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand); 01386 Chain = getChain(SeqStart); 01387 SDValue OperFlag = getOutFlag(SeqStart); // To manage the data dependency 01388 std::string Name; 01389 01390 // For any direct call - callee will be GlobalAddressNode or 01391 // ExternalSymbol 01392 SDValue ArgLabel, RetLabel; 01393 if (IsDirectCall) { 01394 // Considering the GlobalAddressNode case here. 01395 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 01396 GlobalValue *GV = G->getGlobal(); 01397 Callee = DAG.getTargetGlobalAddress(GV, MVT::i8); 01398 Name = G->getGlobal()->getName(); 01399 } else {// Considering the ExternalSymbol case here 01400 ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Callee); 01401 Callee = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8); 01402 Name = ES->getSymbol(); 01403 } 01404 01405 // Label for argument passing 01406 const char *argFrame = createESName(PAN::getArgsLabel(Name)); 01407 ArgLabel = DAG.getTargetExternalSymbol(argFrame, MVT::i8); 01408 01409 // Label for reading return value 01410 const char *retName = createESName(PAN::getRetvalLabel(Name)); 01411 RetLabel = DAG.getTargetExternalSymbol(retName, MVT::i8); 01412 } else { 01413 // if indirect call 01414 SDValue CodeAddr_Lo = Callee.getOperand(0); 01415 SDValue CodeAddr_Hi = Callee.getOperand(1); 01416 01417 /*CodeAddr_Lo = DAG.getNode(ISD::ADD, dl, MVT::i8, CodeAddr_Lo, 01418 DAG.getConstant(2, MVT::i8));*/ 01419 01420 // move Hi part in PCLATH 01421 CodeAddr_Hi = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, CodeAddr_Hi); 01422 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, CodeAddr_Lo, 01423 CodeAddr_Hi); 01424 } 01425 01426 // Pass the argument to function before making the call. 01427 SDValue CallArgs; 01428 if (IsDirectCall) { 01429 CallArgs = LowerDirectCallArguments(ArgLabel, Chain, OperFlag, 01430 Outs, dl, DAG); 01431 Chain = getChain(CallArgs); 01432 OperFlag = getOutFlag(CallArgs); 01433 } else { 01434 CallArgs = LowerIndirectCallArguments(Chain, OperFlag, DataAddr_Lo, 01435 DataAddr_Hi, Outs, Ins, dl, DAG); 01436 Chain = getChain(CallArgs); 01437 OperFlag = getOutFlag(CallArgs); 01438 } 01439 01440 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag); 01441 SDValue PICCall = DAG.getNode(PIC16ISD::CALL, dl, Tys, Chain, Callee, 01442 OperFlag); 01443 Chain = getChain(PICCall); 01444 OperFlag = getOutFlag(PICCall); 01445 01446 01447 // Carrying the Constant 0 along the CALLSEQSTART 01448 // because there is nothing else to carry. 01449 SDValue SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand, 01450 OperFlag); 01451 Chain = getChain(SeqEnd); 01452 OperFlag = getOutFlag(SeqEnd); 01453 01454 // Lower the return value reading after the call. 01455 if (IsDirectCall) 01456 return LowerDirectCallReturn(RetLabel, Chain, OperFlag, 01457 Ins, dl, DAG, InVals); 01458 else 01459 return LowerIndirectCallReturn(Chain, OperFlag, DataAddr_Lo, 01460 DataAddr_Hi, Ins, dl, DAG, InVals); 01461 } 01462 01463 bool PIC16TargetLowering::isDirectLoad(const SDValue Op) { 01464 if (Op.getOpcode() == PIC16ISD::PIC16Load) 01465 if (Op.getOperand(1).getOpcode() == ISD::TargetGlobalAddress 01466 || Op.getOperand(1).getOpcode() == ISD::TargetExternalSymbol) 01467 return true; 01468 return false; 01469 } 01470 01471 // NeedToConvertToMemOp - Returns true if one of the operands of the 01472 // operation 'Op' needs to be put into memory. Also returns the 01473 // operand no. of the operand to be converted in 'MemOp'. Remember, PIC16 has 01474 // no instruction that can operation on two registers. Most insns take 01475 // one register and one memory operand (addwf) / Constant (addlw). 01476 bool PIC16TargetLowering::NeedToConvertToMemOp(SDValue Op, unsigned &MemOp) { 01477 // If one of the operand is a constant, return false. 01478 if (Op.getOperand(0).getOpcode() == ISD::Constant || 01479 Op.getOperand(1).getOpcode() == ISD::Constant) 01480 return false; 01481 01482 // Return false if one of the operands is already a direct 01483 // load and that operand has only one use. 01484 if (isDirectLoad(Op.getOperand(0))) { 01485 if (Op.getOperand(0).hasOneUse()) 01486 return false; 01487 else 01488 MemOp = 0; 01489 } 01490 if (isDirectLoad(Op.getOperand(1))) { 01491 if (Op.getOperand(1).hasOneUse()) 01492 return false; 01493 else 01494 MemOp = 1; 01495 } 01496 return true; 01497 } 01498 01499 // LowerBinOp - Lower a commutative binary operation that does not 01500 // affect status flag carry. 01501 SDValue PIC16TargetLowering::LowerBinOp(SDValue Op, SelectionDAG &DAG) { 01502 DebugLoc dl = Op.getDebugLoc(); 01503 01504 // We should have handled larger operands in type legalizer itself. 01505 assert (Op.getValueType() == MVT::i8 && "illegal Op to lower"); 01506 01507 unsigned MemOp = 1; 01508 if (NeedToConvertToMemOp(Op, MemOp)) { 01509 // Put one value on stack. 01510 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl); 01511 01512 return DAG.getNode(Op.getOpcode(), dl, MVT::i8, Op.getOperand(MemOp ^ 1), 01513 NewVal); 01514 } 01515 else { 01516 return Op; 01517 } 01518 } 01519 01520 // LowerADD - Lower all types of ADD operations including the ones 01521 // that affects carry. 01522 SDValue PIC16TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) { 01523 // We should have handled larger operands in type legalizer itself. 01524 assert (Op.getValueType() == MVT::i8 && "illegal add to lower"); 01525 DebugLoc dl = Op.getDebugLoc(); 01526 unsigned MemOp = 1; 01527 if (NeedToConvertToMemOp(Op, MemOp)) { 01528 // Put one value on stack. 01529 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl); 01530 01531 // ADDC and ADDE produce two results. 01532 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag); 01533 01534 // ADDE has three operands, the last one is the carry bit. 01535 if (Op.getOpcode() == ISD::ADDE) 01536 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1), 01537 NewVal, Op.getOperand(2)); 01538 // ADDC has two operands. 01539 else if (Op.getOpcode() == ISD::ADDC) 01540 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1), 01541 NewVal); 01542 // ADD it is. It produces only one result. 01543 else 01544 return DAG.getNode(Op.getOpcode(), dl, MVT::i8, Op.getOperand(MemOp ^ 1), 01545 NewVal); 01546 } 01547 else 01548 return Op; 01549 } 01550 01551 SDValue PIC16TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) { 01552 DebugLoc dl = Op.getDebugLoc(); 01553 // We should have handled larger operands in type legalizer itself. 01554 assert (Op.getValueType() == MVT::i8 && "illegal sub to lower"); 01555 01556 // Nothing to do if the first operand is already a direct load and it has 01557 // only one use. 01558 if (isDirectLoad(Op.getOperand(0)) && Op.getOperand(0).hasOneUse()) 01559 return Op; 01560 01561 // Put first operand on stack. 01562 SDValue NewVal = ConvertToMemOperand (Op.getOperand(0), DAG, dl); 01563 01564 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag); 01565 switch (Op.getOpcode()) { 01566 default: 01567 assert (0 && "Opcode unknown."); 01568 case ISD::SUBE: 01569 return DAG.getNode(Op.getOpcode(), dl, Tys, NewVal, Op.getOperand(1), 01570 Op.getOperand(2)); 01571 break; 01572 case ISD::SUBC: 01573 return DAG.getNode(Op.getOpcode(), dl, Tys, NewVal, Op.getOperand(1)); 01574 break; 01575 case ISD::SUB: 01576 return DAG.getNode(Op.getOpcode(), dl, MVT::i8, NewVal, Op.getOperand(1)); 01577 break; 01578 } 01579 } 01580 01581 void PIC16TargetLowering::InitReservedFrameCount(const Function *F) { 01582 unsigned NumArgs = F->arg_size(); 01583 01584 bool isVoidFunc = (F->getReturnType()->getTypeID() == Type::VoidTyID); 01585 01586 if (isVoidFunc) 01587 ReservedFrameCount = NumArgs; 01588 else 01589 ReservedFrameCount = NumArgs + 1; 01590 } 01591 01592 // LowerFormalArguments - Argument values are loaded from the 01593 // <fname>.args + offset. All arguments are already broken to leaglized 01594 // types, so the offset just runs from 0 to NumArgVals - 1. 01595 01596 SDValue 01597 PIC16TargetLowering::LowerFormalArguments(SDValue Chain, 01598 unsigned CallConv, 01599 bool isVarArg, 01600 const SmallVectorImpl<ISD::InputArg> &Ins, 01601 DebugLoc dl, 01602 SelectionDAG &DAG, 01603 SmallVectorImpl<SDValue> &InVals) { 01604 unsigned NumArgVals = Ins.size(); 01605 01606 // Get the callee's name to create the <fname>.args label to pass args. 01607 MachineFunction &MF = DAG.getMachineFunction(); 01608 const Function *F = MF.getFunction(); 01609 std::string FuncName = F->getName(); 01610 01611 // Reset the map of FI and TmpOffset 01612 ResetTmpOffsetMap(); 01613 // Initialize the ReserveFrameCount 01614 InitReservedFrameCount(F); 01615 01616 // Create the <fname>.args external symbol. 01617 const char *tmpName = createESName(PAN::getArgsLabel(FuncName)); 01618 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8); 01619 01620 // Load arg values from the label + offset. 01621 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Other); 01622 SDValue BS = DAG.getConstant(1, MVT::i8); 01623 for (unsigned i = 0; i < NumArgVals ; ++i) { 01624 SDValue Offset = DAG.getConstant(i, MVT::i8); 01625 SDValue PICLoad = DAG.getNode(PIC16ISD::PIC16LdArg, dl, VTs, Chain, ES, BS, 01626 Offset); 01627 Chain = getChain(PICLoad); 01628 InVals.push_back(PICLoad); 01629 } 01630 01631 return Chain; 01632 } 01633 01634 // Perform DAGCombine of PIC16Load. 01635 // FIXME - Need a more elaborate comment here. 01636 SDValue PIC16TargetLowering:: 01637 PerformPIC16LoadCombine(SDNode *N, DAGCombinerInfo &DCI) const { 01638 SelectionDAG &DAG = DCI.DAG; 01639 SDValue Chain = N->getOperand(0); 01640 if (N->hasNUsesOfValue(0, 0)) { 01641 DAG.ReplaceAllUsesOfValueWith(SDValue(N,1), Chain); 01642 } 01643 return SDValue(); 01644 } 01645 01646 // For all the functions with arguments some STORE nodes are generated 01647 // that store the argument on the frameindex. However in PIC16 the arguments 01648 // are passed on stack only. Therefore these STORE nodes are redundant. 01649 // To remove these STORE nodes will be removed in PerformStoreCombine 01650 // 01651 // Currently this function is doint nothing and will be updated for removing 01652 // unwanted store operations 01653 SDValue PIC16TargetLowering:: 01654 PerformStoreCombine(SDNode *N, DAGCombinerInfo &DCI) const { 01655 return SDValue(N, 0); 01656 /* 01657 // Storing an undef value is of no use, so remove it 01658 if (isStoringUndef(N, Chain, DAG)) { 01659 return Chain; // remove the store and return the chain 01660 } 01661 //else everything is ok. 01662 return SDValue(N, 0); 01663 */ 01664 } 01665 01666 SDValue PIC16TargetLowering::PerformDAGCombine(SDNode *N, 01667 DAGCombinerInfo &DCI) const { 01668 switch (N->getOpcode()) { 01669 case ISD::STORE: 01670 return PerformStoreCombine(N, DCI); 01671 case PIC16ISD::PIC16Load: 01672 return PerformPIC16LoadCombine(N, DCI); 01673 } 01674 return SDValue(); 01675 } 01676 01677 static PIC16CC::CondCodes IntCCToPIC16CC(ISD::CondCode CC) { 01678 switch (CC) { 01679 default: llvm_unreachable("Unknown condition code!"); 01680 case ISD::SETNE: return PIC16CC::NE; 01681 case ISD::SETEQ: return PIC16CC::EQ; 01682 case ISD::SETGT: return PIC16CC::GT; 01683 case ISD::SETGE: return PIC16CC::GE; 01684 case ISD::SETLT: return PIC16CC::LT; 01685 case ISD::SETLE: return PIC16CC::LE; 01686 case ISD::SETULT: return PIC16CC::ULT; 01687 case ISD::SETULE: return PIC16CC::ULE; 01688 case ISD::SETUGE: return PIC16CC::UGE; 01689 case ISD::SETUGT: return PIC16CC::UGT; 01690 } 01691 } 01692 01693 // Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so 01694 // set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition. 01695 static void LookThroughSetCC(SDValue &LHS, SDValue &RHS, 01696 ISD::CondCode CC, unsigned &SPCC) { 01697 if (isa<ConstantSDNode>(RHS) && 01698 cast<ConstantSDNode>(RHS)->getZExtValue() == 0 && 01699 CC == ISD::SETNE && 01700 (LHS.getOpcode() == PIC16ISD::SELECT_ICC && 01701 LHS.getOperand(3).getOpcode() == PIC16ISD::SUBCC) && 01702 isa<ConstantSDNode>(LHS.getOperand(0)) && 01703 isa<ConstantSDNode>(LHS.getOperand(1)) && 01704 cast<ConstantSDNode>(LHS.getOperand(0))->getZExtValue() == 1 && 01705 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() == 0) { 01706 SDValue CMPCC = LHS.getOperand(3); 01707 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue(); 01708 LHS = CMPCC.getOperand(0); 01709 RHS = CMPCC.getOperand(1); 01710 } 01711 } 01712 01713 // Returns appropriate CMP insn and corresponding condition code in PIC16CC 01714 SDValue PIC16TargetLowering::getPIC16Cmp(SDValue LHS, SDValue RHS, 01715 unsigned CC, SDValue &PIC16CC, 01716 SelectionDAG &DAG, DebugLoc dl) { 01717 PIC16CC::CondCodes CondCode = (PIC16CC::CondCodes) CC; 01718 01719 // PIC16 sub is literal - W. So Swap the operands and condition if needed. 01720 // i.e. a < 12 can be rewritten as 12 > a. 01721 if (RHS.getOpcode() == ISD::Constant) { 01722 01723 SDValue Tmp = LHS; 01724 LHS = RHS; 01725 RHS = Tmp; 01726 01727 switch (CondCode) { 01728 default: break; 01729 case PIC16CC::LT: 01730 CondCode = PIC16CC::GT; 01731 break; 01732 case PIC16CC::GT: 01733 CondCode = PIC16CC::LT; 01734 break; 01735 case PIC16CC::ULT: 01736 CondCode = PIC16CC::UGT; 01737 break; 01738 case PIC16CC::UGT: 01739 CondCode = PIC16CC::ULT; 01740 break; 01741 case PIC16CC::GE: 01742 CondCode = PIC16CC::LE; 01743 break; 01744 case PIC16CC::LE: 01745 CondCode = PIC16CC::GE; 01746 break; 01747 case PIC16CC::ULE: 01748 CondCode = PIC16CC::UGE; 01749 break; 01750 case PIC16CC::UGE: 01751 CondCode = PIC16CC::ULE; 01752 break; 01753 } 01754 } 01755 01756 PIC16CC = DAG.getConstant(CondCode, MVT::i8); 01757 01758 // These are signed comparisons. 01759 SDValue Mask = DAG.getConstant(128, MVT::i8); 01760 if (isSignedComparison(CondCode)) { 01761 LHS = DAG.getNode (ISD::XOR, dl, MVT::i8, LHS, Mask); 01762 RHS = DAG.getNode (ISD::XOR, dl, MVT::i8, RHS, Mask); 01763 } 01764 01765 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Flag); 01766 // We can use a subtract operation to set the condition codes. But 01767 // we need to put one operand in memory if required. 01768 // Nothing to do if the first operand is already a valid type (direct load 01769 // for subwf and literal for sublw) and it is used by this operation only. 01770 if ((LHS.getOpcode() == ISD::Constant || isDirectLoad(LHS)) 01771 && LHS.hasOneUse()) 01772 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS); 01773 01774 // else convert the first operand to mem. 01775 LHS = ConvertToMemOperand (LHS, DAG, dl); 01776 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS); 01777 } 01778 01779 01780 SDValue PIC16TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) { 01781 SDValue LHS = Op.getOperand(0); 01782 SDValue RHS = Op.getOperand(1); 01783 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); 01784 SDValue TrueVal = Op.getOperand(2); 01785 SDValue FalseVal = Op.getOperand(3); 01786 unsigned ORIGCC = ~0; 01787 DebugLoc dl = Op.getDebugLoc(); 01788 01789 // If this is a select_cc of a "setcc", and if the setcc got lowered into 01790 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values. 01791 // i.e. 01792 // A setcc: lhs, rhs, cc is expanded by llvm to 01793 // select_cc: result of setcc, 0, 1, 0, setne 01794 // We can think of it as: 01795 // select_cc: lhs, rhs, 1, 0, cc 01796 LookThroughSetCC(LHS, RHS, CC, ORIGCC); 01797 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC); 01798 01799 SDValue PIC16CC; 01800 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl); 01801 01802 return DAG.getNode (PIC16ISD::SELECT_ICC, dl, TrueVal.getValueType(), TrueVal, 01803 FalseVal, PIC16CC, Cmp.getValue(1)); 01804 } 01805 01806 MachineBasicBlock * 01807 PIC16TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI, 01808 MachineBasicBlock *BB) const { 01809 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo(); 01810 unsigned CC = (PIC16CC::CondCodes)MI->getOperand(3).getImm(); 01811 DebugLoc dl = MI->getDebugLoc(); 01812 01813 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond 01814 // control-flow pattern. The incoming instruction knows the destination vreg 01815 // to set, the condition code register to branch on, the true/false values to 01816 // select between, and a branch opcode to use. 01817 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 01818 MachineFunction::iterator It = BB; 01819 ++It; 01820 01821 // thisMBB: 01822 // ... 01823 // TrueVal = ... 01824 // [f]bCC copy1MBB 01825 // fallthrough --> copy0MBB 01826 MachineBasicBlock *thisMBB = BB; 01827 MachineFunction *F = BB->getParent(); 01828 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB); 01829 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB); 01830 BuildMI(BB, dl, TII.get(PIC16::pic16brcond)).addMBB(sinkMBB).addImm(CC); 01831 F->insert(It, copy0MBB); 01832 F->insert(It, sinkMBB); 01833 01834 // Update machine-CFG edges by transferring all successors of the current 01835 // block to the new block which will contain the Phi node for the select. 01836 sinkMBB->transferSuccessors(BB); 01837 // Next, add the true and fallthrough blocks as its successors. 01838 BB->addSuccessor(copy0MBB); 01839 BB->addSuccessor(sinkMBB); 01840 01841 // copy0MBB: 01842 // %FalseValue = ... 01843 // # fallthrough to sinkMBB 01844 BB = copy0MBB; 01845 01846 // Update machine-CFG edges 01847 BB->addSuccessor(sinkMBB); 01848 01849 // sinkMBB: 01850 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] 01851 // ... 01852 BB = sinkMBB; 01853 BuildMI(BB, dl, TII.get(PIC16::PHI), MI->getOperand(0).getReg()) 01854 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB) 01855 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB); 01856 01857 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now. 01858 return BB; 01859 } 01860 01861 01862 SDValue PIC16TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) { 01863 SDValue Chain = Op.getOperand(0); 01864 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get(); 01865 SDValue LHS = Op.getOperand(2); // LHS of the condition. 01866 SDValue RHS = Op.getOperand(3); // RHS of the condition. 01867 SDValue Dest = Op.getOperand(4); // BB to jump to 01868 unsigned ORIGCC = ~0; 01869 DebugLoc dl = Op.getDebugLoc(); 01870 01871 // If this is a br_cc of a "setcc", and if the setcc got lowered into 01872 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values. 01873 LookThroughSetCC(LHS, RHS, CC, ORIGCC); 01874 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC); 01875 01876 // Get the Compare insn and condition code. 01877 SDValue PIC16CC; 01878 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl); 01879 01880 return DAG.getNode(PIC16ISD::BRCOND, dl, MVT::Other, Chain, Dest, PIC16CC, 01881 Cmp.getValue(1)); 01882 } 01883