forked from Hoernchen/Epiphany
-
Notifications
You must be signed in to change notification settings - Fork 2
/
EpiphanyISelLowering.cpp
1716 lines (1486 loc) · 66.1 KB
/
EpiphanyISelLowering.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===-- EpiphanyISelLowering.cpp - Epiphany DAG Lowering Implementation -----===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines the interfaces that Epiphany uses to lower LLVM code into a
// selection DAG.
//
//===----------------------------------------------------------------------===//
#include "EpiphanyISelLowering.h"
#include "MCTargetDesc/EpiphanyBaseInfo.h"
#include "MCTargetDesc/EpiphanyAddressingModes.h"
#include "EpiphanyMachineFunction.h"
#include "EpiphanyTargetMachine.h"
#include "EpiphanyTargetObjectFile.h"
#include "EpiphanySubtarget.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/CodeGen/CallingConvLower.h"
#include "llvm/CodeGen/MachineFrameInfo.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/CodeGen/SelectionDAG.h"
#include "llvm/CodeGen/ValueTypes.h"
#include "llvm/IR/CallingConv.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetInstrInfo.h"
using namespace llvm;
#define DEBUG_TYPE "epiphany-lower"
static cl::opt<bool> EnableFastMath("ffast-math",
cl::Hidden, cl::ZeroOrMore, cl::init(false),
cl::desc("Enable Fast Math processing"));
const char *EpiphanyTargetLowering::getTargetNodeName(unsigned Opcode) const {
switch (Opcode) {
case EpiphanyISD::Call: return "EpiphanyISD::Call";
case EpiphanyISD::RTI: return "EpiphanyISD::RTI";
case EpiphanyISD::RTS: return "EpiphanyISD::RTS";
case EpiphanyISD::MOV: return "EpiphanyISD::MOV";
case EpiphanyISD::MOVT: return "EpiphanyISD::MOVT";
case EpiphanyISD::MOVCC: return "EpiphanyISD::MOVCC";
case EpiphanyISD::STORE: return "EpiphanyISD::STORE";
case EpiphanyISD::LOAD: return "EpiphanyISD::LOAD";
case EpiphanyISD::CMP: return "EpiphanyISD::CMP";
case EpiphanyISD::BRCC: return "EpiphanyISD::BRCC";
case EpiphanyISD::BRCC64: return "EpiphanyISD::BRCC64";
case EpiphanyISD::FIX: return "EpiphanyISD::FIX";
case EpiphanyISD::FLOAT: return "EpiphanyISD::FLOAT";
default: return NULL;
}
}
//@EpiphanyTargetLowering {
EpiphanyTargetLowering::EpiphanyTargetLowering(const EpiphanyTargetMachine &TM,
const EpiphanySubtarget &STI)
: TargetLowering(TM), Subtarget(STI), ABI(TM.getABI()) {
// Set up the register classes
addRegisterClass(MVT::i32, &Epiphany::GPR16RegClass);
addRegisterClass(MVT::i32, &Epiphany::GPR32RegClass);
// addRegisterClass(MVT::v2i16, &Epiphany::GPR32RegClass);
// addRegisterClass(MVT::v4i8, &Epiphany::GPR32RegClass);
addRegisterClass(MVT::f32, &Epiphany::FPR32RegClass);
addRegisterClass(MVT::i64, &Epiphany::GPR64RegClass);
// addRegisterClass(MVT::v2i32, &Epiphany::GPR64RegClass);
addRegisterClass(MVT::f64, &Epiphany::FPR64RegClass);
// Max atomic instruction size is 64 for load/store instruction
setMaxAtomicSizeInBitsSupported(64);
//- Set function alignment to 2 bytes
// It will emit .align 2 later
setMinFunctionAlignment(STI.stackAlignment());
// Set boolean to i32 for now (as we don't have i1)
setBooleanContents(ZeroOrOneBooleanContent);
setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
// must, computeRegisterProperties - Once all of the register classes are
// added, this allows us to compute derived properties we expose.
computeRegisterProperties(STI.getRegisterInfo());
// Provide all sorts of operation actions
setStackPointerRegisterToSaveRestore(Epiphany::SP);
// Provide ops that we don't have
setOperationAction(ISD::SDIV, MVT::i32, Expand);
setOperationAction(ISD::SREM, MVT::i32, Expand);
setOperationAction(ISD::UDIV, MVT::i32, Expand);
setOperationAction(ISD::UREM, MVT::i32, Expand);
setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
setOperationAction(ISD::MULHS, MVT::i32, Expand);
setOperationAction(ISD::MULHU, MVT::i32, Expand);
setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
// Legalize some vector stores and loads
// for (MVT VT : MVT::vector_valuetypes()) {
// ValueTypeActions.setTypeAction(VT, TypeScalarizeVector);
// }
// ValueTypeActions.setTypeAction(MVT::v4i8, TypeScalarizeVector);
// ValueTypeActions.setTypeAction(MVT::v2i16, TypeLegal);
// setOperationAction(ISD::LOAD, MVT::v2i16, Legal);
// setOperationAction(ISD::STORE, MVT::v2i16, Legal);
// ValueTypeActions.setTypeAction(MVT::v2i32, TypeLegal);
// setOperationAction(ISD::LOAD, MVT::v2i32, Legal);
// setOperationAction(ISD::STORE, MVT::v2i32, Legal);
for (MVT VT : MVT::fp_valuetypes()) {
setOperationAction(ISD::FDIV, VT, Expand);
setOperationAction(ISD::FSQRT, VT, Expand);
setOperationAction(ISD::FSIN, VT, Expand);
setOperationAction(ISD::FCOS, VT, Expand);
setOperationAction(ISD::FLOG, VT, Expand);
setOperationAction(ISD::FEXP, VT, Expand);
setOperationAction(ISD::FPOW, VT, Expand);
setOperationAction(ISD::FREM, VT, Expand);
}
// Allow pre/post inc stores and loads
for (MVT Ty : {MVT::i8, MVT::i16, MVT::i32, MVT::f32, MVT::i64, MVT::f64}) {
setIndexedLoadAction(ISD::PRE_DEC, Ty, Legal);
setIndexedLoadAction(ISD::PRE_INC, Ty, Legal);
setIndexedLoadAction(ISD::POST_DEC, Ty, Legal);
setIndexedLoadAction(ISD::POST_INC, Ty, Legal);
setIndexedStoreAction(ISD::PRE_DEC, Ty, Legal);
setIndexedStoreAction(ISD::PRE_INC, Ty, Legal);
setIndexedStoreAction(ISD::POST_DEC, Ty, Legal);
setIndexedStoreAction(ISD::POST_INC, Ty, Legal);
}
// Turn FP truncstore into trunc + store.
setTruncStoreAction(MVT::f64, MVT::f32, Expand);
setTruncStoreAction(MVT::f64, MVT::f16, Expand);
setTruncStoreAction(MVT::f32, MVT::f16, Expand);
setTruncStoreAction(MVT::i64, MVT::i32, Expand);
// Turn FP extload to ext + load
setLoadExtAction(ISD::EXTLOAD, MVT::i64, MVT::i32, Expand);
setLoadExtAction(ISD::ZEXTLOAD, MVT::i64, MVT::i32, Expand);
setLoadExtAction(ISD::SEXTLOAD, MVT::i64, MVT::i32, Expand);
setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f32, Expand);
setLoadExtAction(ISD::ZEXTLOAD, MVT::f64, MVT::f32, Expand);
setLoadExtAction(ISD::SEXTLOAD, MVT::f64, MVT::f32, Expand);
// For now - expand i64 ops that were not implemented yet
setOperationAction(ISD::MUL, MVT::i64, Expand);
setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
setOperationAction(ISD::SDIV, MVT::i64, Expand);
setOperationAction(ISD::SREM, MVT::i64, Expand);
setOperationAction(ISD::UDIV, MVT::i64, Expand);
setOperationAction(ISD::UREM, MVT::i64, Expand);
setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
// Same for f64
setOperationAction(ISD::FADD, MVT::f64, Expand);
setOperationAction(ISD::FSUB, MVT::f64, Expand);
setOperationAction(ISD::FMUL, MVT::f64, Expand);
setOperationAction(ISD::FDIV, MVT::f64, Expand);
setOperationAction(ISD::SELECT, MVT::f64, Expand);
setOperationAction(ISD::FP_ROUND, MVT::f64, Expand);
// Custom operations, see below
setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
setOperationAction(ISD::ExternalSymbol, MVT::i32, Custom);
setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
for (MVT Ty : {MVT::i32, MVT::f32, MVT::i64, MVT::f64}) {
setOperationAction(ISD::BR_CC, Ty, Custom);
setOperationAction(ISD::SETCC, Ty, Custom);
setOperationAction(ISD::SELECT, Ty, Custom);
}
setOperationAction(ISD::ADDE, MVT::i32, Custom);
setOperationAction(ISD::SUBE, MVT::i32, Custom);
setOperationAction(ISD::BRCOND, MVT::i32, Custom);
setOperationAction(ISD::BRCOND, MVT::Other, Custom);
setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
setOperationAction(ISD::FP_EXTEND, MVT::f64, Custom);
setOperationAction(ISD::FP_ROUND, MVT::f64, Custom);
setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
setOperationAction(ISD::ADD, MVT::i64, Custom);
setOperationAction(ISD::ADDC, MVT::i64, Custom);
setOperationAction(ISD::SUB, MVT::i64, Custom);
setOperationAction(ISD::SUBC, MVT::i64, Custom);
// setOperationAction(ISD::BUILD_VECTOR, MVT::v2i16, Custom);
// setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
// setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i16, Custom);
// setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i32, Custom);
// Just expand all custom versions, as they're getting on the nerves
for (MVT VT : MVT::all_valuetypes()) {
setOperationAction(ISD::FP_TO_UINT, VT, Custom);
setOperationAction(ISD::FP_TO_SINT, VT, Custom);
setOperationAction(ISD::UINT_TO_FP, VT, Custom);
setOperationAction(ISD::SINT_TO_FP, VT, Custom);
}
// Libraries for fast math
if (EnableFastMath) {
setLibcallName(RTLIB::DIV_F32, "__fast_recipsf2");
setOperationAction(ISD::FDIV, MVT::f32, Custom);
}
}
SDValue EpiphanyTargetLowering::LowerOperation(SDValue Op,
SelectionDAG &DAG) const {
switch (Op.getOpcode()) {
case ISD::GlobalAddress:
return LowerGlobalAddress(Op, DAG);
break;
case ISD::BlockAddress:
return LowerBlockAddress(Op, DAG);
break;
case ISD::ExternalSymbol:
return LowerExternalSymbol(Op, DAG);
break;
case ISD::ConstantPool:
return LowerConstantPool(Op, DAG);
break;
case ISD::GlobalTLSAddress:
return LowerGlobalTLSAddress(Op, DAG);
break;
case ISD::SELECT:
return LowerSelect(Op, DAG);
break;
case ISD::SELECT_CC:
return LowerSelectCC(Op, DAG);
break;
case ISD::SETCC:
return LowerSetCC(Op, DAG);
break;
case ISD::FP_EXTEND:
return LowerFpExtend(Op, DAG);
break;
case ISD::FP_ROUND:
return LowerFpRound(Op, DAG);
break;
case ISD::BR_CC:
return LowerBrCC(Op, DAG);
break;
case ISD::BRCOND:
return LowerBrCond(Op, DAG);
break;
case ISD::FDIV:
return LowerFastDiv(Op, DAG);
break;
case ISD::FP_TO_SINT:
case ISD::FP_TO_UINT:
return LowerFpToInt(Op, DAG);
break;
case ISD::UINT_TO_FP:
case ISD::SINT_TO_FP:
return LowerIntToFp(Op, DAG);
break;
case ISD::ADD:
case ISD::ADDC:
return LowerAdd64(Op, DAG);
break;
case ISD::SUB:
case ISD::SUBC:
return LowerSub64(Op, DAG);
break;
case ISD::ADDE:
return LowerAdde(Op, DAG);
break;
case ISD::SUBE:
return LowerSube(Op, DAG);
break;
case ISD::BUILD_VECTOR:
return LowerBuildVector(Op, DAG);
break;
case ISD::EXTRACT_VECTOR_ELT:
return LowerExtractVectorElt(Op, DAG);
break;
}
return SDValue();
}
static SDValue createGPR64(SelectionDAG &DAG, SDValue Low, SDValue High, MVT VT = MVT::i64) {
SDLoc DL(High.getNode());
SDValue RegClass = DAG.getTargetConstant(Epiphany::GPR64RegClassID, DL, MVT::i32);
SDValue SubRegHi = DAG.getTargetConstant(Epiphany::isub_hi, DL, MVT::i32);
SDValue SubRegLo = DAG.getTargetConstant(Epiphany::isub_lo, DL, MVT::i32);
const SDValue Ops[] = { RegClass, High, SubRegHi, Low, SubRegLo };
return SDValue(DAG.getMachineNode(TargetOpcode::REG_SEQUENCE, DL, VT, Ops), 0);
}
//===----------------------------------------------------------------------===//
// Fast arithmetics lowering
//===----------------------------------------------------------------------===//
SDValue EpiphanyTargetLowering::LowerFastDiv(SDValue Op, SelectionDAG &DAG) const {
SDLoc DL(Op);
// Get operands
SDValue LHS = Op.getOperand(0);
SDValue RHS = Op.getOperand(1);
// Prepare lib call
RTLIB::Libcall LC = RTLIB::DIV_F32;
SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
getPointerTy(DAG.getDataLayout()));
assert(LHS.getSimpleValueType() == MVT::f32 && RHS.getSimpleValueType() == MVT::f32 &&
"Wrong value type in float fast division!");
// Call the library
SmallVector<SDValue, 2> Ops({RHS, Callee});
std::pair<SDValue, SDValue> Divisor = makeLibCall(DAG, LC, MVT::f32, Ops, /* isSigned = */ true, DL);
// Multiply by divident
return DAG.getNode(ISD::FMUL, DL, MVT::f32, Divisor.first, LHS, Divisor.second);
}
SDValue EpiphanyTargetLowering::LowerSube(SDValue Op, SelectionDAG &DAG) const {
SDLoc DL(Op);
// Get operands
SDValue LHS = Op.getOperand(0);
SDValue RHS = Op.getOperand(1);
SDValue Flag = Op.getOperand(2);
// Required constants
SDValue CarryZero = DAG.getConstant(0, DL, MVT::i32);
SDValue CarryOne = DAG.getConstant(1, DL, MVT::i32);
SDValue MaxRegValue = DAG.getConstant(0x7FFFFFFF, DL, MVT::i32);
SDValue Condition = DAG.getConstant(::EpiphanyCC::COND_GTEU, DL, MVT::i32);
SDValue STATUS = DAG.getRegister(Epiphany::STATUS, MVT::i32);
// Required VTs
SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Glue);
// Instructions
SDValue Carry = DAG.getNode(Epiphany::MOVCC, DL, VTs, CarryOne, CarryZero, Condition, STATUS, Flag);
SDValue Result = DAG.getNode(ISD::SUBC, DL, VTs, LHS, RHS, Carry.getValue(1));
SDValue ResultCarry = DAG.getNode(Epiphany::MOVCC, DL, VTs, CarryOne, CarryZero, Condition, Result.getValue(1));
SDValue AddedCarry = DAG.getNode(ISD::SUBC, DL, VTs, Result.getValue(0), Carry, ResultCarry.getValue(1));
SDValue LastCarry = DAG.getNode(Epiphany::MOVCC, DL, VTs, CarryOne, CarryZero, Condition, AddedCarry.getValue(1));
SDValue FinalCarry = DAG.getNode(ISD::OR, DL, MVT::i32, ResultCarry, LastCarry);
SDValue SetFlag = DAG.getNode(ISD::ADDC, DL, VTs, FinalCarry, MaxRegValue, LastCarry.getValue(1));
return DAG.getNode(ISD::SUBC, DL, VTs, AddedCarry, CarryZero, STATUS, SetFlag.getValue(1));
}
SDValue EpiphanyTargetLowering::LowerAdde(SDValue Op, SelectionDAG &DAG) const {
SDLoc DL(Op);
// Get operands
SDValue LHS = Op.getOperand(0);
SDValue RHS = Op.getOperand(1);
SDValue Flag = Op.getOperand(2);
// Required constants
SDValue CarryZero = DAG.getConstant(0, DL, MVT::i32);
SDValue CarryOne = DAG.getConstant(1, DL, MVT::i32);
SDValue MaxRegValue = DAG.getConstant(0x7FFFFFFF, DL, MVT::i32);
SDValue Condition = DAG.getConstant(::EpiphanyCC::COND_GTEU, DL, MVT::i32);
SDValue STATUS = DAG.getRegister(Epiphany::STATUS, MVT::i32);
// Required VTs
SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Glue);
// Instructions
SDValue Carry = DAG.getNode(EpiphanyISD::MOVCC, DL, VTs, CarryOne, CarryZero, Condition, STATUS, Flag);
SDValue Result = DAG.getNode(ISD::ADDC, DL, VTs, LHS, RHS, Carry.getValue(1));
SDValue ResultCarry = DAG.getNode(EpiphanyISD::MOVCC, DL, VTs, CarryOne, CarryZero, Condition, STATUS, Result.getValue(1));
SDValue AddedCarry = DAG.getNode(ISD::ADDC, DL, VTs, Result.getValue(0), Carry, ResultCarry.getValue(1));
SDValue LastCarry = DAG.getNode(EpiphanyISD::MOVCC, DL, VTs, CarryOne, CarryZero, Condition, STATUS, AddedCarry.getValue(1));
SDValue FinalCarry = DAG.getNode(ISD::OR, DL, MVT::i32, ResultCarry, LastCarry);
SDValue SetFlag = DAG.getNode(ISD::ADDC, DL, VTs, FinalCarry, MaxRegValue, LastCarry.getValue(1));
return DAG.getNode(ISD::ADDC, DL, VTs, AddedCarry, CarryZero, STATUS, SetFlag.getValue(1));
}
SDValue EpiphanyTargetLowering::LowerAdd64(SDValue Op, SelectionDAG &DAG) const {
SDLoc DL(Op);
// Get operands
SDValue LHS = Op.getOperand(0);
SDValue RHS = Op.getOperand(1);
// Extract subregs
SDValue LHS_l = DAG.getTargetExtractSubreg(Epiphany::isub_lo, DL, MVT::i32, LHS);
SDValue LHS_h = DAG.getTargetExtractSubreg(Epiphany::isub_hi, DL, MVT::i32, LHS);
SDValue RHS_l = DAG.getTargetExtractSubreg(Epiphany::isub_lo, DL, MVT::i32, RHS);
SDValue RHS_h = DAG.getTargetExtractSubreg(Epiphany::isub_hi, DL, MVT::i32, RHS);
// Create low and high adds
SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Glue);
SDValue Low = DAG.getNode(ISD::ADDC, DL, VTs, LHS_l, RHS_l);
SDValue High = DAG.getNode(ISD::ADDE, DL, VTs, LHS_h, RHS_h, Low.getValue(1));
return createGPR64(DAG, Low, High, MVT::i64);
}
SDValue EpiphanyTargetLowering::LowerSub64(SDValue Op, SelectionDAG &DAG) const {
SDLoc DL(Op);
// Get operands
SDValue LHS = Op.getOperand(0);
SDValue RHS = Op.getOperand(1);
// Extract subregs
SDValue LHS_l = DAG.getTargetExtractSubreg(Epiphany::isub_lo, DL, MVT::i32, LHS);
SDValue LHS_h = DAG.getTargetExtractSubreg(Epiphany::isub_hi, DL, MVT::i32, LHS);
SDValue RHS_l = DAG.getTargetExtractSubreg(Epiphany::isub_lo, DL, MVT::i32, RHS);
SDValue RHS_h = DAG.getTargetExtractSubreg(Epiphany::isub_hi, DL, MVT::i32, RHS);
// Create low and high adds
SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Glue);
SDValue Low = DAG.getNode(ISD::SUBC, DL, VTs, LHS_l, RHS_l);
SDValue High = DAG.getNode(ISD::SUBE, DL, VTs, LHS_h, RHS_h, Low.getValue(1));
return createGPR64(DAG, Low, High, MVT::i64);
}
SDValue EpiphanyTargetLowering::LowerIntToFp(SDValue Op, SelectionDAG &DAG) const {
SDLoc DL(Op);
SDValue arg = Op.getOperand(0);
EVT ArgVT = arg.getValueType();
EVT ResVT = Op.getValueType();
// We have FLOAT op for i32 -> f32 conversion
if (ArgVT.getSimpleVT() == MVT::i32 && ResVT.getSimpleVT() == MVT::f32) {
return DAG.getNode(EpiphanyISD::FLOAT, DL, ResVT, arg);
}
RTLIB::Libcall LC;
if (Op.getOpcode() == ISD::SINT_TO_FP) {
LC = RTLIB::getSINTTOFP(ArgVT, ResVT);
} else {
LC = RTLIB::getUINTTOFP(ArgVT, ResVT);
}
SmallVector<SDValue, 2> Ops(Op->op_begin(), Op->op_end());
return makeLibCall(DAG, LC, ResVT, Ops, false, DL).first;
}
SDValue EpiphanyTargetLowering::LowerFpToInt(SDValue Op, SelectionDAG &DAG) const {
SDLoc DL(Op);
SDValue arg = Op.getOperand(0);
EVT ArgVT = arg.getValueType();
EVT ResVT = Op.getValueType();
// We have FIX op for f32 -> i32 conversion
if (ArgVT.getSimpleVT() == MVT::f32 && ResVT.getSimpleVT() == MVT::i32) {
return DAG.getNode(EpiphanyISD::FIX, DL, ResVT, arg);
}
RTLIB::Libcall LC;
if (Op.getOpcode() == ISD::FP_TO_SINT) {
LC = RTLIB::getFPTOSINT(ArgVT, ResVT);
} else {
LC = RTLIB::getFPTOUINT(ArgVT, ResVT);
}
SmallVector<SDValue, 2> Ops(Op->op_begin(), Op->op_end());
return makeLibCall(DAG, LC, ResVT, Ops, false, DL).first;
}
//===----------------------------------------------------------------------===//
// Lower helper functions
//===----------------------------------------------------------------------===//
static EpiphanyCC::CondCodes ConvertCC(SDValue CC, const SDLoc &DL, SDValue &RHS, bool &swap) {
// Get condition code
ISD::CondCode code = cast<CondCodeSDNode>(CC)->get();
// Get used reg class
MVT Ty = RHS.getSimpleValueType();
// Choose condition
switch (code) {
default:
llvm_unreachable("Unknown condition code: " + code);
break;
// We can also check for NaN as fsub wont return zero/equal
case ISD::SETO:
case ISD::SETEQ:
case ISD::SETOEQ:
case ISD::SETUEQ:
if (Ty.isFloatingPoint()) {
return EpiphanyCC::COND_BEQ;
} else {
return EpiphanyCC::COND_EQ;
}
break;
// We can also check for NaN as fsub wont return zero/equal
case ISD::SETUO:
case ISD::SETNE:
case ISD::SETONE:
case ISD::SETUNE:
if (Ty.isFloatingPoint()) {
return EpiphanyCC::COND_BNE;
} else {
return EpiphanyCC::COND_NE;
}
break;
case ISD::SETGE:
case ISD::SETOGE:
if (Ty.isFloatingPoint()) {
swap = true;
return EpiphanyCC::COND_BLT;
} else {
return EpiphanyCC::COND_GTE;
}
break;
case ISD::SETUGE:
if (Ty.isFloatingPoint()) {
swap = true;
return EpiphanyCC::COND_BLT;
} else {
return EpiphanyCC::COND_GTEU;
}
break;
case ISD::SETGT:
case ISD::SETOGT:
if (Ty.isFloatingPoint()) {
swap = true;
return EpiphanyCC::COND_BLTE;
} else {
return EpiphanyCC::COND_GT;
}
break;
case ISD::SETUGT:
if (Ty.isFloatingPoint()) {
swap = true;
return EpiphanyCC::COND_BLTE;
} else {
return EpiphanyCC::COND_GTU;
}
break;
case ISD::SETLE:
case ISD::SETOLE:
if (Ty.isFloatingPoint()) {
return EpiphanyCC::COND_BLTE;
} else {
return EpiphanyCC::COND_LTE;
}
break;
case ISD::SETULE:
if (Ty.isFloatingPoint()) {
return EpiphanyCC::COND_BLTE;
} else {
return EpiphanyCC::COND_LTEU;
}
break;
case ISD::SETLT:
case ISD::SETOLT:
if (Ty.isFloatingPoint()) {
return EpiphanyCC::COND_BLT;
} else {
return EpiphanyCC::COND_LT;
}
break;
case ISD::SETULT:
if (Ty.isFloatingPoint()) {
return EpiphanyCC::COND_BLT;
} else {
return EpiphanyCC::COND_LTU;
}
break;
}
}
static ISD::CondCode getUnsignedToSigned(SDValue cond) {
// Get condition code
ISD::CondCode code = cast<CondCodeSDNode>(cond)->get();
// Choose function
switch (code) {
default:
return code;
case ISD::SETUEQ:
return ISD::SETEQ;
case ISD::SETUGE:
return ISD::SETGE;
case ISD::SETUGT:
return ISD::SETGT;
case ISD::SETULE:
return ISD::SETLE;
case ISD::SETULT:
return ISD::SETLT;
case ISD::SETUNE:
return ISD::SETNE;
}
}
static RTLIB::Libcall getDoubleCmp(SDValue cond) {
// Get condition code
ISD::CondCode code = cast<CondCodeSDNode>(cond)->get();
// Choose function
switch (code) {
default:
llvm_unreachable("Unknown condition code: " + code);
break;
case ISD::SETEQ:
case ISD::SETOEQ:
case ISD::SETUEQ:
return RTLIB::OEQ_F64;
case ISD::SETGE:
case ISD::SETUGE:
case ISD::SETOGE:
return RTLIB::OGE_F64;
case ISD::SETGT:
case ISD::SETOGT:
case ISD::SETUGT:
return RTLIB::OGT_F64;
case ISD::SETLE:
case ISD::SETOLE:
case ISD::SETULE:
return RTLIB::OLE_F64;
case ISD::SETLT:
case ISD::SETOLT:
case ISD::SETULT:
return RTLIB::OLT_F64;
case ISD::SETNE:
case ISD::SETONE:
case ISD::SETUNE:
return RTLIB::UNE_F64;
}
}
//===----------------------------------------------------------------------===//
// Custom inserter functions
//===----------------------------------------------------------------------===//
MachineBasicBlock *EpiphanyTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI, MachineBasicBlock *MBB) const {
switch (MI.getOpcode()) {
default:
MI.dump();
llvm_unreachable("No custom inserter for the instruction");
break;
case Epiphany::BCC64:
return emitBrCC(MI, MBB);
break;
}
}
MachineBasicBlock *EpiphanyTargetLowering::emitBrCC(MachineInstr &MI, MachineBasicBlock *MBB) const {
// We can have 3 cases - GT, LT and EQ (and their unsigned versions).
// LT is converted to GTE by swapping comparison operands
// EQ does not have the first comparison, we simply jump out if high subregs are not equal
// LowCmpBB is needed because of the MBB elimination mechanism (CMP is not a terminator)
// OrigBB:
// [... previous instrs ...]
// cmp r0_hi, r1_hi
// bgt/bgtu DestBB
// bne FallThroughBB
// LowCmpBB:
// cmp r0_lo, r1_lo
// bgt/bgtu DestBB
// FallThroughBB:
// [... fall-through ...]
const TargetRegisterClass *RC = &Epiphany::GPR32RegClass;
MachineFunction *MF = MBB->getParent();
const TargetInstrInfo *TII = Subtarget.getInstrInfo();
const BasicBlock *LLVM_BB = MBB->getBasicBlock();
DebugLoc DL = MI.getDebugLoc();
MachineFunction::iterator It = ++MBB->getIterator();
MachineRegisterInfo &MRI = MF->getRegInfo();
// Get Operands
MachineBasicBlock *Dest = MI.getOperand(0).getMBB();
unsigned CondCode = MI.getOperand(1).getImm();
unsigned LHS_lo = MI.getOperand(2).getReg();
unsigned RHS_lo = MI.getOperand(3).getReg();
unsigned LHS_hi = MI.getOperand(4).getReg();
unsigned RHS_hi = MI.getOperand(5).getReg();
// Invert cond code if needed
bool swap = true;
switch (CondCode) {
default:
swap = false;
break;
case ::EpiphanyCC::COND_LTE:
CondCode = ::EpiphanyCC::COND_GT;
break;
case ::EpiphanyCC::COND_LTU:
CondCode = ::EpiphanyCC::COND_GTEU;
break;
case ::EpiphanyCC::COND_LTEU:
CondCode = ::EpiphanyCC::COND_GTU;
break;
case ::EpiphanyCC::COND_LT:
CondCode = ::EpiphanyCC::COND_GTE;
break;
}
// Swap LHS/RHS if needed
if (swap) {
std::swap(LHS_lo, RHS_lo);
std::swap(LHS_hi, RHS_hi);
}
// Check if the condition is EQ
bool isEqual = CondCode == ::EpiphanyCC::COND_EQ;
unsigned tempReg1 = MRI.createVirtualRegister(RC);
unsigned tempReg2 = MRI.createVirtualRegister(RC);
// Create fall-thourgh block
MachineBasicBlock *FallThroughBB = MF->CreateMachineBasicBlock(LLVM_BB);
MachineBasicBlock *LowCmpBB = MF->CreateMachineBasicBlock(LLVM_BB);
MF->insert(It, LowCmpBB);
MF->insert(It, FallThroughBB);
// Transfer rest of current basic-block to FallThroughBB
FallThroughBB->splice(FallThroughBB->begin(), MBB,
std::next(MachineBasicBlock::iterator(MI)), MBB->end());
// Do not transfer PHIs as this is essentially a branch which can break the critical path
FallThroughBB->transferSuccessors(MBB);
// Insert instruction sequence
BuildMI(MBB, DL, TII->get(Epiphany::CMPrr_r32), tempReg1).addReg(LHS_hi).addReg(RHS_hi);
if (!isEqual) {
BuildMI(MBB, DL, TII->get(Epiphany::BCC)).addMBB(Dest).addImm(::EpiphanyCC::COND_GT);
}
BuildMI(MBB, DL, TII->get(Epiphany::BCC)).addMBB(FallThroughBB).addImm(::EpiphanyCC::COND_NE);
MBB->addSuccessor(Dest);
MBB->addSuccessor(FallThroughBB);
MBB->addSuccessor(LowCmpBB);
MBB->normalizeSuccProbs();
BuildMI(LowCmpBB, DL, TII->get(Epiphany::CMPrr_r32), tempReg2).addReg(LHS_lo).addReg(RHS_lo);
BuildMI(LowCmpBB, DL, TII->get(Epiphany::BCC)).addMBB(Dest).addImm(CondCode);
LowCmpBB->addSuccessor(Dest);
LowCmpBB->addSuccessor(FallThroughBB);
LowCmpBB->normalizeSuccProbs();
MI.eraseFromParent();
return FallThroughBB;
}
//===----------------------------------------------------------------------===//
// Custom lowering
//===----------------------------------------------------------------------===//
bool EpiphanyTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
return false;
}
SDValue EpiphanyTargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
SDLoc DL(Op);
auto *GA = cast<GlobalAddressSDNode>(Op);
if (DAG.getTarget().Options.EmulatedTLS)
return LowerToTLSEmulatedModel(GA, DAG);
const GlobalValue *GV = GA->getGlobal();
int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
auto PTY = getPointerTy(DAG.getDataLayout());
SDValue AddrLow = DAG.getTargetGlobalAddress(GV, DL, PTY, Offset, EpiphanyII::MO_LOW);
SDValue AddrHigh = DAG.getTargetGlobalAddress(GV, DL, PTY, Offset, EpiphanyII::MO_HIGH);
SDValue Low = DAG.getNode(EpiphanyISD::MOV, DL, PTY, AddrLow);
return DAG.getNode(EpiphanyISD::MOVT, DL, PTY, Low, AddrHigh);
//}
}
SDValue EpiphanyTargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
SDLoc DL(Op);
auto *BA = cast<BlockAddressSDNode>(Op);
const BlockAddress *BV = BA->getBlockAddress();
int64_t Offset = BA->getOffset();
auto PTY = getPointerTy(DAG.getDataLayout());
SDValue AddrLow = DAG.getBlockAddress(BV, PTY, Offset, /* isTarget = */ true, EpiphanyII::MO_LOW);
SDValue AddrHigh = DAG.getBlockAddress(BV, PTY, Offset, /* isTarget = */ true, EpiphanyII::MO_HIGH);
SDValue Low = DAG.getNode(EpiphanyISD::MOV, DL, PTY, AddrLow);
return DAG.getNode(EpiphanyISD::MOVT, DL, PTY, Low, AddrHigh);
//}
}
SDValue EpiphanyTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
SDLoc DL(Op);
auto *GA = cast<GlobalAddressSDNode>(Op);
const GlobalValue *GV = GA->getGlobal();
EVT PTY = getPointerTy(DAG.getDataLayout());
const EpiphanyRegisterInfo *TRI = Subtarget.getRegisterInfo();
auto *FI = DAG.getMachineFunction().getInfo<EpiphanyMachineFunctionInfo>();
// Get TLS model
TLSModel::Model model = getTargetMachine().getTLSModel(GV);
if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
// General Dynamic and Local Dynamic TLS Model.
SDValue Argument = DAG.getRegister(FI->getGlobalBaseReg(), MVT::i32);
unsigned PtrSize = PTY.getSizeInBits();
IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PTY);
ArgListTy Args;
ArgListEntry Entry;
Entry.Node = Argument;
Entry.Ty = PtrTy;
Args.push_back(Entry);
TargetLowering::CallLoweringInfo CLI(DAG);
CLI.setDebugLoc(DL).setChain(DAG.getEntryNode())
.setCallee(CallingConv::C, PtrTy, TlsGetAddr, std::move(Args));
std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
return CallResult.first;
} else if (model == TLSModel::InitialExec) {
SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PTY, 0, EpiphanyII::MO_PCREL16);
TGA = DAG.getNode(EpiphanyISD::MOV, DL, PTY, TGA);
SDValue Offset = DAG.getLoad(PTY, DL, DAG.getEntryNode(), TGA, MachinePointerInfo());
return DAG.getNode(ISD::ADD, DL, PTY, DAG.getRegister(TRI->getBaseRegister(), MVT::i32), Offset);
}
return SDValue();
}
SDValue EpiphanyTargetLowering::LowerExternalSymbol(SDValue Op,
SelectionDAG &DAG) const {
SDLoc DL(Op);
const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
auto PTY = getPointerTy(DAG.getDataLayout());
SDValue AddrLow = DAG.getTargetExternalSymbol(Sym, PTY, EpiphanyII::MO_LOW);
SDValue AddrHigh = DAG.getTargetExternalSymbol(Sym, PTY, EpiphanyII::MO_HIGH);
SDValue Low = DAG.getNode(EpiphanyISD::MOV, DL, PTY, AddrLow);
return DAG.getNode(EpiphanyISD::MOVT, DL, PTY, Low, AddrHigh);
}
SDValue EpiphanyTargetLowering::LowerConstantPool(SDValue Op,
SelectionDAG &DAG) const {
SDLoc DL(Op);
auto *CP = cast<ConstantPoolSDNode>(Op);
EVT PTY = Op.getValueType();
// Get constant pool address
SDValue AddrLow;
SDValue AddrHigh;
if (CP->isMachineConstantPoolEntry()) {
AddrLow = DAG.getTargetConstantPool(CP->getMachineCPVal(), PTY, CP->getAlignment(), CP->getOffset(), EpiphanyII::MO_LOW);
AddrHigh = DAG.getTargetConstantPool(CP->getMachineCPVal(), PTY, CP->getAlignment(), CP->getOffset(), EpiphanyII::MO_HIGH);
} else {
AddrLow = DAG.getTargetConstantPool(CP->getConstVal(), PTY, CP->getAlignment(), CP->getOffset(), EpiphanyII::MO_LOW);
AddrHigh = DAG.getTargetConstantPool(CP->getConstVal(), PTY, CP->getAlignment(), CP->getOffset(), EpiphanyII::MO_HIGH);
}
// Move address to the register
SDValue Low = DAG.getNode(EpiphanyISD::MOV, DL, PTY, AddrLow);
return DAG.getNode(EpiphanyISD::MOVT, DL, PTY, Low, AddrHigh);
}
/// LowerBrCond
// Lower conditional branch selection
SDValue EpiphanyTargetLowering::LowerBrCond(SDValue Op, SelectionDAG &DAG) const {
SDLoc DL(Op);
// Get operands
SDValue Chain = Op.getOperand(0);
SDValue Value = Op.getOperand(1);
SDValue Dest = Op.getOperand(2);
// Set flag
::EpiphanyCC::CondCodes CC = ::EpiphanyCC::COND_GTU;
SDVTList VTs = DAG.getVTList(Value.getValueType(), MVT::i32);
SDValue Flag = DAG.getNode(EpiphanyISD::CMP, DL, VTs, Value, DAG.getConstant(0, DL, MVT::i32));
// Prepare conditional move
assert(Flag && "Can't get op for provided type");
SDValue TargetCC = DAG.getConstant(CC, DL, MVT::i32);
VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
return DAG.getNode(EpiphanyISD::BRCC, DL, VTs, Chain, Dest, TargetCC, Flag.getValue(1));
}
/// LowerBrCC
// Lower conditional branch selection
SDValue EpiphanyTargetLowering::LowerBrCC(SDValue Op, SelectionDAG &DAG) const {
SDLoc DL(Op);
// Get operands
SDValue Chain = Op.getOperand(0);
SDValue Cond = Op.getOperand(1);
SDValue LHS = Op.getOperand(2);
SDValue RHS = Op.getOperand(3);
SDValue Dest = Op.getOperand(4);
// Get operand types
MVT RTy = RHS.getSimpleValueType();
MVT LTy = LHS.getSimpleValueType();
// Set flag
SDValue Flag;
::EpiphanyCC::CondCodes CCode;
bool swap = false;
if (RTy == MVT::i32 && LTy == MVT::i32) {
// Simple i32 case
SDVTList VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
Flag = DAG.getNode(EpiphanyISD::CMP, DL, VTs, LHS, RHS);
CCode = ConvertCC(Cond, DL, LHS, swap);
} else if (RTy == MVT::f32 && LTy == MVT::f32) {
// Floating point f32 case
CCode = ConvertCC(Cond, DL, LHS, swap);
if (swap) {
std::swap(LHS, RHS);
}
SDVTList VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
Flag = DAG.getNode(EpiphanyISD::CMP, DL, VTs, LHS, RHS);
} else if (RTy == MVT::i64 && LTy == MVT::i64) {
// Extract subregs
SDValue LHS_lo = DAG.getTargetExtractSubreg(Epiphany::isub_lo, DL, MVT::i32, LHS);
SDValue RHS_lo = DAG.getTargetExtractSubreg(Epiphany::isub_lo, DL, MVT::i32, RHS);
SDValue LHS_hi = DAG.getTargetExtractSubreg(Epiphany::isub_hi, DL, MVT::i32, LHS);
SDValue RHS_hi = DAG.getTargetExtractSubreg(Epiphany::isub_hi, DL, MVT::i32, RHS);
// Get condition
SDValue TargetCC = DAG.getConstant(ConvertCC(Cond, DL, LHS, swap), DL, MVT::i32);
SmallVector<SDValue, 7> Ops({Chain, Dest, TargetCC, LHS_lo, RHS_lo, LHS_hi, RHS_hi});
return DAG.getNode(EpiphanyISD::BRCC64, DL, Op.getValueType(), Ops);
} else if (RTy == MVT::f64 && LTy == MVT::f64) {
RTLIB::Libcall LC = getDoubleCmp(Cond);
SmallVector<SDValue, 2> Ops({LHS, RHS});
Flag = makeLibCall(DAG, LC, MVT::i32, Ops, /* isSigned = */ true, DL).first;
// Use integer sub to set the flag, see GCC Soft-Float Library Routines
SDVTList VTs = DAG.getVTList(Flag.getValueType(), MVT::i32);
Flag = DAG.getNode(EpiphanyISD::CMP, DL, VTs, Flag, DAG.getConstant(0, DL, MVT::i32));
CCode = ConvertCC(DAG.getCondCode(getUnsignedToSigned(Cond)), DL, Flag, swap);
}
// Prepare conditional move
assert(Flag && "Can't get op for provided type");
SDValue TargetCC = DAG.getConstant(CCode, DL, MVT::i32);
SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
return DAG.getNode(EpiphanyISD::BRCC, DL, VTs, Chain, Dest, TargetCC, Flag.getValue(1));
}
/// LowerSelectCC
// Lower conditional selection. Similar to movcc + cmp
SDValue EpiphanyTargetLowering::LowerSelectCC(SDValue Op, SelectionDAG &DAG) const {
SDLoc DL(Op);
// Get operands
SDValue LHS = Op.getOperand(0);
SDValue RHS = Op.getOperand(1);
SDValue TrueV = Op.getOperand(2);
SDValue FalseV = Op.getOperand(3);
SDValue Cond = Op.getOperand(4);
// Get operand types
MVT RTy = RHS.getSimpleValueType();
MVT LTy = LHS.getSimpleValueType();
// Set the flag
SDValue Flag;
if (RTy == MVT::i32 && LTy == MVT::i32) {
SDVTList VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
Flag = DAG.getNode(EpiphanyISD::CMP, DL, VTs, LHS, RHS);
} else if (RTy == MVT::f32 && LTy == MVT::f32) {
SDVTList VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
Flag = DAG.getNode(EpiphanyISD::CMP, DL, VTs, LHS, RHS);
} else if (RTy == MVT::i64 && LTy == MVT::i64) {
SDVTList VTs = DAG.getVTList(MVT::i32, MVT::i32);
// Extract subregs
SDValue LHS_lo = DAG.getTargetExtractSubreg(Epiphany::isub_lo, DL, MVT::i32, LHS);
SDValue RHS_lo = DAG.getTargetExtractSubreg(Epiphany::isub_lo, DL, MVT::i32, RHS);
SDValue LHS_hi = DAG.getTargetExtractSubreg(Epiphany::isub_hi, DL, MVT::i32, LHS);
SDValue RHS_hi = DAG.getTargetExtractSubreg(Epiphany::isub_hi, DL, MVT::i32, RHS);
// Sub low and high regs
SDValue Low = DAG.getNode(EpiphanyISD::CMP, DL, VTs, LHS_lo, RHS_lo);
SDValue High = DAG.getNode(EpiphanyISD::CMP, DL, VTs, LHS_hi, RHS_hi);
// Sub borrow
SDValue TrueV = DAG.getConstant(1, DL, MVT::i32);
SDValue FalseV = DAG.getConstant(0, DL, MVT::i32);
SDValue CC = DAG.getConstant(::EpiphanyCC::COND_LT, DL, MVT::i32);
Cond = DAG.getCondCode(getUnsignedToSigned(Cond));
SDValue Borrow = DAG.getNode(EpiphanyISD::MOVCC, DL, MVT::i32, TrueV, FalseV, CC, Low.getValue(1));
Flag = DAG.getNode(EpiphanyISD::CMP, DL, VTs, High, Borrow);
} else if (RTy == MVT::f64 && LTy == MVT::f64) {
RTLIB::Libcall LC = getDoubleCmp(Cond);
SmallVector<SDValue, 2> Ops({LHS, RHS});