llvm.org GIT mirror llvm / 941db49
Fix the miscompilation of MiBench/consumer-lame that was exposed by Evan's byval work. This miscompilation is due to the program indexing an array out of range and us doing a transformation that broke this. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@45949 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 11 years ago
2 changed file(s) with 120 addition(s) and 66 deletion(s). Raw diff Collapse all Expand all
2525 #include "llvm/Target/TargetData.h"
2626 #include "llvm/Support/Compiler.h"
2727 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/GetElementPtrTypeIterator.h"
2829 #include "llvm/ADT/SmallPtrSet.h"
2930 #include "llvm/ADT/SmallVector.h"
3031 #include "llvm/ADT/Statistic.h"
334335 return Changed;
335336 }
336337
337
338 /// UsersSafeToSRA - Look at all uses of the global and decide whether it is
339 /// safe for us to perform this transformation.
338 /// isSafeSROAElementUse - Return true if the specified instruction is a safe
339 /// user of a derived expression from a global that we want to SROA.
340 static bool isSafeSROAElementUse(Value *V) {
341 // We might have a dead and dangling constant hanging off of here.
342 if (Constant *C = dyn_cast(V))
343 return ConstantIsDead(C);
344
345 Instruction *I = dyn_cast(V);
346 if (!I) return false;
347
348 // Loads are ok.
349 if (isa(I)) return true;
350
351 // Stores *to* the pointer are ok.
352 if (StoreInst *SI = dyn_cast(I))
353 return SI->getOperand(0) != V;
354
355 // Otherwise, it must be a GEP.
356 GetElementPtrInst *GEPI = dyn_cast(I);
357 if (GEPI == 0) return false;
358
359 if (GEPI->getNumOperands() < 3 || !isa(GEPI->getOperand(1)) ||
360 !cast(GEPI->getOperand(1))->isNullValue())
361 return false;
362
363 for (Value::use_iterator I = GEPI->use_begin(), E = GEPI->use_end();
364 I != E; ++I)
365 if (!isSafeSROAElementUse(*I))
366 return false;
367 return true;
368 }
369
370
371 /// IsUserOfGlobalSafeForSRA - U is a direct user of the specified global value.
372 /// Look at it and its uses and decide whether it is safe to SROA this global.
340373 ///
341 static bool UsersSafeToSRA(Value *V) {
342 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI){
343 if (ConstantExpr *CE = dyn_cast(*UI)) {
344 if (CE->getOpcode() != Instruction::GetElementPtr)
374 static bool IsUserOfGlobalSafeForSRA(User *U, GlobalValue *GV) {
375 // The user of the global must be a GEP Inst or a ConstantExpr GEP.
376 if (!isa(U) &&
377 (!isa(U) ||
378 cast(U)->getOpcode() != Instruction::GetElementPtr))
379 return false;
380
381 // Check to see if this ConstantExpr GEP is SRA'able. In particular, we
382 // don't like < 3 operand CE's, and we don't like non-constant integer
383 // indices. This enforces that all uses are 'gep GV, 0, C, ...' for some
384 // value of C.
385 if (U->getNumOperands() < 3 || !isa(U->getOperand(1)) ||
386 !cast(U->getOperand(1))->isNullValue() ||
387 !isa(U->getOperand(2)))
388 return false;
389
390 gep_type_iterator GEPI = gep_type_begin(U), E = gep_type_end(U);
391 ++GEPI; // Skip over the pointer index.
392
393 // If this is a use of an array allocation, do a bit more checking for sanity.
394 if (const ArrayType *AT = dyn_cast(*GEPI)) {
395 uint64_t NumElements = AT->getNumElements();
396 ConstantInt *Idx = cast(U->getOperand(2));
397
398 // Check to make sure that index falls within the array. If not,
399 // something funny is going on, so we won't do the optimization.
400 //
401 if (Idx->getZExtValue() >= NumElements)
402 return false;
403
404 // We cannot scalar repl this level of the array unless any array
405 // sub-indices are in-range constants. In particular, consider:
406 // A[0][i]. We cannot know that the user isn't doing invalid things like
407 // allowing i to index an out-of-range subscript that accesses A[1].
408 //
409 // Scalar replacing *just* the outer index of the array is probably not
410 // going to be a win anyway, so just give up.
411 for (++GEPI; // Skip array index.
412 GEPI != E && (isa(*GEPI) || isa(*GEPI));
413 ++GEPI) {
414 uint64_t NumElements;
415 if (const ArrayType *SubArrayTy = dyn_cast(*GEPI))
416 NumElements = SubArrayTy->getNumElements();
417 else
418 NumElements = cast(*GEPI)->getNumElements();
419
420 ConstantInt *IdxVal = dyn_cast(GEPI.getOperand());
421 if (!IdxVal || IdxVal->getZExtValue() >= NumElements)
345422 return false;
346
347 // Check to see if this ConstantExpr GEP is SRA'able. In particular, we
348 // don't like < 3 operand CE's, and we don't like non-constant integer
349 // indices.
350 if (CE->getNumOperands() < 3 || !CE->getOperand(1)->isNullValue())
351 return false;
352
353 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
354 if (!isa(CE->getOperand(i)))
355 return false;
356
357 if (!UsersSafeToSRA(CE)) return false;
358 continue;
359 }
360
361 if (Instruction *I = dyn_cast(*UI)) {
362 if (isa(I)) continue;
363
364 if (StoreInst *SI = dyn_cast(I)) {
365 // Don't allow a store OF the address, only stores TO the address.
366 if (SI->getOperand(0) == V) return false;
367 continue;
368 }
369
370 if (isa(I)) {
371 if (!UsersSafeToSRA(I)) return false;
372
373 // If the first two indices are constants, this can be SRA'd.
374 if (isa(I->getOperand(0))) {
375 if (I->getNumOperands() < 3 || !isa(I->getOperand(1)) ||
376 !cast(I->getOperand(1))->isNullValue() ||
377 !isa(I->getOperand(2)))
378 return false;
379 continue;
380 }
381
382 if (ConstantExpr *CE = dyn_cast(I->getOperand(0))){
383 if (CE->getOpcode() != Instruction::GetElementPtr ||
384 CE->getNumOperands() < 3 || I->getNumOperands() < 2 ||
385 !isa(I->getOperand(0)) ||
386 !cast(I->getOperand(0))->isNullValue())
387 return false;
388 continue;
389 }
390 return false;
391 }
392 return false; // Any other instruction is not safe.
393 }
394 if (Constant *C = dyn_cast(*UI)) {
395 // We might have a dead and dangling constant hanging off of here.
396 if (!ConstantIsDead(C))
397 return false;
398 continue;
399 }
400 // Otherwise must be some other user.
401 return false;
402 }
403
423 }
424 }
425
426 for (Value::use_iterator I = U->use_begin(), E = U->use_end(); I != E; ++I)
427 if (!isSafeSROAElementUse(*I))
428 return false;
404429 return true;
405430 }
431
432 /// GlobalUsersSafeToSRA - Look at all uses of the global and decide whether it
433 /// is safe for us to perform this transformation.
434 ///
435 static bool GlobalUsersSafeToSRA(GlobalValue *GV) {
436 for (Value::use_iterator UI = GV->use_begin(), E = GV->use_end();
437 UI != E; ++UI) {
438 if (!IsUserOfGlobalSafeForSRA(*UI, GV))
439 return false;
440 }
441 return true;
442 }
443
406444
407445 /// SRAGlobal - Perform scalar replacement of aggregates on the specified global
408446 /// variable. This opens the door for other optimizations by exposing the
411449 /// insert so that the caller can reprocess it.
412450 static GlobalVariable *SRAGlobal(GlobalVariable *GV) {
413451 // Make sure this global only has simple uses that we can SRA.
414 if (!UsersSafeToSRA(GV))
452 if (!GlobalUsersSafeToSRA(GV))
415453 return 0;
416454
417455 assert(GV->hasInternalLinkage() && !GV->isConstant());
0 ; RUN: llvm-as < %s | opt -globalopt | llvm-dis | grep {16 x .31 x double.. zeroinitializer}
1
2 ; The 'X' indices could be larger than 31. Do not SROA the outer indices of this array.
3 @mm = internal global [16 x [31 x double]] zeroinitializer, align 32
4
5 define void @test(i32 %X) {
6 %P = getelementptr [16 x [31 x double]]* @mm, i32 0, i32 0, i32 %X
7 store double 1.0, double* %P
8 ret void
9 }
10
11 define double @get(i32 %X) {
12 %P = getelementptr [16 x [31 x double]]* @mm, i32 0, i32 0, i32 %X
13 %V = load double* %P
14 ret double %V
15 }