[PATCH] Fix for crash during SjLj preparation step
Valerii Hiora
valerii.hiora at gmail.com
Fri Jul 4 01:07:06 PDT 2014
Added a test case and used 'select' as Bill proposed.
Still need advice if it is possible to unify both aggregate type branches into one, as I'm not sure if there is a combination of codegen/optimization flags which might not eliminate loading from null.
http://reviews.llvm.org/D4256
Files:
lib/CodeGen/SjLjEHPrepare.cpp
test/CodeGen/Thumb/sjljehprepare-lower-empty-struct.ll
Index: lib/CodeGen/SjLjEHPrepare.cpp
===================================================================
--- lib/CodeGen/SjLjEHPrepare.cpp
+++ lib/CodeGen/SjLjEHPrepare.cpp
@@ -248,19 +248,38 @@
for (Function::arg_iterator AI = F.arg_begin(), AE = F.arg_end(); AI != AE;
++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);
-
- // Set the operand of the instructions back to the AllocaInst.
- EI->setOperand(0, AI);
- NI->setOperand(0, AI);
+ StructType *ST = dyn_cast<StructType>(Ty);
+ ArrayType *AT = dyn_cast<ArrayType>(Ty);
+
+ if (ST || AT) {
+ // Aggregate types can't be cast, but are legal argument types, so we have
+ // to handle them differently. We use an always true selection to achieve
+ // the same goal
+ if ((ST && ST->getNumElements() != 0)
+ || (AT && AT->getNumElements() != 0)) {
+ Instruction *EI = ExtractValueInst::Create(AI, 0, "", AfterAllocaInsPt);
+ Instruction *NI = InsertValueInst::Create(AI, EI, 0);
+ NI->insertAfter(EI);
+ AI->replaceAllUsesWith(NI);
+
+ // Set the operand of the instructions back to the AllocaInst.
+ EI->setOperand(0, AI);
+ NI->setOperand(0, AI);
+ } else {
+ // If it is empty structure or 0-length array it is impossible
+ // to use ExtractValue/InsertValue pair, so instead
+ // always-true select is used against arg and reconstructed
+ // from null value
+ Value *NullValue = ConstantPointerNull::get(Ty->getPointerTo());
+
+ Instruction *LI = new LoadInst(NullValue, AI->getName() + ".null", AfterAllocaInsPt);
+ Value *TrueValue = ConstantInt::getTrue(F.getContext());
+ Instruction *SI = SelectInst::Create(TrueValue, AI, LI);
+ SI->insertAfter(LI);
+ AI->replaceAllUsesWith(SI);
+
+ 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
Index: test/CodeGen/Thumb/sjljehprepare-lower-empty-struct.ll
===================================================================
--- /dev/null
+++ test/CodeGen/Thumb/sjljehprepare-lower-empty-struct.ll
@@ -0,0 +1,22 @@
+; RUN: llc -mtriple=thumbv7-apple-ios < %s
+; SjLjEHPrepare shouldn't crash when lowering empty structs.
+
+target datalayout = "e-p:32:32:32-i1:8:32-i8:8:32-i16:16:32-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:32:64-v128:32:128-a0:0:32-n32-S32"
+target triple = "thumbv7-apple-ios"
+
+define i8* @foo(i32 %d, {} %c) {
+entry:
+ 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(...)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D4256.11076.patch
Type: text/x-patch
Size: 3330 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140704/501eccc8/attachment.bin>
More information about the llvm-commits
mailing list