Mercurial > hg > CbC > CbC_llvm
diff lib/CodeGen/SafeStack.cpp @ 134:3a76565eade5 LLVM5.0.1
update 5.0.1
author | mir3636 |
---|---|
date | Sat, 17 Feb 2018 09:57:20 +0900 |
parents | 803732b1fca8 |
children | c2174574ed3a |
line wrap: on
line diff
--- a/lib/CodeGen/SafeStack.cpp Fri Feb 16 19:10:49 2018 +0900 +++ b/lib/CodeGen/SafeStack.cpp Sat Feb 17 09:57:20 2018 +0900 @@ -24,11 +24,14 @@ #include "llvm/ADT/Statistic.h" #include "llvm/Analysis/AssumptionCache.h" #include "llvm/Analysis/BranchProbabilityInfo.h" +#include "llvm/Analysis/InlineCost.h" #include "llvm/Analysis/LoopInfo.h" #include "llvm/Analysis/ScalarEvolution.h" #include "llvm/Analysis/ScalarEvolutionExpressions.h" #include "llvm/Analysis/TargetLibraryInfo.h" +#include "llvm/CodeGen/TargetLowering.h" #include "llvm/CodeGen/TargetPassConfig.h" +#include "llvm/CodeGen/TargetSubtargetInfo.h" #include "llvm/IR/Argument.h" #include "llvm/IR/Attributes.h" #include "llvm/IR/CallSite.h" @@ -57,10 +60,9 @@ #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/MathExtras.h" #include "llvm/Support/raw_ostream.h" -#include "llvm/Target/TargetLowering.h" #include "llvm/Target/TargetMachine.h" -#include "llvm/Target/TargetSubtargetInfo.h" #include "llvm/Transforms/Utils/BasicBlockUtils.h" +#include "llvm/Transforms/Utils/Cloning.h" #include "llvm/Transforms/Utils/Local.h" #include <algorithm> #include <cassert> @@ -88,6 +90,13 @@ } // namespace llvm +/// Use __safestack_pointer_address even if the platform has a faster way of +/// access safe stack pointer. +static cl::opt<bool> + SafeStackUsePointerAddress("safestack-use-pointer-address", + cl::init(false), cl::Hidden); + + namespace { /// Rewrite an SCEV expression for a memory access address to an expression that @@ -191,6 +200,9 @@ bool IsAccessSafe(Value *Addr, uint64_t Size, const Value *AllocaPtr, uint64_t AllocaSize); + bool ShouldInlinePointerAddress(CallSite &CS); + void TryInlinePointerAddress(); + public: SafeStack(Function &F, const TargetLoweringBase &TL, const DataLayout &DL, ScalarEvolution &SE) @@ -545,6 +557,7 @@ for (Argument *Arg : ByValArguments) { unsigned Offset = SSL.getObjectOffset(Arg); + unsigned Align = SSL.getObjectAlignment(Arg); Type *Ty = Arg->getType()->getPointerElementType(); uint64_t Size = DL.getTypeStoreSize(Ty); @@ -558,10 +571,10 @@ // Replace alloc with the new location. replaceDbgDeclare(Arg, BasePointer, BasePointer->getNextNode(), DIB, - /*Deref=*/false, -Offset); + DIExpression::NoDeref, -Offset, DIExpression::NoDeref); Arg->replaceAllUsesWith(NewArg); IRB.SetInsertPoint(cast<Instruction>(NewArg)->getNextNode()); - IRB.CreateMemCpy(Off, Arg, Size, Arg->getParamAlignment()); + IRB.CreateMemCpy(Off, Align, Arg, Arg->getParamAlignment(), Size); } // Allocate space for every unsafe static AllocaInst on the unsafe stack. @@ -573,7 +586,8 @@ if (Size == 0) Size = 1; // Don't create zero-sized stack objects. - replaceDbgDeclareForAlloca(AI, BasePointer, DIB, /*Deref=*/false, -Offset); + replaceDbgDeclareForAlloca(AI, BasePointer, DIB, DIExpression::NoDeref, + -Offset, DIExpression::NoDeref); replaceDbgValueForAlloca(AI, BasePointer, DIB, -Offset); // Replace uses of the alloca with the new location. @@ -663,7 +677,8 @@ if (AI->hasName() && isa<Instruction>(NewAI)) NewAI->takeName(AI); - replaceDbgDeclareForAlloca(AI, NewAI, DIB, /*Deref=*/false); + replaceDbgDeclareForAlloca(AI, NewAI, DIB, DIExpression::NoDeref, 0, + DIExpression::NoDeref); AI->replaceAllUsesWith(NewAI); AI->eraseFromParent(); } @@ -693,6 +708,35 @@ } } +bool SafeStack::ShouldInlinePointerAddress(CallSite &CS) { + Function *Callee = CS.getCalledFunction(); + if (CS.hasFnAttr(Attribute::AlwaysInline) && isInlineViable(*Callee)) + return true; + if (Callee->isInterposable() || Callee->hasFnAttribute(Attribute::NoInline) || + CS.isNoInline()) + return false; + return true; +} + +void SafeStack::TryInlinePointerAddress() { + if (!isa<CallInst>(UnsafeStackPtr)) + return; + + if(F.hasFnAttribute(Attribute::OptimizeNone)) + return; + + CallSite CS(UnsafeStackPtr); + Function *Callee = CS.getCalledFunction(); + if (!Callee || Callee->isDeclaration()) + return; + + if (!ShouldInlinePointerAddress(CS)) + return; + + InlineFunctionInfo IFI; + InlineFunction(CS, IFI); +} + bool SafeStack::run() { assert(F.hasFnAttribute(Attribute::SafeStack) && "Can't run SafeStack on a function without the attribute"); @@ -729,7 +773,13 @@ ++NumUnsafeStackRestorePointsFunctions; IRBuilder<> IRB(&F.front(), F.begin()->getFirstInsertionPt()); - UnsafeStackPtr = TL.getSafeStackPointerLocation(IRB); + if (SafeStackUsePointerAddress) { + Value *Fn = F.getParent()->getOrInsertFunction( + "__safestack_pointer_address", StackPtrTy->getPointerTo(0)); + UnsafeStackPtr = IRB.CreateCall(Fn); + } else { + UnsafeStackPtr = TL.getSafeStackPointerLocation(IRB); + } // Load the current stack pointer (we'll also use it as a base pointer). // FIXME: use a dedicated register for it ? @@ -777,6 +827,8 @@ IRB.CreateStore(BasePointer, UnsafeStackPtr); } + TryInlinePointerAddress(); + DEBUG(dbgs() << "[SafeStack] safestack applied\n"); return true; }