- BasicBlock *BB = I;
-
- if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
- if (!FailBB) {
- // Insert code into the entry block that stores the __stack_chk_guard
- // variable onto the stack.
- PointerType *PtrTy = PointerType::getUnqual(Type::Int8Ty);
- StackGuardVar = M->getOrInsertGlobal("__stack_chk_guard", PtrTy);
-
- BasicBlock &Entry = F->getEntryBlock();
- Instruction *InsPt = &Entry.front();
-
- AI = new AllocaInst(PtrTy, "StackGuardSlot", InsPt);
- LoadInst *LI = new LoadInst(StackGuardVar, "StackGuard", false, InsPt);
-
- Value *Args[] = { LI, AI };
- CallInst::
- Create(Intrinsic::getDeclaration(M, Intrinsic::stackprotector_create),
- &Args[0], array_endof(Args), "", InsPt);
-
- // Create the basic block to jump to when the guard check fails.
- FailBB = CreateFailBB();
- }
-
- ++I; // Skip to the next block so that we don't resplit the return block.
-
- // Split the basic block before the return instruction.
- BasicBlock *NewBB = BB->splitBasicBlock(RI, "SP_return");
-
- // Move the newly created basic block to the point right after the old
- // basic block so that it's in the "fall through" position.
- NewBB->removeFromParent();
- F->getBasicBlockList().insert(I, NewBB);
-
- // Generate the stack protector instructions in the old basic block.
- LoadInst *LI1 = new LoadInst(StackGuardVar, "", false, BB);
- CallInst *CI = CallInst::
- Create(Intrinsic::getDeclaration(M, Intrinsic::stackprotector_check),
- AI, "", BB);
- ICmpInst *Cmp = new ICmpInst(CmpInst::ICMP_EQ, CI, LI1, "", BB);
- BranchInst::Create(NewBB, FailBB, Cmp, BB);
- } else {
- ++I;
+ BasicBlock *BB = I++;
+
+ ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator());
+ if (!RI) continue;
+
+ if (!FailBB) {
+ // Insert code into the entry block that stores the __stack_chk_guard
+ // variable onto the stack:
+ //
+ // entry:
+ // StackGuardSlot = alloca i8*
+ // StackGuard = load __stack_chk_guard
+ // call void @llvm.stackprotect.create(StackGuard, StackGuardSlot)
+ //
+ PointerType *PtrTy = PointerType::getUnqual(Type::Int8Ty);
+ StackGuardVar = M->getOrInsertGlobal("__stack_chk_guard", PtrTy);
+
+ BasicBlock &Entry = F->getEntryBlock();
+ Instruction *InsPt = &Entry.front();
+
+ AI = new AllocaInst(PtrTy, "StackGuardSlot", InsPt);
+ LoadInst *LI = new LoadInst(StackGuardVar, "StackGuard", false, InsPt);
+
+ Value *Args[] = { LI, AI };
+ CallInst::
+ Create(Intrinsic::getDeclaration(M, Intrinsic::stackprotector_create),
+ &Args[0], array_endof(Args), "", InsPt);
+
+ // Create the basic block to jump to when the guard check fails.
+ FailBB = CreateFailBB();