@@ -1104,11 +1104,21 @@ void SCCPSolver::visitStoreInst(StoreInst &SI) {
11041104 TrackedGlobals.erase (I); // No need to keep tracking this!
11051105}
11061106
1107+ static ValueLatticeElement getValueFromMetadata (const Instruction *I) {
1108+ if (MDNode *Ranges = I->getMetadata (LLVMContext::MD_range))
1109+ if (I->getType ()->isIntegerTy ())
1110+ return ValueLatticeElement::getRange (
1111+ getConstantRangeFromMetadata (*Ranges));
1112+ // TODO: Also handle MD_nonnull.
1113+ return ValueLatticeElement::getOverdefined ();
1114+ }
1115+
11071116// Handle load instructions. If the operand is a constant pointer to a constant
11081117// global, we can replace the load with the loaded constant value!
11091118void SCCPSolver::visitLoadInst (LoadInst &I) {
1110- // If this load is of a struct, just mark the result overdefined.
1111- if (I.getType ()->isStructTy ())
1119+ // If this load is of a struct or the load is volatile, just mark the result
1120+ // as overdefined.
1121+ if (I.getType ()->isStructTy () || I.isVolatile ())
11121122 return (void )markOverdefined (&I);
11131123
11141124 // ResolvedUndefsIn might mark I as overdefined. Bail out, even if we would
@@ -1122,41 +1132,39 @@ void SCCPSolver::visitLoadInst(LoadInst &I) {
11221132
11231133 ValueLatticeElement &IV = ValueState[&I];
11241134
1125- if (!isConstant (PtrVal) || I.isVolatile ())
1126- return (void )markOverdefined (IV , &I);
1127-
1128- Constant *Ptr = getConstant (PtrVal);
1129-
1130- // load null is undefined.
1131- if (isa<ConstantPointerNull>(Ptr)) {
1132- if (NullPointerIsDefined (I.getFunction (), I.getPointerAddressSpace ()))
1133- return (void )markOverdefined (IV , &I);
1134- else
1135- return ;
1136- }
1135+ if (isConstant (PtrVal)) {
1136+ Constant *Ptr = getConstant (PtrVal);
11371137
1138- // Transform load (constant global) into the value loaded.
1139- if (auto *GV = dyn_cast<GlobalVariable>(Ptr)) {
1140- if (!TrackedGlobals.empty ()) {
1141- // If we are tracking this global, merge in the known value for it.
1142- auto It = TrackedGlobals.find (GV );
1143- if (It != TrackedGlobals.end ()) {
1144- mergeInValue (IV , &I, It->second , getMaxWidenStepsOpts ());
1138+ // load null is undefined.
1139+ if (isa<ConstantPointerNull>(Ptr)) {
1140+ if (NullPointerIsDefined (I.getFunction (), I.getPointerAddressSpace ()))
1141+ return (void )markOverdefined (IV , &I);
1142+ else
11451143 return ;
1144+ }
1145+
1146+ // Transform load (constant global) into the value loaded.
1147+ if (auto *GV = dyn_cast<GlobalVariable>(Ptr)) {
1148+ if (!TrackedGlobals.empty ()) {
1149+ // If we are tracking this global, merge in the known value for it.
1150+ auto It = TrackedGlobals.find (GV );
1151+ if (It != TrackedGlobals.end ()) {
1152+ mergeInValue (IV , &I, It->second , getMaxWidenStepsOpts ());
1153+ return ;
1154+ }
11461155 }
11471156 }
1148- }
11491157
1150- // Transform load from a constant into a constant if possible.
1151- if (Constant *C = ConstantFoldLoadFromConstPtr (Ptr, I.getType (), DL )) {
1152- if (isa<UndefValue>(C))
1153- return ;
1154- return (void )markConstant (IV , &I, C);
1158+ // Transform load from a constant into a constant if possible.
1159+ if (Constant *C = ConstantFoldLoadFromConstPtr (Ptr, I.getType (), DL )) {
1160+ if (isa<UndefValue>(C))
1161+ return ;
1162+ return (void )markConstant (IV , &I, C);
1163+ }
11551164 }
11561165
1157- // Otherwise we cannot say for certain what value this load will produce.
1158- // Bail out.
1159- markOverdefined (IV , &I);
1166+ // Fall back to metadata.
1167+ mergeInValue (&I, getValueFromMetadata (&I));
11601168}
11611169
11621170void SCCPSolver::visitCallBase (CallBase &CB ) {
@@ -1171,10 +1179,13 @@ void SCCPSolver::handleCallOverdefined(CallBase &CB) {
11711179 if (CB .getType ()->isVoidTy ())
11721180 return ;
11731181
1182+ // Always mark struct return as overdefined.
1183+ if (CB .getType ()->isStructTy ())
1184+ return (void )markOverdefined (&CB );
1185+
11741186 // Otherwise, if we have a single return value case, and if the function is
11751187 // a declaration, maybe we can constant fold it.
1176- if (F && F->isDeclaration () && !CB .getType ()->isStructTy () &&
1177- canConstantFoldCallTo (&CB , F)) {
1188+ if (F && F->isDeclaration () && canConstantFoldCallTo (&CB , F)) {
11781189 SmallVector<Constant *, 8 > Operands;
11791190 for (auto AI = CB .arg_begin (), E = CB .arg_end (); AI != E; ++AI ) {
11801191 if (AI ->get ()->getType ()->isStructTy ())
@@ -1202,8 +1213,8 @@ void SCCPSolver::handleCallOverdefined(CallBase &CB) {
12021213 }
12031214 }
12041215
1205- // Otherwise, we don't know anything about this call, mark it overdefined .
1206- return ( void ) markOverdefined ( &CB );
1216+ // Fall back to metadata .
1217+ mergeInValue (& CB , getValueFromMetadata ( &CB ) );
12071218}
12081219
12091220void SCCPSolver::handleCallArguments (CallBase &CB ) {
0 commit comments