<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - [AArch64] Assertion when lowering LOAD_STACK_GUARD"
   href="http://llvm.org/bugs/show_bug.cgi?id=20558">20558</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[AArch64] Assertion when lowering LOAD_STACK_GUARD
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>tools
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Windows NT
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>llvmc
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>apazos@codeaurora.org
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=12859" name="attach_12859" title="Fix assertion when lowering LOAD-STACK-GUARD">attachment 12859</a> <a href="attachment.cgi?id=12859&action=edit" title="Fix assertion when lowering LOAD-STACK-GUARD">[details]</a></span>
Fix assertion when lowering LOAD-STACK-GUARD

Hi Akira,

We hit an assertion in expandPostRAPseudo when lowering LOAD_STACK_GUARD.

The call to cast<GlobalValue> asserts with

/llvm/include/llvm/Support/Casting.h:237: typename llvm::cast_retty<X,
Y*>::ret_type llvm::cast(Y*) [with X = llvm::GlobalValue; Y = const
llvm::Value; typename llvm::cast_retty<X, Y*>::ret_type = const
llvm::GlobalValue*]: Assertion `isa<X>(Val) && "cast<Ty>() argument of
incompatible type!"' failed.

The memory operand expected in the exapandPostRAPseudo is a GlobalValue but
instead in the failing test case we have a ConstantExpr.

I tried to get a simplified test case that reproduces the issue. Here it is:

define i32  @test_stack_guard_remat2() {
entry:
  %StackGuardSlot = alloca i8*
  %StackGuard = load i8** bitcast ([0 x i32]* @__stack_chk_guard to i8**)
  call void @llvm.stackprotector(i8* %StackGuard, i8** %StackGuardSlot)
  %container = alloca [32 x i8], align 1
  call void @llvm.stackprotectorcheck(i8** bitcast ([0 x i32]*
@__stack_chk_guard to i8**))
  ret i32 -1
}

I also tried to fix the issue. See the patch below.

--- a/lib/Target/AArch64/AArch64InstrInfo.cpp
+++ b/lib/Target/AArch64/AArch64InstrInfo.cpp
@@ -901,8 +901,12 @@
AArch64InstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
   MachineBasicBlock &MBB = *MI->getParent();
   DebugLoc DL = MI->getDebugLoc();
   unsigned Reg = MI->getOperand(0).getReg();
-  const GlobalValue *GV =
-      cast<GlobalValue>((*MI->memoperands_begin())->getValue());
+  const GlobalValue *GV = nullptr;
+
+  const Value *Val = (*MI->memoperands_begin())->getValue();
+  if (const ConstantExpr *CE = dyn_cast<const ConstantExpr>(Val))
+    Val = CE->getOperand(0);
+  GV = cast<GlobalValue>(Val);
   const TargetMachine &TM = MBB.getParent()->getTarget();
   unsigned char OpFlags = Subtarget.ClassifyGlobalReference(GV, TM);
   const unsigned char MO_NC = AArch64II::MO_NC;


Let me know what you think.

Thanks,
Ana.</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>