@@ -30,7 +30,7 @@ template<typename T>
30
30
struct CAPIDenseMap {};
31
31
32
32
// The default DenseMapInfo require to know about pointer alignment.
33
- // Because the C API uses opaques pointer types, their alignment is unknown.
33
+ // Because the C API uses opaque pointer types, their alignment is unknown.
34
34
// As a result, we need to roll out our own implementation.
35
35
template <typename T>
36
36
struct CAPIDenseMap <T*> {
@@ -306,7 +306,7 @@ static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M) {
306
306
return LLVMConstArray (LLVMGetElementType (Ty), Elts.data (), EltCount);
307
307
}
308
308
309
- // Try contant data array
309
+ // Try constant data array
310
310
if (LLVMIsAConstantDataArray (Cst)) {
311
311
check_value_kind (Cst, LLVMConstantDataArrayValueKind);
312
312
LLVMTypeRef Ty = TypeCloner (M).Clone (Cst);
@@ -357,9 +357,32 @@ static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M) {
357
357
report_fatal_error (" ConstantFP is not supported" );
358
358
}
359
359
360
- // This kind of constant is not supported
360
+ // Try ConstantVector
361
+ if (LLVMIsAConstantVector (Cst)) {
362
+ check_value_kind (Cst, LLVMConstantVectorValueKind);
363
+ LLVMTypeRef Ty = TypeCloner (M).Clone (Cst);
364
+ unsigned EltCount = LLVMGetVectorSize (Ty);
365
+ SmallVector<LLVMValueRef, 8 > Elts;
366
+ for (unsigned i = 0 ; i < EltCount; i++)
367
+ Elts.push_back (clone_constant (LLVMGetOperand (Cst, i), M));
368
+ return LLVMConstVector (Elts.data (), EltCount);
369
+ }
370
+
371
+ // Try ConstantDataVector
372
+ if (LLVMIsAConstantDataVector (Cst)) {
373
+ check_value_kind (Cst, LLVMConstantDataVectorValueKind);
374
+ LLVMTypeRef Ty = TypeCloner (M).Clone (Cst);
375
+ unsigned EltCount = LLVMGetVectorSize (Ty);
376
+ SmallVector<LLVMValueRef, 8 > Elts;
377
+ for (unsigned i = 0 ; i < EltCount; i++)
378
+ Elts.push_back (clone_constant (LLVMGetElementAsConstant (Cst, i), M));
379
+ return LLVMConstVector (Elts.data (), EltCount);
380
+ }
381
+
382
+ // At this point, if it's not a constant expression, it's a kind of constant
383
+ // which is not supported
361
384
if (!LLVMIsAConstantExpr (Cst))
362
- report_fatal_error (" Expected a constant expression " );
385
+ report_fatal_error (" Unsupported constant kind " );
363
386
364
387
// At this point, it must be a constant expression
365
388
check_value_kind (Cst, LLVMConstantExprValueKind);
@@ -370,7 +393,8 @@ static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M) {
370
393
return LLVMConstBitCast (clone_constant (LLVMGetOperand (Cst, 0 ), M),
371
394
TypeCloner (M).Clone (Cst));
372
395
default :
373
- fprintf (stderr, " %d is not a supported opcode\n " , Op);
396
+ fprintf (stderr, " %d is not a supported opcode for constant expressions\n " ,
397
+ Op);
374
398
exit (-1 );
375
399
}
376
400
}
@@ -443,7 +467,7 @@ struct FunCloner {
443
467
auto i = VMap.find (Src);
444
468
if (i != VMap.end ()) {
445
469
// If we have a hit, it means we already generated the instruction
446
- // as a dependancy to somethign else. We need to make sure
470
+ // as a dependency to something else. We need to make sure
447
471
// it is ordered properly.
448
472
auto I = i->second ;
449
473
LLVMInstructionRemoveFromParent (I);
@@ -746,21 +770,55 @@ struct FunCloner {
746
770
}
747
771
case LLVMExtractValue: {
748
772
LLVMValueRef Agg = CloneValue (LLVMGetOperand (Src, 0 ));
749
- if (LLVMGetNumIndices (Src) != 1 )
750
- report_fatal_error (" Expected only one indice" );
773
+ if (LLVMGetNumIndices (Src) > 1 )
774
+ report_fatal_error (" ExtractValue: Expected only one index" );
775
+ else if (LLVMGetNumIndices (Src) < 1 )
776
+ report_fatal_error (" ExtractValue: Expected an index" );
751
777
auto I = LLVMGetIndices (Src)[0 ];
752
778
Dst = LLVMBuildExtractValue (Builder, Agg, I, Name);
753
779
break ;
754
780
}
755
781
case LLVMInsertValue: {
756
782
LLVMValueRef Agg = CloneValue (LLVMGetOperand (Src, 0 ));
757
783
LLVMValueRef V = CloneValue (LLVMGetOperand (Src, 1 ));
758
- if (LLVMGetNumIndices (Src) != 1 )
759
- report_fatal_error (" Expected only one indice" );
784
+ if (LLVMGetNumIndices (Src) > 1 )
785
+ report_fatal_error (" InsertValue: Expected only one index" );
786
+ else if (LLVMGetNumIndices (Src) < 1 )
787
+ report_fatal_error (" InsertValue: Expected an index" );
760
788
auto I = LLVMGetIndices (Src)[0 ];
761
789
Dst = LLVMBuildInsertValue (Builder, Agg, V, I, Name);
762
790
break ;
763
791
}
792
+ case LLVMExtractElement: {
793
+ LLVMValueRef Agg = CloneValue (LLVMGetOperand (Src, 0 ));
794
+ LLVMValueRef Index = CloneValue (LLVMGetOperand (Src, 1 ));
795
+ Dst = LLVMBuildExtractElement (Builder, Agg, Index, Name);
796
+ break ;
797
+ }
798
+ case LLVMInsertElement: {
799
+ LLVMValueRef Agg = CloneValue (LLVMGetOperand (Src, 0 ));
800
+ LLVMValueRef V = CloneValue (LLVMGetOperand (Src, 1 ));
801
+ LLVMValueRef Index = CloneValue (LLVMGetOperand (Src, 2 ));
802
+ Dst = LLVMBuildInsertElement (Builder, Agg, V, Index, Name);
803
+ break ;
804
+ }
805
+ case LLVMShuffleVector: {
806
+ LLVMValueRef Agg0 = CloneValue (LLVMGetOperand (Src, 0 ));
807
+ LLVMValueRef Agg1 = CloneValue (LLVMGetOperand (Src, 1 ));
808
+ SmallVector<LLVMValueRef, 8 > MaskElts;
809
+ unsigned NumMaskElts = LLVMGetNumMaskElements (Src);
810
+ for (unsigned i = 0 ; i < NumMaskElts; i++) {
811
+ int Val = LLVMGetMaskValue (Src, i);
812
+ if (Val == LLVMUndefMaskElem) {
813
+ MaskElts.push_back (LLVMGetUndef (LLVMInt64Type ()));
814
+ } else {
815
+ MaskElts.push_back (LLVMConstInt (LLVMInt64Type (), Val, true ));
816
+ }
817
+ }
818
+ LLVMValueRef Mask = LLVMConstVector (MaskElts.data (), NumMaskElts);
819
+ Dst = LLVMBuildShuffleVector (Builder, Agg0, Agg1, Mask, Name);
820
+ break ;
821
+ }
764
822
case LLVMFreeze: {
765
823
LLVMValueRef Arg = CloneValue (LLVMGetOperand (Src, 0 ));
766
824
Dst = LLVMBuildFreeze (Builder, Arg, Name);
@@ -1102,7 +1160,7 @@ static void clone_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
1102
1160
LLVMGlobalSetMetadata (G, Kind, MD);
1103
1161
}
1104
1162
LLVMDisposeValueMetadataEntries (AllMetadata);
1105
-
1163
+
1106
1164
LLVMSetGlobalConstant (G, LLVMIsGlobalConstant (Cur));
1107
1165
LLVMSetThreadLocal (G, LLVMIsThreadLocal (Cur));
1108
1166
LLVMSetExternallyInitialized (G, LLVMIsExternallyInitialized (Cur));
0 commit comments