37 #include "tce_config.h"
38 #include <llvm/IR/Function.h>
39 #include <llvm/IR/DerivedTypes.h>
40 #include <llvm/IR/Intrinsics.h>
41 #include <llvm/IR/CallingConv.h>
42 #include <llvm/CodeGen/TargetLowering.h>
43 #include <llvm/CodeGen/CallingConvLower.h>
44 #include <llvm/CodeGen/SelectionDAG.h>
45 #include <llvm/CodeGen/MachineFrameInfo.h>
46 #include <llvm/CodeGen/MachineRegisterInfo.h>
47 #include <llvm/CodeGen/MachineInstrBuilder.h>
48 #include <llvm/Support/raw_ostream.h>
50 #include <llvm/Target/TargetLoweringObjectFile.h>
60 #include "tce_config.h"
67 #include "llvm/Support/ErrorHandling.h"
73 #define DEFAULT_TYPE MVT::i64
74 #define DEFAULT_IMM_INSTR TCE::MOVI64sa
75 #define DEFAULT_SIZE 8
76 #define DEFAULT_REG_CLASS TCE::R64IRegsRegClass
78 #define DEFAULT_TYPE MVT::i32
79 #define DEFAULT_IMM_INSTR TCE::MOVI32ri
80 #define DEFAULT_SIZE 4
81 #define DEFAULT_REG_CLASS TCE::R32IRegsRegClass
92 #include "TCEGenCallingConv.inc"
98 CallingConv::ID CallConv,
bool isVarArg,
99 const SmallVectorImpl<ISD::OutputArg> &Outs,
100 const SmallVectorImpl<SDValue> &OutVals,
105 SmallVector<CCValAssign, 16> RVLocs;
108 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
109 RVLocs, *DAG.getContext());
112 CCInfo.AnalyzeReturn(Outs, RetCC_TCE);
114 SmallVector<SDValue, 4> RetOps(1, Chain);
119 for (
unsigned i = 0; i != RVLocs.size(); ++i) {
120 CCValAssign &VA = RVLocs[i];
121 assert(VA.isRegLoc() &&
"Can only return in registers!");
123 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
127 Flag = Chain.getValue(1);
128 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
135 RetOps.push_back(Flag);
147 CallingConv::ID CallConv,
149 const SmallVectorImpl<ISD::InputArg> &Ins,
152 SmallVectorImpl<SDValue> &InVals)
const
155 MachineFunction &MF = DAG.getMachineFunction();
156 auto& frameInfo = MF.getFrameInfo();
157 MachineRegisterInfo &RegInfo = MF.getRegInfo();
160 SmallVector<CCValAssign, 16> ArgLocs;
162 CallConv, isVarArg, DAG.getMachineFunction(),
163 ArgLocs, *DAG.getContext());
165 CCInfo.AnalyzeFormalArguments(Ins, CC_TCE);
167 const unsigned *CurArgReg = ArgRegs, *ArgRegEnd = ArgRegs + argRegCount;
169 unsigned ArgOffset = 0;
171 for (
unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
173 CCValAssign &VA = ArgLocs[i];
176 EVT ObjectVT = VA.getValVT();
177 MVT sType = ObjectVT.getSimpleVT().SimpleTy;
179 if (sType == MVT::i1 || sType == MVT::i8 || sType == MVT::i16 ||
186 if (CurArgReg < ArgRegEnd) {
190 InVals.push_back(DAG.getUNDEF(ObjectVT));
191 }
else if (CurArgReg < ArgRegEnd && !isVarArg) {
192 unsigned VReg = RegInfo.createVirtualRegister(
194 MF.getRegInfo().addLiveIn(*CurArgReg++, VReg);
195 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg,
DEFAULT_TYPE);
197 unsigned AssertOp = ISD::AssertSext;
200 DAG.getValueType(ObjectVT));
201 Arg = DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, Arg);
203 InVals.push_back(Arg);
206 int FrameIdx = frameInfo.CreateFixedObject(
209 SDValue FIPtr = DAG.getFrameIndex(FrameIdx,
DEFAULT_TYPE);
215 ISD::LoadExtType LoadOp = ISD::SEXTLOAD;
217 #ifndef LITTLE_ENDIAN_TARGET // big endian extload hack starts
223 1UL, ObjectVT.getSizeInBits().getFixedSize()/8);
228 #endif // big endian hack ends
230 Load = DAG.getExtLoad(
232 MachinePointerInfo(), ObjectVT);
233 Load = DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, Load);
235 InVals.push_back(Load);
237 }
else if (sType == MVT::f16) {
239 if (CurArgReg < ArgRegEnd) {
242 InVals.push_back(DAG.getUNDEF(ObjectVT));
243 }
else if (CurArgReg < ArgRegEnd && !isVarArg) {
244 unsigned VReg = RegInfo.createVirtualRegister(
245 &TCE::HFPRegsRegClass);
246 MF.getRegInfo().addLiveIn(*CurArgReg++, VReg);
247 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::f16);
248 InVals.push_back(Arg);
250 int FrameIdx = frameInfo.CreateFixedObject(
252 SDValue FIPtr = DAG.getFrameIndex(FrameIdx,
DEFAULT_TYPE);
253 SDValue Load = DAG.getLoad(
254 MVT::f16, dl, Chain, FIPtr, MachinePointerInfo());
255 InVals.push_back(Load);
257 }
else if (sType == MVT::f32 || sType == MVT::f64) {
259 if (CurArgReg < ArgRegEnd) {
262 InVals.push_back(DAG.getUNDEF(ObjectVT));
263 }
else if (CurArgReg < ArgRegEnd && !isVarArg) {
264 auto regClass = sType == MVT::f32 ?
265 &TCE::FPRegsRegClass:
266 &TCE::R64DFPRegsRegClass;
267 unsigned VReg = RegInfo.createVirtualRegister(regClass);
268 MF.getRegInfo().addLiveIn(*CurArgReg++, VReg);
269 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, sType);
270 InVals.push_back(Arg);
272 int FrameIdx = frameInfo.CreateFixedObject(
274 SDValue FIPtr = DAG.getFrameIndex(FrameIdx,
DEFAULT_TYPE);
275 SDValue Load = DAG.getLoad(
276 sType, dl, Chain, FIPtr, MachinePointerInfo());
277 InVals.push_back(Load);
279 }
else if (sType.isVector()) {
281 InVals.push_back(DAG.getUNDEF(ObjectVT));
283 int FrameIdx = MF.getFrameInfo().CreateFixedObject(
284 sType.getStoreSize(), ArgOffset,
true);
285 SDValue FIPtr = DAG.getFrameIndex(FrameIdx,
DEFAULT_TYPE);
286 SDValue Load = DAG.getLoad(
287 sType, dl, Chain, FIPtr, MachinePointerInfo());
288 InVals.push_back(Load);
291 std::cerr <<
"Unhandled argument type: "
292 << ObjectVT.getEVTString() << std::endl;
293 std::cerr <<
"sType size in bits: " << sType.getSizeInBits() << std::endl;
294 std::cerr <<
"is a vector? " << sType.isVector() << std::endl;
298 unsigned argumentByteSize = sType.getStoreSize();
301 if (argumentByteSize <= maxMemAlignment) {
302 ArgOffset += maxMemAlignment;
304 unsigned alignBytes = maxMemAlignment - 1;
305 ArgOffset += (argumentByteSize + alignBytes) & (~alignBytes);
324 SmallVectorImpl<SDValue> &InVals)
const {
326 SelectionDAG &DAG = CLI.DAG;
328 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
329 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
330 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
331 SDValue Chain = CLI.Chain;
332 SDValue Callee = CLI.Callee;
333 bool &isTailCall = CLI.IsTailCall;
334 CallingConv::ID CallConv = CLI.CallConv;
335 bool isVarArg = CLI.IsVarArg;
344 unsigned ArgsSize = 0;
347 for (
unsigned i = 0, e = Outs.size(); i != e; ++i) {
348 EVT ObjectVT = Outs[i].VT;
349 MVT sType = Outs[i].VT.SimpleTy;
351 if (sType == MVT::i1 || sType == MVT::i8 || sType == MVT::i16 ||
352 sType == MVT::i32 || sType == MVT::f16 || sType == MVT::f32) {
353 if (regParams < argRegCount) {
356 }
else if (sType == MVT::i64 || sType == MVT::f64) {
358 }
else if (sType.isVector()) {
361 std::cerr <<
"Unknown argument type: "
362 << ObjectVT.getEVTString() << std::endl;
366 if (sType == MVT::i1 || sType == MVT::i8 || sType == MVT::i16 ||
367 sType == MVT::i32 || sType == MVT::i64 || sType == MVT::f16 ||
368 sType == MVT::f32 || sType == MVT::f64) {
369 if (regParams < argRegCount) {
372 }
else if (sType.isVector()) {
375 std::cerr <<
"Unknown argument type: "
376 << ObjectVT.getEVTString() << std::endl;
381 unsigned argumentByteSize = sType.getStoreSize();
384 if (argumentByteSize <= maxMemAlignment) {
385 ArgsSize += maxMemAlignment;
387 unsigned alignBytes = maxMemAlignment - 1;
388 ArgsSize += (argumentByteSize + alignBytes) & (~alignBytes);
391 Chain = DAG.getCALLSEQ_START(Chain, ArgsSize, 0, dl);
392 SmallVector<SDValue, 8> MemOpChains;
394 SmallVector<std::pair<unsigned, SDValue>, argRegCount> RegsToPass;
396 unsigned ArgOffset = 0;
398 for (
unsigned i = 0, e = Outs.size(); i != e; ++i) {
399 SDValue Val = OutVals[i];
400 EVT ObjectVT = Val.getValueType();
401 MVT sType = ObjectVT.getSimpleVT().SimpleTy;
402 SDValue ValToStore(0, 0);
405 if (sType == MVT::i1 || sType == MVT::i8 || sType == MVT::i16 ||
406 sType == MVT::i32 || sType == MVT::f32 || sType == MVT::f16) {
407 if (RegsToPass.size() >= argRegCount || isVarArg) {
410 if (RegsToPass.size() < argRegCount) {
411 RegsToPass.push_back(
412 std::make_pair(ArgRegs[RegsToPass.size()], Val));
414 }
else if (sType.isVector()) {
417 std::cerr <<
"Unknown argument type: "
418 << ObjectVT.getEVTString() << std::endl;
422 if (sType == MVT::i1 || sType == MVT::i8 || sType == MVT::i16 ||
423 sType == MVT::i32 || sType == MVT::i64 || sType == MVT::f32 ||
425 if (RegsToPass.size() >= argRegCount || isVarArg) {
428 if (RegsToPass.size() < argRegCount) {
429 RegsToPass.push_back(
430 std::make_pair(ArgRegs[RegsToPass.size()], Val));
432 }
else if (sType.isVector()) {
435 std::cerr <<
"Unknown argument type: "
436 << ObjectVT.getEVTString() << std::endl;
441 if (ValToStore.getNode()) {
442 SDValue StackPtr = DAG.getCopyFromReg(
443 Chain, dl, TCE::SP, getPointerTy(
444 getTargetMachine().createDataLayout(), 0));
445 SDValue PtrOff = DAG.getConstant(ArgOffset, dl,
DEFAULT_TYPE);
446 PtrOff = DAG.getNode(ISD::ADD, dl,
DEFAULT_TYPE, StackPtr, PtrOff);
448 MemOpChains.push_back(DAG.getStore(Chain, dl, ValToStore,
449 PtrOff, MachinePointerInfo()));
452 unsigned argumentByteSize = sType.getStoreSize();
455 if (argumentByteSize <= maxMemAlignment) {
456 ArgOffset += maxMemAlignment;
458 unsigned alignBytes = maxMemAlignment - 1;
459 ArgOffset += (argumentByteSize + alignBytes) & (~alignBytes);
464 if (!MemOpChains.empty()) {
466 ISD::TokenFactor, dl, MVT::Other, ArrayRef<SDValue>(MemOpChains));
475 for (
unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
476 unsigned Reg = RegsToPass[i].first;
477 Chain = DAG.getCopyToReg(Chain, dl, Reg, RegsToPass[i].second, InFlag);
478 InFlag = Chain.getValue(1);
484 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
485 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
DEFAULT_TYPE);
486 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
487 Callee = DAG.getTargetExternalSymbol(E->getSymbol(),
DEFAULT_TYPE);
488 std::vector<EVT> NodeTys;
489 NodeTys.push_back(MVT::Other);
490 NodeTys.push_back(MVT::Glue);
491 SDValue Ops[] = { Chain, Callee, InFlag };
495 ArrayRef<SDValue>(Ops, InFlag.getNode() ? 3 : 2));
497 InFlag = Chain.getValue(1);
499 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, dl,
true),
500 DAG.getIntPtrConstant(0, dl,
true), InFlag, dl);
501 InFlag = Chain.getValue(1);
504 SmallVector<CCValAssign, 16> RVLocs;
505 CCState RVInfo(CallConv, isVarArg, DAG.getMachineFunction(),
506 RVLocs, *DAG.getContext());
508 RVInfo.AnalyzeCallResult(Ins, RetCC_TCE);
511 for (
unsigned i = 0; i != RVLocs.size(); ++i) {
512 unsigned Reg = RVLocs[i].getLocReg();
514 Chain = DAG.getCopyFromReg(Chain, dl, Reg,
515 RVLocs[i].getValVT(), InFlag).getValue(1);
516 InFlag = Chain.getValue(2);
517 InVals.push_back(Chain.getValue(0));
535 setSchedulingPreference(llvm::Sched::RegPressure);
540 addRegisterClass(MVT::i1, &TCE::R1RegsRegClass);
543 addRegisterClass(MVT::i64, &TCE::R64IRegsRegClass);
544 addRegisterClass(MVT::f64, &TCE::R64DFPRegsRegClass);
546 addRegisterClass(MVT::i32, &TCE::R32IRegsRegClass);
548 addRegisterClass(MVT::f32, &TCE::FPRegsRegClass);
549 addRegisterClass(MVT::f16, &TCE::HFPRegsRegClass);
551 setOperationAction(ISD::UINT_TO_FP, MVT::i1 , Promote);
552 setOperationAction(ISD::UINT_TO_FP, MVT::i8 , Promote);
553 setOperationAction(ISD::UINT_TO_FP, MVT::i16 , Promote);
555 setOperationAction(ISD::SINT_TO_FP, MVT::i1 , Promote);
556 setOperationAction(ISD::SINT_TO_FP, MVT::i8 , Promote);
557 setOperationAction(ISD::SINT_TO_FP, MVT::i16 , Promote);
559 setOperationAction(ISD::FP_TO_UINT, MVT::i1 , Promote);
560 setOperationAction(ISD::FP_TO_UINT, MVT::i8 , Promote);
561 setOperationAction(ISD::FP_TO_UINT, MVT::i16 , Promote);
563 setOperationAction(ISD::FP_TO_SINT, MVT::i1 , Promote);
564 setOperationAction(ISD::FP_TO_SINT, MVT::i8 , Promote);
565 setOperationAction(ISD::FP_TO_SINT, MVT::i16 , Promote);
567 setOperationAction(ISD::GlobalAddress,
DEFAULT_TYPE, Custom);
568 setOperationAction(ISD::BlockAddress,
DEFAULT_TYPE, Custom);
569 setOperationAction(ISD::ConstantPool ,
DEFAULT_TYPE, Custom);
571 setOperationAction(ISD::TRAP, MVT::Other, Custom);
578 setOperationAction(ISD::SELECT_CC, MVT::f16, Expand);
579 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
580 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
581 setOperationAction(ISD::SELECT_CC, MVT::f80, Expand);
582 setOperationAction(ISD::SELECT_CC, MVT::i1, Expand);
583 setOperationAction(ISD::SELECT_CC, MVT::i8, Expand);
584 setOperationAction(ISD::SELECT_CC, MVT::i16, Expand);
585 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
586 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
592 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
595 setOperationAction(ISD::BRIND, MVT::Other, Expand);
597 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
603 std::cerr <<
"Only port guarded jumps supported, not expanding bc_cc" << std::endl;
605 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
606 setOperationAction(ISD::BRCOND, MVT::i1, Expand);
607 setOperationAction(ISD::BRCOND, MVT::i32, Expand);
608 setOperationAction(ISD::BRCOND, MVT::f16, Expand);
609 setOperationAction(ISD::BRCOND, MVT::f32, Expand);
610 setOperationAction(ISD::BRCOND, MVT::i64, Expand);
612 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
613 setOperationAction(ISD::BR_CC, MVT::i1, Expand);
614 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
615 setOperationAction(ISD::BR_CC, MVT::f16, Expand);
616 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
617 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
621 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
623 setTargetDAGCombine(ISD::BRCOND);
627 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
629 setOperationAction(ISD::MULHU, MVT::i32, Expand);
630 setOperationAction(ISD::MULHS, MVT::i32, Expand);
633 setOperationAction(ISD::MULHU, MVT::i64, Expand);
634 setOperationAction(ISD::MULHS, MVT::i64, Expand);
636 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
637 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
638 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
640 setOperationAction(ISD::VASTART , MVT::Other, Custom);
642 setOperationAction(ISD::VAARG , MVT::Other, Expand);
643 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
644 setOperationAction(ISD::VAEND , MVT::Other, Expand);
645 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
646 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
648 setOperationAction(ISD::DYNAMIC_STACKALLOC,
DEFAULT_TYPE, Expand);
650 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
651 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
653 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
655 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
656 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
657 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
658 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
662 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
663 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
666 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
667 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
668 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
671 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
674 setLoadExtAction(ISD::EXTLOAD, MVT::f32, MVT::f16, Expand);
675 setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, MVT::v2f16, Expand);
676 setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, MVT::v4f16, Expand);
677 setLoadExtAction(ISD::EXTLOAD, MVT::v8f32, MVT::v8f16, Expand);
678 setLoadExtAction(ISD::EXTLOAD, MVT::v16f32, MVT::v16f16, Expand);
679 setLoadExtAction(ISD::EXTLOAD, MVT::v32f32, MVT::v32f16, Expand);
682 setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f32, Expand);
685 #if LLVM_HAS_CUSTOM_VECTOR_EXTENSION == 2
686 setLoadExtAction(ISD::EXTLOAD, MVT::v64f32, MVT::v64f16, Expand);
687 setLoadExtAction(ISD::EXTLOAD, MVT::v128f32, MVT::v128f16, Expand);
692 std::cout <<
"No 8-bit loads in the processor. "
693 <<
"Emulating 8-bit loads with wider loads. "
694 <<
"This may be very slow if the program performs "
695 <<
"lots of 8-bit loads." << std::endl;
699 setLoadExtAction(ISD::EXTLOAD, MVT::i64, MVT::i8, Custom);
700 setLoadExtAction(ISD::SEXTLOAD, MVT::i64, MVT::i8, Custom);
701 setLoadExtAction(ISD::ZEXTLOAD, MVT::i64, MVT::i8, Custom);
702 setOperationAction(ISD::LOAD, MVT::i8, Custom);
703 setOperationAction(ISD::LOAD, MVT::i1, Custom);
705 setLoadExtAction(ISD::EXTLOAD, MVT::i64, MVT::i1, Custom);
706 setLoadExtAction(ISD::SEXTLOAD, MVT::i64, MVT::i1, Custom);
707 setLoadExtAction(ISD::ZEXTLOAD, MVT::i64, MVT::i1, Custom);
709 setLoadExtAction(ISD::EXTLOAD, MVT::i32, MVT::i8, Custom);
710 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, MVT::i8, Custom);
711 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, MVT::i8, Custom);
712 setOperationAction(ISD::LOAD, MVT::i8, Custom);
713 setOperationAction(ISD::LOAD, MVT::i1, Custom);
715 setLoadExtAction(ISD::EXTLOAD, MVT::i32, MVT::i1, Custom);
716 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, MVT::i1, Custom);
717 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, MVT::i1, Custom);
723 std::cout <<
"No 16-bit loads in the processor. "
724 <<
"Emulating 16-bit loads with wider loads. "
725 <<
"This may be very slow if the program performs "
726 <<
"lots of 16-bit loads." << std::endl;
729 setLoadExtAction(ISD::EXTLOAD, MVT::i64, MVT::i16, Custom);
730 setLoadExtAction(ISD::SEXTLOAD, MVT::i64, MVT::i16, Custom);
731 setLoadExtAction(ISD::ZEXTLOAD, MVT::i64, MVT::i16, Custom);
732 setOperationAction(ISD::LOAD, MVT::i16, Custom);
734 setLoadExtAction(ISD::EXTLOAD, MVT::i32, MVT::i16, Custom);
735 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, MVT::i16, Custom);
736 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, MVT::i16, Custom);
737 setOperationAction(ISD::LOAD, MVT::i16, Custom);
741 setOperationAction(ISD::ADDE, MVT::i32, Expand);
742 setOperationAction(ISD::ADDC, MVT::i32, Expand);
743 setOperationAction(ISD::ADDE, MVT::i16, Expand);
744 setOperationAction(ISD::ADDC, MVT::i16, Expand);
745 setOperationAction(ISD::ADDE, MVT::i8, Expand);
746 setOperationAction(ISD::ADDC, MVT::i8, Expand);
748 setOperationAction(ISD::Constant, MVT::i64, Custom);
750 setOperationAction(ISD::Constant, MVT::i32, Custom);
753 setStackPointerRegisterToSaveRestore(TCE::SP);
757 const std::set<std::pair<unsigned, llvm::MVT::SimpleValueType> >*
760 std::set<std::pair<unsigned, llvm::MVT::SimpleValueType> >::const_iterator
761 iter = missingOps->begin();
767 while (iter != missingOps->end()) {
768 unsigned nodetype = (*iter).first;
769 llvm::MVT::SimpleValueType valuetype = (*iter).second;
772 case ISD::SDIV: std::cerr <<
"SDIV,";
break;
773 case ISD::UDIV: std::cerr <<
"UDIV,";
break;
774 case ISD::SREM: std::cerr <<
"SREM,";
break;
775 case ISD::UREM: std::cerr <<
"UREM,";
break;
776 case ISD::ROTL: std::cerr <<
"ROTL,";
break;
777 case ISD::ROTR: std::cerr <<
"ROTR,";
break;
778 case ISD::MUL: std::cerr <<
"MUL,";
break;
779 case ISD::SIGN_EXTEND_INREG:
780 if (valuetype == MVT::i8) std::cerr <<
"SXQW,";
781 if (valuetype == MVT::i16) std::cerr <<
"SXHW,";
783 default: std::cerr << nodetype <<
", ";
break;
786 setOperationAction(nodetype, valuetype, Expand);
790 const std::set<std::pair<unsigned, llvm::MVT::SimpleValueType> >*
793 iter = promotedOps->begin();
794 while (iter != promotedOps->end()) {
795 unsigned nodetype = (*iter).first;
796 llvm::MVT::SimpleValueType valuetype = (*iter).second;
797 llvm::EVT evt(valuetype);
798 setOperationAction(nodetype, valuetype, Promote);
803 std::cerr << std::endl;
807 for (
auto i : *customLegalizedOps) {
808 unsigned nodetype = i.first;
809 llvm::MVT::SimpleValueType valuetype = i.second;
810 llvm::EVT evt(valuetype);
811 setOperationAction(nodetype, valuetype, Custom);
814 setJumpIsExpensive(
true);
818 PredictableSelectIsExpensive =
false;
825 if (as->numericalIds().empty()) {
829 std::cerr <<
"Global addresses by "
830 <<
"address space id of 0"
831 <<
" (implicitly specified by AS: " << as->name()
832 <<
") will be stored in constant pool."
842 for (
unsigned id : as->numericalIds()) {
845 std::cerr <<
"Global addresses belonging to "
846 <<
"address space id of " <<
id
847 <<
" (specified by AS: " << as->name()
848 <<
") will be stored in constant pool."
858 setBooleanContents(ZeroOrOneBooleanContent);
859 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
875 default:
return NULL;
896 TargetLowering::ArgListTy Args;
898 TargetLowering::CallLoweringInfo CLI(DAG);
900 CLI.setChain(Op->getOperand(0));
903 Type::getVoidTy(*DAG.getContext()),
904 DAG.getExternalSymbol(
"_exit",
905 getPointerTy(getTargetMachine().createDataLayout(), 0)),
907 CLI.setInRegister(
false);
908 CLI.setNoReturn(
true);
909 CLI.setVarArg(
false);
910 CLI.setTailCall(
false);
911 CLI.setDiscardResult(
false);
912 CLI.setSExtResult(
false);
913 CLI.setZExtResult(
false);
915 std::pair<SDValue, SDValue> CallResult =
917 return CallResult.second;
924 const GlobalAddressSDNode* gn = cast<GlobalAddressSDNode>(Op);
925 const GlobalValue* gv = gn->getGlobal();
930 std::cerr <<
"lowering GA: AS = " << gn->getAddressSpace() <<
", ";
931 gv->getValueType()->dump();
937 auto vt = getPointerTy(DAG.getDataLayout(), gn->getAddressSpace());
938 SDValue cpIdx = DAG.getConstantPool(
939 gv, getPointerTy(DAG.getDataLayout()));
940 llvm::Align Alignment = cast<ConstantPoolSDNode>(cpIdx)->getAlign();
941 SDValue result = DAG.getLoad(vt, dl, DAG.getEntryNode(), cpIdx,
942 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())
946 std::cerr <<
"Expanded Global Value to a load from "
947 <<
"the constant pool." << std::endl;
951 SDValue tga = DAG.getTargetGlobalAddress(gv, dl,
DEFAULT_TYPE);
957 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
959 SDValue BA_SD = DAG.getTargetBlockAddress(BA,
DEFAULT_TYPE);
963 getPointerTy(getTargetMachine().createDataLayout(), 0), BA_SD);
968 llvm::MVT ptrVT = Op.getValueType().getSimpleVT();
969 ConstantPoolSDNode* cp = cast<ConstantPoolSDNode>(Op);
971 if (cp->isMachineConstantPoolEntry()) {
972 res = DAG.getTargetConstantPool(
973 cp->getMachineCPVal(), ptrVT,
976 res = DAG.getTargetConstantPool(
977 cp->getConstVal(), ptrVT,
986 ConstantSDNode* cn = cast<ConstantSDNode>(Op.getNode());
995 std::cerr <<
"Expand constant of " << cn->getSExtValue();
996 std::cerr <<
" to a load from the constant pool." << std::endl;
1000 return SDValue(
nullptr, 0);
1005 SDValue Op, MVT newElementVT,
int elemCount, SelectionDAG &DAG)
const {
1007 BuildVectorSDNode* node = cast<BuildVectorSDNode>(Op);
1008 MVT mvt = Op.getSimpleValueType();
1009 int laneWidth = newElementVT.getSizeInBits();
1011 std::vector<SDValue> packedConstants(elemCount/laneWidth);
1012 for (
int i = 0; i < elemCount; i+=laneWidth) {
1013 unsigned int packedVal = 0;
1014 for (
int j = 0; j < laneWidth; j++) {
1015 const SDValue& operand = node->getOperand(i+j);
1016 SDNode* opdNode = operand.getNode();
1017 if (isa<ConstantSDNode>(opdNode)) {
1018 ConstantSDNode* cn = cast<ConstantSDNode>(opdNode);
1020 packedVal += (1<< j);
1024 packedConstants[i/laneWidth] = DAG.getConstant(packedVal, Op, newElementVT);
1026 EVT wvt = EVT::getVectorVT(*DAG.getContext(), newElementVT, elemCount/laneWidth);
1027 SDValue intVectorBuild = DAG.getNode(ISD::BUILD_VECTOR, Op, wvt, packedConstants);
1028 SDValue retValue = DAG.getNode(ISD::BITCAST, Op, mvt, intVectorBuild);
1035 MVT elemVT = Op.getSimpleValueType().getScalarType();
1036 BuildVectorSDNode* node = cast<BuildVectorSDNode>(Op);
1037 int elemCount = node->getNumOperands();
1044 auto vt = Op.getValueType();
1045 bool scalarizedPack =
false;
1046 if (vt.isVector() && vt.getSizeInBits() == 32) {
1047 unsigned int packedVal = 0;
1048 unsigned int laneW = vt.getScalarSizeInBits();
1050 i < vt.getVectorElementCount().getKnownMinValue(); i++) {
1051 auto oprd = node->getOperand(i);
1052 ConstantSDNode* cn = cast<ConstantSDNode>(oprd);
1053 unsigned int val = cn->getZExtValue();
1054 val = val & (~0u >> (32 - laneW));
1055 packedVal |= (val << (laneW*i));
1059 DAG.getConstant(packedVal, Op, MVT::i32);
1060 return DAG.getNode(ISD::BITCAST, Op, vt, packedNode);
1064 if (elemVT == MVT::i1) {
1065 if (elemCount > 31) {
1066 assert(elemCount % 32 == 0);
1067 int intElemCount = elemCount/32;
1068 TCEString wideOpName =
"PACK32X"; wideOpName << intElemCount;
1071 Op, MVT::i32, elemCount, DAG);
1094 if (elemCount > 255) {
1095 std::cerr <<
"Warning: Lowering Boolean vector build with"
1096 <<
" more than 255 elements. LLVM does not"
1097 <<
" support instructions with more than"
1098 <<
" 255 operands so this will probably fail."
1099 <<
" Add a pack instruction using wider lane"
1100 <<
" width, such as PACK32X" << (elemCount/32)
1101 <<
" into your architecture."
1108 switch (elemVT.SimpleTy) {
1109 case MVT::i8: packName <<
"8";
break;
1110 case MVT::i16: packName <<
"16";
break;
1111 case MVT::i32: packName <<
"32";
break;
1112 default: std::cerr << elemVT.SimpleTy <<
", ";
break;
1114 packName <<
"X" << elemCount;
1117 return SDValue(
nullptr, 0);
1137 std::cerr <<
"Expanding build_vector of "
1138 << Op->getValueType(0).getEVTString()
1140 for (
unsigned i = 0; i < node->getNumOperands(); i++) {
1141 auto opdNode = node->getOperand(i).getNode();
1142 if (isa<ConstantSDNode>(opdNode)) {
1143 ConstantSDNode* cn = cast<ConstantSDNode>(opdNode);
1144 std::cerr << cn->getSExtValue() <<
" ";
1146 std::cerr <<
"Reg ";
1149 std::cerr <<
"}" << std::endl;
1156 return SDValue(
nullptr, 0);
1168 DAG.getTargetLoweringInfo().getPointerTy(
1169 getTargetMachine().createDataLayout(), 0);
1171 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
1172 return DAG.getStore(
1173 Op.getOperand(0), dl, FR, Op.getOperand(1), MachinePointerInfo(SV));
1184 const DataLayout &DL, LLVMContext &context, llvm::EVT VT)
const {
1185 if (VT.isVector()) {
1187 if (resultVectorType != MVT::INVALID_SIMPLE_VALUE_TYPE) {
1188 return resultVectorType;
1191 if (!VT.isVector())
return hasI1RC_ ? llvm::MVT::i1 : llvm::MVT::i32;
1192 return VT.changeVectorElementTypeToInteger();
1195 #ifdef OLD_VECTOR_CODE
1197 SDValue LowerLOAD(SDValue Op, SelectionDAG &DAG) {
1198 EVT VT = Op.getValueType();
1199 DebugLoc dl = Op.getDebugLoc();
1200 SDValue Chain = Op.getOperand(0);
1203 if (VT == MVT::v4i32) {
1204 EVT ptrVT = Op.getOperand(1).getValueType();
1206 SDValue Ptr0, Ptr1, Ptr2, Ptr3;
1207 SDValue Imm0 = DAG.getConstant(0, ptrVT);
1208 SDValue Imm1 = DAG.getConstant(1, ptrVT);
1209 SDValue Imm2 = DAG.getConstant(2, ptrVT);
1210 SDValue Imm3 = DAG.getConstant(3, ptrVT);
1212 Ptr0 = Op.getOperand(1);
1213 Ptr1 = DAG.getNode(ISD::ADD, dl, ptrVT,
1214 Op.getOperand(1), Imm1);
1215 Ptr2 = DAG.getNode(ISD::ADD, dl, ptrVT,
1216 Op.getOperand(1), Imm2);
1217 Ptr3 = DAG.getNode(ISD::ADD, dl, ptrVT,
1218 Op.getOperand(1), Imm3);
1219 SDValue Elt0 = DAG.getLoad(
1220 MVT::i32, dl, Chain, Ptr0, MachinePointerInfo(),
false,
false, 0);
1221 SDValue Elt1 = DAG.getLoad(
1222 MVT::i32, dl, Chain, Ptr1, MachinePointerInfo(),
false,
false, 0);
1223 SDValue Elt2 = DAG.getLoad(
1224 MVT::i32, dl, Chain, Ptr2, MachinePointerInfo(),
false,
false, 0);
1225 SDValue Elt3 = DAG.getLoad(
1226 MVT::i32, dl, Chain, Ptr3, MachinePointerInfo(),
false,
false, 0);
1237 SDValue Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v4i32,
1238 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v4i32,
1239 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v4i32,
1240 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v4i32,
1241 DAG.getNode(ISD::UNDEF, dl, MVT::v4i32),
1247 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1248 Elt0.getValue(1), Elt1.getValue(1),
1249 Elt2.getValue(1), Elt3.getValue(1));
1251 SDValue Ops[] = {Result, Chain};
1253 return DAG.getMergeValues(Ops, 2, dl);
1256 llvm_unreachable(
"Invalid LOAD to lower!");
1261 switch(op.getOpcode()) {
1269 return std::make_pair(0,
TCEString(
"unknown op"));
1277 int shiftOpcode = shiftOpcodes.first;
1278 assert(shiftOpcode &&
"Shift opcide not supported, should not be here");
1280 SDValue
R = op.getOperand(0);
1281 SDValue Amt = op.getOperand(1);
1282 const DebugLoc& dl = op.getDebugLoc();
1283 std::set<unsigned long> supportedShifts;
1287 for (
int i = 1; i < 32; i++) {
1288 TCEString opName = shiftOpcodes.second; opName << i <<
"_32";
1290 supportedShifts.insert(i);
1297 supportedShifts.insert(1);
1300 if (Amt.getOpcode() == ISD::Constant) {
1301 unsigned long amount = op.getConstantOperandVal(1);
1303 if (supportedShifts.find(amount) == supportedShifts.end()) {
1305 for (
auto i = supportedShifts.rbegin();
1306 i != supportedShifts.rend(); i++) {
1309 dag.getConstant(*i, op, Amt.getValueType());
1311 dag.getConstant(amount - *i, op, Amt.getValueType());
1312 SDValue remaining = dag.getNode(
1313 op.getOpcode(), op, op.getValueType(),
R, remVal);
1314 SDValue lowered =
LowerShift(remaining, dag);
1315 SDValue shift = dag.getNode(
1316 shiftOpcode, op, op.getValueType(), lowered, shiftVal);
1324 unsigned Opc = op.getOpcode();
1327 return ExpandLibCall(RTLIB::SRA_I32, op.getNode(),
true, dag);
1329 return ExpandLibCall(RTLIB::SRL_I32, op.getNode(),
false, dag);
1331 return ExpandLibCall(RTLIB::SHL_I32, op.getNode(),
false, dag);
1333 std::cerr <<
"Invalid dynamic shift opcode" << std::endl;
1341 if (cast<ConstantSDNode>(op->getOperand(1))->getZExtValue() !=
1342 Intrinsic::set_loop_iterations) {
1343 std::cerr <<
"Trying to lower invalid hwloop instruction"
1349 if (op->use_empty())
return op;
1353 if (op->use_size() > 1) {
1355 assert(
false &&
"HWLoop should not have more than one Use");
1357 auto linkNode = op->use_begin();
1358 auto linkOpc = linkNode->getOpcode();
1359 if (linkOpc == ISD::BR || linkOpc == ISD::HANDLENODE) {
1368 if (linkOpc != ISD::TokenFactor || linkNode->use_size() > 1) {
1370 assert(
false &&
"HWLoop loop pattern not implemented.");
1375 SmallVector<SDValue, 8> ops;
1376 SmallVector<SDValue, 8> linkOps;
1377 bool replaceLinkNode =
false;
1378 for (
int i = 0; i < op.getNumOperands(); i++) {
1382 ops.push_back(SDValue(*linkNode, 0));
1385 for (
int j = 0; j < linkNode->getNumOperands(); j++) {
1386 if (linkNode->getOperand(j) == op) {
1387 linkOps.push_back(op.getOperand(i));
1389 linkOps.push_back(linkNode->getOperand(j));
1392 replaceLinkNode =
true;
1395 ops.push_back(op.getOperand(i));
1399 dag.ReplaceAllUsesWith(*linkNode, &op);
1400 auto Chain = dag.UpdateNodeOperands(op.getNode(), ArrayRef<SDValue>(ops));
1401 if (replaceLinkNode) {
1402 SDValue newLinkNode = dag.getNode(
1403 linkNode->getOpcode(), dl, MVT::Other,
1404 ArrayRef<SDValue>(linkOps));
1405 dag.ReplaceAllUsesWith(*linkNode, &newLinkNode);
1415 switch(op.getOpcode()) {
1416 case ISD::TRAP:
return LowerTRAP(op, dag);
1427 case ISD::INTRINSIC_VOID:
1429 case ISD::DYNAMIC_STACKALLOC: {
1430 assert(
false &&
"Dynamic stack allocation not yet implemented.");
1432 #ifdef OLD_VECTOR_CODE
1433 case ISD::LOAD:
return LowerLOAD(op, dag);
1436 op.getNode()->dump(&dag);
1437 assert(0 &&
"Custom lowerings not implemented!");
1442 SelectionDAG &DAG = DCI.DAG;
1444 switch (N->getOpcode()) {
1448 SDValue Cond = N->getOperand(1);
1449 SDValue Target = N->getOperand(2);
1451 if (Cond.getOpcode() == ISD::SETCC)
1452 Cond = Cond.getOperand(0);
1453 if (Cond.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
1454 cast<ConstantSDNode>(Cond.getOperand(1))->getZExtValue() ==
1455 Intrinsic::loop_decrement) {
1460 auto chain = N->getOperand(0);
1462 if (chain.getNode() == Cond.getNode()) {
1463 DAG.SelectNodeTo(N, TCE::LJUMP, MVT::Other,
1464 N->getOperand(2), Cond->getOperand(0));
1466 assert((chain.getOpcode() == ISD::TokenFactor) &&
1467 "chain to brcond is not TokenFactor.");
1468 assert((N->use_begin()->getOpcode() == ISD::BR) &&
1469 "brcond is not connected to br.");
1470 SmallVector<SDValue, 8> Ops;
1471 bool hasDecrement =
false;
1472 for (
unsigned i = 0, e = chain->getNumOperands(); i != e;
1474 if (chain->getOperand(i).getNode() ==
1476 hasDecrement =
true;
1477 Ops.push_back(Cond->getOperand(0));
1479 Ops.push_back(chain->getOperand(i));
1483 "Unable to find Chain for loop decrement");
1484 auto newChain = DAG.getNode(
1485 ISD::TokenFactor, SDLoc(chain), MVT::Other, Ops);
1486 DAG.ReplaceAllUsesOfValueWith(chain, newChain);
1489 DAG.UpdateNodeOperands(
1490 N, newChain, N->getOperand(1), N->getOperand(2));
1492 N, TCE::LJUMP, MVT::Other, N->getOperand(2),
1507 TCETargetLowering::ConstraintType
1509 if (Constraint.size() == 1) {
1510 switch (Constraint[0]) {
1512 case 'r':
return C_RegisterClass;
1515 return TargetLowering::getConstraintType(Constraint);
1518 const TargetRegisterClass*
1520 const TargetRegisterInfo* TRI,
1523 if (!VT.isVector())
return nullptr;
1525 const TargetRegisterClass* bestVRC =
nullptr;
1527 for (
unsigned i = 0U; i < TRI->getNumRegClasses(); i++) {
1528 auto vrc = TRI->getRegClass(i);
1529 if (TRI->isTypeLegalForClass(*vrc, VT) &&
1530 (!bestVRC || vrc->MC->RegsSize < bestVRC->MC->RegsSize)) {
1543 std::pair<unsigned, const TargetRegisterClass *>
1545 const TargetRegisterInfo *TRI,
1546 StringRef Constraint, MVT VT)
const {
1547 if (Constraint.size() == 1) {
1549 std::pair<unsigned, const TargetRegisterClass *> rcPair =
1552 switch (Constraint[0]) {
1555 if (rcPair.second != NULL) {
1561 bool isPhysicalRegister = Constraint.size() > 3
1562 && Constraint.front() ==
'{' && Constraint.back() ==
'}';
1564 const TargetRegisterClass* vrc =
nullptr;
1565 if (Constraint.size() == 1) {
1566 switch (Constraint[0]) {
1571 if (vrc)
return std::make_pair(0U, vrc);
1573 switch (VT.getSizeInBits()) {
1582 return std::make_pair(0U,
nullptr);
1585 return std::make_pair(0U, &TCE::R64RegsRegClass);
1587 if (VT == MVT::f32) {
1588 return std::make_pair(0U, &TCE::FPRegsRegClass);
1592 return std::make_pair(0U, &TCE::R64DFPRegsRegClass);
1595 }
else if (isPhysicalRegister) {
1597 const std::string regName = Constraint.substr(1, Constraint.size()-2).str();
1599 if (regId == TCE::NoRegister) {
1601 return std::make_pair(0,
nullptr);
1606 if (TCE::R1RegsRegClass.contains(regId)) {
1607 return std::make_pair(regId, &TCE::R1RegsRegClass);
1609 if (TCE::GuardRegsRegClass.contains(regId)) {
1610 return std::make_pair(regId, &TCE::GuardRegsRegClass);
1613 return std::make_pair(regId, TRI->getMinimalPhysRegClass(regId, VT));
1615 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
1622 std::string& Constraint,
1623 std::vector<SDValue>& Ops,
1624 SelectionDAG& DAG)
const {
1626 if (Constraint.length() == 1) {
1627 switch (Constraint[0]) {
1629 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
1640 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
1646 if (Constraint.size() != 1)
1647 return std::vector<unsigned>();
1649 switch (Constraint[0]) {
1653 return std::vector<unsigned>(1,0);
1657 return std::vector<unsigned>();
1680 for (
unsigned i = 0; i < node.getNumOperands(); i++) {
1681 auto opc = node.getOperand(i)->getOpcode();
1682 if (opc != ISD::Constant && opc != ISD::UNDEF) {
1695 for (
unsigned i = 0; i < node.getNumOperands(); i++) {
1696 if (node.getOperand(i)->getOpcode() != ISD::Constant)
continue;
1697 ConstantSDNode* cn =
1698 cast<ConstantSDNode>(node.getOperand(i).getNode());
1710 int64_t val = node.getSExtValue();
1711 MVT vt = node.getSimpleValueType(0);
1757 if (TCE::R1RegsRegClass.getNumRegs() == 0)
return false;
1761 if (TCE::R1RegsRegClass.getNumRegs() == 1) {
1762 std::string regName =
tm_.
rfName(TCE::R1RegsRegClass.getRegister(0));
1763 if (regName.find(
"dummy") != std::string::npos)
return false;
1774 const APFloat& apf, EVT VT,
bool forCodeSize)
const {
1775 if (VT==MVT::f32 || VT==MVT::f16) {
1783 if (n->getOpcode() != ISD::BUILD_VECTOR) {
1786 SDValue val = n->getOperand(0);
1787 int operandCount = n->getNumOperands();
1788 for (
unsigned i = 1; i <operandCount; i++) {
1789 SDValue val2 = n->getOperand(i);
1790 SDNode* node2 = dyn_cast<SDNode>(val2);
1791 if (node2->getOpcode() != ISD::UNDEF) {
1804 bool isSigned, SelectionDAG &DAG)
const {
1806 TargetLowering::ArgListTy Args;
1807 TargetLowering::ArgListEntry Entry;
1808 for (
const SDValue &Op : Node->op_values()) {
1809 EVT ArgVT = Op.getValueType();
1810 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
1813 Entry.IsSExt = shouldSignExtendTypeInLibCall(ArgVT, isSigned);
1814 Entry.IsZExt = !shouldSignExtendTypeInLibCall(ArgVT, isSigned);
1815 Args.push_back(Entry);
1817 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
1818 getPointerTy(DAG.getDataLayout(),0));
1820 EVT RetVT = Node->getValueType(0);
1821 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
1827 SDValue InChain = DAG.getEntryNode();
1831 SDValue TCChain = InChain;
1832 const Function &F = DAG.getMachineFunction().getFunction();
1834 isInTailCallPosition(DAG, Node, TCChain) &&
1835 (RetTy == F.getReturnType() || F.getReturnType()->isVoidTy());
1839 TargetLowering::CallLoweringInfo CLI(DAG);
1840 bool signExtend = shouldSignExtendTypeInLibCall(RetVT, isSigned);
1841 CLI.setDebugLoc(SDLoc(Node))
1843 .setLibCallee(getLibcallCallingConv(LC), RetTy, Callee,
1845 .setTailCall(isTailCall)
1846 .setSExtResult(signExtend)
1847 .setZExtResult(!signExtend)
1848 .setIsPostTypeLegalization(
true);
1850 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
1852 if (!CallInfo.second.getNode()) {
1854 return DAG.getRoot();
1857 return CallInfo.first;
1861 SDNode* node, SmallVectorImpl<SDValue>& Results,
1862 SelectionDAG& DAG)
const {
1863 auto fnName = DAG.getMachineFunction().getName().str();
1867 if (node->getOpcode() == ISD::LOAD) {
1868 auto lsdn = dyn_cast<LoadSDNode>(node);
1869 if (lsdn ==
nullptr) {
1870 std::cerr <<
"Error: null loadsdnde!" << std::endl;
1874 if (lsdn->getAlignment() < 2 &&
1875 lsdn->getMemoryVT() != MVT::i8 && lsdn->getMemoryVT() != MVT::i1) {
1876 assert(0 &&
"Cannot lower 16-bit memory op with only one byte alignment");
1879 auto chain = node->getOperand(0);
1883 if (lsdn->getAlignment() >= 4) {
1885 MVT::i32, node, chain, lsdn->getBasePtr(), MachinePointerInfo());
1889 ISD::AND, node, MVT::i32, lsdn->getBasePtr(),
1890 DAG.getConstant(-4l, node, MVT::i32));
1892 auto lowBytes = DAG.getNode(
1893 ISD::AND, node, MVT::i32, lsdn->getBasePtr(),
1894 DAG.getConstant(3l, node, MVT::i32));
1896 lowBits = DAG.getNode(
1897 ISD::SHL, node, MVT::i32, lowBytes,
1898 DAG.getConstant(3l, node, MVT::i32));
1901 MVT::i32, node, chain, alignedAddr, MachinePointerInfo());
1906 MVT vt = node->getSimpleValueType(0);
1907 if (vt == MVT::i32) {
1908 assert(0 &&
"Result i32? this should be extload?");
1909 Results.push_back(SDValue(load));
1910 Results.push_back(SDValue(load.getNode(),1));
1915 if (lsdn->getExtensionType() == ISD::ZEXTLOAD) {
1916 shiftedVal = lsdn->getAlignment() < 4 ?
1917 DAG.getNode(ISD::SRA, node, MVT::i32, load, lowBits):
1920 if (lsdn->getMemoryVT() == MVT::i1) {
1921 finalVal = DAG.getNode(
1922 ISD::AND, node, MVT::i32, shiftedVal,
1923 DAG.getConstant(1l, node, MVT::i32));
1924 }
else if (lsdn->getMemoryVT() == MVT::i8) {
1925 finalVal = DAG.getNode(
1926 ISD::AND, node, MVT::i32, shiftedVal,
1927 DAG.getConstant(255l, node, MVT::i32));
1930 assert(0 &&
"Wrong memory vt in zextload!");
1932 }
else if (lsdn->getExtensionType() == ISD::SEXTLOAD) {
1933 if (lsdn->getMemoryVT() == MVT::i1) {
1935 DAG.getNode(ISD::SUB, node, MVT::i32,
1936 DAG.getConstant(31l, node, MVT::i32),lowBits);
1937 auto shiftUp = DAG.getNode(
1938 ISD::SHL, node, MVT::i32, load, shiftsLeft);
1939 finalVal = DAG.getNode(
1940 ISD::SRA, node, MVT::i32, shiftUp,
1941 DAG.getConstant(31l, node, MVT::i32));
1942 }
else if (lsdn->getMemoryVT() == MVT::i8) {
1944 DAG.getNode(ISD::SUB, node, MVT::i32,
1945 DAG.getConstant(24l, node, MVT::i32),lowBits);
1946 auto shiftUp = DAG.getNode(
1947 ISD::SHL, node, MVT::i32, load, shiftsLeft);
1948 finalVal = DAG.getNode(
1949 ISD::SRA, node, MVT::i32, shiftUp,
1950 DAG.getConstant(24l, node, MVT::i32));
1953 assert(0 &&
"Wrong memory vt in sextload!");
1956 finalVal = lsdn->getAlignment() < 4 ?
1957 DAG.getNode(ISD::SRA, node, MVT::i32, load, lowBits):
1962 if (vt == MVT::i16) {
1963 rv = DAG.getAnyExtOrTrunc(finalVal, node, MVT::i16);
1964 }
else if (vt == MVT::i8) {
1965 rv = DAG.getAnyExtOrTrunc(finalVal, node, MVT::i8);
1966 }
else if (vt == MVT::i1) {
1967 rv = DAG.getAnyExtOrTrunc(finalVal, node, MVT::i1);
1969 assert(0 &&
"Wrong vt in load lowering!");
1972 Results.push_back(rv);
1973 Results.push_back(SDValue(load.getNode(),1));
1975 assert(
false &&
"ReplaceNodeResults not load!");
1984 SelectionDAG& DAG)
const {
1986 auto lsdn = dyn_cast<LoadSDNode>(op.getNode());
1987 if (lsdn ==
nullptr) {
1988 assert(
false &&
"Not a lodsdnode on LowerExtLoad!");
1991 auto chain = op.getOperand(0);
1992 SDValue alignedAddr;
1995 if (lsdn->getAlignment() >= 4) {
1996 alignedAddr = lsdn->getBasePtr();
1997 lowBits = DAG.getConstant(0l, op, MVT::i32);
1999 alignedAddr = DAG.getNode(
2000 ISD::AND, op, MVT::i32, lsdn->getBasePtr(),
2001 DAG.getConstant(-4l, op, MVT::i32));
2003 auto lowBytes = DAG.getNode(
2004 ISD::AND, op, MVT::i32, lsdn->getBasePtr(),
2005 DAG.getConstant(3l, op, MVT::i32));
2007 lowBits = DAG.getNode(
2008 ISD::SHL, op, MVT::i32, lowBytes,
2009 DAG.getConstant(3l, op, MVT::i32));
2012 auto load = DAG.getLoad(
2013 MVT::i32, op, chain, alignedAddr, MachinePointerInfo());
2016 if (lsdn->getExtensionType() == ISD::ZEXTLOAD) {
2017 auto shiftedValue = lsdn->getAlignment() < 4 ?
2018 DAG.getNode(ISD::SRA, op, MVT::i32, load, lowBits) :
2020 if (lsdn->getMemoryVT() == MVT::i16) {
2021 assert(lsdn->getAlignment() >= 2 &&
2022 "Cannot (yet?) emulate a 16-bit load which has 1-byte alignment. "
2023 " 16-bit memory operations needed to compile this code." );
2024 std::cerr <<
"\t\tSource is 16 bits." << std::endl;
2025 auto zext = DAG.getNode(
2026 ISD::AND, op, MVT::i32, shiftedValue,
2027 DAG.getConstant(65535l, op, MVT::i32));
2029 }
else if (lsdn->getMemoryVT() == MVT::i8) {
2030 auto zext = DAG.getNode(
2031 ISD::AND, op, MVT::i32, shiftedValue,
2032 DAG.getConstant(255l, op, MVT::i32));
2034 }
else if (lsdn->getMemoryVT() == MVT::i1) {
2035 auto zext = DAG.getNode(
2036 ISD::AND, op, MVT::i32, shiftedValue,
2037 DAG.getConstant(1l, op, MVT::i32));
2040 assert(
false &&
"Unknown data type on LowerSExtLoad!");
2043 if (lsdn->getExtensionType() == ISD::SEXTLOAD) {
2046 if (lsdn->getMemoryVT() == MVT::i16) {
2047 auto shiftsLeft = lsdn->getAlignment() < 4 ?
2048 DAG.getNode(ISD::SUB, op, MVT::i32,
2049 DAG.getConstant(16l, op, MVT::i32),
2051 DAG.getConstant(16l, op, MVT::i32);
2052 auto shiftUp = DAG.getNode(
2053 ISD::SHL, op, MVT::i32, load, shiftsLeft);
2054 auto shiftDown = DAG.getNode(
2055 ISD::SRA, op, MVT::i32, shiftUp,
2056 DAG.getConstant(16l, op, MVT::i32));
2058 }
else if (lsdn->getMemoryVT() == MVT::i8) {
2059 auto shiftsLeft = lsdn->getAlignment() < 4 ?
2060 DAG.getNode(ISD::SUB, op, MVT::i32,
2061 DAG.getConstant(24l, op, MVT::i32),
2063 DAG.getConstant(24l, op, MVT::i32);
2065 DAG.getNode(ISD::SHL, op, MVT::i32, load, shiftsLeft);
2066 auto shiftDown = DAG.getNode(
2067 ISD::SRA, op, MVT::i32, shiftUp,
2068 DAG.getConstant(24l, op, MVT::i32));
2070 }
else if (lsdn->getMemoryVT() == MVT::i1) {
2071 auto shiftsLeft = lsdn->getAlignment() < 4 ?
2072 DAG.getNode(ISD::SUB, op, MVT::i32,
2073 DAG.getConstant(31l, op, MVT::i32),
2075 DAG.getConstant(31l, op, MVT::i32);
2078 DAG.getNode(ISD::SHL, op, MVT::i32, load, shiftsLeft);
2079 auto shiftDown = DAG.getNode(
2080 ISD::SRA, op, MVT::i32, shiftUp,
2081 DAG.getConstant(31l, op, MVT::i32));
2084 assert(
false &&
"Unknown data type on Lower(Z)ExtLoad!");
2089 if (lsdn->getExtensionType() == ISD::EXTLOAD) {
2090 auto shiftedValue = lsdn->getAlignment() < 4 ?
2091 DAG.getNode(ISD::SRA, op, MVT::i32, load, lowBits) :
2093 auto shiftDown = DAG.getNode(ISD::SRA, op, MVT::i32, load, lowBits);
2097 MVT vt = op->getSimpleValueType(0);
2098 if (vt == MVT::i1 && lsdn->getMemoryVT() == MVT::i1) {
2099 SDValue trunc = DAG.getAnyExtOrTrunc(load, op, MVT::i1);
2103 assert(
false &&
"Should not be here, non-ext-load");