Index: llvm/lib/Transforms/Coroutines/CoroCleanup.cpp =================================================================== --- llvm/lib/Transforms/Coroutines/CoroCleanup.cpp +++ llvm/lib/Transforms/Coroutines/CoroCleanup.cpp @@ -12,6 +12,7 @@ #include "llvm/IR/InstIterator.h" #include "llvm/IR/PassManager.h" #include "llvm/Pass.h" +#include "llvm/Transforms/Scalar/EarlyCSE.h" #include "llvm/Transforms/Scalar/SimplifyCFG.h" using namespace llvm; @@ -146,6 +147,7 @@ FunctionPassManager FPM; FPM.addPass(SimplifyCFGPass()); + FPM.addPass(EarlyCSEPass()); Lowerer L(M); for (auto &F : M) Index: llvm/test/Transforms/Coroutines/coro-readnone-06.ll =================================================================== --- /dev/null +++ llvm/test/Transforms/Coroutines/coro-readnone-06.ll @@ -0,0 +1,58 @@ +; Tests that the readnone function which don't cross suspend points could be optimized expectly. +; RUN: opt < %s -S -passes='default' -opaque-pointers | FileCheck %s + +define ptr @f() "coroutine.presplit" { +entry: + %id = call token @llvm.coro.id(i32 0, i8* null, i8* null, i8* null) + %size = call i32 @llvm.coro.size.i32() + %alloc = call i8* @malloc(i32 %size) + %hdl = call i8* @llvm.coro.begin(token %id, i8* %alloc) + %sus_result = call i8 @llvm.coro.suspend(token none, i1 false) + switch i8 %sus_result, label %suspend [i8 0, label %resume + i8 1, label %cleanup] +resume: + %i = call i32 @readnone_func() readnone + %j = call i32 @readnone_func() readnone + %cmp = icmp eq i32 %i, %j + br i1 %cmp, label %same, label %diff + +same: + call void @print_same() + br label %cleanup + +diff: + call void @print_diff() + br label %cleanup + +cleanup: + %mem = call i8* @llvm.coro.free(token %id, i8* %hdl) + call void @free(i8* %mem) + br label %suspend + +suspend: + call i1 @llvm.coro.end(i8* %hdl, i1 0) + ret i8* %hdl +} + +; CHECK-LABEL: f.resume( +; CHECK-NEXT: entry +; CHECK-NEXT: call i32 @readnone_func() +; CHECK-NEXT: call void @print_same() +; CHECK-NEXT: call void @free +; CHECK-NEXT: ret void + +declare i32 @readnone_func() readnone + +declare void @print_same() +declare void @print_diff() +declare i8* @llvm.coro.free(token, i8*) +declare i32 @llvm.coro.size.i32() +declare i8 @llvm.coro.suspend(token, i1) + +declare token @llvm.coro.id(i32, i8*, i8*, i8*) +declare i1 @llvm.coro.alloc(token) +declare i8* @llvm.coro.begin(token, i8*) +declare i1 @llvm.coro.end(i8*, i1) + +declare noalias i8* @malloc(i32) +declare void @free(i8*)