forked from cgeyer/llvm-cbg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-cbg-backend.patch
8896 lines (8893 loc) · 325 KB
/
add-cbg-backend.patch
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
From 5042ccb95df68ae5d049c6caf705a597160e3d40 Mon Sep 17 00:00:00 2001
From: Clemens Bernhard Geyer <[email protected]>
Date: Thu, 16 Feb 2012 18:24:20 +0100
Subject: [PATCH] Added cbg target.
---
configure | 16 +-
include/llvm/ADT/Triple.h | 1 +
lib/Support/Triple.cpp | 1 +
lib/Target/cbg/CMakeLists.txt | 28 +
lib/Target/cbg/DelaySlotFiller.cpp | 335 +++++
lib/Target/cbg/FPMover.cpp | 144 ++
lib/Target/cbg/LICENSE.TXT | 44 +
lib/Target/cbg/Makefile | 23 +
lib/Target/cbg/PredecessorList.cpp | 106 ++
lib/Target/cbg/PredecessorList.h | 75 ++
lib/Target/cbg/README | 27 +
lib/Target/cbg/README_SPARC.txt | 59 +
lib/Target/cbg/TargetInfo/CMakeLists.txt | 7 +
lib/Target/cbg/TargetInfo/Makefile | 15 +
lib/Target/cbg/TargetInfo/cbgTargetInfo.cpp | 19 +
lib/Target/cbg/cbg.h | 129 ++
lib/Target/cbg/cbg.td | 95 ++
lib/Target/cbg/cbgAsmPrinter.cpp | 289 ++++
lib/Target/cbg/cbgCallingConv.td | 36 +
lib/Target/cbg/cbgFrameLowering.cpp | 80 ++
lib/Target/cbg/cbgFrameLowering.h | 41 +
lib/Target/cbg/cbgHWLoop.cpp | 643 +++++++++
lib/Target/cbg/cbgISelDAGToDAG.cpp | 216 +++
lib/Target/cbg/cbgISelLowering.cpp | 1311 ++++++++++++++++++
lib/Target/cbg/cbgISelLowering.h | 111 ++
lib/Target/cbg/cbgInstrFormats.td | 114 ++
lib/Target/cbg/cbgInstrInfo.cpp | 346 +++++
lib/Target/cbg/cbgInstrInfo.h | 126 ++
lib/Target/cbg/cbgInstrInfo.td | 1328 ++++++++++++++++++
lib/Target/cbg/cbgMCAsmInfo.cpp | 34 +
lib/Target/cbg/cbgMCAsmInfo.h | 29 +
lib/Target/cbg/cbgMachineFunctionInfo.h | 47 +
lib/Target/cbg/cbgPredicationPass.cpp | 1921 +++++++++++++++++++++++++++
lib/Target/cbg/cbgRegisterInfo.cpp | 134 ++
lib/Target/cbg/cbgRegisterInfo.h | 59 +
lib/Target/cbg/cbgRegisterInfo.td | 205 +++
lib/Target/cbg/cbgSelectionDAGInfo.cpp | 23 +
lib/Target/cbg/cbgSelectionDAGInfo.h | 31 +
lib/Target/cbg/cbgSubtarget.cpp | 35 +
lib/Target/cbg/cbgSubtarget.h | 66 +
lib/Target/cbg/cbgTargetMachine.cpp | 78 ++
lib/Target/cbg/cbgTargetMachine.h | 78 ++
42 files changed, 8501 insertions(+), 4 deletions(-)
create mode 100644 lib/Target/cbg/CMakeLists.txt
create mode 100644 lib/Target/cbg/DelaySlotFiller.cpp
create mode 100644 lib/Target/cbg/FPMover.cpp
create mode 100644 lib/Target/cbg/LICENSE.TXT
create mode 100644 lib/Target/cbg/Makefile
create mode 100644 lib/Target/cbg/PredecessorList.cpp
create mode 100644 lib/Target/cbg/PredecessorList.h
create mode 100644 lib/Target/cbg/README
create mode 100644 lib/Target/cbg/README_SPARC.txt
create mode 100644 lib/Target/cbg/TargetInfo/CMakeLists.txt
create mode 100644 lib/Target/cbg/TargetInfo/Makefile
create mode 100644 lib/Target/cbg/TargetInfo/cbgTargetInfo.cpp
create mode 100644 lib/Target/cbg/cbg.h
create mode 100644 lib/Target/cbg/cbg.td
create mode 100644 lib/Target/cbg/cbgAsmPrinter.cpp
create mode 100644 lib/Target/cbg/cbgCallingConv.td
create mode 100644 lib/Target/cbg/cbgFrameLowering.cpp
create mode 100644 lib/Target/cbg/cbgFrameLowering.h
create mode 100644 lib/Target/cbg/cbgHWLoop.cpp
create mode 100644 lib/Target/cbg/cbgISelDAGToDAG.cpp
create mode 100644 lib/Target/cbg/cbgISelLowering.cpp
create mode 100644 lib/Target/cbg/cbgISelLowering.h
create mode 100644 lib/Target/cbg/cbgInstrFormats.td
create mode 100644 lib/Target/cbg/cbgInstrInfo.cpp
create mode 100644 lib/Target/cbg/cbgInstrInfo.h
create mode 100644 lib/Target/cbg/cbgInstrInfo.td
create mode 100644 lib/Target/cbg/cbgMCAsmInfo.cpp
create mode 100644 lib/Target/cbg/cbgMCAsmInfo.h
create mode 100644 lib/Target/cbg/cbgMachineFunctionInfo.h
create mode 100644 lib/Target/cbg/cbgPredicationPass.cpp
create mode 100644 lib/Target/cbg/cbgRegisterInfo.cpp
create mode 100644 lib/Target/cbg/cbgRegisterInfo.h
create mode 100644 lib/Target/cbg/cbgRegisterInfo.td
create mode 100644 lib/Target/cbg/cbgSelectionDAGInfo.cpp
create mode 100644 lib/Target/cbg/cbgSelectionDAGInfo.h
create mode 100644 lib/Target/cbg/cbgSubtarget.cpp
create mode 100644 lib/Target/cbg/cbgSubtarget.h
create mode 100644 lib/Target/cbg/cbgTargetMachine.cpp
create mode 100644 lib/Target/cbg/cbgTargetMachine.h
diff --git a/configure b/configure
index 52bf988..bb10b9d 100755
--- a/configure
+++ b/configure
@@ -1427,9 +1427,9 @@ Optional Features:
(default is YES)
--enable-targets Build specific host targets: all or
target1,target2,... Valid targets are: host, x86,
- x86_64, sparc, powerpc, alpha, arm, mips, spu,
- xcore, msp430, systemz, blackfin, ptx, cbe, and cpp
- (default=all)
+ x86_64, sparc, cbg, powerpc, alpha, arm, mips,
+ spu, xcore, msp430, systemz, blackfin, ptx, cbe,
+ and cpp (default=all)
--enable-cbe-printf-a Enable C Backend output with hex floating point via
%a (default is YES)
--enable-bindings Build specific language bindings:
@@ -2394,6 +2394,7 @@ else
case $target in
i?86-*) llvm_cv_target_arch="x86" ;;
amd64-* | x86_64-*) llvm_cv_target_arch="x86_64" ;;
+ cbg*-*) llvm_cv_target_arch="cbg" ;;
sparc*-*) llvm_cv_target_arch="Sparc" ;;
powerpc*-*) llvm_cv_target_arch="PowerPC" ;;
alpha*-*) llvm_cv_target_arch="Alpha" ;;
@@ -4848,6 +4849,8 @@ else
case "$llvm_cv_target_arch" in
x86) TARGET_HAS_JIT=1
;;
+ cbg) TARGET_HAS_JIT=0
+ ;;
Sparc) TARGET_HAS_JIT=0
;;
PowerPC) TARGET_HAS_JIT=1
@@ -5056,11 +5059,15 @@ if test "$enableval" = host-only ; then
enableval=host
fi
case "$enableval" in
- all) TARGETS_TO_BUILD="X86 Sparc PowerPC Alpha ARM Mips CellSPU XCore MSP430 SystemZ Blackfin CBackend CppBackend MBlaze PTX" ;;
+ all) TARGETS_TO_BUILD="X86 cbg PowerPC Alpha ARM Mips CellSPU XCore MSP430 SystemZ Blackfin CBackend CppBackend MBlaze PTX" ;;
+# all) TARGETS_TO_BUILD="X86 Sparc PowerPC Alpha ARM Mips CellSPU XCore MSP430 SystemZ Blackfin CBackend CppBackend MBlaze PTX" ;;
+# all) TARGETS_TO_BUILD="cbg Sparc" ;;
+# all) TARGETS_TO_BUILD="mySparc" ;;
*)for a_target in `echo $enableval|sed -e 's/,/ /g' ` ; do
case "$a_target" in
x86) TARGETS_TO_BUILD="X86 $TARGETS_TO_BUILD" ;;
x86_64) TARGETS_TO_BUILD="X86 $TARGETS_TO_BUILD" ;;
+ cbg) TARGETS_TO_BUILD="cbg $TARGETS_TO_BUILD" ;;
sparc) TARGETS_TO_BUILD="Sparc $TARGETS_TO_BUILD" ;;
powerpc) TARGETS_TO_BUILD="PowerPC $TARGETS_TO_BUILD" ;;
alpha) TARGETS_TO_BUILD="Alpha $TARGETS_TO_BUILD" ;;
@@ -5078,6 +5085,7 @@ case "$enableval" in
host) case "$llvm_cv_target_arch" in
x86) TARGETS_TO_BUILD="X86 $TARGETS_TO_BUILD" ;;
x86_64) TARGETS_TO_BUILD="X86 $TARGETS_TO_BUILD" ;;
+ cbg) TARGETS_TO_BUILD="cbg $TARGETS_TO_BUILD" ;;
Sparc) TARGETS_TO_BUILD="Sparc $TARGETS_TO_BUILD" ;;
PowerPC) TARGETS_TO_BUILD="PowerPC $TARGETS_TO_BUILD" ;;
Alpha) TARGETS_TO_BUILD="Alpha $TARGETS_TO_BUILD" ;;
diff --git a/include/llvm/ADT/Triple.h b/include/llvm/ADT/Triple.h
index e6dcc23..819087f 100644
--- a/include/llvm/ADT/Triple.h
+++ b/include/llvm/ADT/Triple.h
@@ -57,6 +57,7 @@ public:
ppc64, // PPC64: powerpc64, ppu
sparc, // Sparc: sparc
sparcv9, // Sparcv9: Sparcv9
+ cbg, // CBG
systemz, // SystemZ: s390x
tce, // TCE (http://tce.cs.tut.fi/): tce
thumb, // Thumb: thumb, thumbv.*
diff --git a/lib/Support/Triple.cpp b/lib/Support/Triple.cpp
index 36edf6e..b25533d 100644
--- a/lib/Support/Triple.cpp
+++ b/lib/Support/Triple.cpp
@@ -34,6 +34,7 @@ const char *Triple::getArchTypeName(ArchType Kind) {
case ppc: return "powerpc";
case sparc: return "sparc";
case sparcv9: return "sparcv9";
+ case cbg: return "cbg";
case systemz: return "s390x";
case tce: return "tce";
case thumb: return "thumb";
diff --git a/lib/Target/cbg/CMakeLists.txt b/lib/Target/cbg/CMakeLists.txt
new file mode 100644
index 0000000..74ca3a0
--- /dev/null
+++ b/lib/Target/cbg/CMakeLists.txt
@@ -0,0 +1,28 @@
+set(LLVM_TARGET_DEFINITIONS cbg.td)
+
+tablegen(cbgGenRegisterInfo.h.inc -gen-register-desc-header)
+tablegen(cbgGenRegisterNames.inc -gen-register-enums)
+tablegen(cbgGenRegisterInfo.inc -gen-register-desc)
+tablegen(cbgGenInstrNames.inc -gen-instr-enums)
+tablegen(cbgGenInstrInfo.inc -gen-instr-desc)
+tablegen(cbgGenAsmWriter.inc -gen-asm-writer)
+tablegen(cbgGenDAGISel.inc -gen-dag-isel)
+tablegen(cbgGenSubtarget.inc -gen-subtarget)
+tablegen(cbgGenCallingConv.inc -gen-callingconv)
+
+add_llvm_target(cbgCodeGen
+ DelaySlotFiller.cpp
+ FPMover.cpp
+ cbgAsmPrinter.cpp
+ cbgInstrInfo.cpp
+ cbgISelDAGToDAG.cpp
+ cbgISelLowering.cpp
+ cbgFrameLowering.cpp
+ cbgMCAsmInfo.cpp
+ cbgRegisterInfo.cpp
+ cbgSubtarget.cpp
+ cbgTargetMachine.cpp
+ cbgSelectionDAGInfo.cpp
+ )
+
+add_subdirectory(TargetInfo)
diff --git a/lib/Target/cbg/DelaySlotFiller.cpp b/lib/Target/cbg/DelaySlotFiller.cpp
new file mode 100644
index 0000000..15d2c3b
--- /dev/null
+++ b/lib/Target/cbg/DelaySlotFiller.cpp
@@ -0,0 +1,335 @@
+//===-- DelaySlotFiller.cpp - CBG delay slot filler ---------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This is a simple local pass that attempts to fill delay slots with useful
+// instructions. If no instructions can be moved into the delay slot, then a
+// NOP is placed.
+//===----------------------------------------------------------------------===//
+
+#define DEBUG_TYPE "delay-slot-filler"
+#include "cbg.h"
+#include "llvm/CodeGen/MachineFunctionPass.h"
+#include "llvm/CodeGen/MachineInstrBuilder.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Target/TargetMachine.h"
+#include "llvm/Target/TargetInstrInfo.h"
+#include "llvm/Target/TargetRegisterInfo.h"
+#include "llvm/ADT/SmallSet.h"
+#include "llvm/ADT/Statistic.h"
+
+#include <iostream>
+
+using namespace llvm;
+
+STATISTIC(FilledSlots, "Number of delay slots filled");
+
+static cl::opt<bool> DisableDelaySlotFiller(
+ "disable-cbg-delay-filler",
+ cl::init(false),
+ cl::desc("Disable the cbg delay slot filler."),
+ cl::Hidden);
+
+namespace {
+ struct Filler : public MachineFunctionPass {
+ /// Target machine description which we query for reg. names, data
+ /// layout, etc.
+ ///
+ TargetMachine &TM;
+ const TargetInstrInfo *TII;
+
+ static char ID;
+ Filler(TargetMachine &tm)
+ : MachineFunctionPass(ID), TM(tm), TII(tm.getInstrInfo()) { }
+
+ virtual const char *getPassName() const {
+ return "CBG Delay Slot Filler";
+ }
+
+ bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
+ bool runOnMachineFunction(MachineFunction &F) {
+ bool Changed = false;
+ for (MachineFunction::iterator FI = F.begin(), FE = F.end();
+ FI != FE; ++FI)
+ Changed |= runOnMachineBasicBlock(*FI);
+ return Changed;
+ }
+
+ bool isDelayFiller(MachineBasicBlock &MBB,
+ MachineBasicBlock::iterator candidate);
+
+ void insertCallUses(MachineBasicBlock::iterator MI,
+ SmallSet<unsigned, 32>& RegUses);
+
+ void insertDefsUses(MachineBasicBlock::iterator MI,
+ SmallSet<unsigned, 32>& RegDefs,
+ SmallSet<unsigned, 32>& RegUses);
+
+ bool IsRegInSet(SmallSet<unsigned, 32>& RegSet,
+ unsigned Reg);
+
+ bool delayHasHazard(MachineBasicBlock::iterator candidate,
+ bool &sawLoad, bool &sawStore,
+ SmallSet<unsigned, 32> &RegDefs,
+ SmallSet<unsigned, 32> &RegUses);
+
+ MachineBasicBlock::iterator
+ findDelayInstr(MachineBasicBlock &MBB, MachineBasicBlock::iterator slot);
+
+ bool needsUnimp(MachineBasicBlock::iterator I, unsigned &StructSize);
+
+ };
+ char Filler::ID = 0;
+} // end of anonymous namespace
+
+/// createcbgDelaySlotFillerPass - Returns a pass that fills in delay
+/// slots in cbg MachineFunctions
+///
+FunctionPass *llvm::createcbgDelaySlotFillerPass(TargetMachine &tm) {
+ return new Filler(tm);
+}
+
+
+/// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
+/// We assume there is only one delay slot per delayed instruction.
+///
+bool Filler::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
+ bool Changed = false;
+
+ // check, whether current MBB is successor of a hwloop MBB
+ if (MBB.size() == 1) {
+ MachineBasicBlock* predecessor = MBB.getPrevNode();
+ // a hardware loop has to consist of at least two instructions => insert nop
+ if (predecessor->rbegin()->getOpcode() == CBG::HWLOOP) {
+ BuildMI(MBB, MBB.end(), MBB.begin()->getDebugLoc(), TII->get(CBG::NOP));
+ return true;
+ }
+ }
+
+ for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I)
+ if (I->getDesc().hasDelaySlot()) {
+ MachineBasicBlock::iterator D = MBB.end();
+ MachineBasicBlock::iterator J = I;
+
+ if (!DisableDelaySlotFiller)
+ D = findDelayInstr(MBB, I);
+
+ ++FilledSlots;
+ Changed = true;
+
+ if (D == MBB.end())
+ BuildMI(MBB, ++J, I->getDebugLoc(), TII->get(CBG::NOP));
+ else
+ MBB.splice(++J, &MBB, D);
+ unsigned structSize = 0;
+ if (needsUnimp(I, structSize)) {
+ MachineBasicBlock::iterator J = I;
+ ++J; //skip the delay filler.
+ BuildMI(MBB, ++J, I->getDebugLoc(),
+ TII->get(CBG::UNIMP)).addImm(structSize);
+ }
+ }
+ return Changed;
+}
+
+MachineBasicBlock::iterator
+Filler::findDelayInstr(MachineBasicBlock &MBB,
+ MachineBasicBlock::iterator slot)
+{
+ SmallSet<unsigned, 32> RegDefs;
+ SmallSet<unsigned, 32> RegUses;
+ bool sawLoad = false;
+ bool sawStore = false;
+
+ MachineBasicBlock::iterator I = slot;
+
+ if (slot->getOpcode() == CBG::RET)
+ return MBB.end();
+
+ if (slot->getOpcode() == CBG::RETL) {
+ --I;
+ if (I->getOpcode() != CBG::RESTORErr)
+ return MBB.end();
+ //change retl to ret
+ slot->setDesc(TII->get(CBG::RET));
+ return I;
+ }
+
+ //Call's delay filler can def some of call's uses.
+ if (slot->getDesc().isCall())
+ insertCallUses(slot, RegUses);
+ else
+ insertDefsUses(slot, RegDefs, RegUses);
+
+ bool done = false;
+
+ while (!done) {
+ done = (I == MBB.begin());
+
+ if (!done)
+ --I;
+
+ // skip debug value
+ if (I->isDebugValue())
+ continue;
+
+
+ if (I->hasUnmodeledSideEffects()
+ || I->isInlineAsm()
+ || I->isLabel()
+ || I->getDesc().hasDelaySlot()
+ || isDelayFiller(MBB, I))
+ break;
+
+ if (delayHasHazard(I, sawLoad, sawStore, RegDefs, RegUses)) {
+ insertDefsUses(I, RegDefs, RegUses);
+ continue;
+ }
+
+ return I;
+ }
+ return MBB.end();
+}
+
+bool Filler::delayHasHazard(MachineBasicBlock::iterator candidate,
+ bool &sawLoad,
+ bool &sawStore,
+ SmallSet<unsigned, 32> &RegDefs,
+ SmallSet<unsigned, 32> &RegUses)
+{
+
+ if (candidate->isImplicitDef() || candidate->isKill())
+ return true;
+
+ if (candidate->getDesc().mayLoad()) {
+ sawLoad = true;
+ if (sawStore)
+ return true;
+ }
+
+ if (candidate->getDesc().mayStore()) {
+ if (sawStore)
+ return true;
+ sawStore = true;
+ if (sawLoad)
+ return true;
+ }
+
+ for (unsigned i = 0, e = candidate->getNumOperands(); i!= e; ++i) {
+ const MachineOperand &MO = candidate->getOperand(i);
+ if (!MO.isReg())
+ continue; // skip
+
+ unsigned Reg = MO.getReg();
+
+ if (MO.isDef()) {
+ //check whether Reg is defined or used before delay slot.
+ if (IsRegInSet(RegDefs, Reg) || IsRegInSet(RegUses, Reg))
+ return true;
+ }
+ if (MO.isUse()) {
+ //check whether Reg is defined before delay slot.
+ if (IsRegInSet(RegDefs, Reg))
+ return true;
+ }
+ }
+ return false;
+}
+
+
+void Filler::insertCallUses(MachineBasicBlock::iterator MI,
+ SmallSet<unsigned, 32>& RegUses)
+{
+
+ switch(MI->getOpcode()) {
+ default: llvm_unreachable("Unknown opcode.");
+ case CBG::CALL: break;
+ case CBG::JMPLrr:
+ case CBG::JMPLri:
+ assert(MI->getNumOperands() >= 2);
+ const MachineOperand &Reg = MI->getOperand(0);
+ assert(Reg.isReg() && "JMPL first operand is not a register.");
+ assert(Reg.isUse() && "JMPL first operand is not a use.");
+ RegUses.insert(Reg.getReg());
+
+ const MachineOperand &RegOrImm = MI->getOperand(1);
+ if (RegOrImm.isImm())
+ break;
+ assert(RegOrImm.isReg() && "JMPLrr second operand is not a register.");
+ assert(RegOrImm.isUse() && "JMPLrr second operand is not a use.");
+ RegUses.insert(RegOrImm.getReg());
+ break;
+ }
+}
+
+//Insert Defs and Uses of MI into the sets RegDefs and RegUses.
+void Filler::insertDefsUses(MachineBasicBlock::iterator MI,
+ SmallSet<unsigned, 32>& RegDefs,
+ SmallSet<unsigned, 32>& RegUses)
+{
+ for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
+ const MachineOperand &MO = MI->getOperand(i);
+ if (!MO.isReg())
+ continue;
+
+ unsigned Reg = MO.getReg();
+ if (Reg == 0)
+ continue;
+ if (MO.isDef())
+ RegDefs.insert(Reg);
+ if (MO.isUse())
+ RegUses.insert(Reg);
+
+ }
+}
+
+//returns true if the Reg or its alias is in the RegSet.
+bool Filler::IsRegInSet(SmallSet<unsigned, 32>& RegSet, unsigned Reg)
+{
+ if (RegSet.count(Reg))
+ return true;
+ // check Aliased Registers
+ for (const unsigned *Alias = TM.getRegisterInfo()->getAliasSet(Reg);
+ *Alias; ++ Alias)
+ if (RegSet.count(*Alias))
+ return true;
+
+ return false;
+}
+
+// return true if the candidate is a delay filler.
+bool Filler::isDelayFiller(MachineBasicBlock &MBB,
+ MachineBasicBlock::iterator candidate)
+{
+ if (candidate == MBB.begin())
+ return false;
+ if (candidate->getOpcode() == CBG::UNIMP)
+ return true;
+ const TargetInstrDesc &prevdesc = (--candidate)->getDesc();
+ return prevdesc.hasDelaySlot();
+}
+
+bool Filler::needsUnimp(MachineBasicBlock::iterator I, unsigned &StructSize)
+{
+ if (!I->getDesc().isCall())
+ return false;
+
+ unsigned structSizeOpNum = 0;
+ switch (I->getOpcode()) {
+ default: llvm_unreachable("Unknown call opcode.");
+ case CBG::CALL: structSizeOpNum = 1; break;
+ case CBG::JMPLrr:
+ case CBG::JMPLri: structSizeOpNum = 2; break;
+ }
+
+ const MachineOperand &MO = I->getOperand(structSizeOpNum);
+ if (!MO.isImm())
+ return false;
+ StructSize = MO.getImm();
+ return true;
+}
diff --git a/lib/Target/cbg/FPMover.cpp b/lib/Target/cbg/FPMover.cpp
new file mode 100644
index 0000000..8c2904c
--- /dev/null
+++ b/lib/Target/cbg/FPMover.cpp
@@ -0,0 +1,144 @@
+//===-- FPMover.cpp - cbg double-precision floating point move fixer ----===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Expand FpMOVD/FpABSD/FpNEGD instructions into their single-precision pieces.
+//
+//===----------------------------------------------------------------------===//
+
+#define DEBUG_TYPE "fpmover"
+#include "cbg.h"
+#include "cbgSubtarget.h"
+#include "llvm/CodeGen/MachineFunctionPass.h"
+#include "llvm/CodeGen/MachineInstrBuilder.h"
+#include "llvm/Target/TargetMachine.h"
+#include "llvm/Target/TargetInstrInfo.h"
+#include "llvm/ADT/Statistic.h"
+#include "llvm/Support/Debug.h"
+#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/raw_ostream.h"
+
+#include <iostream>
+
+using namespace llvm;
+
+STATISTIC(NumFpDs , "Number of instructions translated");
+STATISTIC(NoopFpDs, "Number of noop instructions removed");
+
+namespace {
+ struct FPMover : public MachineFunctionPass {
+ /// Target machine description which we query for reg. names, data
+ /// layout, etc.
+ ///
+ TargetMachine &TM;
+
+ static char ID;
+ explicit FPMover(TargetMachine &tm)
+ : MachineFunctionPass(ID), TM(tm) { }
+
+ virtual const char *getPassName() const {
+ return "CBG Double-FP Move Fixer";
+ }
+
+ bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
+ bool runOnMachineFunction(MachineFunction &F);
+ };
+ char FPMover::ID = 0;
+} // end of anonymous namespace
+
+/// createcbgFPMoverPass - Returns a pass that turns FpMOVD
+/// instructions into FMOVS instructions
+///
+FunctionPass *llvm::createcbgFPMoverPass(TargetMachine &tm) {
+ return new FPMover(tm);
+}
+
+/// getDoubleRegPair - Given a DFP register, return the even and odd FP
+/// registers that correspond to it.
+static void getDoubleRegPair(unsigned DoubleReg, unsigned &EvenReg,
+ unsigned &OddReg) {
+ static const unsigned EvenHalvesOfPairs[] = {
+ CBG::F0, CBG::F2, CBG::F4, CBG::F6, CBG::F8, CBG::F10, CBG::F12, CBG::F14,
+ CBG::F16, CBG::F18, CBG::F20, CBG::F22, CBG::F24, CBG::F26, CBG::F28, CBG::F30
+ };
+ static const unsigned OddHalvesOfPairs[] = {
+ CBG::F1, CBG::F3, CBG::F5, CBG::F7, CBG::F9, CBG::F11, CBG::F13, CBG::F15,
+ CBG::F17, CBG::F19, CBG::F21, CBG::F23, CBG::F25, CBG::F27, CBG::F29, CBG::F31
+ };
+ static const unsigned DoubleRegsInOrder[] = {
+ CBG::D0, CBG::D1, CBG::D2, CBG::D3, CBG::D4, CBG::D5, CBG::D6, CBG::D7, CBG::D8,
+ CBG::D9, CBG::D10, CBG::D11, CBG::D12, CBG::D13, CBG::D14, CBG::D15
+ };
+ for (unsigned i = 0; i < sizeof(DoubleRegsInOrder)/sizeof(unsigned); ++i)
+ if (DoubleRegsInOrder[i] == DoubleReg) {
+ EvenReg = EvenHalvesOfPairs[i];
+ OddReg = OddHalvesOfPairs[i];
+ return;
+ }
+ llvm_unreachable("Can't find reg");
+}
+
+/// runOnMachineBasicBlock - Fixup FpMOVD instructions in this MBB.
+///
+bool FPMover::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
+ bool Changed = false;
+ for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ) {
+ MachineInstr *MI = I++;
+ DebugLoc dl = MI->getDebugLoc();
+ if (MI->getOpcode() == CBG::FpMOVD || MI->getOpcode() == CBG::FpABSD ||
+ MI->getOpcode() == CBG::FpNEGD) {
+ Changed = true;
+ unsigned DestDReg = MI->getOperand(0).getReg();
+ unsigned SrcDReg = MI->getOperand(1).getReg();
+ if (DestDReg == SrcDReg && MI->getOpcode() == CBG::FpMOVD) {
+ MBB.erase(MI); // Eliminate the noop copy.
+ ++NoopFpDs;
+ continue;
+ }
+
+ unsigned EvenSrcReg = 0, OddSrcReg = 0, EvenDestReg = 0, OddDestReg = 0;
+ getDoubleRegPair(DestDReg, EvenDestReg, OddDestReg);
+ getDoubleRegPair(SrcDReg, EvenSrcReg, OddSrcReg);
+
+ const TargetInstrInfo *TII = TM.getInstrInfo();
+ if (MI->getOpcode() == CBG::FpMOVD)
+ MI->setDesc(TII->get(CBG::FMOVS));
+ else if (MI->getOpcode() == CBG::FpNEGD)
+ MI->setDesc(TII->get(CBG::FNEGS));
+ else if (MI->getOpcode() == CBG::FpABSD)
+ MI->setDesc(TII->get(CBG::FABSS));
+ else
+ llvm_unreachable("Unknown opcode!");
+
+ MI->getOperand(0).setReg(EvenDestReg);
+ MI->getOperand(1).setReg(EvenSrcReg);
+ DEBUG(errs() << "FPMover: the modified instr is: " << *MI);
+ // Insert copy for the other half of the double.
+ if (DestDReg != SrcDReg) {
+ MI = BuildMI(MBB, I, dl, TM.getInstrInfo()->get(CBG::FMOVS), OddDestReg)
+ .addReg(OddSrcReg);
+ DEBUG(errs() << "FPMover: the inserted instr is: " << *MI);
+ }
+ ++NumFpDs;
+ }
+ }
+ return Changed;
+}
+
+bool FPMover::runOnMachineFunction(MachineFunction &F) {
+ // If the target has V9 instructions, the fp-mover pseudos will never be
+ // emitted. Avoid a scan of the instructions to improve compile time.
+ /*if (TM.getSubtarget<cbgSubtarget>().isV9())
+ return false;*/
+// std::cerr << __func__ << std::endl;
+ bool Changed = false;
+ for (MachineFunction::iterator FI = F.begin(), FE = F.end();
+ FI != FE; ++FI)
+ Changed |= runOnMachineBasicBlock(*FI);
+ return Changed;
+}
diff --git a/lib/Target/cbg/LICENSE.TXT b/lib/Target/cbg/LICENSE.TXT
new file mode 100644
index 0000000..522ff85
--- /dev/null
+++ b/lib/Target/cbg/LICENSE.TXT
@@ -0,0 +1,44 @@
+==============================================================================
+LLVM Release License
+==============================================================================
+University of Illinois/NCSA
+Open Source License
+
+Copyright (c) 2003-2010 University of Illinois at Urbana-Champaign.
+All rights reserved.
+
+Developed by:
+
+ LLVM Team
+
+ University of Illinois at Urbana-Champaign
+
+ http://llvm.org
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal with
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+of the Software, and to permit persons to whom the Software is furnished to do
+so, subject to the following conditions:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimers.
+
+ * Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimers in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the names of the LLVM Team, University of Illinois at
+ Urbana-Champaign, nor the names of its contributors may be used to
+ endorse or promote products derived from this Software without specific
+ prior written permission.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
+SOFTWARE.
+
diff --git a/lib/Target/cbg/Makefile b/lib/Target/cbg/Makefile
new file mode 100644
index 0000000..784ae47
--- /dev/null
+++ b/lib/Target/cbg/Makefile
@@ -0,0 +1,23 @@
+##===- lib/Target/cbg/Makefile ---------------------------*- Makefile -*-===##
+#
+# The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+
+LEVEL = ../../..
+LIBRARYNAME = LLVMcbgCodeGen
+TARGET = cbg
+
+# Make sure that tblgen is run, first thing.
+BUILT_SOURCES = cbgGenRegisterInfo.h.inc cbgGenRegisterNames.inc \
+ cbgGenRegisterInfo.inc cbgGenInstrNames.inc \
+ cbgGenInstrInfo.inc cbgGenAsmWriter.inc \
+ cbgGenDAGISel.inc cbgGenSubtarget.inc cbgGenCallingConv.inc
+
+DIRS = TargetInfo
+
+include $(LEVEL)/Makefile.common
+
diff --git a/lib/Target/cbg/PredecessorList.cpp b/lib/Target/cbg/PredecessorList.cpp
new file mode 100644
index 0000000..6da755d
--- /dev/null
+++ b/lib/Target/cbg/PredecessorList.cpp
@@ -0,0 +1,106 @@
+/*
+ * PredecessorList.cpp
+ *
+ * Created on: Nov 7, 2011
+ * Author: cbg
+ *
+ * This file is distributed under the University of Illinois Open Source
+ * License. See LICENSE.TXT for details.
+ *
+ */
+
+#include "PredecessorList.h"
+
+#include <iostream>
+
+using namespace llvm;
+
+PredecessorList::~PredecessorList() {
+ for (PredList::iterator mbb_it = listEntries.begin();
+ mbb_it != listEntries.end();
+ ++mbb_it) {
+ delete(*mbb_it);
+ }
+}
+
+PredecessorList::ListEntry& PredecessorList::ListEntry::operator=(const ListEntry& le) {
+ if (this != &le) {
+ MBB = le.MBB;
+ predecessor = le.predecessor;
+ }
+ return (*this);
+}
+
+bool PredecessorList::ListEntry::operator==(const MachineBasicBlock* mbb) const {
+ return (mbb->getNumber() == MBB->getNumber());
+}
+
+bool PredecessorList::ListEntry::operator!=(const MachineBasicBlock* mbb) const {
+ return (mbb->getNumber() != MBB->getNumber());
+}
+
+/**
+ * @brief Searches the list if it contains the specified machine basic block and returns
+ * the corresponding list entry.
+ * @param mbb Machine basic block to find.
+ * @return The corresponding list entry on success, NULL if the list does not contain
+ * the specified entry.
+ */
+PredecessorList::ListEntry* PredecessorList::findMachineBasicBlock(const MachineBasicBlock* mbb) {
+ ListEntry* listEntry = 0;
+ PredList::iterator list_it;
+
+ for (list_it = listEntries.begin(); list_it != listEntries.end(); ++list_it) {
+ if (*(*list_it) == mbb) {
+ listEntry = *list_it;
+ break;
+ }
+ }
+ return listEntry;
+}
+
+void PredecessorList::insertRoot(MachineBasicBlock* mbb) {
+ ListEntry* newEntry = new ListEntry(mbb);
+ listEntries.push_back(newEntry);
+}
+
+/**
+ * @brief Inserts a machine basic block with the specified predecessor
+ * into list. If mbb is NULL, nothing will be inserted.
+ * @param mbb The machine basic block which will be inserted.
+ * @param predecessor The predecessor of the basic block.
+ */
+void PredecessorList::insertSuccessor(MachineBasicBlock* mbb,
+ const MachineBasicBlock* predecessor) {
+
+ ListEntry* pred = findMachineBasicBlock(predecessor);
+ ListEntry* newEntry;
+ if (mbb) {
+ newEntry = new ListEntry(mbb, pred);
+ listEntries.push_back(newEntry);
+ }
+
+}
+
+PredecessorList::MBB_list PredecessorList::getPredecessors(const MachineBasicBlock* &mbb) {
+ ListEntry* listEntry;
+// ListEntry* predecessor;
+ MBB_list mbb_list;
+
+// std::cerr << "Adding all predecessors of BB#" << mbb->getNumber() << " to list." << std::endl;
+
+ listEntry = findMachineBasicBlock(mbb);
+ if (mbb) {
+ mbb_list.push_back(const_cast<MachineBasicBlock*>(mbb));
+// std::cerr << "Add BB#" << mbb->getNumber() << std::endl;
+ }
+
+// std::cerr << "Address of predecessor: " << listEntry->getPredecessor() << std::endl;
+
+ while (listEntry->getPredecessor()) {
+ listEntry = listEntry->getPredecessor();
+// std::cerr << "Add BB#" << listEntry->getMachineBasicBlock()->getNumber() << std::endl;
+ mbb_list.push_front(listEntry->getMachineBasicBlock());
+ }
+ return mbb_list;
+}
diff --git a/lib/Target/cbg/PredecessorList.h b/lib/Target/cbg/PredecessorList.h
new file mode 100644
index 0000000..ad0763d
--- /dev/null
+++ b/lib/Target/cbg/PredecessorList.h
@@ -0,0 +1,75 @@
+/*
+ * PredecessorList.h
+ *
+ * Created on: Nov 7, 2011
+ * Author: cbg
+ *
+ * This file is distributed under the University of Illinois Open Source
+ * License. See LICENSE.TXT for details.
+ *
+ */
+
+#ifndef PREDECESSORLIST_H_
+#define PREDECESSORLIST_H_
+
+#include "llvm/CodeGen/MachineBasicBlock.h"
+
+#include <list>
+
+using namespace llvm;
+
+class PredecessorList {
+protected:
+
+ class ListEntry {
+ private:
+ MachineBasicBlock* MBB;
+ ListEntry* predecessor;
+ public:
+ ListEntry(MachineBasicBlock* mbb, ListEntry* pred) :
+ MBB(mbb), predecessor(pred) {}
+
+ ListEntry(MachineBasicBlock* mbb) :
+ MBB(mbb), predecessor(0) {}
+
+ ListEntry* getPredecessor(void) const {
+ return predecessor;
+ }
+
+ MachineBasicBlock* getMachineBasicBlock(void) const {
+ return MBB;
+ }
+
+ ListEntry& operator=(const ListEntry&);
+
+ bool operator==(const MachineBasicBlock*) const;
+ bool operator!=(const MachineBasicBlock*) const;
+ };
+
+ typedef std::list<ListEntry*> PredList;
+
+private:
+ PredList listEntries;
+
+protected:
+ ListEntry* findMachineBasicBlock(const MachineBasicBlock*);
+
+public:
+
+ typedef std::list<MachineBasicBlock*> MBB_list;
+
+ PredecessorList() :
+ listEntries(0) {}
+
+ ~PredecessorList();
+
+ void insertRoot(MachineBasicBlock* mbb);
+ void insertSuccessor(MachineBasicBlock* mbb,
+ const MachineBasicBlock* predecessor);
+
+ MBB_list getPredecessors(const MachineBasicBlock* &mbb);
+
+};
+
+
+#endif /* PREDECESSORLIST_H_ */
diff --git a/lib/Target/cbg/README b/lib/Target/cbg/README
new file mode 100644
index 0000000..5bcc120
--- /dev/null
+++ b/lib/Target/cbg/README
@@ -0,0 +1,27 @@
+LLVM SPARC V8 Backend with Instruction Set Extensions
+
+File: README
+
+Copyright (c) 2012 Clemens Bernhard Geyer <[email protected]>
+
+The current LLVM 2.9 Extension is available under the same license
+conditions as LLVM. See LICENSE.TXT for details.
+
+Version 1.00
+
+How to get the backend working:
+ (1) Download LLVM 2.9 from http://llvm.org/
+ (2) Extract the archive 'tar xvf llvm-2.9.tgz'
+ (4) Copy the patch file (add-cbg-backend.patch) to your llvm root directory
+ (usually something like /home/user/llvm-2.9/)
+ (5) Change to llvm root directory and apply patch file
+ 'patch -p1 < add-cbg-backend.patch'
+ (6) Run configure: './configure --enable-targets=cbg --enable-optimized'
+ (7) Run make with multiple jobs to speed-up the comipling process:
+ 'make -j <NUM>'
+ (8) Change to <llvm-root>/Release/bin directory and test for cbg target:
+ './llvm-as < /dev/null | ./llc --march=cbg --mcpu=help'
+ (9) To be able to run all benchmarks, make sure you install or download llvm-gcc
+