Skip to content

Commit 49e5f60

Browse files
committed
Rename/refactor isIntegerConstantExpression to getIntegerConstantExpression
There is a version that just tests (also called isIntegerConstantExpression) & whereas this version is specifically used when the value is of interest (a few call sites were actually refactored to calling the test-only version) so let's make the API look more like it. Reviewers: aaron.ballman Differential Revision: https://reviews.llvm.org/D76646
1 parent b4dbb37 commit 49e5f60

15 files changed

+255
-275
lines changed

clang/include/clang/AST/Expr.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -510,16 +510,15 @@ class Expr : public ValueStmt {
510510
/// semantically correspond to a bool.
511511
bool isKnownToHaveBooleanValue(bool Semantic = true) const;
512512

513-
/// isIntegerConstantExpr - Return true if this expression is a valid integer
514-
/// constant expression, and, if so, return its value in Result. If not a
515-
/// valid i-c-e, return false and fill in Loc (if specified) with the location
516-
/// of the invalid expression.
513+
/// isIntegerConstantExpr - Return the value if this expression is a valid
514+
/// integer constant expression. If not a valid i-c-e, return None and fill
515+
/// in Loc (if specified) with the location of the invalid expression.
517516
///
518517
/// Note: This does not perform the implicit conversions required by C++11
519518
/// [expr.const]p5.
520-
bool isIntegerConstantExpr(llvm::APSInt &Result, const ASTContext &Ctx,
521-
SourceLocation *Loc = nullptr,
522-
bool isEvaluated = true) const;
519+
Optional<llvm::APSInt> getIntegerConstantExpr(const ASTContext &Ctx,
520+
SourceLocation *Loc = nullptr,
521+
bool isEvaluated = true) const;
523522
bool isIntegerConstantExpr(const ASTContext &Ctx,
524523
SourceLocation *Loc = nullptr) const;
525524

clang/lib/AST/ASTContext.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9471,17 +9471,15 @@ QualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
94719471
const ConstantArrayType* CAT)
94729472
-> std::pair<bool,llvm::APInt> {
94739473
if (VAT) {
9474-
llvm::APSInt TheInt;
9474+
Optional<llvm::APSInt> TheInt;
94759475
Expr *E = VAT->getSizeExpr();
9476-
if (E && E->isIntegerConstantExpr(TheInt, *this))
9477-
return std::make_pair(true, TheInt);
9478-
else
9479-
return std::make_pair(false, TheInt);
9480-
} else if (CAT) {
9481-
return std::make_pair(true, CAT->getSize());
9482-
} else {
9483-
return std::make_pair(false, llvm::APInt());
9476+
if (E && (TheInt = E->getIntegerConstantExpr(*this)))
9477+
return std::make_pair(true, *TheInt);
9478+
return std::make_pair(false, llvm::APSInt());
94849479
}
9480+
if (CAT)
9481+
return std::make_pair(true, CAT->getSize());
9482+
return std::make_pair(false, llvm::APInt());
94859483
};
94869484

94879485
bool HaveLSize, HaveRSize;

clang/lib/AST/ExprConstant.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14883,16 +14883,22 @@ bool Expr::isIntegerConstantExpr(const ASTContext &Ctx,
1488314883
return true;
1488414884
}
1488514885

14886-
bool Expr::isIntegerConstantExpr(llvm::APSInt &Value, const ASTContext &Ctx,
14887-
SourceLocation *Loc, bool isEvaluated) const {
14886+
Optional<llvm::APSInt> Expr::getIntegerConstantExpr(const ASTContext &Ctx,
14887+
SourceLocation *Loc,
14888+
bool isEvaluated) const {
1488814889
assert(!isValueDependent() &&
1488914890
"Expression evaluator can't be called on a dependent expression.");
1489014891

14891-
if (Ctx.getLangOpts().CPlusPlus11)
14892-
return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value, Loc);
14892+
APSInt Value;
14893+
14894+
if (Ctx.getLangOpts().CPlusPlus11) {
14895+
if (EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value, Loc))
14896+
return Value;
14897+
return None;
14898+
}
1489314899

1489414900
if (!isIntegerConstantExpr(Ctx, Loc))
14895-
return false;
14901+
return None;
1489614902

1489714903
// The only possible side-effects here are due to UB discovered in the
1489814904
// evaluation (for instance, INT_MAX + 1). In such a case, we are still
@@ -14906,8 +14912,7 @@ bool Expr::isIntegerConstantExpr(llvm::APSInt &Value, const ASTContext &Ctx,
1490614912
if (!::EvaluateAsInt(this, ExprResult, Ctx, SE_AllowSideEffects, Info))
1490714913
llvm_unreachable("ICE cannot be evaluated!");
1490814914

14909-
Value = ExprResult.Val.getInt();
14910-
return true;
14915+
return ExprResult.Val.getInt();
1491114916
}
1491214917

1491314918
bool Expr::isCXX98IntegralConstantExpr(const ASTContext &Ctx) const {

clang/lib/AST/MicrosoftMangle.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,9 +1372,9 @@ void MicrosoftCXXNameMangler::mangleIntegerLiteral(const llvm::APSInt &Value,
13721372

13731373
void MicrosoftCXXNameMangler::mangleExpression(const Expr *E) {
13741374
// See if this is a constant expression.
1375-
llvm::APSInt Value;
1376-
if (E->isIntegerConstantExpr(Value, Context.getASTContext())) {
1377-
mangleIntegerLiteral(Value, E->getType()->isBooleanType());
1375+
if (Optional<llvm::APSInt> Value =
1376+
E->getIntegerConstantExpr(Context.getASTContext())) {
1377+
mangleIntegerLiteral(*Value, E->getType()->isBooleanType());
13781378
return;
13791379
}
13801380

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 49 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4419,11 +4419,9 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
44194419
} else {
44204420
// If this is required to be a constant, constant fold it so that we
44214421
// know that the generated intrinsic gets a ConstantInt.
4422-
llvm::APSInt Result;
4423-
bool IsConst = E->getArg(i)->isIntegerConstantExpr(Result,getContext());
4424-
assert(IsConst && "Constant arg isn't actually constant?");
4425-
(void)IsConst;
4426-
ArgValue = llvm::ConstantInt::get(getLLVMContext(), Result);
4422+
ArgValue = llvm::ConstantInt::get(
4423+
getLLVMContext(),
4424+
*E->getArg(i)->getIntegerConstantExpr(getContext()));
44274425
}
44284426

44294427
// If the intrinsic arg type is different from the builtin arg type
@@ -5596,13 +5594,14 @@ Value *CodeGenFunction::EmitCommonNeonBuiltinExpr(
55965594
SmallVectorImpl<llvm::Value *> &Ops, Address PtrOp0, Address PtrOp1,
55975595
llvm::Triple::ArchType Arch) {
55985596
// Get the last argument, which specifies the vector type.
5599-
llvm::APSInt NeonTypeConst;
56005597
const Expr *Arg = E->getArg(E->getNumArgs() - 1);
5601-
if (!Arg->isIntegerConstantExpr(NeonTypeConst, getContext()))
5598+
Optional<llvm::APSInt> NeonTypeConst =
5599+
Arg->getIntegerConstantExpr(getContext());
5600+
if (!NeonTypeConst)
56025601
return nullptr;
56035602

56045603
// Determine the type of this overloaded NEON intrinsic.
5605-
NeonTypeFlags Type(NeonTypeConst.getZExtValue());
5604+
NeonTypeFlags Type(NeonTypeConst->getZExtValue());
56065605
bool Usgn = Type.isUnsigned();
56075606
bool Quad = Type.isQuad();
56085607
const bool HasLegalHalfType = getTarget().hasLegalHalfType();
@@ -6885,10 +6884,9 @@ Value *CodeGenFunction::EmitARMBuiltinExpr(unsigned BuiltinID,
68856884
} else {
68866885
// If this is required to be a constant, constant fold it so that we know
68876886
// that the generated intrinsic gets a ConstantInt.
6888-
llvm::APSInt Result;
6889-
bool IsConst = E->getArg(i)->isIntegerConstantExpr(Result, getContext());
6890-
assert(IsConst && "Constant arg isn't actually constant?"); (void)IsConst;
6891-
Ops.push_back(llvm::ConstantInt::get(getLLVMContext(), Result));
6887+
Ops.push_back(llvm::ConstantInt::get(
6888+
getLLVMContext(),
6889+
*E->getArg(i)->getIntegerConstantExpr(getContext())));
68926890
}
68936891
}
68946892

@@ -7099,9 +7097,9 @@ Value *CodeGenFunction::EmitARMBuiltinExpr(unsigned BuiltinID,
70997097

71007098
// Get the last argument, which specifies the vector type.
71017099
assert(HasExtraArg);
7102-
llvm::APSInt Result;
71037100
const Expr *Arg = E->getArg(E->getNumArgs()-1);
7104-
if (!Arg->isIntegerConstantExpr(Result, getContext()))
7101+
Optional<llvm::APSInt> Result = Arg->getIntegerConstantExpr(getContext());
7102+
if (!Result)
71057103
return nullptr;
71067104

71077105
if (BuiltinID == ARM::BI__builtin_arm_vcvtr_f ||
@@ -7114,7 +7112,7 @@ Value *CodeGenFunction::EmitARMBuiltinExpr(unsigned BuiltinID,
71147112
Ty = DoubleTy;
71157113

71167114
// Determine whether this is an unsigned conversion or not.
7117-
bool usgn = Result.getZExtValue() == 1;
7115+
bool usgn = Result->getZExtValue() == 1;
71187116
unsigned Int = usgn ? Intrinsic::arm_vcvtru : Intrinsic::arm_vcvtr;
71197117

71207118
// Call the appropriate intrinsic.
@@ -7123,7 +7121,7 @@ Value *CodeGenFunction::EmitARMBuiltinExpr(unsigned BuiltinID,
71237121
}
71247122

71257123
// Determine the type of this overloaded NEON intrinsic.
7126-
NeonTypeFlags Type(Result.getZExtValue());
7124+
NeonTypeFlags Type = Result->getZExtValue();
71277125
bool usgn = Type.isUnsigned();
71287126
bool rightShift = false;
71297127

@@ -7267,11 +7265,7 @@ Value *CodeGenFunction::EmitARMBuiltinExpr(unsigned BuiltinID,
72677265

72687266
template<typename Integer>
72697267
static Integer GetIntegerConstantValue(const Expr *E, ASTContext &Context) {
7270-
llvm::APSInt IntVal;
7271-
bool IsConst = E->isIntegerConstantExpr(IntVal, Context);
7272-
assert(IsConst && "Sema should have checked this was a constant");
7273-
(void)IsConst;
7274-
return IntVal.getExtValue();
7268+
return E->getIntegerConstantExpr(Context)->getExtValue();
72757269
}
72767270

72777271
static llvm::Value *SignOrZeroExtend(CGBuilderTy &Builder, llvm::Value *V,
@@ -7544,13 +7538,13 @@ static Value *EmitAArch64TblBuiltinExpr(CodeGenFunction &CGF, unsigned BuiltinID
75447538
assert(E->getNumArgs() >= 3);
75457539

75467540
// Get the last argument, which specifies the vector type.
7547-
llvm::APSInt Result;
75487541
const Expr *Arg = E->getArg(E->getNumArgs() - 1);
7549-
if (!Arg->isIntegerConstantExpr(Result, CGF.getContext()))
7542+
Optional<llvm::APSInt> Result = Arg->getIntegerConstantExpr(CGF.getContext());
7543+
if (!Result)
75507544
return nullptr;
75517545

75527546
// Determine the type of this overloaded NEON intrinsic.
7553-
NeonTypeFlags Type(Result.getZExtValue());
7547+
NeonTypeFlags Type = Result->getZExtValue();
75547548
llvm::VectorType *Ty = GetNeonType(&CGF, Type);
75557549
if (!Ty)
75567550
return nullptr;
@@ -8936,11 +8930,9 @@ Value *CodeGenFunction::EmitAArch64BuiltinExpr(unsigned BuiltinID,
89368930
} else {
89378931
// If this is required to be a constant, constant fold it so that we know
89388932
// that the generated intrinsic gets a ConstantInt.
8939-
llvm::APSInt Result;
8940-
bool IsConst = E->getArg(i)->isIntegerConstantExpr(Result, getContext());
8941-
assert(IsConst && "Constant arg isn't actually constant?");
8942-
(void)IsConst;
8943-
Ops.push_back(llvm::ConstantInt::get(getLLVMContext(), Result));
8933+
Ops.push_back(llvm::ConstantInt::get(
8934+
getLLVMContext(),
8935+
*E->getArg(i)->getIntegerConstantExpr(getContext())));
89448936
}
89458937
}
89468938

@@ -8955,12 +8947,11 @@ Value *CodeGenFunction::EmitAArch64BuiltinExpr(unsigned BuiltinID,
89558947
return Result;
89568948
}
89578949

8958-
llvm::APSInt Result;
89598950
const Expr *Arg = E->getArg(E->getNumArgs()-1);
89608951
NeonTypeFlags Type(0);
8961-
if (Arg->isIntegerConstantExpr(Result, getContext()))
8952+
if (Optional<llvm::APSInt> Result = Arg->getIntegerConstantExpr(getContext()))
89628953
// Determine the type of this overloaded NEON intrinsic.
8963-
Type = NeonTypeFlags(Result.getZExtValue());
8954+
Type = NeonTypeFlags(Result->getZExtValue());
89648955

89658956
bool usgn = Type.isUnsigned();
89668957
bool quad = Type.isQuad();
@@ -11791,10 +11782,8 @@ Value *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID,
1179111782

1179211783
// If this is required to be a constant, constant fold it so that we know
1179311784
// that the generated intrinsic gets a ConstantInt.
11794-
llvm::APSInt Result;
11795-
bool IsConst = E->getArg(i)->isIntegerConstantExpr(Result, getContext());
11796-
assert(IsConst && "Constant arg isn't actually constant?"); (void)IsConst;
11797-
Ops.push_back(llvm::ConstantInt::get(getLLVMContext(), Result));
11785+
Ops.push_back(llvm::ConstantInt::get(
11786+
getLLVMContext(), *E->getArg(i)->getIntegerConstantExpr(getContext())));
1179811787
}
1179911788

1180011789
// These exist so that the builtin that takes an immediate can be bounds
@@ -15073,11 +15062,8 @@ Value *CodeGenFunction::EmitSystemZBuiltinExpr(unsigned BuiltinID,
1507315062
llvm::Type *ResultType = ConvertType(E->getType());
1507415063
Value *X = EmitScalarExpr(E->getArg(0));
1507515064
// Constant-fold the M4 and M5 mask arguments.
15076-
llvm::APSInt M4, M5;
15077-
bool IsConstM4 = E->getArg(1)->isIntegerConstantExpr(M4, getContext());
15078-
bool IsConstM5 = E->getArg(2)->isIntegerConstantExpr(M5, getContext());
15079-
assert(IsConstM4 && IsConstM5 && "Constant arg isn't actually constant?");
15080-
(void)IsConstM4; (void)IsConstM5;
15065+
llvm::APSInt M4 = *E->getArg(1)->getIntegerConstantExpr(getContext());
15066+
llvm::APSInt M5 = *E->getArg(2)->getIntegerConstantExpr(getContext());
1508115067
// Check whether this instance can be represented via a LLVM standard
1508215068
// intrinsic. We only support some combinations of M4 and M5.
1508315069
Intrinsic::ID ID = Intrinsic::not_intrinsic;
@@ -15132,10 +15118,7 @@ Value *CodeGenFunction::EmitSystemZBuiltinExpr(unsigned BuiltinID,
1513215118
Value *X = EmitScalarExpr(E->getArg(0));
1513315119
Value *Y = EmitScalarExpr(E->getArg(1));
1513415120
// Constant-fold the M4 mask argument.
15135-
llvm::APSInt M4;
15136-
bool IsConstM4 = E->getArg(2)->isIntegerConstantExpr(M4, getContext());
15137-
assert(IsConstM4 && "Constant arg isn't actually constant?");
15138-
(void)IsConstM4;
15121+
llvm::APSInt M4 = *E->getArg(2)->getIntegerConstantExpr(getContext());
1513915122
// Check whether this instance can be represented via a LLVM standard
1514015123
// intrinsic. We only support some values of M4.
1514115124
Intrinsic::ID ID = Intrinsic::not_intrinsic;
@@ -15169,10 +15152,7 @@ Value *CodeGenFunction::EmitSystemZBuiltinExpr(unsigned BuiltinID,
1516915152
Value *X = EmitScalarExpr(E->getArg(0));
1517015153
Value *Y = EmitScalarExpr(E->getArg(1));
1517115154
// Constant-fold the M4 mask argument.
15172-
llvm::APSInt M4;
15173-
bool IsConstM4 = E->getArg(2)->isIntegerConstantExpr(M4, getContext());
15174-
assert(IsConstM4 && "Constant arg isn't actually constant?");
15175-
(void)IsConstM4;
15155+
llvm::APSInt M4 = *E->getArg(2)->getIntegerConstantExpr(getContext());
1517615156
// Check whether this instance can be represented via a LLVM standard
1517715157
// intrinsic. We only support some values of M4.
1517815158
Intrinsic::ID ID = Intrinsic::not_intrinsic;
@@ -15839,10 +15819,11 @@ CodeGenFunction::EmitNVPTXBuiltinExpr(unsigned BuiltinID, const CallExpr *E) {
1583915819
Address Dst = EmitPointerWithAlignment(E->getArg(0));
1584015820
Value *Src = EmitScalarExpr(E->getArg(1));
1584115821
Value *Ldm = EmitScalarExpr(E->getArg(2));
15842-
llvm::APSInt isColMajorArg;
15843-
if (!E->getArg(3)->isIntegerConstantExpr(isColMajorArg, getContext()))
15822+
Optional<llvm::APSInt> isColMajorArg =
15823+
E->getArg(3)->getIntegerConstantExpr(getContext());
15824+
if (!isColMajorArg)
1584415825
return nullptr;
15845-
bool isColMajor = isColMajorArg.getSExtValue();
15826+
bool isColMajor = isColMajorArg->getSExtValue();
1584615827
NVPTXMmaLdstInfo II = getNVPTXMmaLdstInfo(BuiltinID);
1584715828
unsigned IID = isColMajor ? II.IID_col : II.IID_row;
1584815829
if (IID == 0)
@@ -15883,10 +15864,11 @@ CodeGenFunction::EmitNVPTXBuiltinExpr(unsigned BuiltinID, const CallExpr *E) {
1588315864
Value *Dst = EmitScalarExpr(E->getArg(0));
1588415865
Address Src = EmitPointerWithAlignment(E->getArg(1));
1588515866
Value *Ldm = EmitScalarExpr(E->getArg(2));
15886-
llvm::APSInt isColMajorArg;
15887-
if (!E->getArg(3)->isIntegerConstantExpr(isColMajorArg, getContext()))
15867+
Optional<llvm::APSInt> isColMajorArg =
15868+
E->getArg(3)->getIntegerConstantExpr(getContext());
15869+
if (!isColMajorArg)
1588815870
return nullptr;
15889-
bool isColMajor = isColMajorArg.getSExtValue();
15871+
bool isColMajor = isColMajorArg->getSExtValue();
1589015872
NVPTXMmaLdstInfo II = getNVPTXMmaLdstInfo(BuiltinID);
1589115873
unsigned IID = isColMajor ? II.IID_col : II.IID_row;
1589215874
if (IID == 0)
@@ -15933,16 +15915,20 @@ CodeGenFunction::EmitNVPTXBuiltinExpr(unsigned BuiltinID, const CallExpr *E) {
1593315915
Address SrcA = EmitPointerWithAlignment(E->getArg(1));
1593415916
Address SrcB = EmitPointerWithAlignment(E->getArg(2));
1593515917
Address SrcC = EmitPointerWithAlignment(E->getArg(3));
15936-
llvm::APSInt LayoutArg;
15937-
if (!E->getArg(4)->isIntegerConstantExpr(LayoutArg, getContext()))
15918+
Optional<llvm::APSInt> LayoutArg =
15919+
E->getArg(4)->getIntegerConstantExpr(getContext());
15920+
if (!LayoutArg)
1593815921
return nullptr;
15939-
int Layout = LayoutArg.getSExtValue();
15922+
int Layout = LayoutArg->getSExtValue();
1594015923
if (Layout < 0 || Layout > 3)
1594115924
return nullptr;
1594215925
llvm::APSInt SatfArg;
1594315926
if (BuiltinID == NVPTX::BI__bmma_m8n8k128_mma_xor_popc_b1)
1594415927
SatfArg = 0; // .b1 does not have satf argument.
15945-
else if (!E->getArg(5)->isIntegerConstantExpr(SatfArg, getContext()))
15928+
else if (Optional<llvm::APSInt> OptSatfArg =
15929+
E->getArg(5)->getIntegerConstantExpr(getContext()))
15930+
SatfArg = *OptSatfArg;
15931+
else
1594615932
return nullptr;
1594715933
bool Satf = SatfArg.getSExtValue();
1594815934
NVPTXMmaInfo MI = getNVPTXMmaInfo(BuiltinID);
@@ -16271,9 +16257,8 @@ Value *CodeGenFunction::EmitWebAssemblyBuiltinExpr(unsigned BuiltinID,
1627116257
case WebAssembly::BI__builtin_wasm_extract_lane_i64x2:
1627216258
case WebAssembly::BI__builtin_wasm_extract_lane_f32x4:
1627316259
case WebAssembly::BI__builtin_wasm_extract_lane_f64x2: {
16274-
llvm::APSInt LaneConst;
16275-
if (!E->getArg(1)->isIntegerConstantExpr(LaneConst, getContext()))
16276-
llvm_unreachable("Constant arg isn't actually constant?");
16260+
llvm::APSInt LaneConst =
16261+
*E->getArg(1)->getIntegerConstantExpr(getContext());
1627716262
Value *Vec = EmitScalarExpr(E->getArg(0));
1627816263
Value *Lane = llvm::ConstantInt::get(getLLVMContext(), LaneConst);
1627916264
Value *Extract = Builder.CreateExtractElement(Vec, Lane);
@@ -16299,9 +16284,8 @@ Value *CodeGenFunction::EmitWebAssemblyBuiltinExpr(unsigned BuiltinID,
1629916284
case WebAssembly::BI__builtin_wasm_replace_lane_i64x2:
1630016285
case WebAssembly::BI__builtin_wasm_replace_lane_f32x4:
1630116286
case WebAssembly::BI__builtin_wasm_replace_lane_f64x2: {
16302-
llvm::APSInt LaneConst;
16303-
if (!E->getArg(1)->isIntegerConstantExpr(LaneConst, getContext()))
16304-
llvm_unreachable("Constant arg isn't actually constant?");
16287+
llvm::APSInt LaneConst =
16288+
*E->getArg(1)->getIntegerConstantExpr(getContext());
1630516289
Value *Vec = EmitScalarExpr(E->getArg(0));
1630616290
Value *Lane = llvm::ConstantInt::get(getLLVMContext(), LaneConst);
1630716291
Value *Val = EmitScalarExpr(E->getArg(2));

0 commit comments

Comments
 (0)