Skip to content

Commit

Permalink
Refactor handling of signext/zeroext in ABIArgInfo
Browse files Browse the repository at this point in the history
As @rjmccall suggested in D40023, we can get rid of 
ABIInfo::shouldSignExtUnsignedType (used to handle cases like the Mips calling 
convention where 32-bit integers are always sign extended regardless of the 
sign of the type) by adding a SignExt field to ABIArgInfo. In the common case, 
this new field is set automatically by ABIArgInfo::getExtend based on the sign 
of the type. For targets that want greater control, they can use 
ABIArgInfo::getSignExtend or ABIArgInfo::getZeroExtend when necessary. This 
change also cleans up logic in CGCall.cpp.

There is no functional change intended in this patch, and all tests pass 
unchanged. As noted in D40023, Mips might want to sign-extend unsigned 32-bit 
integer return types. A future patch might modify 
MipsABIInfo::classifyReturnType to use MipsABIInfo::extendType.

Differential Revision: https://reviews.llvm.org/D41999

llvm-svn: 322396
  • Loading branch information
asb committed Jan 12, 2018
1 parent fc63551 commit e41a5e2
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 65 deletions.
39 changes: 36 additions & 3 deletions clang/include/clang/CodeGen/CGFunctionInfo.h
Expand Up @@ -95,6 +95,7 @@ class ABIArgInfo {
bool SRetAfterThis : 1; // isIndirect()
bool InReg : 1; // isDirect() || isExtend() || isIndirect()
bool CanBeFlattened: 1; // isDirect()
bool SignExt : 1; // isExtend()

bool canHavePaddingType() const {
return isDirect() || isExtend() || isIndirect() || isExpand();
Expand Down Expand Up @@ -133,15 +134,38 @@ class ABIArgInfo {
AI.setInReg(true);
return AI;
}
static ABIArgInfo getExtend(llvm::Type *T = nullptr) {

static ABIArgInfo getSignExtend(QualType Ty, llvm::Type *T = nullptr) {
assert(Ty->isIntegralOrEnumerationType() && "Unexpected QualType");
auto AI = ABIArgInfo(Extend);
AI.setCoerceToType(T);
AI.setPaddingType(nullptr);
AI.setDirectOffset(0);
AI.setSignExt(true);
return AI;
}

static ABIArgInfo getZeroExtend(QualType Ty, llvm::Type *T = nullptr) {
assert(Ty->isIntegralOrEnumerationType() && "Unexpected QualType");
auto AI = ABIArgInfo(Extend);
AI.setCoerceToType(T);
AI.setPaddingType(nullptr);
AI.setDirectOffset(0);
AI.setSignExt(false);
return AI;
}
static ABIArgInfo getExtendInReg(llvm::Type *T = nullptr) {
auto AI = getExtend(T);

// ABIArgInfo will record the argument as being extended based on the sign
// of its type.
static ABIArgInfo getExtend(QualType Ty, llvm::Type *T = nullptr) {
assert(Ty->isIntegralOrEnumerationType() && "Unexpected QualType");
if (Ty->hasSignedIntegerRepresentation())
return getSignExtend(Ty, T);
return getZeroExtend(Ty, T);
}

static ABIArgInfo getExtendInReg(QualType Ty, llvm::Type *T = nullptr) {
auto AI = getExtend(Ty, T);
AI.setInReg(true);
return AI;
}
Expand Down Expand Up @@ -254,6 +278,15 @@ class ABIArgInfo {
DirectOffset = Offset;
}

bool isSignExt() const {
assert(isExtend() && "Invalid kind!");
return SignExt;
}
void setSignExt(bool SExt) {
assert(isExtend() && "Invalid kind!");
SignExt = SExt;
}

llvm::Type *getPaddingType() const {
return (canHavePaddingType() ? PaddingType : nullptr);
}
Expand Down
2 changes: 0 additions & 2 deletions clang/lib/CodeGen/ABIInfo.h
Expand Up @@ -108,8 +108,6 @@ namespace swiftcall {
virtual bool isHomogeneousAggregateSmallEnough(const Type *Base,
uint64_t Members) const;

virtual bool shouldSignExtUnsignedType(QualType Ty) const;

bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
uint64_t &Members) const;

Expand Down
14 changes: 5 additions & 9 deletions clang/lib/CodeGen/CGCall.cpp
Expand Up @@ -1925,9 +1925,9 @@ void CodeGenModule::ConstructAttributeList(
const ABIArgInfo &RetAI = FI.getReturnInfo();
switch (RetAI.getKind()) {
case ABIArgInfo::Extend:
if (RetTy->hasSignedIntegerRepresentation())
if (RetAI.isSignExt())
RetAttrs.addAttribute(llvm::Attribute::SExt);
else if (RetTy->hasUnsignedIntegerRepresentation())
else
RetAttrs.addAttribute(llvm::Attribute::ZExt);
LLVM_FALLTHROUGH;
case ABIArgInfo::Direct:
Expand Down Expand Up @@ -2006,14 +2006,10 @@ void CodeGenModule::ConstructAttributeList(
// sense to do it here because parameters are so messed up.
switch (AI.getKind()) {
case ABIArgInfo::Extend:
if (ParamType->isSignedIntegerOrEnumerationType())
if (AI.isSignExt())
Attrs.addAttribute(llvm::Attribute::SExt);
else if (ParamType->isUnsignedIntegerOrEnumerationType()) {
if (getTypes().getABIInfo().shouldSignExtUnsignedType(ParamType))
Attrs.addAttribute(llvm::Attribute::SExt);
else
Attrs.addAttribute(llvm::Attribute::ZExt);
}
else
Attrs.addAttribute(llvm::Attribute::ZExt);
LLVM_FALLTHROUGH;
case ABIArgInfo::Direct:
if (ArgNo == 0 && FI.isChainCall())
Expand Down

0 comments on commit e41a5e2

Please sign in to comment.