[llvm] r212922 - Support lowering of empty aggregates.

Bill Wendling isanbard at gmail.com
Sun Jul 13 23:22:36 PDT 2014


Author: void
Date: Mon Jul 14 01:22:36 2014
New Revision: 212922

URL: http://llvm.org/viewvc/llvm-project?rev=212922&view=rev
Log:
Support lowering of empty aggregates.

This crash was pretty common while compiling Rust for iOS (armv7). Reason -
SjLj preparation step was lowering aggregate arguments as ExtractValue +
InsertValue. ExtractValue has assertion which checks that there is some data in
value, which is not true in case of empty (no fields) structures. Rust uses
them quite extensively so this patch uses a 'select true, %val, undef'
instruction to lower the argument.

Patch by Valerii Hiora.

Added:
    llvm/trunk/test/CodeGen/ARM/sjljehprepare-lower-empty-struct.ll
Modified:
    llvm/trunk/lib/CodeGen/SjLjEHPrepare.cpp

Modified: llvm/trunk/lib/CodeGen/SjLjEHPrepare.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SjLjEHPrepare.cpp?rev=212922&r1=212921&r2=212922&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/SjLjEHPrepare.cpp (original)
+++ llvm/trunk/lib/CodeGen/SjLjEHPrepare.cpp Mon Jul 14 01:22:36 2014
@@ -249,18 +249,18 @@ void SjLjEHPrepare::lowerIncomingArgumen
        ++AI) {
     Type *Ty = AI->getType();
 
-    // Aggregate types can't be cast, but are legal argument types, so we have
-    // to handle them differently. We use an extract/insert pair as a
-    // lightweight method to achieve the same goal.
     if (isa<StructType>(Ty) || isa<ArrayType>(Ty)) {
-      Instruction *EI = ExtractValueInst::Create(AI, 0, "", AfterAllocaInsPt);
-      Instruction *NI = InsertValueInst::Create(AI, EI, 0);
-      NI->insertAfter(EI);
-      AI->replaceAllUsesWith(NI);
+      // Aggregate types can't be cast, but are legal argument types,
+      // so we have to handle them differently.  We use
+      // select i8 true, %arg, undef to achieve the same goal
+      Value *TrueValue = ConstantInt::getTrue(F.getContext());
+      Value *UndefValue = UndefValue::get(Ty);
+      Instruction *SI = SelectInst::Create(TrueValue, AI, UndefValue,
+                                           AI->getName() + ".tmp",
+                                           AfterAllocaInsPt);
+      AI->replaceAllUsesWith(SI);
 
-      // Set the operand of the instructions back to the AllocaInst.
-      EI->setOperand(0, AI);
-      NI->setOperand(0, AI);
+      SI->setOperand(1, AI);
     } else {
       // This is always a no-op cast because we're casting AI to AI->getType()
       // so src and destination types are identical. BitCast is the only

Added: llvm/trunk/test/CodeGen/ARM/sjljehprepare-lower-empty-struct.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/ARM/sjljehprepare-lower-empty-struct.ll?rev=212922&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/ARM/sjljehprepare-lower-empty-struct.ll (added)
+++ llvm/trunk/test/CodeGen/ARM/sjljehprepare-lower-empty-struct.ll Mon Jul 14 01:22:36 2014
@@ -0,0 +1,31 @@
+; RUN: llc -mtriple=armv7-apple-ios -O0 < %s | FileCheck %s
+; RUN: llc -mtriple=armv7-apple-ios -O1 < %s | FileCheck %s
+; RUN: llc -mtriple=armv7-apple-ios -O2 < %s | FileCheck %s
+; RUN: llc -mtriple=armv7-apple-ios -O3 < %s | FileCheck %s
+
+; SjLjEHPrepare shouldn't crash when lowering empty structs.
+;
+; Checks that between in case of empty structs used as arguments
+; nothing happens, i.e. there are no instructions between
+; __Unwind_SjLj_Register and actual @bar invocation
+
+
+define i8* @foo({} %c) {
+entry:
+; CHECK: bl __Unwind_SjLj_Register
+; CHECK-NEXT: {{[A-Z][a-zA-Z0-9]*}}:
+; CHECK-NEXT: bl _bar
+  invoke void @bar ()
+    to label %unreachable unwind label %handler
+
+unreachable:
+  unreachable
+
+handler:
+  %tmp = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @baz to i8*)
+  cleanup
+  resume { i8*, i32 } undef
+}
+
+declare void @bar()
+declare i32 @baz(...)





More information about the llvm-commits mailing list