@@ -1093,38 +1093,52 @@ static void AddAliasScopeMetadata(CallSite CS, ValueToValueMapTy &VMap,
1093
1093
}
1094
1094
}
1095
1095
1096
- // / If the inlined function has non-byval align arguments, then
1097
- // / add @llvm.assume-based alignment assumptions to preserve this information .
1098
- static void AddAlignmentAssumptions (CallSite CS, InlineFunctionInfo &IFI) {
1099
- if (!PreserveAlignmentAssumptions || ! IFI.GetAssumptionCache )
1096
+ // / Add @llvm.assume-based assumptions to preserve information supplied by
1097
+ // / argument attributes because the attributes will disappear after inlining .
1098
+ static void addAssumptions (CallSite CS, InlineFunctionInfo &IFI) {
1099
+ if (!IFI.GetAssumptionCache )
1100
1100
return ;
1101
1101
1102
1102
AssumptionCache *AC = &(*IFI.GetAssumptionCache )(*CS.getCaller ());
1103
1103
auto &DL = CS.getCaller ()->getParent ()->getDataLayout ();
1104
1104
1105
- // To avoid inserting redundant assumptions, we should check for assumptions
1106
- // already in the caller. To do this, we might need a DT of the caller .
1105
+ // To avoid inserting redundant assumptions, check that an assumption provides
1106
+ // new information in the caller. This might require a dominator tree .
1107
1107
DominatorTree DT;
1108
1108
bool DTCalculated = false ;
1109
+ auto calcDomTreeIfNeeded = [&]() {
1110
+ if (!DTCalculated) {
1111
+ DT.recalculate (*CS.getCaller ());
1112
+ DTCalculated = true ;
1113
+ }
1114
+ };
1109
1115
1110
1116
Function *CalledFunc = CS.getCalledFunction ();
1117
+ IRBuilder<> Builder (CS.getInstruction ());
1111
1118
for (Argument &Arg : CalledFunc->args ()) {
1112
- unsigned Align = Arg.getType ()->isPointerTy () ? Arg.getParamAlignment () : 0 ;
1113
- if (Align && !Arg.hasByValOrInAllocaAttr () && !Arg.hasNUses (0 )) {
1114
- if (!DTCalculated) {
1115
- DT.recalculate (*CS.getCaller ());
1116
- DTCalculated = true ;
1117
- }
1119
+ Value *ArgVal = CS.getArgument (Arg.getArgNo ());
1118
1120
1121
+ unsigned Align = Arg.getType ()->isPointerTy () ? Arg.getParamAlignment () : 0 ;
1122
+ if (PreserveAlignmentAssumptions && Align &&
1123
+ !Arg.hasByValOrInAllocaAttr () && !Arg.hasNUses (0 )) {
1119
1124
// If we can already prove the asserted alignment in the context of the
1120
1125
// caller, then don't bother inserting the assumption.
1121
- Value *ArgVal = CS.getArgument (Arg.getArgNo ());
1122
- if (getKnownAlignment (ArgVal, DL, CS.getInstruction (), AC, &DT) >= Align)
1123
- continue ;
1126
+ calcDomTreeIfNeeded ();
1127
+ if (getKnownAlignment (ArgVal, DL, CS.getInstruction (), AC, &DT) < Align) {
1128
+ CallInst *Asmp = Builder.CreateAlignmentAssumption (DL, ArgVal, Align);
1129
+ AC->registerAssumption (Asmp);
1130
+ }
1131
+ }
1124
1132
1125
- CallInst *NewAsmp = IRBuilder<>(CS.getInstruction ())
1126
- .CreateAlignmentAssumption (DL, ArgVal, Align);
1127
- AC->registerAssumption (NewAsmp);
1133
+ if (Arg.hasNonNullAttr ()) {
1134
+ // If we can already prove nonnull in the context of the caller, then
1135
+ // don't bother inserting the assumption.
1136
+ calcDomTreeIfNeeded ();
1137
+ if (!isKnownNonNullAt (ArgVal, CS.getInstruction (), &DT)) {
1138
+ Value *NotNull = Builder.CreateIsNotNull (ArgVal);
1139
+ CallInst *Asmp = Builder.CreateAssumption (NotNull);
1140
+ AC->registerAssumption (Asmp);
1141
+ }
1128
1142
}
1129
1143
}
1130
1144
}
@@ -1621,10 +1635,10 @@ bool llvm::InlineFunction(CallSite CS, InlineFunctionInfo &IFI,
1621
1635
VMap[&*I] = ActualArg;
1622
1636
}
1623
1637
1624
- // Add alignment assumptions if necessary. We do this before the inlined
1625
- // instructions are actually cloned into the caller so that we can easily
1626
- // check what will be known at the start of the inlined code.
1627
- AddAlignmentAssumptions (CS, IFI);
1638
+ // Add assumptions if necessary. We do this before the inlined instructions
1639
+ // are actually cloned into the caller so that we can easily check what will
1640
+ // be known at the start of the inlined code.
1641
+ addAssumptions (CS, IFI);
1628
1642
1629
1643
// We want the inliner to prune the code as it copies. We would LOVE to
1630
1644
// have no dead or constant instructions leftover after inlining occurs
0 commit comments