@@ -8175,8 +8175,8 @@ static bool passingValueIsAlwaysUndefined(Value *V, Instruction *I, bool PtrValu
8175
8175
if (C->isNullValue () || isa<UndefValue>(C)) {
8176
8176
// Only look at the first use we can handle, avoid hurting compile time with
8177
8177
// long uselists
8178
- auto FindUse = llvm::find_if (I->users (), [](auto * U) {
8179
- auto *Use = cast<Instruction>(U);
8178
+ auto FindUse = llvm::find_if (I->uses (), [](auto & U) {
8179
+ auto *Use = cast<Instruction>(U. getUser () );
8180
8180
// Change this list when we want to add new instructions.
8181
8181
switch (Use->getOpcode ()) {
8182
8182
default :
@@ -8199,26 +8199,28 @@ static bool passingValueIsAlwaysUndefined(Value *V, Instruction *I, bool PtrValu
8199
8199
return true ;
8200
8200
}
8201
8201
});
8202
- if (FindUse == I->user_end ())
8202
+ if (FindUse == I->use_end ())
8203
8203
return false ;
8204
- auto *Use = cast<Instruction>(*FindUse);
8205
- // Bail out if Use is not in the same BB as I or Use == I or Use comes
8206
- // before I in the block. The latter two can be the case if Use is a
8204
+ auto &Use = *FindUse;
8205
+ auto *User = cast<Instruction>(Use.getUser ());
8206
+ // Bail out if User is not in the same BB as I or User == I or User comes
8207
+ // before I in the block. The latter two can be the case if User is a
8207
8208
// PHI node.
8208
- if (Use->getParent () != I->getParent () || Use == I || Use->comesBefore (I))
8209
+ if (User->getParent () != I->getParent () || User == I ||
8210
+ User->comesBefore (I))
8209
8211
return false ;
8210
8212
8211
8213
// Now make sure that there are no instructions in between that can alter
8212
8214
// control flow (eg. calls)
8213
8215
auto InstrRange =
8214
- make_range (std::next (I->getIterator ()), Use ->getIterator ());
8216
+ make_range (std::next (I->getIterator ()), User ->getIterator ());
8215
8217
if (any_of (InstrRange, [](Instruction &I) {
8216
8218
return !isGuaranteedToTransferExecutionToSuccessor (&I);
8217
8219
}))
8218
8220
return false ;
8219
8221
8220
8222
// Look through GEPs. A load from a GEP derived from NULL is still undefined
8221
- if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Use ))
8223
+ if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(User ))
8222
8224
if (GEP->getPointerOperand () == I) {
8223
8225
// The current base address is null, there are four cases to consider:
8224
8226
// getelementptr (TY, null, 0) -> null
@@ -8235,7 +8237,7 @@ static bool passingValueIsAlwaysUndefined(Value *V, Instruction *I, bool PtrValu
8235
8237
}
8236
8238
8237
8239
// Look through return.
8238
- if (ReturnInst *Ret = dyn_cast<ReturnInst>(Use )) {
8240
+ if (ReturnInst *Ret = dyn_cast<ReturnInst>(User )) {
8239
8241
bool HasNoUndefAttr =
8240
8242
Ret->getFunction ()->hasRetAttribute (Attribute::NoUndef);
8241
8243
// Return undefined to a noundef return value is undefined.
@@ -8249,56 +8251,45 @@ static bool passingValueIsAlwaysUndefined(Value *V, Instruction *I, bool PtrValu
8249
8251
}
8250
8252
8251
8253
// Load from null is undefined.
8252
- if (LoadInst *LI = dyn_cast<LoadInst>(Use ))
8254
+ if (LoadInst *LI = dyn_cast<LoadInst>(User ))
8253
8255
if (!LI->isVolatile ())
8254
8256
return !NullPointerIsDefined (LI->getFunction (),
8255
8257
LI->getPointerAddressSpace ());
8256
8258
8257
8259
// Store to null is undefined.
8258
- if (StoreInst *SI = dyn_cast<StoreInst>(Use ))
8260
+ if (StoreInst *SI = dyn_cast<StoreInst>(User ))
8259
8261
if (!SI->isVolatile ())
8260
8262
return (!NullPointerIsDefined (SI->getFunction (),
8261
8263
SI->getPointerAddressSpace ())) &&
8262
8264
SI->getPointerOperand () == I;
8263
8265
8264
8266
// llvm.assume(false/undef) always triggers immediate UB.
8265
- if (auto *Assume = dyn_cast<AssumeInst>(Use )) {
8267
+ if (auto *Assume = dyn_cast<AssumeInst>(User )) {
8266
8268
// Ignore assume operand bundles.
8267
8269
if (I == Assume->getArgOperand (0 ))
8268
8270
return true ;
8269
8271
}
8270
8272
8271
- if (auto *CB = dyn_cast<CallBase>(Use )) {
8273
+ if (auto *CB = dyn_cast<CallBase>(User )) {
8272
8274
if (C->isNullValue () && NullPointerIsDefined (CB->getFunction ()))
8273
8275
return false ;
8274
8276
// A call to null is undefined.
8275
8277
if (CB->getCalledOperand () == I)
8276
8278
return true ;
8277
8279
8278
- if (C->isNullValue ()) {
8279
- for (const llvm::Use &Arg : CB->args ())
8280
- if (Arg == I) {
8281
- unsigned ArgIdx = CB->getArgOperandNo (&Arg);
8282
- if (CB->isPassingUndefUB (ArgIdx) &&
8283
- CB->paramHasAttr (ArgIdx, Attribute::NonNull)) {
8284
- // Passing null to a nonnnull+noundef argument is undefined.
8285
- return !PtrValueMayBeModified;
8286
- }
8287
- }
8288
- } else if (isa<UndefValue>(C)) {
8280
+ if (CB->isArgOperand (&Use)) {
8281
+ unsigned ArgIdx = CB->getArgOperandNo (&Use);
8282
+ // Passing null to a nonnnull+noundef argument is undefined.
8283
+ if (C->isNullValue () && CB->isPassingUndefUB (ArgIdx) &&
8284
+ CB->paramHasAttr (ArgIdx, Attribute::NonNull))
8285
+ return !PtrValueMayBeModified;
8289
8286
// Passing undef to a noundef argument is undefined.
8290
- for (const llvm::Use &Arg : CB->args ())
8291
- if (Arg == I) {
8292
- unsigned ArgIdx = CB->getArgOperandNo (&Arg);
8293
- if (CB->isPassingUndefUB (ArgIdx)) {
8294
- // Passing undef to a noundef argument is undefined.
8295
- return true ;
8296
- }
8297
- }
8287
+ if (isa<UndefValue>(C) && CB->isPassingUndefUB (ArgIdx))
8288
+ return true ;
8298
8289
}
8299
8290
}
8300
8291
// Div/Rem by zero is immediate UB
8301
- if (match (Use , m_BinOp (m_Value (), m_Specific (I))) && Use ->isIntDivRem ())
8292
+ if (match (User , m_BinOp (m_Value (), m_Specific (I))) && User ->isIntDivRem ())
8302
8293
return true ;
8303
8294
}
8304
8295
return false ;
0 commit comments