llvm.org GIT mirror llvm / 0cd19b9
Stack Coloring: We have code that checks that all of the uses of allocas are within the lifetime zone. Sometime legitimate usages of allocas are hoisted outside of the lifetime zone. For example, GEPS may calculate the address of a member of an allocated struct. This commit makes sure that we only check (abort regions or assert) for instructions that read and write memory using stack frames directly. Notice that by allowing legitimate usages outside the lifetime zone we also stop checking for instructions which use derivatives of allocas. We will catch less bugs in user code and in the compiler itself. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@163791 91177308-0d34-0410-b5e6-96231b3b80d8 Nadav Rotem 7 years ago
2 changed file(s) with 41 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
7272 STATISTIC(EscapedAllocas,
7373 "Number of allocas that escaped the lifetime region");
7474
75
7675 //===----------------------------------------------------------------------===//
7776 // StackColoring Pass
7877 //===----------------------------------------------------------------------===//
258257
259258 const Value *Allocation = MFI->getObjectAllocation(Slot);
260259 if (Allocation) {
261 DEBUG(dbgs()<<"Found lifetime marker for allocation: "<<
262 Allocation->getName()<<"\n");
260 DEBUG(dbgs()<<"Found a lifetime marker for slot #"<
261 " with allocation: "<< Allocation->getName()<<"\n");
263262 }
264263
265264 if (IsStart) {
537536 // inside the expected live range. If the instruction is not inside
538537 // the calculated range then it means that the alloca usage moved
539538 // outside of the lifetime markers.
539 // NOTE: Alloca address calculations which happen outside the lifetime
540 // zone are are okay, despite the fact that we don't have a good way
541 // for validating all of the usages of the calculation.
540542 #ifndef NDEBUG
541 if (!I->isDebugValue()) {
543 bool TouchesMemory = I->mayLoad() || I->mayStore();
544 if (!I->isDebugValue() && TouchesMemory) {
542545 SlotIndex Index = Indexes->getInstructionIndex(I);
543546 LiveInterval *Interval = Intervals[FromSlot];
544547 assert(Interval->find(Index) != Interval->end() &&
566569
567570 if (I->getOpcode() == TargetOpcode::LIFETIME_START ||
568571 I->getOpcode() == TargetOpcode::LIFETIME_END || I->isDebugValue())
572 continue;
573
574 // Some intervals are suspicious! In some cases we find address
575 // calculations outside of the lifetime zone, but not actual memory
576 // read or write. Memory accesses outside of the lifetime zone are a clear
577 // violation, but address calculations are okay. This can happen when
578 // GEPs are hoisted outside of the lifetime zone.
579 // So, in here we only check instrucitons which can read or write memory.
580 if (!I->mayLoad() && !I->mayStore())
569581 continue;
570582
571583 // Check all of the machine operands.
651663 DEBUG(dbgs()<<"Total Stack size: "<
652664
653665 // Don't continue because there are not enough lifetime markers, or the
654 // stack or too small, or we are told not to optimize the slots.
666 // stack is too small, or we are told not to optimize the slots.
655667 if (NumMarkers < 2 || TotalSize < 16 || DisableColoring) {
656668 DEBUG(dbgs()<<"Will not try to merge slots.\n");
657669 return removeAllMarkers();
375375 }
376376
377377
378 ; Check that we don't assert and crash even when there are usages
379 ; of allocas which do not read or write outside the declared lifetime regions.
380 ;YESCOLOR: shady_range
381 ;NOCOLOR: shady_range
382
383 %struct.Klass = type { i32, i32 }
384
385 define i32 @shady_range(i32 %argc, i8** nocapture %argv) uwtable {
386 %a.i = alloca [4 x %struct.Klass], align 16
387 %b.i = alloca [4 x %struct.Klass], align 16
388 %a8 = bitcast [4 x %struct.Klass]* %a.i to i8*
389 %b8 = bitcast [4 x %struct.Klass]* %b.i to i8*
390 ; I am used outside the lifetime zone below:
391 %z2 = getelementptr inbounds [4 x %struct.Klass]* %a.i, i64 0, i64 0, i32 0
392 call void @llvm.lifetime.start(i64 -1, i8* %a8)
393 call void @llvm.lifetime.start(i64 -1, i8* %b8)
394 %z3 = load i32* %z2, align 16
395 %r = call i32 @foo(i32 %z3, i8* %a8)
396 %r2 = call i32 @foo(i32 %z3, i8* %b8)
397 call void @llvm.lifetime.end(i64 -1, i8* %a8)
398 call void @llvm.lifetime.end(i64 -1, i8* %b8)
399 ret i32 9
400 }
401
378402 declare void @bar([100 x i32]* , [100 x i32]*) nounwind
379403
380404 declare void @llvm.lifetime.start(i64, i8* nocapture) nounwind