Skip to content

Commit d4b0aa9

Browse files
committed
[NFC][SCEV] BuildConstantFromSCEV(): reformat, NFC
Makes diff in next commit more readable
1 parent 3355284 commit d4b0aa9

File tree

1 file changed

+85
-82
lines changed

1 file changed

+85
-82
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 85 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -7971,97 +7971,100 @@ const SCEV *ScalarEvolution::getSCEVAtScope(const SCEV *V, const Loop *L) {
79717971
/// Returns NULL if the SCEV isn't representable as a Constant.
79727972
static Constant *BuildConstantFromSCEV(const SCEV *V) {
79737973
switch (static_cast<SCEVTypes>(V->getSCEVType())) {
7974-
case scCouldNotCompute:
7975-
case scAddRecExpr:
7976-
break;
7977-
case scConstant:
7978-
return cast<SCEVConstant>(V)->getValue();
7979-
case scUnknown:
7980-
return dyn_cast<Constant>(cast<SCEVUnknown>(V)->getValue());
7981-
case scSignExtend: {
7982-
const SCEVSignExtendExpr *SS = cast<SCEVSignExtendExpr>(V);
7983-
if (Constant *CastOp = BuildConstantFromSCEV(SS->getOperand()))
7984-
return ConstantExpr::getSExt(CastOp, SS->getType());
7985-
break;
7986-
}
7987-
case scZeroExtend: {
7988-
const SCEVZeroExtendExpr *SZ = cast<SCEVZeroExtendExpr>(V);
7989-
if (Constant *CastOp = BuildConstantFromSCEV(SZ->getOperand()))
7990-
return ConstantExpr::getZExt(CastOp, SZ->getType());
7991-
break;
7992-
}
7993-
case scTruncate: {
7994-
const SCEVTruncateExpr *ST = cast<SCEVTruncateExpr>(V);
7995-
if (Constant *CastOp = BuildConstantFromSCEV(ST->getOperand()))
7996-
return ConstantExpr::getTrunc(CastOp, ST->getType());
7997-
break;
7998-
}
7999-
case scAddExpr: {
8000-
const SCEVAddExpr *SA = cast<SCEVAddExpr>(V);
8001-
if (Constant *C = BuildConstantFromSCEV(SA->getOperand(0))) {
8002-
if (PointerType *PTy = dyn_cast<PointerType>(C->getType())) {
8003-
unsigned AS = PTy->getAddressSpace();
7974+
case scCouldNotCompute:
7975+
case scAddRecExpr:
7976+
break;
7977+
case scConstant:
7978+
return cast<SCEVConstant>(V)->getValue();
7979+
case scUnknown:
7980+
return dyn_cast<Constant>(cast<SCEVUnknown>(V)->getValue());
7981+
case scSignExtend: {
7982+
const SCEVSignExtendExpr *SS = cast<SCEVSignExtendExpr>(V);
7983+
if (Constant *CastOp = BuildConstantFromSCEV(SS->getOperand()))
7984+
return ConstantExpr::getSExt(CastOp, SS->getType());
7985+
break;
7986+
}
7987+
case scZeroExtend: {
7988+
const SCEVZeroExtendExpr *SZ = cast<SCEVZeroExtendExpr>(V);
7989+
if (Constant *CastOp = BuildConstantFromSCEV(SZ->getOperand()))
7990+
return ConstantExpr::getZExt(CastOp, SZ->getType());
7991+
break;
7992+
}
7993+
case scTruncate: {
7994+
const SCEVTruncateExpr *ST = cast<SCEVTruncateExpr>(V);
7995+
if (Constant *CastOp = BuildConstantFromSCEV(ST->getOperand()))
7996+
return ConstantExpr::getTrunc(CastOp, ST->getType());
7997+
break;
7998+
}
7999+
case scAddExpr: {
8000+
const SCEVAddExpr *SA = cast<SCEVAddExpr>(V);
8001+
if (Constant *C = BuildConstantFromSCEV(SA->getOperand(0))) {
8002+
if (PointerType *PTy = dyn_cast<PointerType>(C->getType())) {
8003+
unsigned AS = PTy->getAddressSpace();
8004+
Type *DestPtrTy = Type::getInt8PtrTy(C->getContext(), AS);
8005+
C = ConstantExpr::getBitCast(C, DestPtrTy);
8006+
}
8007+
for (unsigned i = 1, e = SA->getNumOperands(); i != e; ++i) {
8008+
Constant *C2 = BuildConstantFromSCEV(SA->getOperand(i));
8009+
if (!C2)
8010+
return nullptr;
8011+
8012+
// First pointer!
8013+
if (!C->getType()->isPointerTy() && C2->getType()->isPointerTy()) {
8014+
unsigned AS = C2->getType()->getPointerAddressSpace();
8015+
std::swap(C, C2);
80048016
Type *DestPtrTy = Type::getInt8PtrTy(C->getContext(), AS);
8017+
// The offsets have been converted to bytes. We can add bytes to an
8018+
// i8* by GEP with the byte count in the first index.
80058019
C = ConstantExpr::getBitCast(C, DestPtrTy);
80068020
}
8007-
for (unsigned i = 1, e = SA->getNumOperands(); i != e; ++i) {
8008-
Constant *C2 = BuildConstantFromSCEV(SA->getOperand(i));
8009-
if (!C2) return nullptr;
8010-
8011-
// First pointer!
8012-
if (!C->getType()->isPointerTy() && C2->getType()->isPointerTy()) {
8013-
unsigned AS = C2->getType()->getPointerAddressSpace();
8014-
std::swap(C, C2);
8015-
Type *DestPtrTy = Type::getInt8PtrTy(C->getContext(), AS);
8016-
// The offsets have been converted to bytes. We can add bytes to an
8017-
// i8* by GEP with the byte count in the first index.
8018-
C = ConstantExpr::getBitCast(C, DestPtrTy);
8019-
}
80208021

8021-
// Don't bother trying to sum two pointers. We probably can't
8022-
// statically compute a load that results from it anyway.
8023-
if (C2->getType()->isPointerTy())
8024-
return nullptr;
8022+
// Don't bother trying to sum two pointers. We probably can't
8023+
// statically compute a load that results from it anyway.
8024+
if (C2->getType()->isPointerTy())
8025+
return nullptr;
80258026

8026-
if (PointerType *PTy = dyn_cast<PointerType>(C->getType())) {
8027-
if (PTy->getElementType()->isStructTy())
8028-
C2 = ConstantExpr::getIntegerCast(
8029-
C2, Type::getInt32Ty(C->getContext()), true);
8030-
C = ConstantExpr::getGetElementPtr(PTy->getElementType(), C, C2);
8031-
} else
8032-
C = ConstantExpr::getAdd(C, C2);
8033-
}
8034-
return C;
8027+
if (PointerType *PTy = dyn_cast<PointerType>(C->getType())) {
8028+
if (PTy->getElementType()->isStructTy())
8029+
C2 = ConstantExpr::getIntegerCast(
8030+
C2, Type::getInt32Ty(C->getContext()), true);
8031+
C = ConstantExpr::getGetElementPtr(PTy->getElementType(), C, C2);
8032+
} else
8033+
C = ConstantExpr::getAdd(C, C2);
80358034
}
8036-
break;
8035+
return C;
80378036
}
8038-
case scMulExpr: {
8039-
const SCEVMulExpr *SM = cast<SCEVMulExpr>(V);
8040-
if (Constant *C = BuildConstantFromSCEV(SM->getOperand(0))) {
8041-
// Don't bother with pointers at all.
8042-
if (C->getType()->isPointerTy()) return nullptr;
8043-
for (unsigned i = 1, e = SM->getNumOperands(); i != e; ++i) {
8044-
Constant *C2 = BuildConstantFromSCEV(SM->getOperand(i));
8045-
if (!C2 || C2->getType()->isPointerTy()) return nullptr;
8046-
C = ConstantExpr::getMul(C, C2);
8047-
}
8048-
return C;
8037+
break;
8038+
}
8039+
case scMulExpr: {
8040+
const SCEVMulExpr *SM = cast<SCEVMulExpr>(V);
8041+
if (Constant *C = BuildConstantFromSCEV(SM->getOperand(0))) {
8042+
// Don't bother with pointers at all.
8043+
if (C->getType()->isPointerTy())
8044+
return nullptr;
8045+
for (unsigned i = 1, e = SM->getNumOperands(); i != e; ++i) {
8046+
Constant *C2 = BuildConstantFromSCEV(SM->getOperand(i));
8047+
if (!C2 || C2->getType()->isPointerTy())
8048+
return nullptr;
8049+
C = ConstantExpr::getMul(C, C2);
80498050
}
8050-
break;
8051-
}
8052-
case scUDivExpr: {
8053-
const SCEVUDivExpr *SU = cast<SCEVUDivExpr>(V);
8054-
if (Constant *LHS = BuildConstantFromSCEV(SU->getLHS()))
8055-
if (Constant *RHS = BuildConstantFromSCEV(SU->getRHS()))
8056-
if (LHS->getType() == RHS->getType())
8057-
return ConstantExpr::getUDiv(LHS, RHS);
8058-
break;
8051+
return C;
80598052
}
8060-
case scSMaxExpr:
8061-
case scUMaxExpr:
8062-
case scSMinExpr:
8063-
case scUMinExpr:
8064-
break; // TODO: smax, umax, smin, umax.
8053+
break;
8054+
}
8055+
case scUDivExpr: {
8056+
const SCEVUDivExpr *SU = cast<SCEVUDivExpr>(V);
8057+
if (Constant *LHS = BuildConstantFromSCEV(SU->getLHS()))
8058+
if (Constant *RHS = BuildConstantFromSCEV(SU->getRHS()))
8059+
if (LHS->getType() == RHS->getType())
8060+
return ConstantExpr::getUDiv(LHS, RHS);
8061+
break;
8062+
}
8063+
case scSMaxExpr:
8064+
case scUMaxExpr:
8065+
case scSMinExpr:
8066+
case scUMinExpr:
8067+
break; // TODO: smax, umax, smin, umax.
80658068
}
80668069
return nullptr;
80678070
}

0 commit comments

Comments
 (0)