Skip to content

Commit cc4f398

Browse files
authored
Merge pull request swiftlang#20694 from carlos4242/master
Improve the compiler around 16-bit pointers
2 parents e94361e + 3d61e9f commit cc4f398

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

lib/IRGen/GenType.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ static llvm::Value *computeExtraTagBytes(IRGenFunction &IGF, IRBuilder &Builder,
392392
auto *entryBB = Builder.GetInsertBlock();
393393
llvm::Value *size = asSizeConstant(IGM, fixedSize);
394394
auto *returnBB = llvm::BasicBlock::Create(Ctx);
395-
size = Builder.CreateTrunc(size, int32Ty); // We know size < 4.
395+
size = Builder.CreateZExtOrTrunc(size, int32Ty); // We know size < 4.
396396

397397
auto *two = llvm::ConstantInt::get(int32Ty, 2U);
398398
auto *four = llvm::ConstantInt::get(int32Ty, 4U);
@@ -487,7 +487,7 @@ llvm::Value *FixedTypeInfo::getEnumTagSinglePayload(IRGenFunction &IGF,
487487

488488
Builder.emitBlock(extraTagBitsBB);
489489

490-
auto *truncSize = Builder.CreateTrunc(size, IGM.Int32Ty);
490+
auto *truncSize = Builder.CreateZExtOrTrunc(size, IGM.Int32Ty);
491491
Address caseIndexFromValueSlot = IGF.createAlloca(IGM.Int32Ty, Alignment(4));
492492
Builder.CreateStore(zero, caseIndexFromValueSlot);
493493

@@ -683,7 +683,7 @@ void FixedTypeInfo::storeEnumTagSinglePayload(IRGenFunction &IGF,
683683
auto *nonPayloadElementIndex = Builder.CreateSub(whichCase, one);
684684
auto *caseIndex =
685685
Builder.CreateSub(nonPayloadElementIndex, numExtraInhabitants);
686-
auto *truncSize = Builder.CreateTrunc(size, IGM.Int32Ty);
686+
auto *truncSize = Builder.CreateZExtOrTrunc(size, IGM.Int32Ty);
687687
auto *isFourBytesPayload = Builder.CreateICmpUGE(truncSize, four);
688688
auto *payloadGE4BB = Builder.GetInsertBlock();
689689
auto *payloadLT4BB = llvm::BasicBlock::Create(Ctx);

lib/IRGen/IRGenModule.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,18 @@ IRGenModule::IRGenModule(IRGenerator &irgen,
169169
Int8PtrPtrTy = Int8PtrTy->getPointerTo(0);
170170
SizeTy = DataLayout.getIntPtrType(getLLVMContext(), /*addrspace*/ 0);
171171

172+
// For the relative address type, we want to use the int32 bit type
173+
// on most architectures, e.g. x86_64, because it produces valid
174+
// fixups/relocations. The exception is 16-bit architectures,
175+
// so we shorten the relative address type there.
176+
if (SizeTy->getBitWidth()<32) {
177+
RelativeAddressTy = SizeTy;
178+
} else {
179+
RelativeAddressTy = Int32Ty;
180+
}
181+
182+
RelativeAddressPtrTy = RelativeAddressTy->getPointerTo();
183+
172184
FloatTy = llvm::Type::getFloatTy(getLLVMContext());
173185
DoubleTy = llvm::Type::getDoubleTy(getLLVMContext());
174186

lib/IRGen/IRGenModule.h

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -511,14 +511,10 @@ class IRGenModule {
511511
llvm::IntegerType *Int1Ty; /// i1
512512
llvm::IntegerType *Int8Ty; /// i8
513513
llvm::IntegerType *Int16Ty; /// i16
514-
union {
515-
llvm::IntegerType *Int32Ty; /// i32
516-
llvm::IntegerType *RelativeAddressTy;
517-
};
518-
union {
519-
llvm::PointerType *Int32PtrTy; /// i32 *
520-
llvm::PointerType *RelativeAddressPtrTy;
521-
};
514+
llvm::IntegerType *Int32Ty; /// i32
515+
llvm::PointerType *Int32PtrTy; /// i32 *
516+
llvm::IntegerType *RelativeAddressTy;
517+
llvm::PointerType *RelativeAddressPtrTy;
522518
llvm::IntegerType *Int64Ty; /// i64
523519
union {
524520
llvm::IntegerType *SizeTy; /// usually i32 or i64
@@ -541,7 +537,7 @@ class IRGenModule {
541537
llvm::PointerType *WitnessTablePtrTy;
542538
};
543539
llvm::StructType *RefCountedStructTy;/// %swift.refcounted = type { ... }
544-
Size RefCountedStructSize; /// sizeof(%swift.refcounted)
540+
Size RefCountedStructSize; /// sizeof(%swift.refcounted)
545541
llvm::PointerType *RefCountedPtrTy; /// %swift.refcounted*
546542
#define CHECKED_REF_STORAGE(Name, ...) \
547543
llvm::PointerType *Name##ReferencePtrTy; /// %swift. #name _reference*
@@ -569,11 +565,11 @@ class IRGenModule {
569565
llvm::PointerType *ProtocolDescriptorPtrTy; /// %swift.protocol*
570566
llvm::StructType *ProtocolRequirementStructTy; /// %swift.protocol_requirement
571567
union {
572-
llvm::PointerType *ObjCPtrTy; /// %objc_object*
568+
llvm::PointerType *ObjCPtrTy; /// %objc_object*
573569
llvm::PointerType *UnknownRefCountedPtrTy;
574570
};
575-
llvm::PointerType *BridgeObjectPtrTy; /// %swift.bridge*
576-
llvm::StructType *OpaqueTy; /// %swift.opaque
571+
llvm::PointerType *BridgeObjectPtrTy;/// %swift.bridge*
572+
llvm::StructType *OpaqueTy; /// %swift.opaque
577573
llvm::PointerType *OpaquePtrTy; /// %swift.opaque*
578574
llvm::StructType *ObjCClassStructTy; /// %objc_class
579575
llvm::PointerType *ObjCClassPtrTy; /// %objc_class*

0 commit comments

Comments
 (0)