From: Peizhao Ou Date: Wed, 29 Nov 2017 00:37:38 +0000 (-0800) Subject: Generates conditional branch instead of fake ones for Select instruction in some... X-Git-Url: http://plrg.eecs.uci.edu/git/?p=oota-llvm.git;a=commitdiff_plain;h=20a42bb20d43b80e322c95dd99b64a5a4566fe08 Generates conditional branch instead of fake ones for Select instruction in some cases --- diff --git a/include/llvm/CodeGen/MachineBasicBlock.h b/include/llvm/CodeGen/MachineBasicBlock.h index 9585b29a5dc..fd786f68fb6 100644 --- a/include/llvm/CodeGen/MachineBasicBlock.h +++ b/include/llvm/CodeGen/MachineBasicBlock.h @@ -81,10 +81,6 @@ public: }; private: - // XXX-update: A flag that checks whether we can eliminate this machine basic - // block. - bool canEliminateMachineBB; - typedef ilist Instructions; Instructions Insts; const BasicBlock *BB; @@ -128,6 +124,10 @@ private: /// is only computed once and is cached. mutable MCSymbol *CachedMCSymbol = nullptr; + // XXX-update: A flag that checks whether we can eliminate this machine basic + // block. + bool canEliminateMachineBB; + // Intrusive list support MachineBasicBlock() {} diff --git a/include/llvm/CodeGen/MachineInstr.h b/include/llvm/CodeGen/MachineInstr.h index 3e76ea9c31d..05c9a9e0b07 100644 --- a/include/llvm/CodeGen/MachineInstr.h +++ b/include/llvm/CodeGen/MachineInstr.h @@ -71,9 +71,6 @@ public: BundledSucc = 1 << 3 // Instruction has bundled successors. }; private: - // XXX-update: A flag that checks whether we can eliminate this instruction. - bool canEliminateMachineInstr; - const MCInstrDesc *MCID; // Instruction descriptor. MachineBasicBlock *Parent; // Pointer to the owning basic block. @@ -129,15 +126,6 @@ private: friend class MachineFunction; public: - // XXX-update: - void disableCanEliminateMachineInstr() { - canEliminateMachineInstr = false; - } - - bool getCanEliminateMachineInstr() { - return canEliminateMachineInstr; - } - const MachineBasicBlock* getParent() const { return Parent; } MachineBasicBlock* getParent() { return Parent; } diff --git a/lib/CodeGen/CodeGenPrepare.cpp b/lib/CodeGen/CodeGenPrepare.cpp index 1a07bfc0c35..5c9858dcec2 100644 --- a/lib/CodeGen/CodeGenPrepare.cpp +++ b/lib/CodeGen/CodeGenPrepare.cpp @@ -518,6 +518,8 @@ Value* getRootDependence(Value* DepVal) { bool taintStoreAddress(StoreInst* SI, Value* DepVal, const char* calling_func = __builtin_FUNCTION()) { DEBUG(dbgs() << "Called from " << calling_func << '\n'); + // Set the insertion point right after the 'DepVal'. + Instruction* Inst = nullptr; IRBuilder Builder(SI); BasicBlock* BB = SI->getParent(); Value* Address = SI->getPointerOperand(); @@ -749,9 +751,21 @@ void AddFakeConditionalBranch(Instruction* SplitInst, Value* Condition) { // Returns true if the code is changed, and false otherwise. void TaintRelaxedLoads(LoadInst* LI) { - IRBuilder Builder(LI->getNextNode()); + // For better performance, we can add a "AND X 0" instruction before the + // condition. + auto* FirstInst = findFirstStoreCondBranchInst(LI); + Instruction* InsertPoint = nullptr; + if (FirstInst == nullptr) { + InsertPoint = LI->getParent()->getTerminator(); + InsertPoint = LI->getNextNode(); + } else { + InsertPoint = LI->getNextNode(); + } + IRBuilder Builder(InsertPoint); + auto* AndZero = dyn_cast( + Builder.CreateAnd(LI, Constant::getNullValue(LI->getType()))); auto* FakeCondition = dyn_cast(Builder.CreateICmp( - CmpInst::ICMP_EQ, LI, Constant::getNullValue(LI->getType()))); + CmpInst::ICMP_NE, AndZero, Constant::getNullValue(LI->getType()))); AddFakeConditionalBranch(FakeCondition->getNextNode(), FakeCondition); } @@ -779,8 +793,15 @@ bool AddFakeConditionalBranchAfterMonotonicLoads( } // We really need to process the relaxed load now. - TaintRelaxedLoads(LI); - Changed = true; + StoreInst* SI = nullptr;; + if (FirstInst && (SI = dyn_cast(FirstInst))) { + // For immediately coming stores, taint the address of the store. + taintStoreAddress(SI, LI); + } else { + // For immediately coming branch, directly add a fake branch. + TaintRelaxedLoads(LI); + Changed = true; + } } return Changed; } diff --git a/lib/CodeGen/MachineInstr.cpp b/lib/CodeGen/MachineInstr.cpp index 5e542757b63..6dca74d6002 100644 --- a/lib/CodeGen/MachineInstr.cpp +++ b/lib/CodeGen/MachineInstr.cpp @@ -647,7 +647,7 @@ MachineInstr::MachineInstr(MachineFunction &MF, const MCInstrDesc &tid, DebugLoc dl, bool NoImp) : MCID(&tid), Parent(nullptr), Operands(nullptr), NumOperands(0), Flags(0), AsmPrinterFlags(0), NumMemRefs(0), MemRefs(nullptr), - debugLoc(std::move(dl)), canEliminateMachineInstr(true) { + debugLoc(std::move(dl)) { assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor"); // Reserve space for the expected number of operands. @@ -667,7 +667,7 @@ MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI) : MCID(&MI.getDesc()), Parent(nullptr), Operands(nullptr), NumOperands(0), Flags(0), AsmPrinterFlags(0), NumMemRefs(MI.NumMemRefs), MemRefs(MI.MemRefs), - debugLoc(MI.getDebugLoc()), canEliminateMachineInstr(true) { + debugLoc(MI.getDebugLoc()) { assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor"); CapOperands = OperandCapacity::get(MI.getNumOperands()); diff --git a/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/lib/CodeGen/SelectionDAG/DAGCombiner.cpp index bdcf5a2a89e..f119023d217 100644 --- a/lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ b/lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -3101,7 +3101,7 @@ SDValue DAGCombiner::visitAND(SDNode *N) { // XXX-disabled: (and x, 0) should not be folded. // (and (and x, 0), y) shouldn't either. - if (!N0C && N1C->isNullValue()) { + if (!N0C && N1C && N1C->isNullValue()) { return SDValue(); } if (!N0C) { diff --git a/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp b/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp index 1676e2dfe08..491d7ec451c 100644 --- a/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp +++ b/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp @@ -663,11 +663,15 @@ bool SelectionDAGISel::runOnMachineFunction(MachineFunction &mf) { MachineBasicBlock* Succ2Succ = nullptr; if ((Succ1->size() == 1 && Succ1->begin()->isUnconditionalBranch()) || (Succ1->size() == 0)) { - Succ1Succ = *Succ1->succ_begin(); + if (Succ1->succ_size()) { + Succ1Succ = *Succ1->succ_begin(); + } } if ((Succ2->size() == 1 && Succ2->begin()->isUnconditionalBranch()) || (Succ2->size() == 0)) { - Succ2Succ = *Succ2->succ_begin(); + if (Succ1->succ_size()) { + Succ2Succ = *Succ2->succ_begin(); + } } bool HasCommonDest = Succ1Succ && Succ1Succ == Succ2Succ; @@ -675,7 +679,6 @@ bool SelectionDAGISel::runOnMachineFunction(MachineFunction &mf) { auto MBBIter = MBB.end(); std::advance(MBBIter, -2); assert(MBBIter->isConditionalBranch()); - MBBIter->disableCanEliminateMachineInstr(); MBB.disableCanEliminateMachineBB(); Succ1->disableCanEliminateMachineBB(); Succ2->disableCanEliminateMachineBB();