Please use GitHub pull requests for new patches. Phabricator shutdown timeline
Changeset View
Changeset View
Standalone View
Standalone View
llvm/lib/IR/ConstantFold.cpp
Show First 20 Lines • Show All 773 Lines • ▼ Show 20 Lines | if (ConstantVector *CondV = dyn_cast<ConstantVector>(Cond)) { | ||||
if (Result.size() == V1VTy->getNumElements()) | if (Result.size() == V1VTy->getNumElements()) | ||||
return ConstantVector::get(Result); | return ConstantVector::get(Result); | ||||
} | } | ||||
if (isa<UndefValue>(Cond)) { | if (isa<UndefValue>(Cond)) { | ||||
if (isa<UndefValue>(V1)) return V1; | if (isa<UndefValue>(V1)) return V1; | ||||
return V2; | return V2; | ||||
} | } | ||||
if (isa<UndefValue>(V1)) return V2; | |||||
if (isa<UndefValue>(V2)) return V1; | |||||
if (V1 == V2) return V1; | if (V1 == V2) return V1; | ||||
// If the true or false value is undef, we can fold to the other value as | |||||
// long as the other value isn't poison. | |||||
auto NotPoison = [](Constant *C) { | |||||
// TODO: We can analyze ConstExpr by opcode to determine if there is any | |||||
// possibility of poison. | |||||
if (isa<ConstantExpr>(C)) | |||||
return false; | |||||
if (isa<ConstantInt>(C) || isa<GlobalVariable>(C) || isa<ConstantFP>(C) || | |||||
isa<ConstantPointerNull>(C) || isa<Function>(C)) | |||||
return true; | |||||
if (C->getType()->isVectorTy()) | |||||
return !C->containsUndefElement() && !C->containsConstantExpression(); | |||||
// TODO: Recursively analyze aggregates or other constants. | |||||
return false; | |||||
}; | |||||
if (isa<UndefValue>(V1) && NotPoison(V2)) return V2; | |||||
if (isa<UndefValue>(V2) && NotPoison(V1)) return V1; | |||||
if (ConstantExpr *TrueVal = dyn_cast<ConstantExpr>(V1)) { | if (ConstantExpr *TrueVal = dyn_cast<ConstantExpr>(V1)) { | ||||
if (TrueVal->getOpcode() == Instruction::Select) | if (TrueVal->getOpcode() == Instruction::Select) | ||||
if (TrueVal->getOperand(0) == Cond) | if (TrueVal->getOperand(0) == Cond) | ||||
return ConstantExpr::getSelect(Cond, TrueVal->getOperand(1), V2); | return ConstantExpr::getSelect(Cond, TrueVal->getOperand(1), V2); | ||||
} | } | ||||
if (ConstantExpr *FalseVal = dyn_cast<ConstantExpr>(V2)) { | if (ConstantExpr *FalseVal = dyn_cast<ConstantExpr>(V2)) { | ||||
if (FalseVal->getOpcode() == Instruction::Select) | if (FalseVal->getOpcode() == Instruction::Select) | ||||
if (FalseVal->getOperand(0) == Cond) | if (FalseVal->getOperand(0) == Cond) | ||||
▲ Show 20 Lines • Show All 1,785 Lines • Show Last 20 Lines |