[PATCH] D37617: Fix for unchecked dyn_cast in globalot created in rL312318

Nikola Prica via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 8 02:48:20 PDT 2017


NikolaPrica created this revision.

The following test case used to produce crash because of use of unchecked dyn_cast to CIInit.

long a[];
static long b = a;
fn1() {

  a > b;
  b = 0;

}


https://reviews.llvm.org/D37617

Files:
  lib/Transforms/IPO/GlobalOpt.cpp


Index: lib/Transforms/IPO/GlobalOpt.cpp
===================================================================
--- lib/Transforms/IPO/GlobalOpt.cpp
+++ lib/Transforms/IPO/GlobalOpt.cpp
@@ -1610,35 +1610,40 @@
   // If initialized to zero and storing one into the global, we can use a cast
   // instead of a select to synthesize the desired value.
   bool IsOneZero = false;
+  bool EmitOneOrZero = true;
   if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)){
     IsOneZero = InitVal->isNullValue() && CI->isOne();
 
-    ConstantInt *CIInit = dyn_cast<ConstantInt>(GV->getInitializer());
-    uint64_t ValInit = CIInit->getZExtValue();
-    uint64_t ValOther = CI->getZExtValue();
-    uint64_t ValMinus = ValOther - ValInit;
-
-    for(auto *GVe : GVs){
-      DIGlobalVariable *DGV = GVe->getVariable();
-      DIExpression *E = GVe->getExpression();
-
-      // val * (ValOther - ValInit) + ValInit:
-      // DW_OP_deref DW_OP_constu <ValMinus>
-      // DW_OP_mul DW_OP_constu <ValInit> DW_OP_plus DW_OP_stack_value
-      E = DIExpression::get(NewGV->getContext(),
-                           {dwarf::DW_OP_deref,
-                            dwarf::DW_OP_constu,
-                            ValMinus,
-                            dwarf::DW_OP_mul,
-                            dwarf::DW_OP_constu,
-                            ValInit,
-                            dwarf::DW_OP_plus,
-                            dwarf::DW_OP_stack_value});
-      DIGlobalVariableExpression *DGVE =
-        DIGlobalVariableExpression::get(NewGV->getContext(), DGV, E);
-      NewGV->addDebugInfo(DGVE);
+    if (ConstantInt *CIInit = dyn_cast<ConstantInt>(GV->getInitializer())){
+      uint64_t ValInit = CIInit->getZExtValue();
+      uint64_t ValOther = CI->getZExtValue();
+      uint64_t ValMinus = ValOther - ValInit;
+
+      for(auto *GVe : GVs){
+        DIGlobalVariable *DGV = GVe->getVariable();
+        DIExpression *E = GVe->getExpression();
+
+        // val * (ValOther - ValInit) + ValInit:
+        // DW_OP_deref DW_OP_constu <ValMinus>
+        // DW_OP_mul DW_OP_constu <ValInit> DW_OP_plus DW_OP_stack_value
+        E = DIExpression::get(NewGV->getContext(),
+                             {dwarf::DW_OP_deref,
+                              dwarf::DW_OP_constu,
+                              ValMinus,
+                              dwarf::DW_OP_mul,
+                              dwarf::DW_OP_constu,
+                              ValInit,
+                              dwarf::DW_OP_plus,
+                              dwarf::DW_OP_stack_value});
+        DIGlobalVariableExpression *DGVE =
+          DIGlobalVariableExpression::get(NewGV->getContext(), DGV, E);
+        NewGV->addDebugInfo(DGVE);
+      }
+      EmitOneOrZero = false;
     }
-  } else {
+  }
+
+  if (EmitOneOrZero) {
     // FIXME: This will only emit address for debugger on which will
     // be written only 0 or 1.
     for(auto *GV : GVs)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D37617.114324.patch
Type: text/x-patch
Size: 2942 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170908/419b262c/attachment.bin>


More information about the llvm-commits mailing list