summaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib/Transforms')
-rw-r--r--llvm/lib/Transforms/IPO/FunctionSpecialization.cpp41
-rw-r--r--llvm/lib/Transforms/Utils/CodeExtractor.cpp11
2 files changed, 27 insertions, 25 deletions
diff --git a/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp b/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
index 1efec22624dd..17d8283255d2 100644
--- a/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
+++ b/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
@@ -472,16 +472,24 @@ Constant *InstCostVisitor::visitCastInst(CastInst &I) {
Constant *InstCostVisitor::visitCmpInst(CmpInst &I) {
assert(LastVisited != KnownConstants.end() && "Invalid iterator!");
- bool Swap = I.getOperand(1) == LastVisited->first;
- Value *V = Swap ? I.getOperand(0) : I.getOperand(1);
+ Constant *Const = LastVisited->second;
+ bool ConstOnRHS = I.getOperand(1) == LastVisited->first;
+ Value *V = ConstOnRHS ? I.getOperand(0) : I.getOperand(1);
Constant *Other = findConstantFor(V, KnownConstants);
- if (!Other)
- return nullptr;
- Constant *Const = LastVisited->second;
- return Swap ?
- ConstantFoldCompareInstOperands(I.getPredicate(), Other, Const, DL)
- : ConstantFoldCompareInstOperands(I.getPredicate(), Const, Other, DL);
+ if (Other) {
+ if (ConstOnRHS)
+ std::swap(Const, Other);
+ return ConstantFoldCompareInstOperands(I.getPredicate(), Const, Other, DL);
+ }
+
+ // If we haven't found Other to be a specific constant value, we may still be
+ // able to constant fold using information from the lattice value.
+ const ValueLatticeElement &ConstLV = ValueLatticeElement::get(Const);
+ const ValueLatticeElement &OtherLV = Solver.getLatticeValueFor(V);
+ auto &V1State = ConstOnRHS ? OtherLV : ConstLV;
+ auto &V2State = ConstOnRHS ? ConstLV : OtherLV;
+ return V1State.getCompare(I.getPredicate(), I.getType(), V2State, DL);
}
Constant *InstCostVisitor::visitUnaryOperator(UnaryOperator &I) {
@@ -493,16 +501,17 @@ Constant *InstCostVisitor::visitUnaryOperator(UnaryOperator &I) {
Constant *InstCostVisitor::visitBinaryOperator(BinaryOperator &I) {
assert(LastVisited != KnownConstants.end() && "Invalid iterator!");
- bool Swap = I.getOperand(1) == LastVisited->first;
- Value *V = Swap ? I.getOperand(0) : I.getOperand(1);
+ bool ConstOnRHS = I.getOperand(1) == LastVisited->first;
+ Value *V = ConstOnRHS ? I.getOperand(0) : I.getOperand(1);
Constant *Other = findConstantFor(V, KnownConstants);
- if (!Other)
- return nullptr;
+ Value *OtherVal = Other ? Other : V;
+ Value *ConstVal = LastVisited->second;
- Constant *Const = LastVisited->second;
- return dyn_cast_or_null<Constant>(Swap ?
- simplifyBinOp(I.getOpcode(), Other, Const, SimplifyQuery(DL))
- : simplifyBinOp(I.getOpcode(), Const, Other, SimplifyQuery(DL)));
+ if (ConstOnRHS)
+ std::swap(ConstVal, OtherVal);
+
+ return dyn_cast_or_null<Constant>(
+ simplifyBinOp(I.getOpcode(), ConstVal, OtherVal, SimplifyQuery(DL)));
}
Constant *FunctionSpecializer::getPromotableAlloca(AllocaInst *Alloca,
diff --git a/llvm/lib/Transforms/Utils/CodeExtractor.cpp b/llvm/lib/Transforms/Utils/CodeExtractor.cpp
index 440912e6d61d..b6906ab1f220 100644
--- a/llvm/lib/Transforms/Utils/CodeExtractor.cpp
+++ b/llvm/lib/Transforms/Utils/CodeExtractor.cpp
@@ -823,21 +823,16 @@ Function *CodeExtractor::constructFunctionDeclaration(
// Assemble the function's parameter lists.
std::vector<Type *> ParamTy;
std::vector<Type *> AggParamTy;
- std::vector<std::tuple<unsigned, Value *>> NumberedInputs;
- std::vector<std::tuple<unsigned, Value *>> NumberedOutputs;
const DataLayout &DL = M->getDataLayout();
// Add the types of the input values to the function's argument list
- unsigned ArgNum = 0;
for (Value *value : inputs) {
LLVM_DEBUG(dbgs() << "value used in func: " << *value << "\n");
if (AggregateArgs && !ExcludeArgsFromAggregate.contains(value)) {
AggParamTy.push_back(value->getType());
StructValues.insert(value);
- } else {
+ } else
ParamTy.push_back(value->getType());
- NumberedInputs.emplace_back(ArgNum++, value);
- }
}
// Add the types of the output values to the function's argument list.
@@ -846,11 +841,9 @@ Function *CodeExtractor::constructFunctionDeclaration(
if (AggregateArgs && !ExcludeArgsFromAggregate.contains(output)) {
AggParamTy.push_back(output->getType());
StructValues.insert(output);
- } else {
+ } else
ParamTy.push_back(
PointerType::get(output->getType(), DL.getAllocaAddrSpace()));
- NumberedOutputs.emplace_back(ArgNum++, output);
- }
}
assert(