[llvm] r220156 - Switch how the datalayout availability test is handled in this code to

Chandler Carruth chandlerc at gmail.com
Sat Oct 18 17:42:16 PDT 2014


Author: chandlerc
Date: Sat Oct 18 19:42:16 2014
New Revision: 220156

URL: http://llvm.org/viewvc/llvm-project?rev=220156&view=rev
Log:
Switch how the datalayout availability test is handled in this code to
make much more sense and in theory be more correct.

If you trace the code alllll the way back to when it was first
introduced, the comments make it slightly more clear what was going on
here. At that time, the only way Base != V was if DL (then TD) was
non-null. As a consequence, if DL *was* null, that meant we were loading
directly from the alloca or global found above the test. After
refactoring, this has become at least terribly subtle and potentially
incorrect. There are many forms of pointer manipulation that can be
traversed without DataLayout, and some of them would in fact change the
size of object being loaded vs. allocated.

Rather than this subtlety, I've hoisted the actual 'return true' bits
into the code which actually found an alloca or global and based them on
the loaded pointer being that alloca or global. This is both more clear
and safer. I've also added comments about exactly why this set of
predicates is used.

I've also corrected a misleading comment about globals -- if overridden
they may not just have a different size, they may be null and completely
unsafe to load from!

Hopefully this confuses the next reader a bit less. I don't have any
test cases or anything, the patch is motivated strictly to improve the
readability of the code.

Modified:
    llvm/trunk/lib/Analysis/Loads.cpp

Modified: llvm/trunk/lib/Analysis/Loads.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/Loads.cpp?rev=220156&r1=220155&r2=220156&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/Loads.cpp (original)
+++ llvm/trunk/lib/Analysis/Loads.cpp Sat Oct 18 19:42:16 2014
@@ -73,26 +73,39 @@ bool llvm::isSafeToLoadUnconditionally(V
   Type *BaseType = nullptr;
   unsigned BaseAlign = 0;
   if (const AllocaInst *AI = dyn_cast<AllocaInst>(Base)) {
+    // Loading directly from an alloca is trivially safe. We can't even look
+    // through pointer casts here though, as that might change the size loaded.
+    if (AI == V)
+      return true;
+
     // An alloca is safe to load from as load as it is suitably aligned.
     BaseType = AI->getAllocatedType();
     BaseAlign = AI->getAlignment();
   } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) {
-    // Global variables are safe to load from but their size cannot be
-    // guaranteed if they are overridden.
+    // Global variables are not necessarily safe to load from if they are
+    // overridden. Their size may change or they may be weak and require a test
+    // to determine if they were in fact provided.
     if (!GV->mayBeOverridden()) {
+      // Loading directly from the non-overridden global is trivially safe. We
+      // can't even look through pointer casts here though, as that might change
+      // the size loaded.
+      if (GV == V)
+        return true;
+
       BaseType = GV->getType()->getElementType();
       BaseAlign = GV->getAlignment();
     }
   }
 
-  if (BaseType && BaseType->isSized()) {
-    if (DL && BaseAlign == 0)
+  // If we found a base allocated type from either an alloca or global variable,
+  // try to see if we are definitively within the allocated region. We need to
+  // know the size of the base type and the loaded type to do anything in this
+  // case, so only try this when we have the DataLayout available.
+  if (BaseType && BaseType->isSized() && DL) {
+    if (BaseAlign == 0)
       BaseAlign = DL->getPrefTypeAlignment(BaseType);
 
     if (Align <= BaseAlign) {
-      if (!DL)
-        return true; // Loading directly from an alloca or global is OK.
-
       // Check if the load is within the bounds of the underlying object.
       PointerType *AddrTy = cast<PointerType>(V->getType());
       uint64_t LoadSize = DL->getTypeStoreSize(AddrTy->getElementType());





More information about the llvm-commits mailing list