LLVM  10.0.0
XCoreISelLowering.cpp
Go to the documentation of this file.
1 //===-- XCoreISelLowering.cpp - XCore DAG Lowering Implementation ---------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the XCoreTargetLowering class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "XCoreISelLowering.h"
14 #include "XCore.h"
16 #include "XCoreSubtarget.h"
17 #include "XCoreTargetMachine.h"
18 #include "XCoreTargetObjectFile.h"
27 #include "llvm/IR/CallingConv.h"
28 #include "llvm/IR/Constants.h"
29 #include "llvm/IR/DerivedTypes.h"
30 #include "llvm/IR/Function.h"
31 #include "llvm/IR/GlobalAlias.h"
32 #include "llvm/IR/GlobalVariable.h"
33 #include "llvm/IR/Intrinsics.h"
34 #include "llvm/IR/IntrinsicsXCore.h"
35 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/KnownBits.h"
39 #include <algorithm>
40 
41 using namespace llvm;
42 
43 #define DEBUG_TYPE "xcore-lower"
44 
45 const char *XCoreTargetLowering::
46 getTargetNodeName(unsigned Opcode) const
47 {
48  switch ((XCoreISD::NodeType)Opcode)
49  {
50  case XCoreISD::FIRST_NUMBER : break;
51  case XCoreISD::BL : return "XCoreISD::BL";
52  case XCoreISD::PCRelativeWrapper : return "XCoreISD::PCRelativeWrapper";
53  case XCoreISD::DPRelativeWrapper : return "XCoreISD::DPRelativeWrapper";
54  case XCoreISD::CPRelativeWrapper : return "XCoreISD::CPRelativeWrapper";
55  case XCoreISD::LDWSP : return "XCoreISD::LDWSP";
56  case XCoreISD::STWSP : return "XCoreISD::STWSP";
57  case XCoreISD::RETSP : return "XCoreISD::RETSP";
58  case XCoreISD::LADD : return "XCoreISD::LADD";
59  case XCoreISD::LSUB : return "XCoreISD::LSUB";
60  case XCoreISD::LMUL : return "XCoreISD::LMUL";
61  case XCoreISD::MACCU : return "XCoreISD::MACCU";
62  case XCoreISD::MACCS : return "XCoreISD::MACCS";
63  case XCoreISD::CRC8 : return "XCoreISD::CRC8";
64  case XCoreISD::BR_JT : return "XCoreISD::BR_JT";
65  case XCoreISD::BR_JT32 : return "XCoreISD::BR_JT32";
66  case XCoreISD::FRAME_TO_ARGS_OFFSET : return "XCoreISD::FRAME_TO_ARGS_OFFSET";
67  case XCoreISD::EH_RETURN : return "XCoreISD::EH_RETURN";
68  case XCoreISD::MEMBARRIER : return "XCoreISD::MEMBARRIER";
69  }
70  return nullptr;
71 }
72 
74  const XCoreSubtarget &Subtarget)
75  : TargetLowering(TM), TM(TM), Subtarget(Subtarget) {
76 
77  // Set up the register classes.
78  addRegisterClass(MVT::i32, &XCore::GRRegsRegClass);
79 
80  // Compute derived properties from the register classes
82 
84 
86 
87  // Use i32 for setcc operations results (slt, sgt, ...).
89  setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
90 
91  // XCore does not have the NodeTypes below.
94 
95  // 64bit
105 
106  // Bit Manipulation
110 
112 
113  // Jump tables.
115 
118 
119  // Conversion of i64 -> double produces constantpool nodes
121 
122  // Loads
123  for (MVT VT : MVT::integer_valuetypes()) {
127 
130  }
131 
132  // Custom expand misaligned loads / stores.
135 
136  // Varargs
141 
142  // Dynamic stack
146 
147  // Exception handling
150 
151  // Atomic operations
152  // We request a fence for ATOMIC_* instructions, to reduce them to Monotonic.
153  // As we are always Sequential Consistent, an ATOMIC_FENCE becomes a no OP.
157 
158  // TRAMPOLINE is custom lowered.
161 
162  // We want to custom lower some of our intrinsics.
164 
168 
169  // We have target-specific dag combine patterns for the following nodes:
174 
177 }
178 
180  if (Val.getOpcode() != ISD::LOAD)
181  return false;
182 
183  EVT VT1 = Val.getValueType();
184  if (!VT1.isSimple() || !VT1.isInteger() ||
185  !VT2.isSimple() || !VT2.isInteger())
186  return false;
187 
188  switch (VT1.getSimpleVT().SimpleTy) {
189  default: break;
190  case MVT::i8:
191  return true;
192  }
193 
194  return false;
195 }
196 
199  switch (Op.getOpcode())
200  {
201  case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
202  case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
203  case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
204  case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
205  case ISD::BR_JT: return LowerBR_JT(Op, DAG);
206  case ISD::LOAD: return LowerLOAD(Op, DAG);
207  case ISD::STORE: return LowerSTORE(Op, DAG);
208  case ISD::VAARG: return LowerVAARG(Op, DAG);
209  case ISD::VASTART: return LowerVASTART(Op, DAG);
210  case ISD::SMUL_LOHI: return LowerSMUL_LOHI(Op, DAG);
211  case ISD::UMUL_LOHI: return LowerUMUL_LOHI(Op, DAG);
212  // FIXME: Remove these when LegalizeDAGTypes lands.
213  case ISD::ADD:
214  case ISD::SUB: return ExpandADDSUB(Op.getNode(), DAG);
215  case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
216  case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
217  case ISD::FRAME_TO_ARGS_OFFSET: return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
218  case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
219  case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
220  case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
221  case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
222  case ISD::ATOMIC_LOAD: return LowerATOMIC_LOAD(Op, DAG);
223  case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op, DAG);
224  default:
225  llvm_unreachable("unimplemented operand");
226  }
227 }
228 
229 /// ReplaceNodeResults - Replace the results of node with an illegal result
230 /// type with new values built out of custom code.
233  SelectionDAG &DAG) const {
234  switch (N->getOpcode()) {
235  default:
236  llvm_unreachable("Don't know how to custom expand this!");
237  case ISD::ADD:
238  case ISD::SUB:
239  Results.push_back(ExpandADDSUB(N, DAG));
240  return;
241  }
242 }
243 
244 //===----------------------------------------------------------------------===//
245 // Misc Lower Operation implementation
246 //===----------------------------------------------------------------------===//
247 
248 SDValue XCoreTargetLowering::getGlobalAddressWrapper(SDValue GA,
249  const GlobalValue *GV,
250  SelectionDAG &DAG) const {
251  // FIXME there is no actual debug info here
252  SDLoc dl(GA);
253 
254  if (GV->getValueType()->isFunctionTy())
255  return DAG.getNode(XCoreISD::PCRelativeWrapper, dl, MVT::i32, GA);
256 
257  const auto *GVar = dyn_cast<GlobalVariable>(GV);
258  if ((GV->hasSection() && GV->getSection().startswith(".cp.")) ||
259  (GVar && GVar->isConstant() && GV->hasLocalLinkage()))
260  return DAG.getNode(XCoreISD::CPRelativeWrapper, dl, MVT::i32, GA);
261 
262  return DAG.getNode(XCoreISD::DPRelativeWrapper, dl, MVT::i32, GA);
263 }
264 
265 static bool IsSmallObject(const GlobalValue *GV, const XCoreTargetLowering &XTL) {
267  return true;
268 
269  Type *ObjType = GV->getValueType();
270  if (!ObjType->isSized())
271  return false;
272 
273  auto &DL = GV->getParent()->getDataLayout();
274  unsigned ObjSize = DL.getTypeAllocSize(ObjType);
275  return ObjSize < CodeModelLargeSize && ObjSize != 0;
276 }
277 
278 SDValue XCoreTargetLowering::
279 LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const
280 {
281  const GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Op);
282  const GlobalValue *GV = GN->getGlobal();
283  SDLoc DL(GN);
284  int64_t Offset = GN->getOffset();
285  if (IsSmallObject(GV, *this)) {
286  // We can only fold positive offsets that are a multiple of the word size.
287  int64_t FoldedOffset = std::max(Offset & ~3, (int64_t)0);
288  SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, FoldedOffset);
289  GA = getGlobalAddressWrapper(GA, GV, DAG);
290  // Handle the rest of the offset.
291  if (Offset != FoldedOffset) {
292  SDValue Remaining = DAG.getConstant(Offset - FoldedOffset, DL, MVT::i32);
293  GA = DAG.getNode(ISD::ADD, DL, MVT::i32, GA, Remaining);
294  }
295  return GA;
296  } else {
297  // Ideally we would not fold in offset with an index <= 11.
298  Type *Ty = Type::getInt8PtrTy(*DAG.getContext());
299  Constant *GA = ConstantExpr::getBitCast(const_cast<GlobalValue*>(GV), Ty);
300  Ty = Type::getInt32Ty(*DAG.getContext());
301  Constant *Idx = ConstantInt::get(Ty, Offset);
303  Type::getInt8Ty(*DAG.getContext()), GA, Idx);
304  SDValue CP = DAG.getConstantPool(GAI, MVT::i32);
305  return DAG.getLoad(getPointerTy(DAG.getDataLayout()), DL,
307  }
308 }
309 
310 SDValue XCoreTargetLowering::
311 LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const
312 {
313  SDLoc DL(Op);
314  auto PtrVT = getPointerTy(DAG.getDataLayout());
315  const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
316  SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT);
317 
318  return DAG.getNode(XCoreISD::PCRelativeWrapper, DL, PtrVT, Result);
319 }
320 
321 SDValue XCoreTargetLowering::
322 LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
323 {
324  ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
325  // FIXME there isn't really debug info here
326  SDLoc dl(CP);
327  EVT PtrVT = Op.getValueType();
328  SDValue Res;
329  if (CP->isMachineConstantPoolEntry()) {
330  Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
331  CP->getAlignment(), CP->getOffset());
332  } else {
333  Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
334  CP->getAlignment(), CP->getOffset());
335  }
336  return DAG.getNode(XCoreISD::CPRelativeWrapper, dl, MVT::i32, Res);
337 }
338 
341 }
342 
343 SDValue XCoreTargetLowering::
344 LowerBR_JT(SDValue Op, SelectionDAG &DAG) const
345 {
346  SDValue Chain = Op.getOperand(0);
347  SDValue Table = Op.getOperand(1);
348  SDValue Index = Op.getOperand(2);
349  SDLoc dl(Op);
350  JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
351  unsigned JTI = JT->getIndex();
353  const MachineJumpTableInfo *MJTI = MF.getJumpTableInfo();
354  SDValue TargetJT = DAG.getTargetJumpTable(JT->getIndex(), MVT::i32);
355 
356  unsigned NumEntries = MJTI->getJumpTables()[JTI].MBBs.size();
357  if (NumEntries <= 32) {
358  return DAG.getNode(XCoreISD::BR_JT, dl, MVT::Other, Chain, TargetJT, Index);
359  }
360  assert((NumEntries >> 31) == 0);
361  SDValue ScaledIndex = DAG.getNode(ISD::SHL, dl, MVT::i32, Index,
362  DAG.getConstant(1, dl, MVT::i32));
363  return DAG.getNode(XCoreISD::BR_JT32, dl, MVT::Other, Chain, TargetJT,
364  ScaledIndex);
365 }
366 
367 SDValue XCoreTargetLowering::lowerLoadWordFromAlignedBasePlusOffset(
368  const SDLoc &DL, SDValue Chain, SDValue Base, int64_t Offset,
369  SelectionDAG &DAG) const {
370  auto PtrVT = getPointerTy(DAG.getDataLayout());
371  if ((Offset & 0x3) == 0) {
372  return DAG.getLoad(PtrVT, DL, Chain, Base, MachinePointerInfo());
373  }
374  // Lower to pair of consecutive word aligned loads plus some bit shifting.
375  int32_t HighOffset = alignTo(Offset, 4);
376  int32_t LowOffset = HighOffset - 4;
377  SDValue LowAddr, HighAddr;
378  if (GlobalAddressSDNode *GASD =
379  dyn_cast<GlobalAddressSDNode>(Base.getNode())) {
380  LowAddr = DAG.getGlobalAddress(GASD->getGlobal(), DL, Base.getValueType(),
381  LowOffset);
382  HighAddr = DAG.getGlobalAddress(GASD->getGlobal(), DL, Base.getValueType(),
383  HighOffset);
384  } else {
385  LowAddr = DAG.getNode(ISD::ADD, DL, MVT::i32, Base,
386  DAG.getConstant(LowOffset, DL, MVT::i32));
387  HighAddr = DAG.getNode(ISD::ADD, DL, MVT::i32, Base,
388  DAG.getConstant(HighOffset, DL, MVT::i32));
389  }
390  SDValue LowShift = DAG.getConstant((Offset - LowOffset) * 8, DL, MVT::i32);
391  SDValue HighShift = DAG.getConstant((HighOffset - Offset) * 8, DL, MVT::i32);
392 
393  SDValue Low = DAG.getLoad(PtrVT, DL, Chain, LowAddr, MachinePointerInfo());
394  SDValue High = DAG.getLoad(PtrVT, DL, Chain, HighAddr, MachinePointerInfo());
395  SDValue LowShifted = DAG.getNode(ISD::SRL, DL, MVT::i32, Low, LowShift);
396  SDValue HighShifted = DAG.getNode(ISD::SHL, DL, MVT::i32, High, HighShift);
397  SDValue Result = DAG.getNode(ISD::OR, DL, MVT::i32, LowShifted, HighShifted);
398  Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Low.getValue(1),
399  High.getValue(1));
400  SDValue Ops[] = { Result, Chain };
401  return DAG.getMergeValues(Ops, DL);
402 }
403 
405 {
406  KnownBits Known = DAG.computeKnownBits(Value);
407  return Known.countMinTrailingZeros() >= 2;
408 }
409 
410 SDValue XCoreTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
411  const TargetLowering &TLI = DAG.getTargetLoweringInfo();
412  LLVMContext &Context = *DAG.getContext();
413  LoadSDNode *LD = cast<LoadSDNode>(Op);
415  "Unexpected extension type");
416  assert(LD->getMemoryVT() == MVT::i32 && "Unexpected load EVT");
417 
419  LD->getMemoryVT(), *LD->getMemOperand()))
420  return SDValue();
421 
422  SDValue Chain = LD->getChain();
423  SDValue BasePtr = LD->getBasePtr();
424  SDLoc DL(Op);
425 
426  if (!LD->isVolatile()) {
427  const GlobalValue *GV;
428  int64_t Offset = 0;
429  if (DAG.isBaseWithConstantOffset(BasePtr) &&
430  isWordAligned(BasePtr->getOperand(0), DAG)) {
431  SDValue NewBasePtr = BasePtr->getOperand(0);
432  Offset = cast<ConstantSDNode>(BasePtr->getOperand(1))->getSExtValue();
433  return lowerLoadWordFromAlignedBasePlusOffset(DL, Chain, NewBasePtr,
434  Offset, DAG);
435  }
436  if (TLI.isGAPlusOffset(BasePtr.getNode(), GV, Offset) &&
437  MinAlign(GV->getAlignment(), 4) == 4) {
438  SDValue NewBasePtr = DAG.getGlobalAddress(GV, DL,
439  BasePtr->getValueType(0));
440  return lowerLoadWordFromAlignedBasePlusOffset(DL, Chain, NewBasePtr,
441  Offset, DAG);
442  }
443  }
444 
445  if (LD->getAlignment() == 2) {
446  SDValue Low =
447  DAG.getExtLoad(ISD::ZEXTLOAD, DL, MVT::i32, Chain, BasePtr,
448  LD->getPointerInfo(), MVT::i16,
449  /* Alignment = */ 2, LD->getMemOperand()->getFlags());
450  SDValue HighAddr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
451  DAG.getConstant(2, DL, MVT::i32));
452  SDValue High =
453  DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain, HighAddr,
455  /* Alignment = */ 2, LD->getMemOperand()->getFlags());
456  SDValue HighShifted = DAG.getNode(ISD::SHL, DL, MVT::i32, High,
457  DAG.getConstant(16, DL, MVT::i32));
458  SDValue Result = DAG.getNode(ISD::OR, DL, MVT::i32, Low, HighShifted);
459  Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Low.getValue(1),
460  High.getValue(1));
461  SDValue Ops[] = { Result, Chain };
462  return DAG.getMergeValues(Ops, DL);
463  }
464 
465  // Lower to a call to __misaligned_load(BasePtr).
466  Type *IntPtrTy = DAG.getDataLayout().getIntPtrType(Context);
468  TargetLowering::ArgListEntry Entry;
469 
470  Entry.Ty = IntPtrTy;
471  Entry.Node = BasePtr;
472  Args.push_back(Entry);
473 
475  CLI.setDebugLoc(DL).setChain(Chain).setLibCallee(
476  CallingConv::C, IntPtrTy,
477  DAG.getExternalSymbol("__misaligned_load",
478  getPointerTy(DAG.getDataLayout())),
479  std::move(Args));
480 
481  std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
482  SDValue Ops[] = { CallResult.first, CallResult.second };
483  return DAG.getMergeValues(Ops, DL);
484 }
485 
486 SDValue XCoreTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
487  LLVMContext &Context = *DAG.getContext();
488  StoreSDNode *ST = cast<StoreSDNode>(Op);
489  assert(!ST->isTruncatingStore() && "Unexpected store type");
490  assert(ST->getMemoryVT() == MVT::i32 && "Unexpected store EVT");
491 
493  ST->getMemoryVT(), *ST->getMemOperand()))
494  return SDValue();
495 
496  SDValue Chain = ST->getChain();
497  SDValue BasePtr = ST->getBasePtr();
498  SDValue Value = ST->getValue();
499  SDLoc dl(Op);
500 
501  if (ST->getAlignment() == 2) {
502  SDValue Low = Value;
503  SDValue High = DAG.getNode(ISD::SRL, dl, MVT::i32, Value,
504  DAG.getConstant(16, dl, MVT::i32));
505  SDValue StoreLow = DAG.getTruncStore(
506  Chain, dl, Low, BasePtr, ST->getPointerInfo(), MVT::i16,
507  /* Alignment = */ 2, ST->getMemOperand()->getFlags());
508  SDValue HighAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, BasePtr,
509  DAG.getConstant(2, dl, MVT::i32));
510  SDValue StoreHigh = DAG.getTruncStore(
511  Chain, dl, High, HighAddr, ST->getPointerInfo().getWithOffset(2),
512  MVT::i16, /* Alignment = */ 2, ST->getMemOperand()->getFlags());
513  return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, StoreLow, StoreHigh);
514  }
515 
516  // Lower to a call to __misaligned_store(BasePtr, Value).
517  Type *IntPtrTy = DAG.getDataLayout().getIntPtrType(Context);
519  TargetLowering::ArgListEntry Entry;
520 
521  Entry.Ty = IntPtrTy;
522  Entry.Node = BasePtr;
523  Args.push_back(Entry);
524 
525  Entry.Node = Value;
526  Args.push_back(Entry);
527 
529  CLI.setDebugLoc(dl).setChain(Chain).setCallee(
531  DAG.getExternalSymbol("__misaligned_store",
532  getPointerTy(DAG.getDataLayout())),
533  std::move(Args));
534 
535  std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
536  return CallResult.second;
537 }
538 
539 SDValue XCoreTargetLowering::
540 LowerSMUL_LOHI(SDValue Op, SelectionDAG &DAG) const
541 {
542  assert(Op.getValueType() == MVT::i32 && Op.getOpcode() == ISD::SMUL_LOHI &&
543  "Unexpected operand to lower!");
544  SDLoc dl(Op);
545  SDValue LHS = Op.getOperand(0);
546  SDValue RHS = Op.getOperand(1);
547  SDValue Zero = DAG.getConstant(0, dl, MVT::i32);
548  SDValue Hi = DAG.getNode(XCoreISD::MACCS, dl,
549  DAG.getVTList(MVT::i32, MVT::i32), Zero, Zero,
550  LHS, RHS);
551  SDValue Lo(Hi.getNode(), 1);
552  SDValue Ops[] = { Lo, Hi };
553  return DAG.getMergeValues(Ops, dl);
554 }
555 
556 SDValue XCoreTargetLowering::
557 LowerUMUL_LOHI(SDValue Op, SelectionDAG &DAG) const
558 {
559  assert(Op.getValueType() == MVT::i32 && Op.getOpcode() == ISD::UMUL_LOHI &&
560  "Unexpected operand to lower!");
561  SDLoc dl(Op);
562  SDValue LHS = Op.getOperand(0);
563  SDValue RHS = Op.getOperand(1);
564  SDValue Zero = DAG.getConstant(0, dl, MVT::i32);
565  SDValue Hi = DAG.getNode(XCoreISD::LMUL, dl,
566  DAG.getVTList(MVT::i32, MVT::i32), LHS, RHS,
567  Zero, Zero);
568  SDValue Lo(Hi.getNode(), 1);
569  SDValue Ops[] = { Lo, Hi };
570  return DAG.getMergeValues(Ops, dl);
571 }
572 
573 /// isADDADDMUL - Return whether Op is in a form that is equivalent to
574 /// add(add(mul(x,y),a),b). If requireIntermediatesHaveOneUse is true then
575 /// each intermediate result in the calculation must also have a single use.
576 /// If the Op is in the correct form the constituent parts are written to Mul0,
577 /// Mul1, Addend0 and Addend1.
578 static bool
579 isADDADDMUL(SDValue Op, SDValue &Mul0, SDValue &Mul1, SDValue &Addend0,
580  SDValue &Addend1, bool requireIntermediatesHaveOneUse)
581 {
582  if (Op.getOpcode() != ISD::ADD)
583  return false;
584  SDValue N0 = Op.getOperand(0);
585  SDValue N1 = Op.getOperand(1);
586  SDValue AddOp;
587  SDValue OtherOp;
588  if (N0.getOpcode() == ISD::ADD) {
589  AddOp = N0;
590  OtherOp = N1;
591  } else if (N1.getOpcode() == ISD::ADD) {
592  AddOp = N1;
593  OtherOp = N0;
594  } else {
595  return false;
596  }
597  if (requireIntermediatesHaveOneUse && !AddOp.hasOneUse())
598  return false;
599  if (OtherOp.getOpcode() == ISD::MUL) {
600  // add(add(a,b),mul(x,y))
601  if (requireIntermediatesHaveOneUse && !OtherOp.hasOneUse())
602  return false;
603  Mul0 = OtherOp.getOperand(0);
604  Mul1 = OtherOp.getOperand(1);
605  Addend0 = AddOp.getOperand(0);
606  Addend1 = AddOp.getOperand(1);
607  return true;
608  }
609  if (AddOp.getOperand(0).getOpcode() == ISD::MUL) {
610  // add(add(mul(x,y),a),b)
611  if (requireIntermediatesHaveOneUse && !AddOp.getOperand(0).hasOneUse())
612  return false;
613  Mul0 = AddOp.getOperand(0).getOperand(0);
614  Mul1 = AddOp.getOperand(0).getOperand(1);
615  Addend0 = AddOp.getOperand(1);
616  Addend1 = OtherOp;
617  return true;
618  }
619  if (AddOp.getOperand(1).getOpcode() == ISD::MUL) {
620  // add(add(a,mul(x,y)),b)
621  if (requireIntermediatesHaveOneUse && !AddOp.getOperand(1).hasOneUse())
622  return false;
623  Mul0 = AddOp.getOperand(1).getOperand(0);
624  Mul1 = AddOp.getOperand(1).getOperand(1);
625  Addend0 = AddOp.getOperand(0);
626  Addend1 = OtherOp;
627  return true;
628  }
629  return false;
630 }
631 
632 SDValue XCoreTargetLowering::
633 TryExpandADDWithMul(SDNode *N, SelectionDAG &DAG) const
634 {
635  SDValue Mul;
636  SDValue Other;
637  if (N->getOperand(0).getOpcode() == ISD::MUL) {
638  Mul = N->getOperand(0);
639  Other = N->getOperand(1);
640  } else if (N->getOperand(1).getOpcode() == ISD::MUL) {
641  Mul = N->getOperand(1);
642  Other = N->getOperand(0);
643  } else {
644  return SDValue();
645  }
646  SDLoc dl(N);
647  SDValue LL, RL, AddendL, AddendH;
648  LL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
649  Mul.getOperand(0), DAG.getConstant(0, dl, MVT::i32));
650  RL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
651  Mul.getOperand(1), DAG.getConstant(0, dl, MVT::i32));
652  AddendL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
653  Other, DAG.getConstant(0, dl, MVT::i32));
654  AddendH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
655  Other, DAG.getConstant(1, dl, MVT::i32));
656  APInt HighMask = APInt::getHighBitsSet(64, 32);
657  unsigned LHSSB = DAG.ComputeNumSignBits(Mul.getOperand(0));
658  unsigned RHSSB = DAG.ComputeNumSignBits(Mul.getOperand(1));
659  if (DAG.MaskedValueIsZero(Mul.getOperand(0), HighMask) &&
660  DAG.MaskedValueIsZero(Mul.getOperand(1), HighMask)) {
661  // The inputs are both zero-extended.
662  SDValue Hi = DAG.getNode(XCoreISD::MACCU, dl,
663  DAG.getVTList(MVT::i32, MVT::i32), AddendH,
664  AddendL, LL, RL);
665  SDValue Lo(Hi.getNode(), 1);
666  return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
667  }
668  if (LHSSB > 32 && RHSSB > 32) {
669  // The inputs are both sign-extended.
670  SDValue Hi = DAG.getNode(XCoreISD::MACCS, dl,
671  DAG.getVTList(MVT::i32, MVT::i32), AddendH,
672  AddendL, LL, RL);
673  SDValue Lo(Hi.getNode(), 1);
674  return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
675  }
676  SDValue LH, RH;
677  LH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
678  Mul.getOperand(0), DAG.getConstant(1, dl, MVT::i32));
679  RH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
680  Mul.getOperand(1), DAG.getConstant(1, dl, MVT::i32));
681  SDValue Hi = DAG.getNode(XCoreISD::MACCU, dl,
682  DAG.getVTList(MVT::i32, MVT::i32), AddendH,
683  AddendL, LL, RL);
684  SDValue Lo(Hi.getNode(), 1);
685  RH = DAG.getNode(ISD::MUL, dl, MVT::i32, LL, RH);
686  LH = DAG.getNode(ISD::MUL, dl, MVT::i32, LH, RL);
687  Hi = DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, RH);
688  Hi = DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, LH);
689  return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
690 }
691 
692 SDValue XCoreTargetLowering::
693 ExpandADDSUB(SDNode *N, SelectionDAG &DAG) const
694 {
695  assert(N->getValueType(0) == MVT::i64 &&
696  (N->getOpcode() == ISD::ADD || N->getOpcode() == ISD::SUB) &&
697  "Unknown operand to lower!");
698 
699  if (N->getOpcode() == ISD::ADD)
700  if (SDValue Result = TryExpandADDWithMul(N, DAG))
701  return Result;
702 
703  SDLoc dl(N);
704 
705  // Extract components
707  N->getOperand(0),
708  DAG.getConstant(0, dl, MVT::i32));
710  N->getOperand(0),
711  DAG.getConstant(1, dl, MVT::i32));
713  N->getOperand(1),
714  DAG.getConstant(0, dl, MVT::i32));
716  N->getOperand(1),
717  DAG.getConstant(1, dl, MVT::i32));
718 
719  // Expand
720  unsigned Opcode = (N->getOpcode() == ISD::ADD) ? XCoreISD::LADD :
722  SDValue Zero = DAG.getConstant(0, dl, MVT::i32);
723  SDValue Lo = DAG.getNode(Opcode, dl, DAG.getVTList(MVT::i32, MVT::i32),
724  LHSL, RHSL, Zero);
725  SDValue Carry(Lo.getNode(), 1);
726 
727  SDValue Hi = DAG.getNode(Opcode, dl, DAG.getVTList(MVT::i32, MVT::i32),
728  LHSH, RHSH, Carry);
729  SDValue Ignored(Hi.getNode(), 1);
730  // Merge the pieces
731  return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
732 }
733 
734 SDValue XCoreTargetLowering::
735 LowerVAARG(SDValue Op, SelectionDAG &DAG) const
736 {
737  // Whist llvm does not support aggregate varargs we can ignore
738  // the possibility of the ValueType being an implicit byVal vararg.
739  SDNode *Node = Op.getNode();
740  EVT VT = Node->getValueType(0); // not an aggregate
741  SDValue InChain = Node->getOperand(0);
742  SDValue VAListPtr = Node->getOperand(1);
743  EVT PtrVT = VAListPtr.getValueType();
744  const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
745  SDLoc dl(Node);
746  SDValue VAList =
747  DAG.getLoad(PtrVT, dl, InChain, VAListPtr, MachinePointerInfo(SV));
748  // Increment the pointer, VAList, to the next vararg
749  SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAList,
750  DAG.getIntPtrConstant(VT.getSizeInBits() / 8,
751  dl));
752  // Store the incremented VAList to the legalized pointer
753  InChain = DAG.getStore(VAList.getValue(1), dl, nextPtr, VAListPtr,
754  MachinePointerInfo(SV));
755  // Load the actual argument out of the pointer VAList
756  return DAG.getLoad(VT, dl, InChain, VAList, MachinePointerInfo());
757 }
758 
759 SDValue XCoreTargetLowering::
760 LowerVASTART(SDValue Op, SelectionDAG &DAG) const
761 {
762  SDLoc dl(Op);
763  // vastart stores the address of the VarArgsFrameIndex slot into the
764  // memory location argument
768  return DAG.getStore(Op.getOperand(0), dl, Addr, Op.getOperand(1),
770 }
771 
772 SDValue XCoreTargetLowering::LowerFRAMEADDR(SDValue Op,
773  SelectionDAG &DAG) const {
774  // This nodes represent llvm.frameaddress on the DAG.
775  // It takes one operand, the index of the frame address to return.
776  // An index of zero corresponds to the current function's frame address.
777  // An index of one to the parent's frame address, and so on.
778  // Depths > 0 not supported yet!
779  if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
780  return SDValue();
781 
783  const TargetRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
784  return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op),
785  RegInfo->getFrameRegister(MF), MVT::i32);
786 }
787 
788 SDValue XCoreTargetLowering::
789 LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const {
790  // This nodes represent llvm.returnaddress on the DAG.
791  // It takes one operand, the index of the return address to return.
792  // An index of zero corresponds to the current function's return address.
793  // An index of one to the parent's return address, and so on.
794  // Depths > 0 not supported yet!
795  if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
796  return SDValue();
797 
800  int FI = XFI->createLRSpillSlot(MF);
801  SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
802  return DAG.getLoad(getPointerTy(DAG.getDataLayout()), SDLoc(Op),
803  DAG.getEntryNode(), FIN,
805 }
806 
807 SDValue XCoreTargetLowering::
808 LowerFRAME_TO_ARGS_OFFSET(SDValue Op, SelectionDAG &DAG) const {
809  // This node represents offset from frame pointer to first on-stack argument.
810  // This is needed for correct stack adjustment during unwind.
811  // However, we don't know the offset until after the frame has be finalised.
812  // This is done during the XCoreFTAOElim pass.
814 }
815 
816 SDValue XCoreTargetLowering::
817 LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
818  // OUTCHAIN = EH_RETURN(INCHAIN, OFFSET, HANDLER)
819  // This node represents 'eh_return' gcc dwarf builtin, which is used to
820  // return from exception. The general meaning is: adjust stack by OFFSET and
821  // pass execution to HANDLER.
823  SDValue Chain = Op.getOperand(0);
824  SDValue Offset = Op.getOperand(1);
825  SDValue Handler = Op.getOperand(2);
826  SDLoc dl(Op);
827 
828  // Absolute SP = (FP + FrameToArgs) + Offset
829  const TargetRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
830  SDValue Stack = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
831  RegInfo->getFrameRegister(MF), MVT::i32);
832  SDValue FrameToArgs = DAG.getNode(XCoreISD::FRAME_TO_ARGS_OFFSET, dl,
833  MVT::i32);
834  Stack = DAG.getNode(ISD::ADD, dl, MVT::i32, Stack, FrameToArgs);
835  Stack = DAG.getNode(ISD::ADD, dl, MVT::i32, Stack, Offset);
836 
837  // R0=ExceptionPointerRegister R1=ExceptionSelectorRegister
838  // which leaves 2 caller saved registers, R2 & R3 for us to use.
839  unsigned StackReg = XCore::R2;
840  unsigned HandlerReg = XCore::R3;
841 
842  SDValue OutChains[] = {
843  DAG.getCopyToReg(Chain, dl, StackReg, Stack),
844  DAG.getCopyToReg(Chain, dl, HandlerReg, Handler)
845  };
846 
847  Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
848 
849  return DAG.getNode(XCoreISD::EH_RETURN, dl, MVT::Other, Chain,
850  DAG.getRegister(StackReg, MVT::i32),
851  DAG.getRegister(HandlerReg, MVT::i32));
852 
853 }
854 
855 SDValue XCoreTargetLowering::
856 LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) const {
857  return Op.getOperand(0);
858 }
859 
860 SDValue XCoreTargetLowering::
861 LowerINIT_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) const {
862  SDValue Chain = Op.getOperand(0);
863  SDValue Trmp = Op.getOperand(1); // trampoline
864  SDValue FPtr = Op.getOperand(2); // nested function
865  SDValue Nest = Op.getOperand(3); // 'nest' parameter value
866 
867  const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
868 
869  // .align 4
870  // LDAPF_u10 r11, nest
871  // LDW_2rus r11, r11[0]
872  // STWSP_ru6 r11, sp[0]
873  // LDAPF_u10 r11, fptr
874  // LDW_2rus r11, r11[0]
875  // BAU_1r r11
876  // nest:
877  // .word nest
878  // fptr:
879  // .word fptr
880  SDValue OutChains[5];
881 
882  SDValue Addr = Trmp;
883 
884  SDLoc dl(Op);
885  OutChains[0] =
886  DAG.getStore(Chain, dl, DAG.getConstant(0x0a3cd805, dl, MVT::i32), Addr,
887  MachinePointerInfo(TrmpAddr));
888 
889  Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
890  DAG.getConstant(4, dl, MVT::i32));
891  OutChains[1] =
892  DAG.getStore(Chain, dl, DAG.getConstant(0xd80456c0, dl, MVT::i32), Addr,
893  MachinePointerInfo(TrmpAddr, 4));
894 
895  Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
896  DAG.getConstant(8, dl, MVT::i32));
897  OutChains[2] =
898  DAG.getStore(Chain, dl, DAG.getConstant(0x27fb0a3c, dl, MVT::i32), Addr,
899  MachinePointerInfo(TrmpAddr, 8));
900 
901  Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
902  DAG.getConstant(12, dl, MVT::i32));
903  OutChains[3] =
904  DAG.getStore(Chain, dl, Nest, Addr, MachinePointerInfo(TrmpAddr, 12));
905 
906  Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
907  DAG.getConstant(16, dl, MVT::i32));
908  OutChains[4] =
909  DAG.getStore(Chain, dl, FPtr, Addr, MachinePointerInfo(TrmpAddr, 16));
910 
911  return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
912 }
913 
914 SDValue XCoreTargetLowering::
915 LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
916  SDLoc DL(Op);
917  unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
918  switch (IntNo) {
919  case Intrinsic::xcore_crc8:
920  EVT VT = Op.getValueType();
921  SDValue Data =
922  DAG.getNode(XCoreISD::CRC8, DL, DAG.getVTList(VT, VT),
923  Op.getOperand(1), Op.getOperand(2) , Op.getOperand(3));
924  SDValue Crc(Data.getNode(), 1);
925  SDValue Results[] = { Crc, Data };
926  return DAG.getMergeValues(Results, DL);
927  }
928  return SDValue();
929 }
930 
931 SDValue XCoreTargetLowering::
932 LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG) const {
933  SDLoc DL(Op);
934  return DAG.getNode(XCoreISD::MEMBARRIER, DL, MVT::Other, Op.getOperand(0));
935 }
936 
937 SDValue XCoreTargetLowering::
938 LowerATOMIC_LOAD(SDValue Op, SelectionDAG &DAG) const {
939  AtomicSDNode *N = cast<AtomicSDNode>(Op);
940  assert(N->getOpcode() == ISD::ATOMIC_LOAD && "Bad Atomic OP");
943  "setInsertFencesForAtomic(true) expects unordered / monotonic");
944  if (N->getMemoryVT() == MVT::i32) {
945  if (N->getAlignment() < 4)
946  report_fatal_error("atomic load must be aligned");
947  return DAG.getLoad(getPointerTy(DAG.getDataLayout()), SDLoc(Op),
948  N->getChain(), N->getBasePtr(), N->getPointerInfo(),
949  N->getAlignment(), N->getMemOperand()->getFlags(),
950  N->getAAInfo(), N->getRanges());
951  }
952  if (N->getMemoryVT() == MVT::i16) {
953  if (N->getAlignment() < 2)
954  report_fatal_error("atomic load must be aligned");
955  return DAG.getExtLoad(ISD::EXTLOAD, SDLoc(Op), MVT::i32, N->getChain(),
956  N->getBasePtr(), N->getPointerInfo(), MVT::i16,
957  N->getAlignment(), N->getMemOperand()->getFlags(),
958  N->getAAInfo());
959  }
960  if (N->getMemoryVT() == MVT::i8)
961  return DAG.getExtLoad(ISD::EXTLOAD, SDLoc(Op), MVT::i32, N->getChain(),
962  N->getBasePtr(), N->getPointerInfo(), MVT::i8,
963  N->getAlignment(), N->getMemOperand()->getFlags(),
964  N->getAAInfo());
965  return SDValue();
966 }
967 
968 SDValue XCoreTargetLowering::
969 LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) const {
970  AtomicSDNode *N = cast<AtomicSDNode>(Op);
971  assert(N->getOpcode() == ISD::ATOMIC_STORE && "Bad Atomic OP");
974  "setInsertFencesForAtomic(true) expects unordered / monotonic");
975  if (N->getMemoryVT() == MVT::i32) {
976  if (N->getAlignment() < 4)
977  report_fatal_error("atomic store must be aligned");
978  return DAG.getStore(N->getChain(), SDLoc(Op), N->getVal(), N->getBasePtr(),
979  N->getPointerInfo(), N->getAlignment(),
980  N->getMemOperand()->getFlags(), N->getAAInfo());
981  }
982  if (N->getMemoryVT() == MVT::i16) {
983  if (N->getAlignment() < 2)
984  report_fatal_error("atomic store must be aligned");
985  return DAG.getTruncStore(N->getChain(), SDLoc(Op), N->getVal(),
986  N->getBasePtr(), N->getPointerInfo(), MVT::i16,
987  N->getAlignment(), N->getMemOperand()->getFlags(),
988  N->getAAInfo());
989  }
990  if (N->getMemoryVT() == MVT::i8)
991  return DAG.getTruncStore(N->getChain(), SDLoc(Op), N->getVal(),
992  N->getBasePtr(), N->getPointerInfo(), MVT::i8,
993  N->getAlignment(), N->getMemOperand()->getFlags(),
994  N->getAAInfo());
995  return SDValue();
996 }
997 
999 XCoreTargetLowering::getMMOFlags(const Instruction &I) const {
1000  // Because of how we convert atomic_load and atomic_store to normal loads and
1001  // stores in the DAG, we need to ensure that the MMOs are marked volatile
1002  // since DAGCombine hasn't been updated to account for atomic, but non
1003  // volatile loads. (See D57601)
1004  if (auto *SI = dyn_cast<StoreInst>(&I))
1005  if (SI->isAtomic())
1007  if (auto *LI = dyn_cast<LoadInst>(&I))
1008  if (LI->isAtomic())
1010  if (auto *AI = dyn_cast<AtomicRMWInst>(&I))
1011  if (AI->isAtomic())
1013  if (auto *AI = dyn_cast<AtomicCmpXchgInst>(&I))
1014  if (AI->isAtomic())
1017 }
1018 
1019 //===----------------------------------------------------------------------===//
1020 // Calling Convention Implementation
1021 //===----------------------------------------------------------------------===//
1022 
1023 #include "XCoreGenCallingConv.inc"
1024 
1025 //===----------------------------------------------------------------------===//
1026 // Call Calling Convention Implementation
1027 //===----------------------------------------------------------------------===//
1028 
1029 /// XCore call implementation
1030 SDValue
1031 XCoreTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
1032  SmallVectorImpl<SDValue> &InVals) const {
1033  SelectionDAG &DAG = CLI.DAG;
1034  SDLoc &dl = CLI.DL;
1036  SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1038  SDValue Chain = CLI.Chain;
1039  SDValue Callee = CLI.Callee;
1040  bool &isTailCall = CLI.IsTailCall;
1041  CallingConv::ID CallConv = CLI.CallConv;
1042  bool isVarArg = CLI.IsVarArg;
1043 
1044  // XCore target does not yet support tail call optimization.
1045  isTailCall = false;
1046 
1047  // For now, only CallingConv::C implemented
1048  switch (CallConv)
1049  {
1050  default:
1051  report_fatal_error("Unsupported calling convention");
1052  case CallingConv::Fast:
1053  case CallingConv::C:
1054  return LowerCCCCallTo(Chain, Callee, CallConv, isVarArg, isTailCall,
1055  Outs, OutVals, Ins, dl, DAG, InVals);
1056  }
1057 }
1058 
1059 /// LowerCallResult - Lower the result values of a call into the
1060 /// appropriate copies out of appropriate physical registers / memory locations.
1062  const SmallVectorImpl<CCValAssign> &RVLocs,
1063  const SDLoc &dl, SelectionDAG &DAG,
1064  SmallVectorImpl<SDValue> &InVals) {
1065  SmallVector<std::pair<int, unsigned>, 4> ResultMemLocs;
1066  // Copy results out of physical registers.
1067  for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
1068  const CCValAssign &VA = RVLocs[i];
1069  if (VA.isRegLoc()) {
1070  Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getValVT(),
1071  InFlag).getValue(1);
1072  InFlag = Chain.getValue(2);
1073  InVals.push_back(Chain.getValue(0));
1074  } else {
1075  assert(VA.isMemLoc());
1076  ResultMemLocs.push_back(std::make_pair(VA.getLocMemOffset(),
1077  InVals.size()));
1078  // Reserve space for this result.
1079  InVals.push_back(SDValue());
1080  }
1081  }
1082 
1083  // Copy results out of memory.
1084  SmallVector<SDValue, 4> MemOpChains;
1085  for (unsigned i = 0, e = ResultMemLocs.size(); i != e; ++i) {
1086  int offset = ResultMemLocs[i].first;
1087  unsigned index = ResultMemLocs[i].second;
1088  SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
1089  SDValue Ops[] = { Chain, DAG.getConstant(offset / 4, dl, MVT::i32) };
1090  SDValue load = DAG.getNode(XCoreISD::LDWSP, dl, VTs, Ops);
1091  InVals[index] = load;
1092  MemOpChains.push_back(load.getValue(1));
1093  }
1094 
1095  // Transform all loads nodes into one single node because
1096  // all load nodes are independent of each other.
1097  if (!MemOpChains.empty())
1098  Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
1099 
1100  return Chain;
1101 }
1102 
1103 /// LowerCCCCallTo - functions arguments are copied from virtual
1104 /// regs to (physical regs)/(stack frame), CALLSEQ_START and
1105 /// CALLSEQ_END are emitted.
1106 /// TODO: isTailCall, sret.
1107 SDValue XCoreTargetLowering::LowerCCCCallTo(
1108  SDValue Chain, SDValue Callee, CallingConv::ID CallConv, bool isVarArg,
1109  bool isTailCall, const SmallVectorImpl<ISD::OutputArg> &Outs,
1110  const SmallVectorImpl<SDValue> &OutVals,
1111  const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
1112  SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
1113 
1114  // Analyze operands of the call, assigning locations to each operand.
1116  CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1117  *DAG.getContext());
1118 
1119  // The ABI dictates there should be one stack slot available to the callee
1120  // on function entry (for saving lr).
1121  CCInfo.AllocateStack(4, 4);
1122 
1123  CCInfo.AnalyzeCallOperands(Outs, CC_XCore);
1124 
1126  // Analyze return values to determine the number of bytes of stack required.
1127  CCState RetCCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1128  *DAG.getContext());
1129  RetCCInfo.AllocateStack(CCInfo.getNextStackOffset(), 4);
1130  RetCCInfo.AnalyzeCallResult(Ins, RetCC_XCore);
1131 
1132  // Get a count of how many bytes are to be pushed on the stack.
1133  unsigned NumBytes = RetCCInfo.getNextStackOffset();
1134  auto PtrVT = getPointerTy(DAG.getDataLayout());
1135 
1136  Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, dl);
1137 
1139  SmallVector<SDValue, 12> MemOpChains;
1140 
1141  // Walk the register/memloc assignments, inserting copies/loads.
1142  for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1143  CCValAssign &VA = ArgLocs[i];
1144  SDValue Arg = OutVals[i];
1145 
1146  // Promote the value if needed.
1147  switch (VA.getLocInfo()) {
1148  default: llvm_unreachable("Unknown loc info!");
1149  case CCValAssign::Full: break;
1150  case CCValAssign::SExt:
1151  Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1152  break;
1153  case CCValAssign::ZExt:
1154  Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1155  break;
1156  case CCValAssign::AExt:
1157  Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1158  break;
1159  }
1160 
1161  // Arguments that can be passed on register must be kept at
1162  // RegsToPass vector
1163  if (VA.isRegLoc()) {
1164  RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1165  } else {
1166  assert(VA.isMemLoc());
1167 
1168  int Offset = VA.getLocMemOffset();
1169 
1170  MemOpChains.push_back(DAG.getNode(XCoreISD::STWSP, dl, MVT::Other,
1171  Chain, Arg,
1172  DAG.getConstant(Offset/4, dl,
1173  MVT::i32)));
1174  }
1175  }
1176 
1177  // Transform all store nodes into one single node because
1178  // all store nodes are independent of each other.
1179  if (!MemOpChains.empty())
1180  Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
1181 
1182  // Build a sequence of copy-to-reg nodes chained together with token
1183  // chain and flag operands which copy the outgoing args into registers.
1184  // The InFlag in necessary since all emitted instructions must be
1185  // stuck together.
1186  SDValue InFlag;
1187  for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1188  Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1189  RegsToPass[i].second, InFlag);
1190  InFlag = Chain.getValue(1);
1191  }
1192 
1193  // If the callee is a GlobalAddress node (quite common, every direct call is)
1194  // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1195  // Likewise ExternalSymbol -> TargetExternalSymbol.
1196  if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1197  Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i32);
1198  else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
1199  Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
1200 
1201  // XCoreBranchLink = #chain, #target_address, #opt_in_flags...
1202  // = Chain, Callee, Reg#1, Reg#2, ...
1203  //
1204  // Returns a chain & a flag for retval copy to use.
1205  SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1207  Ops.push_back(Chain);
1208  Ops.push_back(Callee);
1209 
1210  // Add argument registers to the end of the list so that they are
1211  // known live into the call.
1212  for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1213  Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1214  RegsToPass[i].second.getValueType()));
1215 
1216  if (InFlag.getNode())
1217  Ops.push_back(InFlag);
1218 
1219  Chain = DAG.getNode(XCoreISD::BL, dl, NodeTys, Ops);
1220  InFlag = Chain.getValue(1);
1221 
1222  // Create the CALLSEQ_END node.
1223  Chain = DAG.getCALLSEQ_END(Chain, DAG.getConstant(NumBytes, dl, PtrVT, true),
1224  DAG.getConstant(0, dl, PtrVT, true), InFlag, dl);
1225  InFlag = Chain.getValue(1);
1226 
1227  // Handle result values, copying them out of physregs into vregs that we
1228  // return.
1229  return LowerCallResult(Chain, InFlag, RVLocs, dl, DAG, InVals);
1230 }
1231 
1232 //===----------------------------------------------------------------------===//
1233 // Formal Arguments Calling Convention Implementation
1234 //===----------------------------------------------------------------------===//
1235 
1236 namespace {
1237  struct ArgDataPair { SDValue SDV; ISD::ArgFlagsTy Flags; };
1238 }
1239 
1240 /// XCore formal arguments implementation
1241 SDValue XCoreTargetLowering::LowerFormalArguments(
1242  SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
1243  const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
1244  SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
1245  switch (CallConv)
1246  {
1247  default:
1248  report_fatal_error("Unsupported calling convention");
1249  case CallingConv::C:
1250  case CallingConv::Fast:
1251  return LowerCCCArguments(Chain, CallConv, isVarArg,
1252  Ins, dl, DAG, InVals);
1253  }
1254 }
1255 
1256 /// LowerCCCArguments - transform physical registers into
1257 /// virtual registers and generate load operations for
1258 /// arguments places on the stack.
1259 /// TODO: sret
1260 SDValue XCoreTargetLowering::LowerCCCArguments(
1261  SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
1262  const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
1263  SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
1264  MachineFunction &MF = DAG.getMachineFunction();
1265  MachineFrameInfo &MFI = MF.getFrameInfo();
1266  MachineRegisterInfo &RegInfo = MF.getRegInfo();
1268 
1269  // Assign locations to all of the incoming arguments.
1271  CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1272  *DAG.getContext());
1273 
1274  CCInfo.AnalyzeFormalArguments(Ins, CC_XCore);
1275 
1276  unsigned StackSlotSize = XCoreFrameLowering::stackSlotSize();
1277 
1278  unsigned LRSaveSize = StackSlotSize;
1279 
1280  if (!isVarArg)
1281  XFI->setReturnStackOffset(CCInfo.getNextStackOffset() + LRSaveSize);
1282 
1283  // All getCopyFromReg ops must precede any getMemcpys to prevent the
1284  // scheduler clobbering a register before it has been copied.
1285  // The stages are:
1286  // 1. CopyFromReg (and load) arg & vararg registers.
1287  // 2. Chain CopyFromReg nodes into a TokenFactor.
1288  // 3. Memcpy 'byVal' args & push final InVals.
1289  // 4. Chain mem ops nodes into a TokenFactor.
1290  SmallVector<SDValue, 4> CFRegNode;
1292  SmallVector<SDValue, 4> MemOps;
1293 
1294  // 1a. CopyFromReg (and load) arg registers.
1295  for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1296 
1297  CCValAssign &VA = ArgLocs[i];
1298  SDValue ArgIn;
1299 
1300  if (VA.isRegLoc()) {
1301  // Arguments passed in registers
1302  EVT RegVT = VA.getLocVT();
1303  switch (RegVT.getSimpleVT().SimpleTy) {
1304  default:
1305  {
1306 #ifndef NDEBUG
1307  errs() << "LowerFormalArguments Unhandled argument type: "
1308  << RegVT.getEVTString() << "\n";
1309 #endif
1310  llvm_unreachable(nullptr);
1311  }
1312  case MVT::i32:
1313  Register VReg = RegInfo.createVirtualRegister(&XCore::GRRegsRegClass);
1314  RegInfo.addLiveIn(VA.getLocReg(), VReg);
1315  ArgIn = DAG.getCopyFromReg(Chain, dl, VReg, RegVT);
1316  CFRegNode.push_back(ArgIn.getValue(ArgIn->getNumValues() - 1));
1317  }
1318  } else {
1319  // sanity check
1320  assert(VA.isMemLoc());
1321  // Load the argument to a virtual register
1322  unsigned ObjSize = VA.getLocVT().getSizeInBits()/8;
1323  if (ObjSize > StackSlotSize) {
1324  errs() << "LowerFormalArguments Unhandled argument type: "
1325  << EVT(VA.getLocVT()).getEVTString()
1326  << "\n";
1327  }
1328  // Create the frame index object for this incoming parameter...
1329  int FI = MFI.CreateFixedObject(ObjSize,
1330  LRSaveSize + VA.getLocMemOffset(),
1331  true);
1332 
1333  // Create the SelectionDAG nodes corresponding to a load
1334  //from this parameter
1335  SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
1336  ArgIn = DAG.getLoad(VA.getLocVT(), dl, Chain, FIN,
1338  }
1339  const ArgDataPair ADP = { ArgIn, Ins[i].Flags };
1340  ArgData.push_back(ADP);
1341  }
1342 
1343  // 1b. CopyFromReg vararg registers.
1344  if (isVarArg) {
1345  // Argument registers
1346  static const MCPhysReg ArgRegs[] = {
1347  XCore::R0, XCore::R1, XCore::R2, XCore::R3
1348  };
1350  unsigned FirstVAReg = CCInfo.getFirstUnallocated(ArgRegs);
1351  if (FirstVAReg < array_lengthof(ArgRegs)) {
1352  int offset = 0;
1353  // Save remaining registers, storing higher register numbers at a higher
1354  // address
1355  for (int i = array_lengthof(ArgRegs) - 1; i >= (int)FirstVAReg; --i) {
1356  // Create a stack slot
1357  int FI = MFI.CreateFixedObject(4, offset, true);
1358  if (i == (int)FirstVAReg) {
1359  XFI->setVarArgsFrameIndex(FI);
1360  }
1361  offset -= StackSlotSize;
1362  SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
1363  // Move argument from phys reg -> virt reg
1364  Register VReg = RegInfo.createVirtualRegister(&XCore::GRRegsRegClass);
1365  RegInfo.addLiveIn(ArgRegs[i], VReg);
1366  SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
1367  CFRegNode.push_back(Val.getValue(Val->getNumValues() - 1));
1368  // Move argument from virt reg -> stack
1369  SDValue Store =
1370  DAG.getStore(Val.getValue(1), dl, Val, FIN, MachinePointerInfo());
1371  MemOps.push_back(Store);
1372  }
1373  } else {
1374  // This will point to the next argument passed via stack.
1375  XFI->setVarArgsFrameIndex(
1376  MFI.CreateFixedObject(4, LRSaveSize + CCInfo.getNextStackOffset(),
1377  true));
1378  }
1379  }
1380 
1381  // 2. chain CopyFromReg nodes into a TokenFactor.
1382  if (!CFRegNode.empty())
1383  Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, CFRegNode);
1384 
1385  // 3. Memcpy 'byVal' args & push final InVals.
1386  // Aggregates passed "byVal" need to be copied by the callee.
1387  // The callee will use a pointer to this copy, rather than the original
1388  // pointer.
1389  for (SmallVectorImpl<ArgDataPair>::const_iterator ArgDI = ArgData.begin(),
1390  ArgDE = ArgData.end();
1391  ArgDI != ArgDE; ++ArgDI) {
1392  if (ArgDI->Flags.isByVal() && ArgDI->Flags.getByValSize()) {
1393  unsigned Size = ArgDI->Flags.getByValSize();
1394  unsigned Align = std::max(StackSlotSize, ArgDI->Flags.getByValAlign());
1395  // Create a new object on the stack and copy the pointee into it.
1396  int FI = MFI.CreateStackObject(Size, Align, false);
1397  SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
1398  InVals.push_back(FIN);
1399  MemOps.push_back(DAG.getMemcpy(Chain, dl, FIN, ArgDI->SDV,
1400  DAG.getConstant(Size, dl, MVT::i32),
1401  Align, false, false, false,
1403  MachinePointerInfo()));
1404  } else {
1405  InVals.push_back(ArgDI->SDV);
1406  }
1407  }
1408 
1409  // 4, chain mem ops nodes into a TokenFactor.
1410  if (!MemOps.empty()) {
1411  MemOps.push_back(Chain);
1412  Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
1413  }
1414 
1415  return Chain;
1416 }
1417 
1418 //===----------------------------------------------------------------------===//
1419 // Return Value Calling Convention Implementation
1420 //===----------------------------------------------------------------------===//
1421 
1422 bool XCoreTargetLowering::
1423 CanLowerReturn(CallingConv::ID CallConv, MachineFunction &MF,
1424  bool isVarArg,
1425  const SmallVectorImpl<ISD::OutputArg> &Outs,
1426  LLVMContext &Context) const {
1428  CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
1429  if (!CCInfo.CheckReturn(Outs, RetCC_XCore))
1430  return false;
1431  if (CCInfo.getNextStackOffset() != 0 && isVarArg)
1432  return false;
1433  return true;
1434 }
1435 
1436 SDValue
1437 XCoreTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
1438  bool isVarArg,
1439  const SmallVectorImpl<ISD::OutputArg> &Outs,
1440  const SmallVectorImpl<SDValue> &OutVals,
1441  const SDLoc &dl, SelectionDAG &DAG) const {
1442 
1443  XCoreFunctionInfo *XFI =
1446 
1447  // CCValAssign - represent the assignment of
1448  // the return value to a location
1450 
1451  // CCState - Info about the registers and stack slot.
1452  CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1453  *DAG.getContext());
1454 
1455  // Analyze return values.
1456  if (!isVarArg)
1457  CCInfo.AllocateStack(XFI->getReturnStackOffset(), 4);
1458 
1459  CCInfo.AnalyzeReturn(Outs, RetCC_XCore);
1460 
1461  SDValue Flag;
1462  SmallVector<SDValue, 4> RetOps(1, Chain);
1463 
1464  // Return on XCore is always a "retsp 0"
1465  RetOps.push_back(DAG.getConstant(0, dl, MVT::i32));
1466 
1467  SmallVector<SDValue, 4> MemOpChains;
1468  // Handle return values that must be copied to memory.
1469  for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
1470  CCValAssign &VA = RVLocs[i];
1471  if (VA.isRegLoc())
1472  continue;
1473  assert(VA.isMemLoc());
1474  if (isVarArg) {
1475  report_fatal_error("Can't return value from vararg function in memory");
1476  }
1477 
1478  int Offset = VA.getLocMemOffset();
1479  unsigned ObjSize = VA.getLocVT().getSizeInBits() / 8;
1480  // Create the frame index object for the memory location.
1481  int FI = MFI.CreateFixedObject(ObjSize, Offset, false);
1482 
1483  // Create a SelectionDAG node corresponding to a store
1484  // to this memory location.
1485  SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
1486  MemOpChains.push_back(DAG.getStore(
1487  Chain, dl, OutVals[i], FIN,
1489  }
1490 
1491  // Transform all store nodes into one single node because
1492  // all stores are independent of each other.
1493  if (!MemOpChains.empty())
1494  Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
1495 
1496  // Now handle return values copied to registers.
1497  for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
1498  CCValAssign &VA = RVLocs[i];
1499  if (!VA.isRegLoc())
1500  continue;
1501  // Copy the result values into the output registers.
1502  Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
1503 
1504  // guarantee that all emitted copies are
1505  // stuck together, avoiding something bad
1506  Flag = Chain.getValue(1);
1507  RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
1508  }
1509 
1510  RetOps[0] = Chain; // Update chain.
1511 
1512  // Add the flag if we have it.
1513  if (Flag.getNode())
1514  RetOps.push_back(Flag);
1515 
1516  return DAG.getNode(XCoreISD::RETSP, dl, MVT::Other, RetOps);
1517 }
1518 
1519 //===----------------------------------------------------------------------===//
1520 // Other Lowering Code
1521 //===----------------------------------------------------------------------===//
1522 
1525  MachineBasicBlock *BB) const {
1526  const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1527  DebugLoc dl = MI.getDebugLoc();
1528  assert((MI.getOpcode() == XCore::SELECT_CC) &&
1529  "Unexpected instr type to insert");
1530 
1531  // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1532  // control-flow pattern. The incoming instruction knows the destination vreg
1533  // to set, the condition code register to branch on, the true/false values to
1534  // select between, and a branch opcode to use.
1535  const BasicBlock *LLVM_BB = BB->getBasicBlock();
1537 
1538  // thisMBB:
1539  // ...
1540  // TrueVal = ...
1541  // cmpTY ccX, r1, r2
1542  // bCC copy1MBB
1543  // fallthrough --> copy0MBB
1544  MachineBasicBlock *thisMBB = BB;
1545  MachineFunction *F = BB->getParent();
1546  MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1547  MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
1548  F->insert(It, copy0MBB);
1549  F->insert(It, sinkMBB);
1550 
1551  // Transfer the remainder of BB and its successor edges to sinkMBB.
1552  sinkMBB->splice(sinkMBB->begin(), BB,
1553  std::next(MachineBasicBlock::iterator(MI)), BB->end());
1554  sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
1555 
1556  // Next, add the true and fallthrough blocks as its successors.
1557  BB->addSuccessor(copy0MBB);
1558  BB->addSuccessor(sinkMBB);
1559 
1560  BuildMI(BB, dl, TII.get(XCore::BRFT_lru6))
1561  .addReg(MI.getOperand(1).getReg())
1562  .addMBB(sinkMBB);
1563 
1564  // copy0MBB:
1565  // %FalseValue = ...
1566  // # fallthrough to sinkMBB
1567  BB = copy0MBB;
1568 
1569  // Update machine-CFG edges
1570  BB->addSuccessor(sinkMBB);
1571 
1572  // sinkMBB:
1573  // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1574  // ...
1575  BB = sinkMBB;
1576  BuildMI(*BB, BB->begin(), dl, TII.get(XCore::PHI), MI.getOperand(0).getReg())
1577  .addReg(MI.getOperand(3).getReg())
1578  .addMBB(copy0MBB)
1579  .addReg(MI.getOperand(2).getReg())
1580  .addMBB(thisMBB);
1581 
1582  MI.eraseFromParent(); // The pseudo instruction is gone now.
1583  return BB;
1584 }
1585 
1586 //===----------------------------------------------------------------------===//
1587 // Target Optimization Hooks
1588 //===----------------------------------------------------------------------===//
1589 
1590 SDValue XCoreTargetLowering::PerformDAGCombine(SDNode *N,
1591  DAGCombinerInfo &DCI) const {
1592  SelectionDAG &DAG = DCI.DAG;
1593  SDLoc dl(N);
1594  switch (N->getOpcode()) {
1595  default: break;
1596  case ISD::INTRINSIC_VOID:
1597  switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
1598  case Intrinsic::xcore_outt:
1599  case Intrinsic::xcore_outct:
1600  case Intrinsic::xcore_chkct: {
1601  SDValue OutVal = N->getOperand(3);
1602  // These instructions ignore the high bits.
1603  if (OutVal.hasOneUse()) {
1604  unsigned BitWidth = OutVal.getValueSizeInBits();
1605  APInt DemandedMask = APInt::getLowBitsSet(BitWidth, 8);
1606  KnownBits Known;
1608  !DCI.isBeforeLegalizeOps());
1609  const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1610  if (TLI.ShrinkDemandedConstant(OutVal, DemandedMask, TLO) ||
1611  TLI.SimplifyDemandedBits(OutVal, DemandedMask, Known, TLO))
1612  DCI.CommitTargetLoweringOpt(TLO);
1613  }
1614  break;
1615  }
1616  case Intrinsic::xcore_setpt: {
1617  SDValue Time = N->getOperand(3);
1618  // This instruction ignores the high bits.
1619  if (Time.hasOneUse()) {
1620  unsigned BitWidth = Time.getValueSizeInBits();
1621  APInt DemandedMask = APInt::getLowBitsSet(BitWidth, 16);
1622  KnownBits Known;
1624  !DCI.isBeforeLegalizeOps());
1625  const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1626  if (TLI.ShrinkDemandedConstant(Time, DemandedMask, TLO) ||
1627  TLI.SimplifyDemandedBits(Time, DemandedMask, Known, TLO))
1628  DCI.CommitTargetLoweringOpt(TLO);
1629  }
1630  break;
1631  }
1632  }
1633  break;
1634  case XCoreISD::LADD: {
1635  SDValue N0 = N->getOperand(0);
1636  SDValue N1 = N->getOperand(1);
1637  SDValue N2 = N->getOperand(2);
1640  EVT VT = N0.getValueType();
1641 
1642  // canonicalize constant to RHS
1643  if (N0C && !N1C)
1644  return DAG.getNode(XCoreISD::LADD, dl, DAG.getVTList(VT, VT), N1, N0, N2);
1645 
1646  // fold (ladd 0, 0, x) -> 0, x & 1
1647  if (N0C && N0C->isNullValue() && N1C && N1C->isNullValue()) {
1648  SDValue Carry = DAG.getConstant(0, dl, VT);
1649  SDValue Result = DAG.getNode(ISD::AND, dl, VT, N2,
1650  DAG.getConstant(1, dl, VT));
1651  SDValue Ops[] = { Result, Carry };
1652  return DAG.getMergeValues(Ops, dl);
1653  }
1654 
1655  // fold (ladd x, 0, y) -> 0, add x, y iff carry is unused and y has only the
1656  // low bit set
1657  if (N1C && N1C->isNullValue() && N->hasNUsesOfValue(0, 1)) {
1658  APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
1659  VT.getSizeInBits() - 1);
1660  KnownBits Known = DAG.computeKnownBits(N2);
1661  if ((Known.Zero & Mask) == Mask) {
1662  SDValue Carry = DAG.getConstant(0, dl, VT);
1663  SDValue Result = DAG.getNode(ISD::ADD, dl, VT, N0, N2);
1664  SDValue Ops[] = { Result, Carry };
1665  return DAG.getMergeValues(Ops, dl);
1666  }
1667  }
1668  }
1669  break;
1670  case XCoreISD::LSUB: {
1671  SDValue N0 = N->getOperand(0);
1672  SDValue N1 = N->getOperand(1);
1673  SDValue N2 = N->getOperand(2);
1676  EVT VT = N0.getValueType();
1677 
1678  // fold (lsub 0, 0, x) -> x, -x iff x has only the low bit set
1679  if (N0C && N0C->isNullValue() && N1C && N1C->isNullValue()) {
1680  APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
1681  VT.getSizeInBits() - 1);
1682  KnownBits Known = DAG.computeKnownBits(N2);
1683  if ((Known.Zero & Mask) == Mask) {
1684  SDValue Borrow = N2;
1685  SDValue Result = DAG.getNode(ISD::SUB, dl, VT,
1686  DAG.getConstant(0, dl, VT), N2);
1687  SDValue Ops[] = { Result, Borrow };
1688  return DAG.getMergeValues(Ops, dl);
1689  }
1690  }
1691 
1692  // fold (lsub x, 0, y) -> 0, sub x, y iff borrow is unused and y has only the
1693  // low bit set
1694  if (N1C && N1C->isNullValue() && N->hasNUsesOfValue(0, 1)) {
1695  APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
1696  VT.getSizeInBits() - 1);
1697  KnownBits Known = DAG.computeKnownBits(N2);
1698  if ((Known.Zero & Mask) == Mask) {
1699  SDValue Borrow = DAG.getConstant(0, dl, VT);
1700  SDValue Result = DAG.getNode(ISD::SUB, dl, VT, N0, N2);
1701  SDValue Ops[] = { Result, Borrow };
1702  return DAG.getMergeValues(Ops, dl);
1703  }
1704  }
1705  }
1706  break;
1707  case XCoreISD::LMUL: {
1708  SDValue N0 = N->getOperand(0);
1709  SDValue N1 = N->getOperand(1);
1710  SDValue N2 = N->getOperand(2);
1711  SDValue N3 = N->getOperand(3);
1714  EVT VT = N0.getValueType();
1715  // Canonicalize multiplicative constant to RHS. If both multiplicative
1716  // operands are constant canonicalize smallest to RHS.
1717  if ((N0C && !N1C) ||
1718  (N0C && N1C && N0C->getZExtValue() < N1C->getZExtValue()))
1719  return DAG.getNode(XCoreISD::LMUL, dl, DAG.getVTList(VT, VT),
1720  N1, N0, N2, N3);
1721 
1722  // lmul(x, 0, a, b)
1723  if (N1C && N1C->isNullValue()) {
1724  // If the high result is unused fold to add(a, b)
1725  if (N->hasNUsesOfValue(0, 0)) {
1726  SDValue Lo = DAG.getNode(ISD::ADD, dl, VT, N2, N3);
1727  SDValue Ops[] = { Lo, Lo };
1728  return DAG.getMergeValues(Ops, dl);
1729  }
1730  // Otherwise fold to ladd(a, b, 0)
1731  SDValue Result =
1732  DAG.getNode(XCoreISD::LADD, dl, DAG.getVTList(VT, VT), N2, N3, N1);
1733  SDValue Carry(Result.getNode(), 1);
1734  SDValue Ops[] = { Carry, Result };
1735  return DAG.getMergeValues(Ops, dl);
1736  }
1737  }
1738  break;
1739  case ISD::ADD: {
1740  // Fold 32 bit expressions such as add(add(mul(x,y),a),b) ->
1741  // lmul(x, y, a, b). The high result of lmul will be ignored.
1742  // This is only profitable if the intermediate results are unused
1743  // elsewhere.
1744  SDValue Mul0, Mul1, Addend0, Addend1;
1745  if (N->getValueType(0) == MVT::i32 &&
1746  isADDADDMUL(SDValue(N, 0), Mul0, Mul1, Addend0, Addend1, true)) {
1747  SDValue Ignored = DAG.getNode(XCoreISD::LMUL, dl,
1748  DAG.getVTList(MVT::i32, MVT::i32), Mul0,
1749  Mul1, Addend0, Addend1);
1750  SDValue Result(Ignored.getNode(), 1);
1751  return Result;
1752  }
1753  APInt HighMask = APInt::getHighBitsSet(64, 32);
1754  // Fold 64 bit expression such as add(add(mul(x,y),a),b) ->
1755  // lmul(x, y, a, b) if all operands are zero-extended. We do this
1756  // before type legalization as it is messy to match the operands after
1757  // that.
1758  if (N->getValueType(0) == MVT::i64 &&
1759  isADDADDMUL(SDValue(N, 0), Mul0, Mul1, Addend0, Addend1, false) &&
1760  DAG.MaskedValueIsZero(Mul0, HighMask) &&
1761  DAG.MaskedValueIsZero(Mul1, HighMask) &&
1762  DAG.MaskedValueIsZero(Addend0, HighMask) &&
1763  DAG.MaskedValueIsZero(Addend1, HighMask)) {
1764  SDValue Mul0L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
1765  Mul0, DAG.getConstant(0, dl, MVT::i32));
1766  SDValue Mul1L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
1767  Mul1, DAG.getConstant(0, dl, MVT::i32));
1768  SDValue Addend0L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
1769  Addend0, DAG.getConstant(0, dl, MVT::i32));
1770  SDValue Addend1L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
1771  Addend1, DAG.getConstant(0, dl, MVT::i32));
1772  SDValue Hi = DAG.getNode(XCoreISD::LMUL, dl,
1773  DAG.getVTList(MVT::i32, MVT::i32), Mul0L, Mul1L,
1774  Addend0L, Addend1L);
1775  SDValue Lo(Hi.getNode(), 1);
1776  return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
1777  }
1778  }
1779  break;
1780  case ISD::STORE: {
1781  // Replace unaligned store of unaligned load with memmove.
1782  StoreSDNode *ST = cast<StoreSDNode>(N);
1783  if (!DCI.isBeforeLegalize() ||
1785  ST->getMemoryVT(),
1786  *ST->getMemOperand()) ||
1787  ST->isVolatile() || ST->isIndexed()) {
1788  break;
1789  }
1790  SDValue Chain = ST->getChain();
1791 
1792  unsigned StoreBits = ST->getMemoryVT().getStoreSizeInBits();
1793  assert((StoreBits % 8) == 0 &&
1794  "Store size in bits must be a multiple of 8");
1795  unsigned Alignment = ST->getAlignment();
1796 
1797  if (LoadSDNode *LD = dyn_cast<LoadSDNode>(ST->getValue())) {
1798  if (LD->hasNUsesOfValue(1, 0) && ST->getMemoryVT() == LD->getMemoryVT() &&
1799  LD->getAlignment() == Alignment &&
1800  !LD->isVolatile() && !LD->isIndexed() &&
1802  bool isTail = isInTailCallPosition(DAG, ST, Chain);
1803  return DAG.getMemmove(Chain, dl, ST->getBasePtr(),
1804  LD->getBasePtr(),
1805  DAG.getConstant(StoreBits/8, dl, MVT::i32),
1806  Alignment, false, isTail, ST->getPointerInfo(),
1807  LD->getPointerInfo());
1808  }
1809  }
1810  break;
1811  }
1812  }
1813  return SDValue();
1814 }
1815 
1816 void XCoreTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
1817  KnownBits &Known,
1818  const APInt &DemandedElts,
1819  const SelectionDAG &DAG,
1820  unsigned Depth) const {
1821  Known.resetAll();
1822  switch (Op.getOpcode()) {
1823  default: break;
1824  case XCoreISD::LADD:
1825  case XCoreISD::LSUB:
1826  if (Op.getResNo() == 1) {
1827  // Top bits of carry / borrow are clear.
1828  Known.Zero = APInt::getHighBitsSet(Known.getBitWidth(),
1829  Known.getBitWidth() - 1);
1830  }
1831  break;
1833  {
1834  unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1835  switch (IntNo) {
1836  case Intrinsic::xcore_getts:
1837  // High bits are known to be zero.
1838  Known.Zero = APInt::getHighBitsSet(Known.getBitWidth(),
1839  Known.getBitWidth() - 16);
1840  break;
1841  case Intrinsic::xcore_int:
1842  case Intrinsic::xcore_inct:
1843  // High bits are known to be zero.
1844  Known.Zero = APInt::getHighBitsSet(Known.getBitWidth(),
1845  Known.getBitWidth() - 8);
1846  break;
1847  case Intrinsic::xcore_testct:
1848  // Result is either 0 or 1.
1849  Known.Zero = APInt::getHighBitsSet(Known.getBitWidth(),
1850  Known.getBitWidth() - 1);
1851  break;
1852  case Intrinsic::xcore_testwct:
1853  // Result is in the range 0 - 4.
1854  Known.Zero = APInt::getHighBitsSet(Known.getBitWidth(),
1855  Known.getBitWidth() - 3);
1856  break;
1857  }
1858  }
1859  break;
1860  }
1861 }
1862 
1863 //===----------------------------------------------------------------------===//
1864 // Addressing mode description hooks
1865 //===----------------------------------------------------------------------===//
1866 
1867 static inline bool isImmUs(int64_t val)
1868 {
1869  return (val >= 0 && val <= 11);
1870 }
1871 
1872 static inline bool isImmUs2(int64_t val)
1873 {
1874  return (val%2 == 0 && isImmUs(val/2));
1875 }
1876 
1877 static inline bool isImmUs4(int64_t val)
1878 {
1879  return (val%4 == 0 && isImmUs(val/4));
1880 }
1881 
1882 /// isLegalAddressingMode - Return true if the addressing mode represented
1883 /// by AM is legal for this target, for a load/store of the specified type.
1885  const AddrMode &AM, Type *Ty,
1886  unsigned AS,
1887  Instruction *I) const {
1888  if (Ty->getTypeID() == Type::VoidTyID)
1889  return AM.Scale == 0 && isImmUs(AM.BaseOffs) && isImmUs4(AM.BaseOffs);
1890 
1891  unsigned Size = DL.getTypeAllocSize(Ty);
1892  if (AM.BaseGV) {
1893  return Size >= 4 && !AM.HasBaseReg && AM.Scale == 0 &&
1894  AM.BaseOffs%4 == 0;
1895  }
1896 
1897  switch (Size) {
1898  case 1:
1899  // reg + imm
1900  if (AM.Scale == 0) {
1901  return isImmUs(AM.BaseOffs);
1902  }
1903  // reg + reg
1904  return AM.Scale == 1 && AM.BaseOffs == 0;
1905  case 2:
1906  case 3:
1907  // reg + imm
1908  if (AM.Scale == 0) {
1909  return isImmUs2(AM.BaseOffs);
1910  }
1911  // reg + reg<<1
1912  return AM.Scale == 2 && AM.BaseOffs == 0;
1913  default:
1914  // reg + imm
1915  if (AM.Scale == 0) {
1916  return isImmUs4(AM.BaseOffs);
1917  }
1918  // reg + reg<<2
1919  return AM.Scale == 4 && AM.BaseOffs == 0;
1920  }
1921 }
1922 
1923 //===----------------------------------------------------------------------===//
1924 // XCore Inline Assembly Support
1925 //===----------------------------------------------------------------------===//
1926 
1927 std::pair<unsigned, const TargetRegisterClass *>
1928 XCoreTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
1929  StringRef Constraint,
1930  MVT VT) const {
1931  if (Constraint.size() == 1) {
1932  switch (Constraint[0]) {
1933  default : break;
1934  case 'r':
1935  return std::make_pair(0U, &XCore::GRRegsRegClass);
1936  }
1937  }
1938  // Use the default implementation in TargetLowering to convert the register
1939  // constraint into a member of a register class.
1940  return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
1941 }
SDValue getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, unsigned Alignment=0, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
Helper function to build ISD::STORE nodes.
bool isMachineConstantPoolEntry() const
ADJUST_TRAMPOLINE - This corresponds to the adjust_trampoline intrinsic.
Definition: ISDOpcodes.h:825
int createLRSpillSlot(MachineFunction &MF)
static SDValue LowerCallResult(SDValue Chain, SDValue InFlag, const SmallVectorImpl< CCValAssign > &RVLocs, const SDLoc &dl, SelectionDAG &DAG, SmallVectorImpl< SDValue > &InVals)
LowerCallResult - Lower the result values of a call into the appropriate copies out of appropriate ph...
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
EVT getValueType() const
Return the ValueType of the referenced return value.
This represents an addressing mode of: BaseGV + BaseOffs + BaseReg + Scale*ScaleReg If BaseGV is null...
void setLoadExtAction(unsigned ExtType, MVT ValVT, MVT MemVT, LegalizeAction Action)
Indicate that the specified load with extension does not work with the specified type and indicate wh...
static bool isImmUs2(int64_t val)
const std::vector< MachineJumpTableEntry > & getJumpTables() const
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
bool hasLocalLinkage() const
Definition: GlobalValue.h:445
const GlobalValue * getGlobal() const
EXTRACT_ELEMENT - This is used to get the lower or upper (determined by a Constant, which is required to be operand #1) half of the integer or float value specified as operand #0.
Definition: ISDOpcodes.h:185
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
LLVMContext & Context
typename SuperClass::const_iterator const_iterator
Definition: SmallVector.h:320
bool isIndexed() const
Return true if this is a pre/post inc/dec load/store.
SDValue getCALLSEQ_END(SDValue Chain, SDValue Op1, SDValue Op2, SDValue InGlue, const SDLoc &DL)
Return a new CALLSEQ_END node, which always must have a glue result (to ensure it&#39;s not CSE&#39;d)...
Definition: SelectionDAG.h:895
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
BR_CC - Conditional branch.
Definition: ISDOpcodes.h:708
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Register getLocReg() const
static Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant *> IdxList, bool InBounds=false, Optional< unsigned > InRangeIndex=None, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
Definition: Constants.h:1154
bool isSized(SmallPtrSetImpl< Type *> *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:265
XCoreTargetLowering(const TargetMachine &TM, const XCoreSubtarget &Subtarget)
const SDValue & getVal() const
Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
bool isLegalAddressingMode(const DataLayout &DL, const AddrMode &AM, Type *Ty, unsigned AS, Instruction *I=nullptr) const override
isLegalAddressingMode - Return true if the addressing mode represented by AM is legal for this target...
void addLiveIn(unsigned Reg, unsigned vreg=0)
addLiveIn - Add the specified register as a live-in.
const SDValue & getBasePtr() const
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:277
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Definition: MachineInstr.h:400
void AnalyzeFormalArguments(const SmallVectorImpl< ISD::InputArg > &Ins, CCAssignFn Fn)
AnalyzeFormalArguments - Analyze an array of argument values, incorporating info about the formals in...
TypeSize getSizeInBits() const
Returns the size of the specified MVT in bits.
const SDValue & getValue() const
void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB)
Transfers all the successors, as in transferSuccessors, and update PHI operands in the successor bloc...
STACKRESTORE has two operands, an input chain and a pointer to restore to it returns an output chain...
Definition: ISDOpcodes.h:760
SDValue getTargetExternalSymbol(const char *Sym, EVT VT, unsigned TargetFlags=0)
SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override
LowerOperation - Provide custom lowering hooks for some operations.
EK_Inline - Jump table entries are emitted inline at their point of use.
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Get a value with low bits set.
Definition: APInt.h:664
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition: ValueTypes.h:255
AAMDNodes getAAInfo() const
Returns the AA info that describes the dereference.
const SDValue & getBasePtr() const
const SDValue & getChain() const
Function Alias Analysis Results
bool isZExtFree(SDValue Val, EVT VT2) const override
Return true if zero-extending the specific node Val to type VT2 is free (either because it&#39;s implicit...
unsigned getAlignment() const
virtual std::pair< unsigned, const TargetRegisterClass * > getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const
Given a physical register constraint (e.g.
unsigned second
void setBooleanVectorContents(BooleanContent Ty)
Specify how the target extends the result of a vector boolean value from a vector of i1 to a wider ty...
C - The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
unsigned const TargetRegisterInfo * TRI
A debug info location.
Definition: DebugLoc.h:33
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition: ValueTypes.h:141
F(f)
CallLoweringInfo & setDebugLoc(const SDLoc &dl)
#define R2(n)
SDNode * getNode() const
get the SDNode which holds the desired result
SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
SDValue getConstantPool(const Constant *C, EVT VT, unsigned Align=0, int Offs=0, bool isT=false, unsigned TargetFlags=0)
MachineMemOperand * getMemOperand() const
Return a MachineMemOperand object describing the memory reference performed by operation.
unsigned getBitWidth() const
Get the bit width of this value.
Definition: KnownBits.h:39
uint64_t High
OUTCHAIN = ATOMIC_FENCE(INCHAIN, ordering, scope) This corresponds to the fence instruction.
Definition: ISDOpcodes.h:842
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition: ISDOpcodes.h:496
bool hasOneUse() const
Return true if there is exactly one node using value ResNo of Node.
SDValue getExternalSymbol(const char *Sym, EVT VT)
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition: ISDOpcodes.h:160
bool isTruncatingStore() const
Return true if the op does a truncation before store.
bool isMemLoc() const
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition: KnownBits.h:161
static bool isImmUs(int64_t val)
A convenience struct that encapsulates a DAG, and two SDValues for returning information from TargetL...
static int stackSlotSize()
Stack slot size (4 bytes)
SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded integer shift operations...
Definition: ISDOpcodes.h:517
SDValue getIntPtrConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
The address of a basic block.
Definition: Constants.h:840
bool hasSection() const
Definition: GlobalValue.h:273
virtual bool isGAPlusOffset(SDNode *N, const GlobalValue *&GA, int64_t &Offset) const
Returns true (and the GlobalValue and the offset) if the node is a GlobalAddress + offset...
static bool IsSmallObject(const GlobalValue *GV, const XCoreTargetLowering &XTL)
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:369
const HexagonInstrInfo * TII
TypeID getTypeID() const
Return the type id for the type.
Definition: Type.h:138
Shift and rotation operations.
Definition: ISDOpcodes.h:471
CallLoweringInfo & setChain(SDValue InChain)
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition: ISDOpcodes.h:192
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
void eraseFromParent()
Unlink &#39;this&#39; from the containing basic block and delete it.
ISD::LoadExtType getExtensionType() const
Return whether this is a plain node, or one of the varieties of value-extending loads.
SDValue getTargetJumpTable(int JTI, EVT VT, unsigned TargetFlags=0)
Definition: SelectionDAG.h:660
SimpleValueType SimpleTy
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted...
static const unsigned CodeModelLargeSize
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:426
SDValue getEntryNode() const
Return the token chain corresponding to the entry of the function.
Definition: SelectionDAG.h:485
void setOperationAction(unsigned Op, MVT VT, LegalizeAction Action)
Indicate that the specified operation does not work with the specified type and indicate what to do a...
TypeSize getValueSizeInBits() const
Returns the size of the value in bits.
const DataLayout & getDataLayout() const
Definition: SelectionDAG.h:423
SDVTList getVTList(EVT VT)
Return an SDVTList that represents the list of values specified.
FRAMEADDR, RETURNADDR - These nodes represent llvm.frameaddress and llvm.returnaddress on the DAG...
Definition: ISDOpcodes.h:74
This is an SDNode representing atomic operations.
LocInfo getLocInfo() const
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
AtomicOrdering getOrdering() const
Return the atomic ordering requirements for this memory operation.
This represents a list of ValueType&#39;s that has been intern&#39;d by a SelectionDAG.
SDValue getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, EVT MemVT, unsigned Alignment=0, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
SmallVector< ISD::InputArg, 32 > Ins
STACKSAVE - STACKSAVE has one operand, an input chain.
Definition: ISDOpcodes.h:756
FRAME_TO_ARGS_OFFSET - This node represents offset from frame pointer to first (possible) on-stack ar...
Definition: ISDOpcodes.h:92
SDValue getMergeValues(ArrayRef< SDValue > Ops, const SDLoc &dl)
Create a MERGE_VALUES node from the given operands.
void ReplaceNodeResults(SDNode *N, SmallVectorImpl< SDValue > &Results, SelectionDAG &DAG) const override
ReplaceNodeResults - Replace the results of node with an illegal result type with new values built ou...
unsigned getNextStackOffset() const
getNextStackOffset - Return the next stack offset such that all stack slots satisfy their alignment r...
void setReturnStackOffset(unsigned value)
MachineFunction & getMachineFunction() const
Definition: SelectionDAG.h:420
void computeRegisterProperties(const TargetRegisterInfo *TRI)
Once all of the register classes are added, this allows us to compute derived properties we expose...
void setMinFunctionAlignment(Align Alignment)
Set the target&#39;s minimum function alignment.
Simple integer binary arithmetic operators.
Definition: ISDOpcodes.h:202
SmallVector< ISD::OutputArg, 32 > Outs
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:151
unsigned getAlignment() const
Definition: Globals.cpp:97
MachineBasicBlock * EmitInstrWithCustomInserter(MachineInstr &MI, MachineBasicBlock *MBB) const override
This method should be implemented by targets that mark instructions with the &#39;usesCustomInserter&#39; fla...
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition: ISDOpcodes.h:152
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *bb=nullptr)
CreateMachineBasicBlock - Allocate a new MachineBasicBlock.
This class is used to represent ISD::STORE nodes.
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition: MCRegister.h:19
void setPrefFunctionAlignment(Align Alignment)
Set the target&#39;s preferred function alignment.
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:139
TargetInstrInfo - Interface to description of machine instruction set.
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Get a value with high bits set.
Definition: APInt.h:652
unsigned ComputeNumSignBits(SDValue Op, unsigned Depth=0) const
Return the number of times the sign bit of the register is replicated into the other bits...
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:1872
The memory access is volatile.
constexpr uint64_t MinAlign(uint64_t A, uint64_t B)
A and B are either alignments or offsets.
Definition: MathExtras.h:661
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space...
Definition: DataLayout.cpp:769
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
0: type with no size
Definition: Type.h:57
const SDValue & getBasePtr() const
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition: ISDOpcodes.h:167
Machine Value Type.
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:63
void addRegisterClass(MVT VT, const TargetRegisterClass *RC)
Add the specified register class as an available regclass for the specified value type...
void setTargetDAGCombine(ISD::NodeType NT)
Targets should invoke this method for each target independent node that they want to provide a custom...
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
Definition: Constant.h:41
void resetAll()
Resets the known state of all bits.
Definition: KnownBits.h:65
VAEND, VASTART - VAEND and VASTART have three operands: an input chain, pointer, and a SRCVALUE...
Definition: ISDOpcodes.h:789
const SDValue & getOperand(unsigned Num) const
This file contains the declarations for the subclasses of Constant, which represent the different fla...
TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:486
SDValue getCALLSEQ_START(SDValue Chain, uint64_t InSize, uint64_t OutSize, const SDLoc &DL)
Return a new CALLSEQ_START node, that starts new call frame, in which InSize bytes are set up inside ...
Definition: SelectionDAG.h:883
void setBooleanContents(BooleanContent Ty)
Specify how the target extends the result of integer and floating point boolean values from i1 to a w...
static Type * getVoidTy(LLVMContext &C)
Definition: Type.cpp:165
INIT_TRAMPOLINE - This corresponds to the init_trampoline intrinsic.
Definition: ISDOpcodes.h:819
bool isBaseWithConstantOffset(SDValue Op) const
Return true if the specified operand is an ISD::ADD with a ConstantSDNode on the right-hand side...
constexpr double e
Definition: MathExtras.h:57
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
TRAP - Trapping instruction.
Definition: ISDOpcodes.h:828
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
std::string getEVTString() const
This function returns value type as a string, e.g. "i32".
Definition: ValueTypes.cpp:115
self_iterator getIterator()
Definition: ilist_node.h:81
bool hasNUsesOfValue(unsigned NUses, unsigned Value) const
Return true if there are exactly NUSES uses of the indicated value.
VAARG - VAARG has four operands: an input chain, a pointer, a SRCVALUE, and the alignment.
Definition: ISDOpcodes.h:780
unsigned MaxStoresPerMemmove
Specify maximum number of store instructions per memmove call.
XCoreFunctionInfo - This class is derived from MachineFunction private XCore target-specific informat...
virtual MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
std::vector< ArgListEntry > ArgListTy
Extended Value Type.
Definition: ValueTypes.h:34
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition: ValueTypes.h:298
This structure contains all information that is necessary for lowering calls.
size_t size() const
Definition: SmallVector.h:52
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:224
bool isVolatile() const
const TargetMachine & getTargetMachine() const
StringRef getSection() const
Definition: Globals.cpp:165
This class contains a discriminated union of information about pointers in memory operands...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:40
SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, unsigned Alignment=0, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr)
Loads are not normal binary operators: their result type is not determined by their operands...
SDValue getTargetBlockAddress(const BlockAddress *BA, EVT VT, int64_t Offset=0, unsigned TargetFlags=0)
Definition: SelectionDAG.h:698
unsigned first
bool allowsMemoryAccessForAlignment(LLVMContext &Context, const DataLayout &DL, EVT VT, unsigned AddrSpace=0, unsigned Alignment=1, MachineMemOperand::Flags Flags=MachineMemOperand::MONone, bool *Fast=nullptr) const
This function returns true if the memory access is aligned or if the target allows this specific unal...
TokenFactor - This node takes multiple tokens as input and produces a single token result...
Definition: ISDOpcodes.h:51
const TargetLowering & getTargetLoweringInfo() const
Definition: SelectionDAG.h:426
SDValue getTargetConstantPool(const Constant *C, EVT VT, unsigned Align=0, int Offset=0, unsigned TargetFlags=0)
Definition: SelectionDAG.h:666
Iterator for intrusive lists based on ilist_node.
CCState - This class holds information needed while lowering arguments and return values...
SDValue getTargetGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT, int64_t offset=0, unsigned TargetFlags=0)
Definition: SelectionDAG.h:650
void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:390
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
Definition: SelectionDAG.h:223
CallLoweringInfo & setCallee(CallingConv::ID CC, Type *ResultType, SDValue Target, ArgListTy &&ArgsList)
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
virtual Register getFrameRegister(const MachineFunction &MF) const =0
Debug information queries.
CCValAssign - Represent assignment of one arg/retval to a location.
SDValue getMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, unsigned Align, bool isVol, bool AlwaysInline, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo)
constexpr size_t array_lengthof(T(&)[N])
Find the length of an array.
Definition: STLExtras.h:1027
const DataFlowGraph & G
Definition: RDFGraph.cpp:202
int CreateFixedObject(uint64_t Size, int64_t SPOffset, bool IsImmutable, bool isAliased=false)
Create a new object at a fixed location on the stack.
const Constant * getConstVal() const
SDValue getMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, unsigned Align, bool isVol, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo)
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:704
Represents one node in the SelectionDAG.
SDValue getCopyToReg(SDValue Chain, const SDLoc &dl, unsigned Reg, SDValue N)
Definition: SelectionDAG.h:703
static mvt_range integer_valuetypes()
MachinePointerInfo getWithOffset(int64_t O) const
EVT getMemoryVT() const
Return the type of the in-memory value.
Class for arbitrary precision integers.
Definition: APInt.h:69
CodeModel::Model getCodeModel() const
Returns the code model.
static bool isImmUs4(int64_t val)
amdgpu Simplify well known AMD library false FunctionCallee Callee
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition: ISDOpcodes.h:529
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition: ISDOpcodes.h:532
SDValue getGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT, int64_t offset=0, bool isTargetGA=false, unsigned TargetFlags=0)
Flags
Flags values. These may be or&#39;d together.
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
std::pair< SDValue, SDValue > LowerCallTo(CallLoweringInfo &CLI) const
This function lowers an abstract call to a function into an actual call.
static MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
BR_JT - Jumptable branch.
Definition: ISDOpcodes.h:696
Representation of each machine instruction.
Definition: MachineInstr.h:63
VACOPY - VACOPY has 5 operands: an input chain, a destination pointer, a source pointer, a SRCVALUE for the destination, and a SRCVALUE for the source.
Definition: ISDOpcodes.h:785
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
SmallVector< SDValue, 32 > OutVals
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:163
bool CheckReturn(const SmallVectorImpl< ISD::OutputArg > &Outs, CCAssignFn Fn)
CheckReturn - Analyze the return values of a function, returning true if the return can be performed ...
unsigned AllocateStack(unsigned Size, unsigned Alignment)
AllocateStack - Allocate a chunk of stack space with the specified size and alignment.
bool isFunctionTy() const
True if this is an instance of FunctionType.
Definition: Type.h:215
Bitwise operators - logical and, logical or, logical xor.
Definition: ISDOpcodes.h:448
const TargetRegisterInfo * getRegisterInfo() const override
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB &#39;Other&#39; at the position From, and insert it into this MBB right before &#39;...
SDValue getCopyFromReg(SDValue Chain, const SDLoc &dl, unsigned Reg, EVT VT)
Definition: SelectionDAG.h:729
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:180
unsigned getLocMemOffset() const
SMUL_LOHI/UMUL_LOHI - Multiply two integers of type iN, producing a signed/unsigned value of type i[2...
Definition: ISDOpcodes.h:207
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:55
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode...
Definition: MCInstrInfo.h:44
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
Definition: ISDOpcodes.h:672
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
Flags getFlags() const
Return the raw flags of the source value,.
unsigned getJumpTableEncoding() const override
Return the entry encoding for a jump table in the current function.
unsigned MaxStoresPerMemmoveOptSize
Likewise for functions with the OptSize attribute.
unsigned MaxStoresPerMemcpyOptSize
Likewise for functions with the OptSize attribute.
void setStackPointerRegisterToSaveRestore(unsigned R)
If set to a physical register, this specifies the register that llvm.savestack/llvm.restorestack should save and restore.
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
Type * getValueType() const
Definition: GlobalValue.h:279
uint32_t Size
Definition: Profile.cpp:46
const char * getTargetNodeName(unsigned Opcode) const override
getTargetNodeName - This method returns the name of a target specific
static bool isADDADDMUL(SDValue Op, SDValue &Mul0, SDValue &Mul1, SDValue &Addend0, SDValue &Addend1, bool requireIntermediatesHaveOneUse)
isADDADDMUL - Return whether Op is in a form that is equivalent to add(add(mul(x,y),a),b).
unsigned getOpcode() const
SDValue getValue(unsigned R) const
unsigned MaxStoresPerMemcpy
Specify maximum number of store instructions per memcpy call.
bool reachesChainWithoutSideEffects(SDValue Dest, unsigned Depth=2) const
Return true if this operand (which must be a chain) reaches the specified operand without crossing an...
SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
OUTCHAIN = EH_RETURN(INCHAIN, OFFSET, HANDLER) - This node represents &#39;eh_return&#39; gcc dwarf builtin...
Definition: ISDOpcodes.h:103
bool isRegLoc() const
const MachinePointerInfo & getPointerInfo() const
bool isInTailCallPosition(SelectionDAG &DAG, SDNode *Node, SDValue &Chain) const
Check whether a given call node is in tail position within its function.
bool MaskedValueIsZero(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if &#39;Op & Mask&#39; is known to be zero.
MachineConstantPoolValue * getMachineCPVal() const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
SDValue getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, EVT SVT, unsigned Alignment=0, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
void insert(iterator MBBI, MachineBasicBlock *MBB)
SDValue getFrameIndex(int FI, EVT VT, bool isTarget=false)
int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
Create a new statically sized stack object, returning a nonnegative identifier to represent it...
void setSchedulingPreference(Sched::Preference Pref)
Specify the target scheduling preference.
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:575
LLVM Value Representation.
Definition: Value.h:74
SDValue getRegister(unsigned Reg, EVT VT)
unsigned getResNo() const
get the index which selects a specific result in the SDNode
Fast - This calling convention attempts to make calls as fast as possible (e.g.
Definition: CallingConv.h:42
const MDNode * getRanges() const
Returns the Ranges that describes the dereference.
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E&#39;s largest value.
Definition: BitmaskEnum.h:80
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:65
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
unsigned MaxStoresPerMemset
Specify maximum number of store instructions per memset call.
unsigned MaxStoresPerMemsetOptSize
Likewise for functions with the OptSize attribute.
KnownBits computeKnownBits(SDValue Op, unsigned Depth=0) const
Determine which bits of Op are known to be either zero or one and return them in Known.
static bool isWordAligned(SDValue Value, SelectionDAG &DAG)
Register getReg() const
getReg - Returns the register number.
Conversion operators.
Definition: ISDOpcodes.h:526
const SDValue & getOperand(unsigned i) const
OUTCHAIN = ATOMIC_STORE(INCHAIN, ptr, val) This corresponds to "store atomic" instruction.
Definition: ISDOpcodes.h:850
uint64_t getZExtValue() const
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition: ValueTypes.h:126
TypeSize getStoreSizeInBits() const
Return the number of bits overwritten by a store of the specified value type.
Definition: ValueTypes.h:325
const MachineJumpTableInfo * getJumpTableInfo() const
getJumpTableInfo - Return the jump table info object for the current function.
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:431
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation...
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
static IntegerType * getInt8Ty(LLVMContext &C)
Definition: Type.cpp:178
Val, OUTCHAIN = ATOMIC_LOAD(INCHAIN, ptr) This corresponds to "load atomic" instruction.
Definition: ISDOpcodes.h:846
LLVMContext * getContext() const
Definition: SelectionDAG.h:430
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
CallLoweringInfo & setLibCallee(CallingConv::ID CC, Type *ResultType, SDValue Target, ArgListTy &&ArgsList)
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
void CommitTargetLoweringOpt(const TargetLoweringOpt &TLO)
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition: ISDOpcodes.h:441
This class is used to represent ISD::LOAD nodes.
DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned to a specified boundary...
Definition: ISDOpcodes.h:681