<div dir="ltr">Hi Tom!<div><br></div><div>I think this change may be relevant to the failures we see in our ASan bootstrap buildbot. Can you take a look?</div><div><br></div><div>The following tests fail:</div><div> LLVM :: Transforms/SimplifyCFG/R600/parallelandifcollapse.ll<br>
</div><div><div> LLVM :: Transforms/SimplifyCFG/R600/parallelorifcollapse.ll</div></div><div><br></div><div>with this error report:</div><div><br></div><div>==24655==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60700000deb0 at pc 0x25589c7 bp 0x7fff185cd3f0 sp 0x7fff185cd3e8<br>
</div><div><div>READ of size 8 at 0x60700000deb0 thread T0</div><div> #0 0x25589c6 in getDebugLoc /build/llvm/include/llvm/IR/IRBuilder.h:115</div><div> #1 0x25589c6 in SetInsertPoint /build/llvm/include/llvm/IR/IRBuilder.h:90</div>
<div> #2 0x25589c6 in SimplifyParallelAndOr /build/llvm/lib/Transforms/Utils/SimplifyCFG.cpp:4323</div><div> #3 0x25589c6 in (anonymous namespace)::SimplifyCFGOpt::run(llvm::BasicBlock*) /build/llvm/lib/Transforms/Utils/SimplifyCFG.cpp:4587</div>
<div> #4 0x255444c in llvm::SimplifyCFG(llvm::BasicBlock*, llvm::TargetTransformInfo const&, llvm::DataLayout const*, llvm::AliasAnalysis*) /build/llvm/lib/Transforms/Utils/SimplifyCFG.cpp:4630</div><div> #5 0x231c281 in iterativelySimplifyCFG /build/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp:346</div>
<div> #6 0x231c281 in (anonymous namespace)::CFGSimplifyPass::runOnFunction(llvm::Function&) /build/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp:368</div><div> #7 0x2b10cac in llvm::FPPassManager::runOnFunction(llvm::Function&) /build/llvm/lib/IR/PassManager.cpp:1530</div>
<div> #8 0x2b11275 in llvm::FPPassManager::runOnModule(llvm::Module&) /build/llvm/lib/IR/PassManager.cpp:1550</div><div> #9 0x2b11a9b in llvm::MPPassManager::runOnModule(llvm::Module&) /build/llvm/lib/IR/PassManager.cpp:1608</div>
<div> #10 0x2b12cb3 in llvm::PassManagerImpl::run(llvm::Module&) /build/llvm/lib/IR/PassManager.cpp:1703</div><div> #11 0x2b1312f in llvm::PassManager::run(llvm::Module&) /build/llvm/lib/IR/PassManager.cpp:1738</div>
<div> #12 0x61c930 in main /build/llvm/tools/opt/opt.cpp:827</div><div> #13 0x7f285711b76c (/lib/x86_64-linux-gnu/libc.so.6+0x2176c)</div><div> #14 0x60d97c in _start (/build/llvm_build_asan/bin/opt+0x60d97c)</div>
<div>0x60700000deb0 is located 0 bytes to the right of 80-byte region [0x60700000de60,0x60700000deb0)</div><div>allocated by thread T0 here:</div><div> #0 0x5f7d15 in operator new(unsigned long) /build/llvm/projects/compiler-rt/lib/asan/asan_new_delete.cc:52</div>
<div> #1 0x148367d in Twine /build/llvm/include/llvm/IR/BasicBlock.h:111</div><div> #2 0x148367d in llvm::LLParser::PerFunctionState::GetVal(std::string const&, llvm::Type*, llvm::SMLoc) /build/llvm/lib/AsmParser/LLParser.cpp:2063</div>
<div> #3 0x14890b1 in llvm::LLParser::ConvertValIDToValue(llvm::Type*, llvm::ValID&, llvm::Value*&, llvm::LLParser::PerFunctionState*) /build/llvm/lib/AsmParser/LLParser.cpp:2785</div><div> #4 0x148b4af in ParseValue /build/llvm/lib/AsmParser/LLParser.cpp:2898</div>
<div> #5 0x148b4af in llvm::LLParser::ParseTypeAndValue(llvm::Value*&, llvm::LLParser::PerFunctionState*) /build/llvm/lib/AsmParser/LLParser.cpp:2904</div><div> #6 0x148e68a in ParseTypeAndValue /build/llvm/lib/AsmParser/LLParser.h:319</div>
<div> #7 0x148e68a in ParseTypeAndBasicBlock /build/llvm/lib/AsmParser/LLParser.cpp:2911</div><div> #8 0x148e68a in llvm::LLParser::ParseBr(llvm::Instruction*&, llvm::LLParser::PerFunctionState&) /build/llvm/lib/AsmParser/LLParser.cpp:3428</div>
<div> #9 0x148cce9 in llvm::LLParser::ParseInstruction(llvm::Instruction*&, llvm::BasicBlock*, llvm::LLParser::PerFunctionState&) /build/llvm/lib/AsmParser/LLParser.cpp:3243</div><div> #10 0x148bdf8 in llvm::LLParser::ParseBasicBlock(llvm::LLParser::PerFunctionState&) /build/llvm/lib/AsmParser/LLParser.cpp:3194</div>
<div> #11 0x146459f in llvm::LLParser::ParseFunctionBody(llvm::Function&) /build/llvm/lib/AsmParser/LLParser.cpp:3147</div><div> #12 0x1451ad1 in ParseDefine /build/llvm/lib/AsmParser/LLParser.cpp:424</div><div>
#13 0x1451ad1 in llvm::LLParser::ParseTopLevelEntities() /build/llvm/lib/AsmParser/LLParser.cpp:226</div><div> #14 0x145175e in llvm::LLParser::Run() /build/llvm/lib/AsmParser/LLParser.cpp:41</div><div> #15 0x144324b in llvm::ParseAssembly(llvm::MemoryBuffer*, llvm::Module*, llvm::SMDiagnostic&, llvm::LLVMContext&) /build/llvm/lib/AsmParser/Parser.cpp:38</div>
<div> #16 0x11c299b in llvm::ParseIR(llvm::MemoryBuffer*, llvm::SMDiagnostic&, llvm::LLVMContext&) /build/llvm/lib/IRReader/IRReader.cpp:76</div><div> #17 0x11c32bc in llvm::ParseIRFile(std::string const&, llvm::SMDiagnostic&, llvm::LLVMContext&) /build/llvm/lib/IRReader/IRReader.cpp:88</div>
<div> #18 0x617bbe in main /build/llvm/tools/opt/opt.cpp:593</div><div> #19 0x7f285711b76c (/lib/x86_64-linux-gnu/libc.so.6+0x2176c)</div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sat, Jul 27, 2013 at 4:01 AM, Tom Stellard <span dir="ltr"><<a href="mailto:thomas.stellard@amd.com" target="_blank">thomas.stellard@amd.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: tstellar<br>
Date: Fri Jul 26 19:01:07 2013<br>
New Revision: 187278<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=187278&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=187278&view=rev</a><br>
Log:<br>
SimplifyCFG: Use parallel-and and parallel-or mode to consolidate branch conditions<br>
<br>
Merge consecutive if-regions if they contain identical statements.<br>
Both transformations reduce number of branches. The transformation<br>
is guarded by a target-hook, and is currently enabled only for +R600,<br>
but the correctness has been tested on X86 target using a variety of<br>
CPU benchmarks.<br>
<br>
Patch by: Mei Ye<br>
<br>
Added:<br>
llvm/trunk/lib/Target/R600/AMDGPUTargetTransformInfo.cpp<br>
Modified:<br>
llvm/trunk/include/llvm/Analysis/TargetTransformInfo.h<br>
llvm/trunk/include/llvm/InitializePasses.h<br>
llvm/trunk/include/llvm/Transforms/Scalar.h<br>
llvm/trunk/include/llvm/Transforms/Utils/Local.h<br>
llvm/trunk/lib/Analysis/TargetTransformInfo.cpp<br>
llvm/trunk/lib/CodeGen/BasicTargetTransformInfo.cpp<br>
llvm/trunk/lib/Target/R600/AMDGPU.h<br>
llvm/trunk/lib/Target/R600/AMDGPUTargetMachine.cpp<br>
llvm/trunk/lib/Target/R600/AMDGPUTargetMachine.h<br>
llvm/trunk/lib/Target/R600/CMakeLists.txt<br>
llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp<br>
llvm/trunk/lib/Transforms/Scalar/Scalar.cpp<br>
llvm/trunk/lib/Transforms/Scalar/SimplifyCFGPass.cpp<br>
llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp<br>
llvm/trunk/tools/lto/LTOCodeGenerator.cpp<br>
llvm/trunk/tools/opt/opt.cpp<br>
<br>
Modified: llvm/trunk/include/llvm/Analysis/TargetTransformInfo.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/TargetTransformInfo.h?rev=187278&r1=187277&r2=187278&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/TargetTransformInfo.h?rev=187278&r1=187277&r2=187278&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/Analysis/TargetTransformInfo.h (original)<br>
+++ llvm/trunk/include/llvm/Analysis/TargetTransformInfo.h Fri Jul 26 19:01:07 2013<br>
@@ -171,6 +171,12 @@ public:<br>
/// comments for a detailed explanation of the cost values.<br>
virtual unsigned getUserCost(const User *U) const;<br>
<br>
+ /// \brief hasBranchDivergence - Return true if branch divergence exists.<br>
+ /// Branch divergence has a significantly negative impact on GPU performance<br>
+ /// when threads in the same wavefront take different paths due to conditional<br>
+ /// branches.<br>
+ virtual bool hasBranchDivergence() const;<br>
+<br>
/// \brief Test whether calls to a function lower to actual program function<br>
/// calls.<br>
///<br>
<br>
Modified: llvm/trunk/include/llvm/InitializePasses.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/InitializePasses.h?rev=187278&r1=187277&r2=187278&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/InitializePasses.h?rev=187278&r1=187277&r2=187278&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/InitializePasses.h (original)<br>
+++ llvm/trunk/include/llvm/InitializePasses.h Fri Jul 26 19:01:07 2013<br>
@@ -86,7 +86,8 @@ void initializeCallGraphViewerPass(PassR<br>
void initializeCFGOnlyPrinterPass(PassRegistry&);<br>
void initializeCFGOnlyViewerPass(PassRegistry&);<br>
void initializeCFGPrinterPass(PassRegistry&);<br>
-void initializeCFGSimplifyPassPass(PassRegistry&);<br>
+void initializeCFGOptimizePass(PassRegistry&);<br>
+void initializeCFGCanonicalizePass(PassRegistry&);<br>
void initializeStructurizeCFGPass(PassRegistry&);<br>
void initializeCFGViewerPass(PassRegistry&);<br>
void initializeCalculateSpillWeightsPass(PassRegistry&);<br>
<br>
Modified: llvm/trunk/include/llvm/Transforms/Scalar.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Scalar.h?rev=187278&r1=187277&r2=187278&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Scalar.h?rev=187278&r1=187277&r2=187278&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/Transforms/Scalar.h (original)<br>
+++ llvm/trunk/include/llvm/Transforms/Scalar.h Fri Jul 26 19:01:07 2013<br>
@@ -196,7 +196,7 @@ FunctionPass *createJumpThreadingPass();<br>
// CFGSimplification - Merge basic blocks, eliminate unreachable blocks,<br>
// simplify terminator instructions, etc...<br>
//<br>
-FunctionPass *createCFGSimplificationPass();<br>
+FunctionPass *createCFGSimplificationPass(bool IsTargetAware = false);<br>
<br>
//===----------------------------------------------------------------------===//<br>
//<br>
<br>
Modified: llvm/trunk/include/llvm/Transforms/Utils/Local.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Utils/Local.h?rev=187278&r1=187277&r2=187278&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Utils/Local.h?rev=187278&r1=187277&r2=187278&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/Transforms/Utils/Local.h (original)<br>
+++ llvm/trunk/include/llvm/Transforms/Utils/Local.h Fri Jul 26 19:01:07 2013<br>
@@ -39,6 +39,7 @@ class DataLayout;<br>
class TargetLibraryInfo;<br>
class TargetTransformInfo;<br>
class DIBuilder;<br>
+class AliasAnalysis;<br>
<br>
template<typename T> class SmallVectorImpl;<br>
<br>
@@ -136,7 +137,7 @@ bool EliminateDuplicatePHINodes(BasicBlo<br>
/// the basic block that was pointed to.<br>
///<br>
bool SimplifyCFG(BasicBlock *BB, const TargetTransformInfo &TTI,<br>
- const DataLayout *TD = 0);<br>
+ const DataLayout *TD = 0, AliasAnalysis *AA = 0);<br>
<br>
/// FoldBranchToCommonDest - If this basic block is ONLY a setcc and a branch,<br>
/// and if a predecessor branches to us and one of our successors, fold the<br>
<br>
Modified: llvm/trunk/lib/Analysis/TargetTransformInfo.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/TargetTransformInfo.cpp?rev=187278&r1=187277&r2=187278&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/TargetTransformInfo.cpp?rev=187278&r1=187277&r2=187278&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Analysis/TargetTransformInfo.cpp (original)<br>
+++ llvm/trunk/lib/Analysis/TargetTransformInfo.cpp Fri Jul 26 19:01:07 2013<br>
@@ -88,6 +88,10 @@ unsigned TargetTransformInfo::getUserCos<br>
return PrevTTI->getUserCost(U);<br>
}<br>
<br>
+bool TargetTransformInfo::hasBranchDivergence() const {<br>
+ return PrevTTI->hasBranchDivergence();<br>
+}<br>
+<br>
bool TargetTransformInfo::isLoweredToCall(const Function *F) const {<br>
return PrevTTI->isLoweredToCall(F);<br>
}<br>
@@ -420,6 +424,8 @@ struct NoTTI : ImmutablePass, TargetTran<br>
U->getOperand(0)->getType() : 0);<br>
}<br>
<br>
+ bool hasBranchDivergence() const { return false; }<br>
+<br>
bool isLoweredToCall(const Function *F) const {<br>
// FIXME: These should almost certainly not be handled here, and instead<br>
// handled with the help of TLI or the target itself. This was largely<br>
<br>
Modified: llvm/trunk/lib/CodeGen/BasicTargetTransformInfo.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/BasicTargetTransformInfo.cpp?rev=187278&r1=187277&r2=187278&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/BasicTargetTransformInfo.cpp?rev=187278&r1=187277&r2=187278&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/CodeGen/BasicTargetTransformInfo.cpp (original)<br>
+++ llvm/trunk/lib/CodeGen/BasicTargetTransformInfo.cpp Fri Jul 26 19:01:07 2013<br>
@@ -65,6 +65,8 @@ public:<br>
return this;<br>
}<br>
<br>
+ virtual bool hasBranchDivergence() const;<br>
+<br>
/// \name Scalar TTI Implementations<br>
/// @{<br>
<br>
@@ -124,6 +126,7 @@ llvm::createBasicTargetTransformInfoPass<br>
return new BasicTTI(TM);<br>
}<br>
<br>
+bool BasicTTI::hasBranchDivergence() const { return false; }<br>
<br>
bool BasicTTI::isLegalAddImmediate(int64_t imm) const {<br>
return getTLI()->isLegalAddImmediate(imm);<br>
<br>
Modified: llvm/trunk/lib/Target/R600/AMDGPU.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/R600/AMDGPU.h?rev=187278&r1=187277&r2=187278&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/R600/AMDGPU.h?rev=187278&r1=187277&r2=187278&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Target/R600/AMDGPU.h (original)<br>
+++ llvm/trunk/lib/Target/R600/AMDGPU.h Fri Jul 26 19:01:07 2013<br>
@@ -45,6 +45,10 @@ FunctionPass *createAMDGPUConvertToISAPa<br>
FunctionPass *createAMDGPUIndirectAddressingPass(TargetMachine &tm);<br>
FunctionPass *createAMDGPUISelDag(TargetMachine &tm);<br>
<br>
+/// \brief Creates an AMDGPU-specific Target Transformation Info pass.<br>
+ImmutablePass *<br>
+createAMDGPUTargetTransformInfoPass(const AMDGPUTargetMachine *TM);<br>
+<br>
extern Target TheAMDGPUTarget;<br>
<br>
} // End namespace llvm<br>
<br>
Modified: llvm/trunk/lib/Target/R600/AMDGPUTargetMachine.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/R600/AMDGPUTargetMachine.cpp?rev=187278&r1=187277&r2=187278&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/R600/AMDGPUTargetMachine.cpp?rev=187278&r1=187277&r2=187278&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Target/R600/AMDGPUTargetMachine.cpp (original)<br>
+++ llvm/trunk/lib/Target/R600/AMDGPUTargetMachine.cpp Fri Jul 26 19:01:07 2013<br>
@@ -105,6 +105,18 @@ TargetPassConfig *AMDGPUTargetMachine::c<br>
return new AMDGPUPassConfig(this, PM);<br>
}<br>
<br>
+//===----------------------------------------------------------------------===//<br>
+// AMDGPU Analysis Pass Setup<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+void AMDGPUTargetMachine::addAnalysisPasses(PassManagerBase &PM) {<br>
+ // Add first the target-independent BasicTTI pass, then our AMDGPU pass. This<br>
+ // allows the AMDGPU pass to delegate to the target independent layer when<br>
+ // appropriate.<br>
+ PM.add(createBasicTargetTransformInfoPass(this));<br>
+ PM.add(createAMDGPUTargetTransformInfoPass(this));<br>
+}<br>
+<br>
bool<br>
AMDGPUPassConfig::addPreISel() {<br>
const AMDGPUSubtarget &ST = TM->getSubtarget<AMDGPUSubtarget>();<br>
<br>
Modified: llvm/trunk/lib/Target/R600/AMDGPUTargetMachine.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/R600/AMDGPUTargetMachine.h?rev=187278&r1=187277&r2=187278&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/R600/AMDGPUTargetMachine.h?rev=187278&r1=187277&r2=187278&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Target/R600/AMDGPUTargetMachine.h (original)<br>
+++ llvm/trunk/lib/Target/R600/AMDGPUTargetMachine.h Fri Jul 26 19:01:07 2013<br>
@@ -61,6 +61,9 @@ public:<br>
}<br>
virtual const DataLayout *getDataLayout() const { return &Layout; }<br>
virtual TargetPassConfig *createPassConfig(PassManagerBase &PM);<br>
+<br>
+ /// \brief Register R600 analysis passes with a pass manager.<br>
+ virtual void addAnalysisPasses(PassManagerBase &PM);<br>
};<br>
<br>
} // End namespace llvm<br>
<br>
Added: llvm/trunk/lib/Target/R600/AMDGPUTargetTransformInfo.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/R600/AMDGPUTargetTransformInfo.cpp?rev=187278&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/R600/AMDGPUTargetTransformInfo.cpp?rev=187278&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Target/R600/AMDGPUTargetTransformInfo.cpp (added)<br>
+++ llvm/trunk/lib/Target/R600/AMDGPUTargetTransformInfo.cpp Fri Jul 26 19:01:07 2013<br>
@@ -0,0 +1,90 @@<br>
+//===-- AMDGPUTargetTransformInfo.cpp - AMDGPU specific TTI pass ---------===//<br>
+//<br>
+// The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+//<br>
+// \file<br>
+// This file implements a TargetTransformInfo analysis pass specific to the<br>
+// AMDGPU target machine. It uses the target's detailed information to provide<br>
+// more precise answers to certain TTI queries, while letting the target<br>
+// independent and default TTI implementations handle the rest.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#define DEBUG_TYPE "AMDGPUtti"<br>
+#include "AMDGPU.h"<br>
+#include "AMDGPUTargetMachine.h"<br>
+#include "llvm/Analysis/TargetTransformInfo.h"<br>
+#include "llvm/Support/Debug.h"<br>
+#include "llvm/Target/TargetLowering.h"<br>
+#include "llvm/Target/CostTable.h"<br>
+using namespace llvm;<br>
+<br>
+// Declare the pass initialization routine locally as target-specific passes<br>
+// don't have a target-wide initialization entry point, and so we rely on the<br>
+// pass constructor initialization.<br>
+namespace llvm {<br>
+void initializeAMDGPUTTIPass(PassRegistry &);<br>
+}<br>
+<br>
+namespace {<br>
+<br>
+class AMDGPUTTI : public ImmutablePass, public TargetTransformInfo {<br>
+ const AMDGPUTargetMachine *TM;<br>
+ const AMDGPUSubtarget *ST;<br>
+ const AMDGPUTargetLowering *TLI;<br>
+<br>
+ /// Estimate the overhead of scalarizing an instruction. Insert and Extract<br>
+ /// are set if the result needs to be inserted and/or extracted from vectors.<br>
+ unsigned getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const;<br>
+<br>
+public:<br>
+ AMDGPUTTI() : ImmutablePass(ID), TM(0), ST(0), TLI(0) {<br>
+ llvm_unreachable("This pass cannot be directly constructed");<br>
+ }<br>
+<br>
+ AMDGPUTTI(const AMDGPUTargetMachine *TM)<br>
+ : ImmutablePass(ID), TM(TM), ST(TM->getSubtargetImpl()),<br>
+ TLI(TM->getTargetLowering()) {<br>
+ initializeAMDGPUTTIPass(*PassRegistry::getPassRegistry());<br>
+ }<br>
+<br>
+ virtual void initializePass() { pushTTIStack(this); }<br>
+<br>
+ virtual void finalizePass() { popTTIStack(); }<br>
+<br>
+ virtual void getAnalysisUsage(AnalysisUsage &AU) const {<br>
+ TargetTransformInfo::getAnalysisUsage(AU);<br>
+ }<br>
+<br>
+ /// Pass identification.<br>
+ static char ID;<br>
+<br>
+ /// Provide necessary pointer adjustments for the two base classes.<br>
+ virtual void *getAdjustedAnalysisPointer(const void *ID) {<br>
+ if (ID == &TargetTransformInfo::ID)<br>
+ return (TargetTransformInfo *)this;<br>
+ return this;<br>
+ }<br>
+<br>
+ virtual bool hasBranchDivergence() const;<br>
+<br>
+ /// @}<br>
+};<br>
+<br>
+} // end anonymous namespace<br>
+<br>
+INITIALIZE_AG_PASS(AMDGPUTTI, TargetTransformInfo, "AMDGPUtti",<br>
+ "AMDGPU Target Transform Info", true, true, false)<br>
+char AMDGPUTTI::ID = 0;<br>
+<br>
+ImmutablePass *<br>
+llvm::createAMDGPUTargetTransformInfoPass(const AMDGPUTargetMachine *TM) {<br>
+ return new AMDGPUTTI(TM);<br>
+}<br>
+<br>
+bool AMDGPUTTI::hasBranchDivergence() const { return true; }<br>
<br>
Modified: llvm/trunk/lib/Target/R600/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/R600/CMakeLists.txt?rev=187278&r1=187277&r2=187278&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/R600/CMakeLists.txt?rev=187278&r1=187277&r2=187278&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Target/R600/CMakeLists.txt (original)<br>
+++ llvm/trunk/lib/Target/R600/CMakeLists.txt Fri Jul 26 19:01:07 2013<br>
@@ -23,6 +23,7 @@ add_llvm_target(R600CodeGen<br>
AMDGPUMachineFunction.cpp<br>
AMDGPUSubtarget.cpp<br>
AMDGPUTargetMachine.cpp<br>
+ AMDGPUTargetTransformInfo.cpp<br>
AMDGPUISelLowering.cpp<br>
AMDGPUConvertToISA.cpp<br>
AMDGPUInstrInfo.cpp<br>
<br>
Modified: llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp?rev=187278&r1=187277&r2=187278&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp?rev=187278&r1=187277&r2=187278&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp (original)<br>
+++ llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp Fri Jul 26 19:01:07 2013<br>
@@ -235,7 +235,7 @@ void PassManagerBuilder::populateModuleP<br>
}<br>
<br>
MPM.add(createAggressiveDCEPass()); // Delete dead instructions<br>
- MPM.add(createCFGSimplificationPass()); // Merge & remove BBs<br>
+ MPM.add(createCFGSimplificationPass(true)); // Merge & remove BBs<br>
MPM.add(createInstructionCombiningPass()); // Clean up after everything.<br>
<br>
// As an experimental mode, run any vectorization passes in a separate<br>
@@ -371,7 +371,7 @@ void PassManagerBuilder::populateLTOPass<br>
PM.add(createJumpThreadingPass());<br>
<br>
// Delete basic blocks, which optimization passes may have killed.<br>
- PM.add(createCFGSimplificationPass());<br>
+ PM.add(createCFGSimplificationPass(true));<br>
<br>
// Now that we have optimized the program, discard unreachable functions.<br>
PM.add(createGlobalDCEPass());<br>
<br>
Modified: llvm/trunk/lib/Transforms/Scalar/Scalar.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/Scalar.cpp?rev=187278&r1=187277&r2=187278&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/Scalar.cpp?rev=187278&r1=187277&r2=187278&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/Scalar/Scalar.cpp (original)<br>
+++ llvm/trunk/lib/Transforms/Scalar/Scalar.cpp Fri Jul 26 19:01:07 2013<br>
@@ -57,7 +57,8 @@ void llvm::initializeScalarOpts(PassRegi<br>
initializeSROAPass(Registry);<br>
initializeSROA_DTPass(Registry);<br>
initializeSROA_SSAUpPass(Registry);<br>
- initializeCFGSimplifyPassPass(Registry);<br>
+ initializeCFGCanonicalizePass(Registry);<br>
+ initializeCFGOptimizePass(Registry);<br>
initializeStructurizeCFGPass(Registry);<br>
initializeSinkingPass(Registry);<br>
initializeTailCallElimPass(Registry);<br>
<br>
Modified: llvm/trunk/lib/Transforms/Scalar/SimplifyCFGPass.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/SimplifyCFGPass.cpp?rev=187278&r1=187277&r2=187278&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/SimplifyCFGPass.cpp?rev=187278&r1=187277&r2=187278&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/Scalar/SimplifyCFGPass.cpp (original)<br>
+++ llvm/trunk/lib/Transforms/Scalar/SimplifyCFGPass.cpp Fri Jul 26 19:01:07 2013<br>
@@ -27,6 +27,7 @@<br>
#include "llvm/ADT/SmallVector.h"<br>
#include "llvm/ADT/Statistic.h"<br>
#include "llvm/Analysis/TargetTransformInfo.h"<br>
+#include "llvm/Analysis/AliasAnalysis.h"<br>
#include "llvm/IR/Attributes.h"<br>
#include "llvm/IR/Constants.h"<br>
#include "llvm/IR/DataLayout.h"<br>
@@ -41,30 +42,62 @@ using namespace llvm;<br>
STATISTIC(NumSimpl, "Number of blocks simplified");<br>
<br>
namespace {<br>
- struct CFGSimplifyPass : public FunctionPass {<br>
- static char ID; // Pass identification, replacement for typeid<br>
- CFGSimplifyPass() : FunctionPass(ID) {<br>
- initializeCFGSimplifyPassPass(*PassRegistry::getPassRegistry());<br>
- }<br>
+struct CFGSimplifyPass : public FunctionPass {<br>
+ CFGSimplifyPass(char &ID, bool isTargetAware)<br>
+ : FunctionPass(ID), IsTargetAware(isTargetAware) {}<br>
+ virtual bool runOnFunction(Function &F);<br>
<br>
- virtual bool runOnFunction(Function &F);<br>
+ virtual void getAnalysisUsage(AnalysisUsage &AU) const {<br>
+ AU.addRequired<TargetTransformInfo>();<br>
+ }<br>
+private:<br>
+ AliasAnalysis *AA;<br>
+ bool IsTargetAware; // Should the pass be target-aware?<br>
+};<br>
+<br>
+// CFGSimplifyPass that does optimizations.<br>
+struct CFGOptimize : public CFGSimplifyPass {<br>
+ static char ID; // Pass identification, replacement for typeid<br>
+public:<br>
+ CFGOptimize() : CFGSimplifyPass(ID, true) {<br>
+ initializeCFGOptimizePass(*PassRegistry::getPassRegistry());<br>
+ }<br>
+ virtual void getAnalysisUsage(AnalysisUsage &AU) const {<br>
+ AU.addRequired<TargetTransformInfo>();<br>
+ AU.addRequired<AliasAnalysis>();<br>
+ }<br>
+};<br>
<br>
- virtual void getAnalysisUsage(AnalysisUsage &AU) const {<br>
- AU.addRequired<TargetTransformInfo>();<br>
- }<br>
- };<br>
+// CFGSimplifyPass that does canonicalizations.<br>
+struct CFGCanonicalize : public CFGSimplifyPass {<br>
+ static char ID; // Pass identification, replacement for typeid<br>
+public:<br>
+ CFGCanonicalize() : CFGSimplifyPass(ID, false) {<br>
+ initializeCFGCanonicalizePass(*PassRegistry::getPassRegistry());<br>
+ }<br>
+};<br>
}<br>
<br>
-char CFGSimplifyPass::ID = 0;<br>
-INITIALIZE_PASS_BEGIN(CFGSimplifyPass, "simplifycfg", "Simplify the CFG",<br>
- false, false)<br>
+char CFGCanonicalize::ID = 0;<br>
+char CFGOptimize::ID = 0;<br>
+INITIALIZE_PASS_BEGIN(CFGCanonicalize, "simplifycfg", "Simplify the CFG", false,<br>
+ false)<br>
+INITIALIZE_AG_DEPENDENCY(TargetTransformInfo)<br>
+INITIALIZE_PASS_END(CFGCanonicalize, "simplifycfg", "Simplify the CFG", false,<br>
+ false)<br>
+INITIALIZE_PASS_BEGIN(CFGOptimize, "optimizecfg", "optimize the CFG", false,<br>
+ false)<br>
INITIALIZE_AG_DEPENDENCY(TargetTransformInfo)<br>
-INITIALIZE_PASS_END(CFGSimplifyPass, "simplifycfg", "Simplify the CFG",<br>
- false, false)<br>
+INITIALIZE_AG_DEPENDENCY(AliasAnalysis)<br>
+INITIALIZE_PASS_END(CFGOptimize, "optimizecfg", "Optimize the CFG", false,<br>
+ false)<br>
<br>
// Public interface to the CFGSimplification pass<br>
-FunctionPass *llvm::createCFGSimplificationPass() {<br>
- return new CFGSimplifyPass();<br>
+FunctionPass *llvm::createCFGSimplificationPass(bool IsTargetAware) {<br>
+ if (IsTargetAware)<br>
+ return new CFGOptimize();<br>
+ else<br>
+ return new CFGCanonicalize();<br>
}<br>
<br>
/// changeToUnreachable - Insert an unreachable instruction before the specified<br>
@@ -301,7 +334,7 @@ static bool mergeEmptyReturnBlocks(Funct<br>
/// iterativelySimplifyCFG - Call SimplifyCFG on all the blocks in the function,<br>
/// iterating until no more changes are made.<br>
static bool iterativelySimplifyCFG(Function &F, const TargetTransformInfo &TTI,<br>
- const DataLayout *TD) {<br>
+ const DataLayout *TD, AliasAnalysis *AA) {<br>
bool Changed = false;<br>
bool LocalChange = true;<br>
while (LocalChange) {<br>
@@ -310,7 +343,7 @@ static bool iterativelySimplifyCFG(Funct<br>
// Loop over all of the basic blocks and remove them if they are unneeded...<br>
//<br>
for (Function::iterator BBIt = F.begin(); BBIt != F.end(); ) {<br>
- if (SimplifyCFG(BBIt++, TTI, TD)) {<br>
+ if (SimplifyCFG(BBIt++, TTI, TD, AA)) {<br>
LocalChange = true;<br>
++NumSimpl;<br>
}<br>
@@ -324,11 +357,15 @@ static bool iterativelySimplifyCFG(Funct<br>
// simplify the CFG.<br>
//<br>
bool CFGSimplifyPass::runOnFunction(Function &F) {<br>
+ if (IsTargetAware)<br>
+ AA = &getAnalysis<AliasAnalysis>();<br>
+ else<br>
+ AA = NULL;<br>
const TargetTransformInfo &TTI = getAnalysis<TargetTransformInfo>();<br>
const DataLayout *TD = getAnalysisIfAvailable<DataLayout>();<br>
bool EverChanged = removeUnreachableBlocksFromFn(F);<br>
EverChanged |= mergeEmptyReturnBlocks(F);<br>
- EverChanged |= iterativelySimplifyCFG(F, TTI, TD);<br>
+ EverChanged |= iterativelySimplifyCFG(F, TTI, TD, AA);<br>
<br>
// If neither pass changed anything, we're done.<br>
if (!EverChanged) return false;<br>
@@ -342,7 +379,7 @@ bool CFGSimplifyPass::runOnFunction(Func<br>
return true;<br>
<br>
do {<br>
- EverChanged = iterativelySimplifyCFG(F, TTI, TD);<br>
+ EverChanged = iterativelySimplifyCFG(F, TTI, TD, AA);<br>
EverChanged |= removeUnreachableBlocksFromFn(F);<br>
} while (EverChanged);<br>
<br>
<br>
Modified: llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp?rev=187278&r1=187277&r2=187278&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp?rev=187278&r1=187277&r2=187278&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp (original)<br>
+++ llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp Fri Jul 26 19:01:07 2013<br>
@@ -19,6 +19,7 @@<br>
#include "llvm/ADT/SmallPtrSet.h"<br>
#include "llvm/ADT/SmallVector.h"<br>
#include "llvm/ADT/Statistic.h"<br>
+#include "llvm/Analysis/AliasAnalysis.h"<br>
#include "llvm/Analysis/InstructionSimplify.h"<br>
#include "llvm/Analysis/TargetTransformInfo.h"<br>
#include "llvm/Analysis/ValueTracking.h"<br>
@@ -65,6 +66,10 @@ static cl::opt<bool><br>
HoistCondStores("simplifycfg-hoist-cond-stores", cl::Hidden, cl::init(true),<br>
cl::desc("Hoist conditional stores if an unconditional store preceeds"));<br>
<br>
+static cl::opt<bool><br>
+ParallelAndOr("simplifycfg-parallel-and-or", cl::Hidden, cl::init(true),<br>
+ cl::desc("Use parallel-and-or mode for branch conditions"));<br>
+<br>
STATISTIC(NumBitMaps, "Number of switch instructions turned into bitmaps");<br>
STATISTIC(NumLookupTables, "Number of switch instructions turned into lookup tables");<br>
STATISTIC(NumSinkCommons, "Number of common instructions sunk down to the end block");<br>
@@ -90,6 +95,7 @@ namespace {<br>
class SimplifyCFGOpt {<br>
const TargetTransformInfo &TTI;<br>
const DataLayout *const TD;<br>
+ AliasAnalysis *AA;<br>
<br>
Value *isValueEqualityComparison(TerminatorInst *TI);<br>
BasicBlock *GetValueEqualityComparisonCases(TerminatorInst *TI,<br>
@@ -107,10 +113,25 @@ class SimplifyCFGOpt {<br>
bool SimplifyIndirectBr(IndirectBrInst *IBI);<br>
bool SimplifyUncondBranch(BranchInst *BI, IRBuilder <> &Builder);<br>
bool SimplifyCondBranch(BranchInst *BI, IRBuilder <>&Builder);<br>
+ /// \brief Use parallel-and or parallel-or to generate conditions for<br>
+ /// conditional branches.<br>
+ bool SimplifyParallelAndOr(BasicBlock *BB, IRBuilder<> &Builder, Pass *P = 0);<br>
+ /// \brief If \param BB is the merge block of an if-region, attempt to merge<br>
+ /// the if-region with an adjacent if-region upstream if two if-regions<br>
+ /// contain identical instructions.<br>
+ bool MergeIfRegion(BasicBlock *BB, IRBuilder<> &Builder, Pass *P = 0);<br>
+ /// \brief Compare a pair of blocks: \param Block1 and \param Block2, which<br>
+ /// are from two if-regions whose entry blocks are \param Head1 and \param<br>
+ /// Head2. \returns true if \param Block1 and \param Block2 contain identical<br>
+ /// instructions, and have no memory reference alias with \param Head2.<br>
+ /// This is used as a legality check for merging if-regions.<br>
+ bool CompareIfRegionBlock(BasicBlock *Head1, BasicBlock *Head2,<br>
+ BasicBlock *Block1, BasicBlock *Block2);<br>
<br>
public:<br>
- SimplifyCFGOpt(const TargetTransformInfo &TTI, const DataLayout *TD)<br>
- : TTI(TTI), TD(TD) {}<br>
+ SimplifyCFGOpt(const TargetTransformInfo &TTI, const DataLayout *TD,<br>
+ AliasAnalysis *AA)<br>
+ : TTI(TTI), TD(TD), AA(AA) {}<br>
bool run(BasicBlock *BB);<br>
};<br>
}<br>
@@ -197,8 +218,8 @@ static void AddPredecessorToBlock(BasicB<br>
}<br>
<br>
<br>
-/// GetIfCondition - Given a basic block (BB) with two predecessors (and at<br>
-/// least one PHI node in it), check to see if the merge at this block is due<br>
+/// GetIfCondition - Given a basic block (BB) with two predecessors,<br>
+/// check to see if the merge at this block is due<br>
/// to an "if condition". If so, return the boolean condition that determines<br>
/// which entry into BB will be taken. Also, return by references the block<br>
/// that will be entered from if the condition is true, and the block that will<br>
@@ -208,11 +229,26 @@ static void AddPredecessorToBlock(BasicB<br>
/// instructions in them.<br>
static Value *GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,<br>
BasicBlock *&IfFalse) {<br>
- PHINode *SomePHI = cast<PHINode>(BB->begin());<br>
- assert(SomePHI->getNumIncomingValues() == 2 &&<br>
- "Function can only handle blocks with 2 predecessors!");<br>
- BasicBlock *Pred1 = SomePHI->getIncomingBlock(0);<br>
- BasicBlock *Pred2 = SomePHI->getIncomingBlock(1);<br>
+ PHINode *SomePHI = dyn_cast<PHINode>(BB->begin());<br>
+ BasicBlock *Pred1 = NULL;<br>
+ BasicBlock *Pred2 = NULL;<br>
+<br>
+ if (SomePHI) {<br>
+ if (SomePHI->getNumIncomingValues() != 2)<br>
+ return NULL;<br>
+ Pred1 = SomePHI->getIncomingBlock(0);<br>
+ Pred2 = SomePHI->getIncomingBlock(1);<br>
+ } else {<br>
+ pred_iterator PI = pred_begin(BB), PE = pred_end(BB);<br>
+ if (PI == PE) // No predecessor<br>
+ return NULL;<br>
+ Pred1 = *PI++;<br>
+ if (PI == PE) // Only one predecessor<br>
+ return NULL;<br>
+ Pred2 = *PI++;<br>
+ if (PI != PE) // More than two predecessors<br>
+ return NULL;<br>
+ }<br>
<br>
// We can only handle branches. Other control flow will be lowered to<br>
// branches if possible anyway.<br>
@@ -4066,6 +4102,383 @@ bool SimplifyCFGOpt::SimplifyCondBranch(<br>
return false;<br>
}<br>
<br>
+/// If \param [in] BB has more than one predecessor that is a conditional<br>
+/// branch, attempt to use parallel and/or for the branch condition. \returns<br>
+/// true on success.<br>
+///<br>
+/// Before:<br>
+/// ......<br>
+/// %cmp10 = fcmp une float %tmp1, %tmp2<br>
+/// br i1 %cmp1, label %if.then, label %lor.rhs<br>
+///<br>
+/// lor.rhs:<br>
+/// ......<br>
+/// %cmp11 = fcmp une float %tmp3, %tmp4<br>
+/// br i1 %cmp11, label %if.then, label %ifend<br>
+///<br>
+/// if.end: // the merge block<br>
+/// ......<br>
+///<br>
+/// if.then: // has two predecessors, both of them contains conditional branch.<br>
+/// ......<br>
+/// br label %if.end;<br>
+///<br>
+/// After:<br>
+/// ......<br>
+/// %cmp10 = fcmp une float %tmp1, %tmp2<br>
+/// ......<br>
+/// %cmp11 = fcmp une float %tmp3, %tmp4<br>
+/// %cmp12 = or i1 %cmp10, %cmp11 // parallel-or mode.<br>
+/// br i1 %cmp12, label %if.then, label %ifend<br>
+///<br>
+/// if.end:<br>
+/// ......<br>
+///<br>
+/// if.then:<br>
+/// ......<br>
+/// br label %if.end;<br>
+///<br>
+/// Current implementation handles two cases.<br>
+/// Case 1: \param BB is on the else-path.<br>
+///<br>
+/// BB1<br>
+/// / |<br>
+/// BB2 |<br>
+/// / \ |<br>
+/// BB3 \ | where, BB1, BB2 contain conditional branches.<br>
+/// \ | / BB3 contains unconditional branch.<br>
+/// \ | / BB4 corresponds to \param BB which is also the merge.<br>
+/// BB => BB4<br>
+///<br>
+///<br>
+/// Corresponding source code:<br>
+///<br>
+/// if (a == b && c == d)<br>
+/// statement; // BB3<br>
+///<br>
+/// Case 2: \param BB BB is on the then-path.<br>
+///<br>
+/// BB1<br>
+/// / |<br>
+/// | BB2<br>
+/// \ / | where BB1, BB2 contain conditional branches.<br>
+/// BB => BB3 | BB3 contains unconditiona branch and corresponds<br>
+/// \ / to \param BB. BB4 is the merge.<br>
+/// BB4<br>
+///<br>
+/// Corresponding source code:<br>
+///<br>
+/// if (a == b || c == d)<br>
+/// statement; // BB3<br>
+///<br>
+/// In both cases, \param BB is the common successor of conditional branches.<br>
+/// In Case 1, \param BB (BB4) has an unconditional branch (BB3) as<br>
+/// its predecessor. In Case 2, \param BB (BB3) only has conditional branches<br>
+/// as its predecessors.<br>
+///<br>
+bool SimplifyCFGOpt::SimplifyParallelAndOr(BasicBlock *BB, IRBuilder<> &Builder,<br>
+ Pass *P) {<br>
+ PHINode *PHI = dyn_cast<PHINode>(BB->begin());<br>
+ if (PHI)<br>
+ return false; // For simplicity, avoid cases containing PHI nodes.<br>
+<br>
+ BasicBlock *LastCondBlock = NULL;<br>
+ BasicBlock *FirstCondBlock = NULL;<br>
+ BasicBlock *UnCondBlock = NULL;<br>
+ int Idx = -1;<br>
+<br>
+ // Check predecessors of \param BB.<br>
+ SmallPtrSet<BasicBlock*, 16> Preds(pred_begin(BB), pred_end(BB));<br>
+ for (SmallPtrSetIterator<BasicBlock*> PI = Preds.begin(), PE = Preds.end();<br>
+ PI != PE; ++PI) {<br>
+ BasicBlock *Pred = *PI;<br>
+ BranchInst *PBI = dyn_cast<BranchInst>(Pred->getTerminator());<br>
+<br>
+ // All predecessors should terminate with a branch.<br>
+ if (!PBI)<br>
+ return false;<br>
+<br>
+ BasicBlock *PP = Pred->getSinglePredecessor();<br>
+<br>
+ if (PBI->isUnconditional()) {<br>
+ // Case 1: Pred (BB3) is an unconditional block, it should<br>
+ // have a single predecessor (BB2) that is also a predecessor<br>
+ // of \param BB (BB4) and should not have address-taken.<br>
+ // There should exist only one such unconditional<br>
+ // branch among the predecessors.<br>
+ if (UnCondBlock || !PP || (Preds.count(PP) == 0) ||<br>
+ Pred->hasAddressTaken())<br>
+ return false;<br>
+<br>
+ UnCondBlock = Pred;<br>
+ continue;<br>
+ }<br>
+<br>
+ // Only conditional branches are allowed beyond this point.<br>
+ assert(PBI->isConditional());<br>
+<br>
+ // Condition's unique use should be the branch instruction.<br>
+ Value *PC = PBI->getCondition();<br>
+ if (!PC || !PC->hasOneUse())<br>
+ return false;<br>
+<br>
+ if (PP && Preds.count(PP)) {<br>
+ // These are internal condition blocks to be merged from, e.g.,<br>
+ // BB2 in both cases.<br>
+ // Should not be address-taken.<br>
+ if (Pred->hasAddressTaken())<br>
+ return false;<br>
+<br>
+ // Instructions in the internal condition blocks should be safe<br>
+ // to hoist up.<br>
+ for (BasicBlock::iterator BI = Pred->begin(), BE = PBI; BI != BE;) {<br>
+ Instruction *CI = BI++;<br>
+ if (isa<PHINode>(CI) ||<br>
+ !isSafeToSpeculativelyExecute(CI))<br>
+ return false;<br>
+ }<br>
+ } else {<br>
+ // This is the condition block to be merged into, e.g. BB1 in<br>
+ // both cases.<br>
+ if (FirstCondBlock)<br>
+ return false;<br>
+ FirstCondBlock = Pred;<br>
+ }<br>
+<br>
+ // Find whether BB is uniformly on the true (or false) path<br>
+ // for all of its predecessors.<br>
+ BasicBlock *PS1 = PBI->getSuccessor(0);<br>
+ BasicBlock *PS2 = PBI->getSuccessor(1);<br>
+ BasicBlock *PS = (PS1 == BB) ? PS2 : PS1;<br>
+ int CIdx = (PS1 == BB) ? 0 : 1;<br>
+<br>
+ if (Idx == -1)<br>
+ Idx = CIdx;<br>
+ else if (CIdx != Idx)<br>
+ return false;<br>
+<br>
+ // PS is the successor which is not BB. Check successors to identify<br>
+ // the last conditional branch.<br>
+ if (Preds.count(PS) == 0) {<br>
+ // Case 2.<br>
+ // BB must have an unique successor.<br>
+ TerminatorInst *TBB = BB->getTerminator();<br>
+ if (TBB->getNumSuccessors() != 1)<br>
+ return false;<br>
+<br>
+ BasicBlock *SBB = TBB->getSuccessor(0);<br>
+ PHI = dyn_cast<PHINode>(SBB->begin());<br>
+ if (PHI)<br>
+ return false;<br>
+<br>
+ // PS (BB4) should be BB's successor.<br>
+ if (SBB != PS)<br>
+ return false;<br>
+ LastCondBlock = Pred;<br>
+ } else {<br>
+ BranchInst *BPS = dyn_cast<BranchInst>(PS->getTerminator());<br>
+ if (BPS && BPS->isUnconditional()) {<br>
+ // Case 1: PS(BB3) should be an unconditional branch.<br>
+ LastCondBlock = Pred;<br>
+ }<br>
+ }<br>
+ }<br>
+<br>
+ if (!FirstCondBlock || !LastCondBlock || (FirstCondBlock == LastCondBlock))<br>
+ return false;<br>
+<br>
+ // Do the transformation.<br>
+ BasicBlock *CB;<br>
+ bool Iteration = true;<br>
+ BasicBlock::iterator ItOld = Builder.GetInsertPoint();<br>
+ BranchInst *PBI = dyn_cast<BranchInst>(FirstCondBlock->getTerminator());<br>
+ Value *PC = PBI->getCondition();<br>
+ do {<br>
+ CB = PBI->getSuccessor(1 - Idx);<br>
+ // Delete the conditional branch.<br>
+ FirstCondBlock->getInstList().pop_back();<br>
+ FirstCondBlock->getInstList().splice(FirstCondBlock->end(), CB->getInstList());<br>
+ PBI = cast<BranchInst>(FirstCondBlock->getTerminator());<br>
+ Value *CC = PBI->getCondition();<br>
+ // Merge conditions.<br>
+ Builder.SetInsertPoint(PBI);<br>
+ Value *NC;<br>
+ if (Idx == 0)<br>
+ // Case 2, use parallel or.<br>
+ NC = Builder.CreateOr(PC, CC);<br>
+ else<br>
+ // Case 1, use parallel and.<br>
+ NC = Builder.CreateAnd(PC, CC);<br>
+<br>
+ PBI->replaceUsesOfWith(CC, NC);<br>
+ PC = NC;<br>
+ if (CB == LastCondBlock)<br>
+ Iteration = false;<br>
+ // Remove internal conditional branches.<br>
+ CB->dropAllReferences();<br>
+ // make CB unreachable and let downstream to delete the block.<br>
+ new UnreachableInst(CB->getContext(), CB);<br>
+ } while (Iteration);<br>
+<br>
+ Builder.SetInsertPoint(ItOld);<br>
+ DEBUG(dbgs() << "Use parallel and/or in:\n" << *FirstCondBlock);<br>
+ return true;<br>
+}<br>
+<br>
+/// Compare blocks from two if-regions, where \param Head1 is the entry of the<br>
+/// 1st if-region. \param Head2 is the entry of the 2nd if-region. \param<br>
+/// Block1 is a block in the 1st if-region to compare. \param Block2 is a block<br>
+// in the 2nd if-region to compare. \returns true if \param Block1 and \param<br>
+/// Block2 have identical instructions and do not have memory reference alias<br>
+/// with \param Head2.<br>
+///<br>
+bool SimplifyCFGOpt::CompareIfRegionBlock(BasicBlock *Head1, BasicBlock *Head2,<br>
+ BasicBlock *Block1, BasicBlock *Block2) {<br>
+ TerminatorInst *PTI2 = Head2->getTerminator();<br>
+ Instruction *PBI2 = Head2->begin();<br>
+<br>
+ bool eq1 = (Block1 == Head1);<br>
+ bool eq2 = (Block2 == Head2);<br>
+ if (eq1 || eq2) {<br>
+ // An empty then-path or else-path.<br>
+ return (eq1 == eq2);<br>
+ }<br>
+<br>
+ // Check whether instructions in Block1 and Block2 are identical<br>
+ // and do not alias with instructions in Head2.<br>
+ BasicBlock::iterator iter1 = Block1->begin();<br>
+ BasicBlock::iterator end1 = Block1->getTerminator();<br>
+ BasicBlock::iterator iter2 = Block2->begin();<br>
+ BasicBlock::iterator end2 = Block2->getTerminator();<br>
+<br>
+ while (1) {<br>
+ if (iter1 == end1) {<br>
+ if (iter2 != end2)<br>
+ return false;<br>
+ break;<br>
+ }<br>
+<br>
+ if (!iter1->isIdenticalTo(iter2))<br>
+ return false;<br>
+<br>
+ // Illegal to remove instructions with side effects except<br>
+ // non-volatile stores.<br>
+ if (iter1->mayHaveSideEffects()) {<br>
+ Instruction *CurI = &*iter1;<br>
+ StoreInst *SI = dyn_cast<StoreInst>(CurI);<br>
+ if (!SI || SI->isVolatile())<br>
+ return false;<br>
+ }<br>
+<br>
+ // For simplicity and speed, data dependency check can be<br>
+ // avoided if read from memory doesn't exist.<br>
+ if (iter1->mayReadFromMemory())<br>
+ return false;<br>
+<br>
+ if (iter1->mayWriteToMemory()) {<br>
+ for (BasicBlock::iterator BI = PBI2, BE = PTI2; BI != BE; ++BI) {<br>
+ if (BI->mayReadFromMemory() || BI->mayWriteToMemory()) {<br>
+ // Check alias with Head2.<br>
+ if (!AA || AA->alias(iter1, BI))<br>
+ return false;<br>
+ }<br>
+ }<br>
+ }<br>
+ ++iter1;<br>
+ ++iter2;<br>
+ }<br>
+<br>
+ return true;<br>
+}<br>
+<br>
+/// Check whether \param BB is the merge block of a if-region. If yes, check<br>
+/// whether there exists an adjacent if-region upstream, the two if-regions<br>
+/// contain identical instuctions and can be legally merged. \returns true if<br>
+/// the two if-regions are merged.<br>
+///<br>
+/// From:<br>
+/// if (a)<br>
+/// statement;<br>
+/// if (b)<br>
+/// statement;<br>
+///<br>
+/// To:<br>
+/// if (a || b)<br>
+/// statement;<br>
+///<br>
+bool SimplifyCFGOpt::MergeIfRegion(BasicBlock *BB, IRBuilder<> &Builder,<br>
+ Pass *P) {<br>
+ BasicBlock *IfTrue2, *IfFalse2;<br>
+ Value *IfCond2 = GetIfCondition(BB, IfTrue2, IfFalse2);<br>
+ Instruction *CInst2 = dyn_cast_or_null<Instruction>(IfCond2);<br>
+ if (!CInst2)<br>
+ return false;<br>
+<br>
+ BasicBlock *SecondEntryBlock = CInst2->getParent();<br>
+ if (SecondEntryBlock->hasAddressTaken())<br>
+ return false;<br>
+<br>
+ BasicBlock *IfTrue1, *IfFalse1;<br>
+ Value *IfCond1 = GetIfCondition(SecondEntryBlock, IfTrue1, IfFalse1);<br>
+ Instruction *CInst1 = dyn_cast_or_null<Instruction>(IfCond1);<br>
+ if (!CInst1)<br>
+ return false;<br>
+<br>
+ BasicBlock *FirstEntryBlock = CInst1->getParent();<br>
+<br>
+ // Either then-path or else-path should be empty.<br>
+ if ((IfTrue1 != FirstEntryBlock) && (IfFalse1 != FirstEntryBlock))<br>
+ return false;<br>
+ if ((IfTrue2 != SecondEntryBlock) && (IfFalse2 != SecondEntryBlock))<br>
+ return false;<br>
+<br>
+ TerminatorInst *PTI2 = SecondEntryBlock->getTerminator();<br>
+ Instruction *PBI2 = SecondEntryBlock->begin();<br>
+<br>
+ if (!CompareIfRegionBlock(FirstEntryBlock, SecondEntryBlock, IfTrue1, IfTrue2))<br>
+ return false;<br>
+<br>
+ if (!CompareIfRegionBlock(FirstEntryBlock, SecondEntryBlock, IfFalse1, IfFalse2))<br>
+ return false;<br>
+<br>
+ // Check whether \param SecondEntryBlock has side-effect and is safe to speculate.<br>
+ for (BasicBlock::iterator BI = PBI2, BE = PTI2; BI != BE; ++BI) {<br>
+ Instruction *CI = BI;<br>
+ if (isa<PHINode>(CI) || CI->mayHaveSideEffects() ||<br>
+ !isSafeToSpeculativelyExecute(CI))<br>
+ return false;<br>
+ }<br>
+<br>
+ // Merge \param SecondEntryBlock into \param FirstEntryBlock.<br>
+ FirstEntryBlock->getInstList().pop_back();<br>
+ FirstEntryBlock->getInstList().splice(FirstEntryBlock->end(), SecondEntryBlock->getInstList());<br>
+ BranchInst *PBI = dyn_cast<BranchInst>(FirstEntryBlock->getTerminator());<br>
+ Value *CC = PBI->getCondition();<br>
+ BasicBlock::iterator ItOld = Builder.GetInsertPoint();<br>
+ Builder.SetInsertPoint(PBI);<br>
+ Value *NC = Builder.CreateOr(CInst1, CC);<br>
+ PBI->replaceUsesOfWith(CC, NC);<br>
+ Builder.SetInsertPoint(ItOld);<br>
+<br>
+ // Remove IfTrue1<br>
+ if (IfTrue1 != FirstEntryBlock) {<br>
+ IfTrue1->dropAllReferences();<br>
+ IfTrue1->eraseFromParent();<br>
+ }<br>
+<br>
+ // Remove IfFalse1<br>
+ if (IfFalse1 != FirstEntryBlock) {<br>
+ IfFalse1->dropAllReferences();<br>
+ IfFalse1->eraseFromParent();<br>
+ }<br>
+<br>
+ // Remove \param SecondEntryBlock<br>
+ SecondEntryBlock->dropAllReferences();<br>
+ SecondEntryBlock->eraseFromParent();<br>
+ DEBUG(dbgs() << "If conditions merged into:\n" << *FirstEntryBlock);<br>
+ return true;<br>
+}<br>
+<br>
/// Check if passing a value to an instruction will cause undefined behavior.<br>
static bool passingValueIsAlwaysUndefined(Value *V, Instruction *I) {<br>
Constant *C = dyn_cast<Constant>(V);<br>
@@ -4168,6 +4581,11 @@ bool SimplifyCFGOpt::run(BasicBlock *BB)<br>
return true;<br>
<br>
IRBuilder<> Builder(BB);<br>
+ // Whether to optimize conditional branches.<br>
+ bool OptCB = (ParallelAndOr && AA && TTI.hasBranchDivergence());<br>
+<br>
+ if (OptCB && SimplifyParallelAndOr(BB, Builder))<br>
+ return true;<br>
<br>
// If there is a trivial two-entry PHI node in this basic block, and we can<br>
// eliminate it, do so now.<br>
@@ -4196,6 +4614,9 @@ bool SimplifyCFGOpt::run(BasicBlock *BB)<br>
if (SimplifyIndirectBr(IBI)) return true;<br>
}<br>
<br>
+ if (OptCB && MergeIfRegion(BB, Builder))<br>
+ return true;<br>
+<br>
return Changed;<br>
}<br>
<br>
@@ -4205,6 +4626,6 @@ bool SimplifyCFGOpt::run(BasicBlock *BB)<br>
/// of the CFG. It returns true if a modification was made.<br>
///<br>
bool llvm::SimplifyCFG(BasicBlock *BB, const TargetTransformInfo &TTI,<br>
- const DataLayout *TD) {<br>
- return SimplifyCFGOpt(TTI, TD).run(BB);<br>
+ const DataLayout *TD, AliasAnalysis *AA) {<br>
+ return SimplifyCFGOpt(TTI, TD, AA).run(BB);<br>
}<br>
<br>
Modified: llvm/trunk/tools/lto/LTOCodeGenerator.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lto/LTOCodeGenerator.cpp?rev=187278&r1=187277&r2=187278&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lto/LTOCodeGenerator.cpp?rev=187278&r1=187277&r2=187278&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/tools/lto/LTOCodeGenerator.cpp (original)<br>
+++ llvm/trunk/tools/lto/LTOCodeGenerator.cpp Fri Jul 26 19:01:07 2013<br>
@@ -118,7 +118,7 @@ void LTOCodeGenerator::initializeLTOPass<br>
initializeGVNPass(R);<br>
initializeMemCpyOptPass(R);<br>
initializeDCEPass(R);<br>
- initializeCFGSimplifyPassPass(R);<br>
+ initializeCFGCanonicalizePass(R);<br>
}<br>
<br>
bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg) {<br>
<br>
Modified: llvm/trunk/tools/opt/opt.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/opt/opt.cpp?rev=187278&r1=187277&r2=187278&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/opt/opt.cpp?rev=187278&r1=187277&r2=187278&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/tools/opt/opt.cpp (original)<br>
+++ llvm/trunk/tools/opt/opt.cpp Fri Jul 26 19:01:07 2013<br>
@@ -667,6 +667,9 @@ int main(int argc, char **argv) {<br>
FPasses.reset(new FunctionPassManager(M.get()));<br>
if (TD)<br>
FPasses->add(new DataLayout(*TD));<br>
+ if (TM.get())<br>
+ TM->addAnalysisPasses(*FPasses);<br>
+<br>
}<br>
<br>
if (PrintBreakpoints) {<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div>Alexey Samsonov, MSK</div>
</div>