llvm.org GIT mirror llvm / adc581f
Speculatively revert 112699 and 112702, they seem to be causing self host errors on clang-x86-64. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@112719 91177308-0d34-0410-b5e6-96231b3b80d8 Eric Christopher 10 years ago
8 changed file(s) with 115 addition(s) and 113 deletion(s). Raw diff Collapse all Expand all
3535 //typedef DenseMap AvailableValsTy;
3636 void *AV;
3737
38 /// ProtoType holds the type of the values being rewritten.
39 const Type *ProtoType;
40
41 // PHI nodes are given a name based on ProtoName.
42 std::string ProtoName;
38 /// PrototypeValue is an arbitrary representative value, which we derive names
39 /// and a type for PHI nodes.
40 Value *PrototypeValue;
4341
4442 /// InsertedPHIs - If this is non-null, the SSAUpdater adds all PHI nodes that
4543 /// it creates to the vector.
5250 ~SSAUpdater();
5351
5452 /// Initialize - Reset this object to get ready for a new set of SSA
55 /// updates with type 'Ty'. PHI nodes get a name based on 'Name'.
56 void Initialize(const Type *Ty, StringRef Name);
53 /// updates. ProtoValue is the value used to name PHI nodes.
54 void Initialize(Value *ProtoValue);
5755
5856 /// AddAvailableValue - Indicate that a rewritten value is available at the
5957 /// end of the specified block with the specified value.
2424 #include "llvm/Support/CallSite.h"
2525 #include "llvm/Target/TargetLowering.h"
2626 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
27 #include "llvm/Transforms/Utils/SSAUpdater.h"
2827 using namespace llvm;
2928
3029 STATISTIC(NumLandingPadsSplit, "Number of landing pads split");
3130 STATISTIC(NumUnwindsLowered, "Number of unwind instructions lowered");
3231 STATISTIC(NumExceptionValuesMoved, "Number of eh.exception calls moved");
32 STATISTIC(NumStackTempsIntroduced, "Number of stack temporaries introduced");
3333
3434 namespace {
3535 class DwarfEHPrepare : public FunctionPass {
6161 typedef SmallPtrSet BBSet;
6262 BBSet LandingPads;
6363
64 // Stack temporary used to hold eh.exception values.
65 AllocaInst *ExceptionValueVar;
66
6467 bool NormalizeLandingPads();
6568 bool LowerUnwinds();
6669 bool MoveExceptionValueCalls();
70 bool FinishStackTemporaries();
6771
6872 Instruction *CreateExceptionValueCall(BasicBlock *BB);
73 Instruction *CreateValueLoad(BasicBlock *BB);
74
75 /// CreateReadOfExceptionValue - Return the result of the eh.exception
76 /// intrinsic by calling the intrinsic if in a landing pad, or loading it
77 /// from the exception value variable otherwise.
78 Instruction *CreateReadOfExceptionValue(BasicBlock *BB) {
79 return LandingPads.count(BB) ?
80 CreateExceptionValueCall(BB) : CreateValueLoad(BB);
81 }
6982
7083 /// CleanupSelectors - Any remaining eh.selector intrinsic calls which still
7184 /// use the "llvm.eh.catch.all.value" call need to convert to using its
103116
104117 virtual bool runOnFunction(Function &Fn);
105118
106 // getAnalysisUsage - We need the dominator tree for handling URoR.
119 // getAnalysisUsage - We need dominance frontiers for memory promotion.
107120 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
108121 AU.addRequired();
109122 AU.addPreserved();
503516
504517 // Create the call...
505518 CallInst *CI = CallInst::Create(RewindFunction,
506 CreateExceptionValueCall(TI->getParent()),
519 CreateReadOfExceptionValue(TI->getParent()),
507520 "", TI);
508521 CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME));
509522 // ...followed by an UnreachableInst.
519532 }
520533
521534 /// MoveExceptionValueCalls - Ensure that eh.exception is only ever called from
522 /// landing pads by replacing calls outside of landing pads with direct use of
523 /// a register holding the appropriate value; this requires adding calls inside
524 /// all landing pads to initialize the register. Also, move eh.exception calls
525 /// inside landing pads to the start of the landing pad (optional, but may make
526 /// things simpler for later passes).
535 /// landing pads by replacing calls outside of landing pads with loads from a
536 /// stack temporary. Move eh.exception calls inside landing pads to the start
537 /// of the landing pad (optional, but may make things simpler for later passes).
527538 bool DwarfEHPrepare::MoveExceptionValueCalls() {
528539 // If the eh.exception intrinsic is not declared in the module then there is
529540 // nothing to do. Speed up compilation by checking for this common case.
533544
534545 bool Changed = false;
535546
536 // Move calls to eh.exception that are inside a landing pad to the start of
537 // the landing pad.
538 for (BBSet::const_iterator LI = LandingPads.begin(), LE = LandingPads.end();
547 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
548 for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;)
549 if (IntrinsicInst *CI = dyn_cast(II++))
550 if (CI->getIntrinsicID() == Intrinsic::eh_exception) {
551 if (!CI->use_empty()) {
552 Value *ExceptionValue = CreateReadOfExceptionValue(BB);
553 if (CI == ExceptionValue) {
554 // The call was at the start of a landing pad - leave it alone.
555 assert(LandingPads.count(BB) &&
556 "Created eh.exception call outside landing pad!");
557 continue;
558 }
559 CI->replaceAllUsesWith(ExceptionValue);
560 }
561 CI->eraseFromParent();
562 ++NumExceptionValuesMoved;
563 Changed = true;
564 }
565 }
566
567 return Changed;
568 }
569
570 /// FinishStackTemporaries - If we introduced a stack variable to hold the
571 /// exception value then initialize it in each landing pad.
572 bool DwarfEHPrepare::FinishStackTemporaries() {
573 if (!ExceptionValueVar)
574 // Nothing to do.
575 return false;
576
577 bool Changed = false;
578
579 // Make sure that there is a store of the exception value at the start of
580 // each landing pad.
581 for (BBSet::iterator LI = LandingPads.begin(), LE = LandingPads.end();
539582 LI != LE; ++LI) {
540 BasicBlock *LP = *LI;
541 for (BasicBlock::iterator II = LP->getFirstNonPHIOrDbg(), IE = LP->end();
542 II != IE;)
543 if (EHExceptionInst *EI = dyn_cast(II++)) {
544 // Found a call to eh.exception.
545 if (!EI->use_empty()) {
546 // If there is already a call to eh.exception at the start of the
547 // landing pad, then get hold of it; otherwise create such a call.
548 Value *CallAtStart = CreateExceptionValueCall(LP);
549
550 // If the call was at the start of a landing pad then leave it alone.
551 if (EI == CallAtStart)
552 continue;
553 EI->replaceAllUsesWith(CallAtStart);
554 }
555 EI->eraseFromParent();
556 ++NumExceptionValuesMoved;
557 Changed = true;
558 }
559 }
560
561 // Look for calls to eh.exception that are not in a landing pad. If one is
562 // found, then a register that holds the exception value will be created in
563 // each landing pad, and the SSAUpdater will be used to compute the values
564 // returned by eh.exception calls outside of landing pads.
565 SSAUpdater SSA;
566
567 // Remember where we found the eh.exception call, to avoid rescanning earlier
568 // basic blocks which we already know contain no eh.exception calls.
569 bool FoundCallOutsideLandingPad = false;
570 Function::iterator BB = F->begin();
571 for (Function::iterator BE = F->end(); BB != BE; ++BB) {
572 // Skip over landing pads.
573 if (LandingPads.count(BB))
574 continue;
575
576 for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(), IE = BB->end();
577 II != IE; ++II)
578 if (isa(II)) {
579 SSA.Initialize(II->getType(), II->getName());
580 FoundCallOutsideLandingPad = true;
581 break;
582 }
583
584 if (FoundCallOutsideLandingPad)
585 break;
586 }
587
588 // If all calls to eh.exception are in landing pads then we are done.
589 if (!FoundCallOutsideLandingPad)
590 return Changed;
591
592 // Add a call to eh.exception at the start of each landing pad, and tell the
593 // SSAUpdater that this is the value produced by the landing pad.
594 for (BBSet::iterator LI = LandingPads.begin(), LE = LandingPads.end();
595 LI != LE; ++LI)
596 SSA.AddAvailableValue(*LI, CreateExceptionValueCall(*LI));
597
598 // Now turn all calls to eh.exception that are not in a landing pad into a use
599 // of the appropriate register.
600 for (Function::iterator BE = F->end(); BB != BE; ++BB) {
601 // Skip over landing pads.
602 if (LandingPads.count(BB))
603 continue;
604
605 for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(), IE = BB->end();
606 II != IE;)
607 if (EHExceptionInst *EI = dyn_cast(II++)) {
608 // Found a call to eh.exception, replace it with the value from any
609 // upstream landing pad(s).
610 EI->replaceAllUsesWith(SSA.GetValueAtEndOfBlock(BB));
611 EI->eraseFromParent();
612 ++NumExceptionValuesMoved;
613 }
614 }
615
616 return true;
583 Instruction *ExceptionValue = CreateReadOfExceptionValue(*LI);
584 Instruction *Store = new StoreInst(ExceptionValue, ExceptionValueVar);
585 Store->insertAfter(ExceptionValue);
586 Changed = true;
587 }
588
589 return Changed;
617590 }
618591
619592 /// CreateExceptionValueCall - Insert a call to the eh.exception intrinsic at
636609 return CallInst::Create(ExceptionValueIntrinsic, "eh.value.call", Start);
637610 }
638611
612 /// CreateValueLoad - Insert a load of the exception value stack variable
613 /// (creating it if necessary) at the start of the basic block (unless
614 /// there already is a load, in which case the existing load is returned).
615 Instruction *DwarfEHPrepare::CreateValueLoad(BasicBlock *BB) {
616 Instruction *Start = BB->getFirstNonPHIOrDbg();
617 // Is this a load of the exception temporary?
618 if (ExceptionValueVar)
619 if (LoadInst* LI = dyn_cast(Start))
620 if (LI->getPointerOperand() == ExceptionValueVar)
621 // Reuse the existing load.
622 return Start;
623
624 // Create the temporary if we didn't already.
625 if (!ExceptionValueVar) {
626 ExceptionValueVar = new AllocaInst(PointerType::getUnqual(
627 Type::getInt8Ty(BB->getContext())), "eh.value", F->begin()->begin());
628 ++NumStackTempsIntroduced;
629 }
630
631 // Load the value.
632 return new LoadInst(ExceptionValueVar, "eh.value.load", Start);
633 }
634
639635 bool DwarfEHPrepare::runOnFunction(Function &Fn) {
640636 bool Changed = false;
641637
642638 // Initialize internal state.
643639 DT = &getAnalysis();
640 ExceptionValueVar = 0;
644641 F = &Fn;
645642
646643 // Ensure that only unwind edges end at landing pads (a landing pad is a
655652 // Move eh.exception calls to landing pads.
656653 Changed |= MoveExceptionValueCalls();
657654
655 // Initialize any stack temporaries we introduced.
656 Changed |= FinishStackTemporaries();
657
658 // TODO: Turn any stack temporaries into registers if possible.
659
658660 Changed |= HandleURoRInvokes();
659661
660662 LandingPads.clear();
13091309 // Otherwise, we have to construct SSA form.
13101310 SmallVector NewPHIs;
13111311 SSAUpdater SSAUpdate(&NewPHIs);
1312 SSAUpdate.Initialize(LI->getType(), LI->getName());
1312 SSAUpdate.Initialize(LI);
13131313
13141314 const Type *LoadTy = LI->getType();
13151315
15251525 // We found a use of I outside of BB. Rename all uses of I that are outside
15261526 // its block to be uses of the appropriate PHI node etc. See ValuesInBlocks
15271527 // with the two values we know.
1528 SSAUpdate.Initialize(I->getType(), I->getName());
1528 SSAUpdate.Initialize(I);
15291529 SSAUpdate.AddAvailableValue(BB, I);
15301530 SSAUpdate.AddAvailableValue(NewBB, ValueMapping[I]);
15311531
16801680 // We found a use of I outside of BB. Rename all uses of I that are outside
16811681 // its block to be uses of the appropriate PHI node etc. See ValuesInBlocks
16821682 // with the two values we know.
1683 SSAUpdate.Initialize(I->getType(), I->getName());
1683 SSAUpdate.Initialize(I);
16841684 SSAUpdate.AddAvailableValue(BB, I);
16851685 SSAUpdate.AddAvailableValue(PredBB, ValueMapping[I]);
16861686
525525 SSAUpdater SSA(&NewPHIs);
526526
527527 if (!I.use_empty())
528 SSA.Initialize(I.getType(), I.getName());
528 SSA.Initialize(&I);
529529
530530 // Insert a copy of the instruction in each exit block of the loop that is
531531 // dominated by the instruction. Each exit block is known to only be in the
727727 SomeValue = LoopUses[0];
728728 else
729729 SomeValue = cast(LoopUses[0])->getOperand(0);
730 SSA.Initialize(SomeValue->getType(), SomeValue->getName());
730 SSA.Initialize(SomeValue);
731731
732732 // First step: bucket up uses of the pointers by the block they occur in.
733733 // This is important because we have to handle multiple defs/uses in a block
220220
221221 // The value now exits in two versions: the initial value in the preheader
222222 // and the loop "next" value in the original header.
223 SSA.Initialize(OrigHeaderVal->getType(), OrigHeaderVal->getName());
223 SSA.Initialize(OrigHeaderVal);
224224 SSA.AddAvailableValue(OrigHeader, OrigHeaderVal);
225225 SSA.AddAvailableValue(OrigPreHeader, OrigPreHeaderVal);
226226
205205 DomTreeNode *DomNode = DT->getNode(DomBB);
206206
207207 SSAUpdater SSAUpdate;
208 SSAUpdate.Initialize(Inst->getType(), Inst->getName());
208 SSAUpdate.Initialize(Inst);
209209
210210 // Insert the LCSSA phi's into all of the exit blocks dominated by the
211211 // value, and add them to the Phi's map.
2828 }
2929
3030 SSAUpdater::SSAUpdater(SmallVectorImpl *NewPHI)
31 : AV(0), ProtoType(0), ProtoName(), InsertedPHIs(NewPHI) {}
31 : AV(0), PrototypeValue(0), InsertedPHIs(NewPHI) {}
3232
3333 SSAUpdater::~SSAUpdater() {
3434 delete &getAvailableVals(AV);
3535 }
3636
3737 /// Initialize - Reset this object to get ready for a new set of SSA
38 /// updates with type 'Ty'. PHI nodes get a name based on 'Name'.
39 void SSAUpdater::Initialize(const Type *Ty, StringRef Name) {
38 /// updates. ProtoValue is the value used to name PHI nodes.
39 void SSAUpdater::Initialize(Value *ProtoValue) {
4040 if (AV == 0)
4141 AV = new AvailableValsTy();
4242 else
4343 getAvailableVals(AV).clear();
44 ProtoType = Ty;
45 ProtoName = Name;
44 PrototypeValue = ProtoValue;
4645 }
4746
4847 /// HasValueForBlock - Return true if the SSAUpdater already has a value for
5453 /// AddAvailableValue - Indicate that a rewritten value is available in the
5554 /// specified block with the specified value.
5655 void SSAUpdater::AddAvailableValue(BasicBlock *BB, Value *V) {
57 assert(ProtoType != 0 && "Need to initialize SSAUpdater");
58 assert(ProtoType == V->getType() &&
56 assert(PrototypeValue != 0 && "Need to initialize SSAUpdater");
57 assert(PrototypeValue->getType() == V->getType() &&
5958 "All rewritten values must have the same type");
6059 getAvailableVals(AV)[BB] = V;
6160 }
148147
149148 // If there are no predecessors, just return undef.
150149 if (PredValues.empty())
151 return UndefValue::get(ProtoType);
150 return UndefValue::get(PrototypeValue->getType());
152151
153152 // Otherwise, if all the merged values are the same, just use it.
154153 if (SingularValue != 0)
168167 }
169168
170169 // Ok, we have no way out, insert a new one now.
171 PHINode *InsertedPHI = PHINode::Create(ProtoType, ProtoName, &BB->front());
170 PHINode *InsertedPHI = PHINode::Create(PrototypeValue->getType(),
171 PrototypeValue->getName(),
172 &BB->front());
172173 InsertedPHI->reserveOperandSpace(PredValues.size());
173174
174175 // Fill in all the predecessors of the PHI.
280281 /// GetUndefVal - Get an undefined value of the same type as the value
281282 /// being handled.
282283 static Value *GetUndefVal(BasicBlock *BB, SSAUpdater *Updater) {
283 return UndefValue::get(Updater->ProtoType);
284 return UndefValue::get(Updater->PrototypeValue->getType());
284285 }
285286
286287 /// CreateEmptyPHI - Create a new PHI instruction in the specified block.
287288 /// Reserve space for the operands but do not fill them in yet.
288289 static Value *CreateEmptyPHI(BasicBlock *BB, unsigned NumPreds,
289290 SSAUpdater *Updater) {
290 PHINode *PHI = PHINode::Create(Updater->ProtoType, Updater->ProtoName,
291 PHINode *PHI = PHINode::Create(Updater->PrototypeValue->getType(),
292 Updater->PrototypeValue->getName(),
291293 &BB->front());
292294 PHI->reserveOperandSpace(NumPreds);
293295 return PHI;