Fix Clang-tidy modernize-use-nullptr warnings in source directories and generated...
[oota-llvm.git] / lib / Transforms / Instrumentation / SafeStack.cpp
index 6e5e85f0825aead0b8203d2dc675de25555d81fe..1b28caffd4a886527fcb439c0f64fb549658d8c5 100644 (file)
@@ -210,7 +210,7 @@ class SafeStack : public FunctionPass {
   /// \returns A local variable in which to maintain the dynamic top of the
   /// unsafe stack if needed.
   AllocaInst *
-  createStackRestorePoints(Function &F,
+  createStackRestorePoints(IRBuilder<> &IRB, Function &F,
                            ArrayRef<Instruction *> StackRestorePoints,
                            Value *StaticTop, bool NeedDynamicTop);
 
@@ -254,7 +254,7 @@ Value *SafeStack::getOrCreateUnsafeStackPtr(IRBuilder<> &IRB, Function &F) {
   unsigned Offset;
   unsigned AddressSpace;
   // Check if the target keeps the unsafe stack pointer at a fixed offset.
-  if (TLI->getSafeStackPointerLocation(Offset, AddressSpace)) {
+  if (TLI && TLI->getSafeStackPointerLocation(AddressSpace, Offset)) {
     Constant *OffsetVal =
         ConstantInt::get(Type::getInt32Ty(F.getContext()), Offset);
     return ConstantExpr::getIntToPtr(OffsetVal,
@@ -274,12 +274,11 @@ Value *SafeStack::getOrCreateUnsafeStackPtr(IRBuilder<> &IRB, Function &F) {
     if (!UnsafeStackPtr) {
       // The global variable is not defined yet, define it ourselves.
       // We use the initial-exec TLS model because we do not support the
-      // variable
-      // living anywhere other than in the main executable.
+      // variable living anywhere other than in the main executable.
       UnsafeStackPtr = new GlobalVariable(
           /*Module=*/M, /*Type=*/StackPtrTy,
           /*isConstant=*/false, /*Linkage=*/GlobalValue::ExternalLinkage,
-          /*Initializer=*/0, /*Name=*/kUnsafeStackPtrVar,
+          /*Initializer=*/nullptr, /*Name=*/kUnsafeStackPtrVar,
           /*InsertBefore=*/nullptr,
           /*ThreadLocalMode=*/GlobalValue::InitialExecTLSModel);
     } else {
@@ -333,16 +332,12 @@ void SafeStack::findInsts(Function &F,
 }
 
 AllocaInst *
-SafeStack::createStackRestorePoints(Function &F,
+SafeStack::createStackRestorePoints(IRBuilder<> &IRB, Function &F,
                                     ArrayRef<Instruction *> StackRestorePoints,
                                     Value *StaticTop, bool NeedDynamicTop) {
   if (StackRestorePoints.empty())
     return nullptr;
 
-  IRBuilder<> IRB(StaticTop
-                      ? cast<Instruction>(StaticTop)->getNextNode()
-                      : (Instruction *)F.getEntryBlock().getFirstInsertionPt());
-
   // We need the current value of the shadow stack pointer to restore
   // after longjmp or exception catching.
 
@@ -448,7 +443,7 @@ SafeStack::moveStaticAllocasToUnsafeStack(IRBuilder<> &IRB, Function &F,
       cast<Instruction>(NewAI)->takeName(AI);
 
     // Replace alloc with the new location.
-    replaceDbgDeclareForAlloca(AI, NewAI, DIB, /*Deref=*/true);
+    replaceDbgDeclareForAlloca(AI, BasePointer, DIB, /*Deref=*/true, -StaticOffset);
     AI->replaceAllUsesWith(NewAI);
     AI->eraseFromParent();
   }
@@ -538,10 +533,6 @@ void SafeStack::moveDynamicAllocasToUnsafeStack(
 }
 
 bool SafeStack::runOnFunction(Function &F) {
-  auto AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
-
-  TLI = TM->getSubtargetImpl(F)->getTargetLowering();
-
   DEBUG(dbgs() << "[SafeStack] Function: " << F.getName() << "\n");
 
   if (!F.hasFnAttribute(Attribute::SafeStack)) {
@@ -556,6 +547,10 @@ bool SafeStack::runOnFunction(Function &F) {
     return false;
   }
 
+  auto AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
+
+  TLI = TM ? TM->getSubtargetImpl(F)->getTargetLowering() : nullptr;
+
   {
     // Make sure the regular stack protector won't run on this function
     // (safestack attribute takes precedence).
@@ -614,7 +609,7 @@ bool SafeStack::runOnFunction(Function &F) {
   // FIXME: a better alternative might be to store the unsafe stack pointer
   // before setjmp / invoke instructions.
   AllocaInst *DynamicTop = createStackRestorePoints(
-      F, StackRestorePoints, StaticTop, !DynamicAllocas.empty());
+      IRB, F, StackRestorePoints, StaticTop, !DynamicAllocas.empty());
 
   // Handle dynamic allocas.
   moveDynamicAllocasToUnsafeStack(F, UnsafeStackPtr, DynamicTop,
@@ -624,7 +619,7 @@ bool SafeStack::runOnFunction(Function &F) {
   return true;
 }
 
-} // end anonymous namespace
+} // anonymous namespace
 
 char SafeStack::ID = 0;
 INITIALIZE_TM_PASS_BEGIN(SafeStack, "safe-stack",